<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Hello<br>
<br>
I switched recently from Drools 5.5.0 to Drools 6.0.0.Beta3 and some
of my rule tests failed because Drools started doing weird things.<br>
<br>
I've isolated the problem and I figured out that it has to do with
the combination of "accumulate" and "collect" conditions in the same
rule.<br>
<br>
See this example here:<br>
<blockquote type="cite"><tt>import java.util.ArrayList</tt><tt><br>
</tt><tt><br>
</tt><tt>declare Item</tt><tt><br>
</tt><tt> code: int</tt><tt><br>
</tt><tt> price: int</tt><tt><br>
</tt><tt> present: boolean</tt><tt><br>
</tt><tt>end</tt><tt><br>
</tt><tt><br>
</tt><tt>rule "Init"</tt><tt><br>
</tt><tt>when</tt><tt><br>
</tt><tt>then</tt><tt><br>
</tt><tt> insert(new Item(1,40,false));</tt><tt><br>
</tt><tt> insert(new Item(2,40,false));</tt><tt><br>
</tt><tt> insert(new Item(3,40,false));</tt><tt><br>
</tt><tt> insert(new Item(4,40,false));</tt><tt><br>
</tt><tt>end </tt><tt><br>
</tt><tt><br>
</tt><tt>rule "CollectAndAccumulateRule"</tt><tt><br>
</tt><tt>when</tt><tt><br>
</tt><tt> //At least two items that aren't presents</tt><tt><br>
</tt><tt> objList: ArrayList(size>=2) from collect(
Item(present==false))</tt><tt><br>
</tt><tt> //Total price bigger than 100</tt><tt><br>
</tt><tt> price: Number(intValue>=100) from accumulate(
Item($w:price, present==false), sum($w))</tt><tt><br>
</tt><tt>then</tt><tt><br>
</tt><tt><br>
</tt><tt> System.out.println("Sum: "+price);</tt><tt><br>
</tt><tt> System.out.println("Items size: "+objList.size());</tt><tt><br>
</tt><tt> </tt><tt><br>
</tt><tt> //Look for the minor price item</tt><tt><br>
</tt><tt> Item min = null;</tt><tt><br>
</tt><tt> for(Object obj: objList){</tt><tt><br>
</tt><tt> if (min!=null){</tt><tt><br>
</tt><tt> min =
(min.getPrice()>((Item)obj).getPrice())?(Item)obj:min;</tt><tt><br>
</tt><tt> }</tt><tt><br>
</tt><tt> else {</tt><tt><br>
</tt><tt> min = (Item)obj;</tt><tt><br>
</tt><tt> }</tt><tt><br>
</tt><tt> }</tt><tt><br>
</tt><tt> </tt><tt><br>
</tt><tt> //And make it a present</tt><tt><br>
</tt><tt> if (min!=null){</tt><tt><br>
</tt><tt> modify(min){setPresent(true)};</tt><tt><br>
</tt><tt> }</tt><tt><br>
</tt><tt>end</tt><tt><br>
</tt></blockquote>
<br>
It gets a list of items, and when exist at least two of them and the
sum of its prices is bigger than 100, it makes a present of the
cheapest one. For this, items have a "present" flag that gets
activated when it becomes a present. Also, this flag is part of both
"collect" and "accumulate" conditions, for the rule not to loop over
already "presented" items.<br>
<br>
So, having 4 items with price 40 each (rule "Init" insert them),
expected result is to have 2 presents of price 40. Rule "<tt>CollectAndAccumulateRule"</tt>
gets executed two times and loop stops when there are just two
remaining items, with sum 80 (<100). Expected output would be:<br>
<blockquote><tt>Sum: 160.0</tt><br>
<tt>Items size: 4</tt><br>
<tt>Sum: 120.0</tt><br>
<tt>Items size: 3</tt><br>
</blockquote>
This works fine in Drools 5.5.0, but not in 6.0.0.Beta3. In the
latter rule "<tt>CollectAndAccumulateRule</tt>" gets called 3 times,
and output is like this:<br>
<blockquote><tt>Sum: 160.0</tt><br>
<tt>Items size: 4</tt><br>
<tt>Sum: 120.0</tt><br>
<tt>Items size: 4</tt><br>
<tt>Sum: 120.0</tt><br>
<tt>Items size: 3<br>
<br>
</tt></blockquote>
Is there some overall change in 6.0.0.Beta3 that affects this kind
of rules or conditions? Or could this be an issue?<br>
<br>
Furthermore, including this kind of rules in a package along with
other no-conflictive rules, it makes the hole package start behaving
unexpectedly. I guess it has to do with the way of the Rete tree is
bulided when this kind of rules are present.<br>
<br>
Note: I know there are alternatives and workwarounds to the scenario
shown here (like merging both conditions into a single
"accumulate"). Nevertheless the aim of this post is just to try to
determinate wheter this must be considered an issue or not<br>
<br>
Regards,<br>
--<br>
Alvaro<br>
</body>
</html>