[rules-users] accumulate / sum and ArrayList

Edson Tirelli tirelli at post.com
Thu Mar 13 20:17:14 EDT 2008


   Gerald,

   It is not clear if you want to calculate the sum of all integers in the
original list or the sum of all integers greater than 0.
   Assuming you want to sum all integers greater than 0, see example bellow.
Adjust the example to your needs if you want something different.

   You already know how to access the elements of a list:

Number( intValue > 0 ) from $mo.getNumbers()

   You already know how to collect them:

$i : ArrayList ( ) from collect ( Number( intValue > 0 ) from
$mo.getNumbers() )

   If instead of collecting them, you want to "accumulate" them:

$total : Number() from accumulate( $n:Number( intValue > 0 ) from
$mo.getNumbers(),
                                                   sum( $n ) )

  Hope it helps,
      Edson






2008/3/13, Gerald Cantor <geraldmcantor at yahoo.com>:
>
>
> I have an Object in working memory. This object has an ArrayList of
> Integer
> objects. I am trying to create a rule that will count all occurrences of
> Integers in this ArrayList that are non-zero and then calculate the sum of
> all the Integers in the ArrayList and then check the total against a
> range.
>
> I understand how to count the non-zero occurrences but I am at a complete
> loss as to how to calculate the sum of the Integer objects in the
> ArrayList.
>
> Here is the rule I have thus far
> rule "Rule 1"
>         dialect "java"
>         when
>                 $mo : MyObject( )
>                 $i : ArrayList ( ) from collect ( Number( intValue > 0 )
> from
> $mo.getNumbers() )
>                 $total : Number(  ) from accumulate ???
>         then
>                 System.out.println("Rule 1: i.size() = " + $i.size());
>                 System.out.println("Rule 1: total = " + $total);
> end
>
> I have been unsuccessful in finding a post that deals with how to fill in
> the ??? portion of my rule. My apologies in advance if I happen to miss
> such
> a post.
>
> Any help is greatly appreciated.
>
> Regards,
> Gerald
>
> --
> View this message in context:
> http://www.nabble.com/accumulate---sum-and-ArrayList-tp16033216p16033216.html
> Sent from the drools - user mailing list archive at Nabble.com.
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>



-- 
  Edson Tirelli
  JBoss Drools Core Development
  Office: +55 11 3529-6000
  Mobile: +55 11 9287-5646
  JBoss, a division of Red Hat @ www.jboss.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20080313/a8996c28/attachment.html 


More information about the rules-users mailing list