Dear all,<br><br>In my problem, I have to calculate the sum of "price" field from many Drink facts. Here is the code I have:<br><br>when<br> Likes($nameS : name, $drinkS : drinkSpeciality)<br> Drink(speciality == $drinkS, $price : price) <br>then<br> //calculate something like: totalPrice+=$price<br><br>Inspired from what Edson said, I've defined an Accumulator class and I solve the problem:<br><br>when<br> $a:Accumulator()<br> Likes($nameS : name, $drinkS : drinkSpeciality)<br> Drink($speciality : speciality ->($speciality == $drinkS), $price : price) <br> then<br> $a.incrementDrinksPrice($price.intValue());<br><br>In this context, my questions are: <br><br>1) Is there another appropriate
solution to calculate the sum of prices? <br><br>2) What is the use of global variables? (because I think I missunderstood their meanings). Can I solve my problem using a global variable like an accumulator, knowing that a global variable is acting like a static variable?<br> <br>Best regards,<br> Oana.<br><br><br><b><i>Edson Tirelli <tirelli@post.com></i></b> wrote:<blockquote class="replbq" style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"> <br> Jeevan, sorry, I missed your e-mail in the limbo.<br><br> Just create a class or an attribute to be your accumulator...<br> For instance, if you create a "branchCounter" attribute in the Tree <br>object, you can do that:<br><br>rule "Count branches" salience 10<br>when<br> $t : Tree()<br> Branch( tree == $t )<br>then<br> $t.incrementBranchCounter();<br>
modify( $t );<br>end<br> <br>rule "Check branch count" salience 0<br>when<br> $t : Tree( branchCounter > 100 )<br>then<br> // do something<br>end<br><br> The only trick is the salience. The first rule must have a higher <br>salience in order to only fire the second rule after all branches are <br>counted.<br><br> Yes, in trunk you could do better with "accumulate" or "collect". For <br>your case, "collect" would be simpler:<br><br>rule "Check branches"<br>when<br> $t : Tree()<br> $branches : ArrayList( size > 100 ) from collect( Branch( tree == $t ) )<br>then<br> // do something<br>end<br><br> []s<br> Edson<br><br>Jeevan Tambuluri wrote:<br><br>> Hello all,<br>><br>> I have been told that you could implement a helper class to simulate <br>> "accumulate" command in drools. Could you please ealborate on how to <br>> do this? Some code snippet to illustrate this would be very much <br>> appreciated.<br>><br>> My problem
is as follows.I have two types of facts - Tree and Branch. <br>> Each Branch object has a reference to the Tree object it belongs to. I <br>> want to write a rule: The number of branches in a tree must not <br>> exceed 100. I guess this would be easy to write with "accumulate" <br>> command, which is not yet available in 3.0.x.<br>><br>> Thanks<br>><br>> Jeevan<br>><br>>------------------------------------------------------------------------<br>><br>>_______________________________________________<br>>rules-users mailing list<br>>rules-users@lists.jboss.org<br>>https://lists.jboss.org/mailman/listinfo/rules-users<br>> <br>><br><br><br>-- <br> Edson Tirelli<br> Software Engineer - JBoss Rules Core Developer<br> Office: +55 11 3124-6000<br> Mobile: +55 11 9218-4151<br> JBoss, a division of Red Hat @ www.jboss.com<br><br><br>_______________________________________________<br>rules-users mailing
list<br>rules-users@lists.jboss.org<br>https://lists.jboss.org/mailman/listinfo/rules-users<br></blockquote><br><p> 
<hr size=1><a href="
http://us.rd.yahoo.com/evt=49981/*http://advision.webevents.yahoo.com/mailbeta/features_spam.html">Sucker-punch spam</a> with award-winning protection.<br> Try the <a href="
http://us.rd.yahoo.com/evt=49981/*http://advision.webevents.yahoo.com/mailbeta/features_spam.html">free Yahoo! Mail Beta.</a>