drools

Drools - using accumulate to find min and max


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 find 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.


Solution

  • 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.