Oana,

Your reasoning is correct. The only thing I would change in your rule is that you don't need the predicate. Just do the comparison directly:

Drink($speciality : speciality == $drinkS, $price : price) The only better way I can think of is using the "accumulate" conditional element, but this is only available in trunk:

when
       Likes($nameS : name, $drinkS : drinkSpeciality)
$total : Double() from accumulate ( Drink($speciality : speciality == $drinkS, $price : price) init( double total = 0 ) action( total += $price ) result( new Double( total ) ) ) then
     // do something with the $total
end
You can use a global variable instead of an Accumulator class, but this would force you to set the global variable in your java code (before you call fireAllRules()), what may not ideal for such situations. Using an accumulator instance allows you to define a setup rule whose consequence asserts the accumulator instance, keeping your launching code not dependent from your rules.

   []s
  Edson


nicolae oana wrote:

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 <[EMAIL PROTECTED]>/* 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
    >[email protected]
    >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
    [email protected]
    https://lists.jboss.org/mailman/listinfo/rules-users


------------------------------------------------------------------------
Sucker-punch spam <http://us.rd.yahoo.com/evt=49981/*http://advision.webevents.yahoo.com/mailbeta/features_spam.html> with award-winning protection. Try the free Yahoo! Mail Beta. <http://us.rd.yahoo.com/evt=49981/*http://advision.webevents.yahoo.com/mailbeta/features_spam.html>

------------------------------------------------------------------------

_______________________________________________
rules-users mailing list
[email protected]
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
[email protected]
https://lists.jboss.org/mailman/listinfo/rules-users

Reply via email to