Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drools - using accumulate to find min and max

Tags:

drools

I have a drools question which has been troubling me for some time. I want to find out the min and max price from a list of Item objects (contains price) using accumulate. A Member object (which contails list of Item objects) is inserted which contains the list of Items.

groovy/java source pseudo code
-------------------------------
class Item {
   BigDecimal price
}

class Member {
   List<Item>> items
}

...
droolsStatefulSession.insert(member)
session.fireAllRules()
...

rule.drl
---------
rule "rule1"
when 
   member : Member ($itemList : items)
/*

*/
then
 System.out.println("condition met...")
end

Now the questions is in the above rule is it possible to if so how do I find out the item with the minimum Price and maximum price using drools accumulate feature. I do not want to use an java/groovy utility functions.

I see the "collect" feature allows to use "from" and then a datasource. I wonder if "accumulate" is similar to collect.

like image 385
arrehman Avatar asked Feb 24 '23 04:02

arrehman


1 Answers

No need to use accumulate, just do something like

Item($lowestPrice : price, $id : id)
not Item(price > $lowestPrice, id < $id)

That's if your Items are inserted into the working memory.

like image 86
Geoffrey De Smet Avatar answered Mar 12 '23 19:03

Geoffrey De Smet