[rules-users] Calculate the sum of "price" attribute from many facts.

nicolae oana oananicolae1981 at yahoo.com
Wed Feb 7 11:42:20 EST 2007


Dear all,

In my problem, I have to calculate the sum of "price" field from many Drink facts. Here is the code I have:

when
    Likes($nameS : name, $drinkS : drinkSpeciality)
    Drink(speciality == $drinkS, $price : price)     
then
    //calculate something like:   totalPrice+=$price

Inspired from what Edson said, I've defined an Accumulator class and I solve the problem:

when
        $a:Accumulator()
        Likes($nameS : name, $drinkS : drinkSpeciality)
        Drink($speciality : speciality ->($speciality == $drinkS), $price : price)         
    then
        $a.incrementDrinksPrice($price.intValue());

In this context, my questions are: 

1) Is there another appropriate solution to calculate the sum of prices? 

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?
 
Best regards,
                   Oana.


Edson Tirelli <tirelli at post.com> wrote: 
   Jeevan, sorry, I missed your e-mail in the limbo.

   Just create a class or an attribute to be your accumulator...
   For instance, if you create a "branchCounter" attribute in the Tree 
object, you  can do that:

rule "Count branches" salience 10
when
      $t : Tree()
      Branch( tree == $t )
then
      $t.incrementBranchCounter();
      modify( $t );
end
 
rule "Check branch count" salience 0
when
      $t : Tree( branchCounter > 100 )
then
     // do something
end

   The only trick is the salience. The first rule must have a higher 
salience in order to only fire the second rule after all branches are 
counted.

   Yes, in trunk you could do better with "accumulate" or "collect". For 
your case, "collect" would be simpler:

rule "Check branches"
when
    $t : Tree()
    $branches : ArrayList( size > 100 ) from collect( Branch( tree == $t ) )
then
    // do something
end

   []s
   Edson

Jeevan Tambuluri wrote:

> Hello all,
>
> I have been told that you could implement a helper class to simulate 
> "accumulate" command in drools. Could you please ealborate on how to 
> do this? Some code snippet to illustrate this would be very much 
> appreciated.
>
> My problem is as follows.I have two types of facts - Tree and Branch.  
> Each Branch object has a reference to the Tree object it belongs to. I 
> want to write a rule: The number of  branches in a tree must not 
> exceed 100. I guess this would be easy to write with "accumulate" 
> command, which is not yet available in 3.0.x.
>
> Thanks
>
> Jeevan
>
>------------------------------------------------------------------------
>
>_______________________________________________
>rules-users mailing list
>rules-users at lists.jboss.org
>https://lists.jboss.org/mailman/listinfo/rules-users
>  
>


-- 
 Edson Tirelli
 Software Engineer - JBoss Rules Core Developer
 Office: +55 11 3124-6000
 Mobile: +55 11 9218-4151
 JBoss, a division of Red Hat @ www.jboss.com


_______________________________________________
rules-users mailing list
rules-users at lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


 
---------------------------------
Sucker-punch spam with award-winning protection.
 Try the free Yahoo! Mail Beta.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20070207/72983bb4/attachment.html 


More information about the rules-users mailing list