[rules-users] Simple aggregation problem

Davide Sottara dsotty at gmail.com
Mon Mar 3 17:37:51 EST 2014


Drools does not have "groupBy", the rule you have written will react to
each Product.

You need something like:

                rule Acc
                when
                  // get all Ids
                  accumulate( Product( $id : id ), $ids : collectSet(
$id ) ) 
                  // for each Id
                  $idx : String() from $ids
                  // accumulate the Products
                  accumulate( Product( id == $idx, $p : price ), $sum :
sum( $p ) )
                then
                   System.out.println( $idx + ' : ' + $sum );
                end

This is to give you an idea, for example you could split the rule in two
or use helper facts



On 03/03/2014 12:43 PM, Raja Sekhar wrote:
> Hi Davide,
> I am trying to group by Hild. Here first i will get the hlld into id
> and segregate the results by hild and sum the value t1
> If you take an sql 
> select sum(t1),hlid from product group by hild
> so i will get
> if the data is 
> hlid t1
> 1    10 
> 1    10
> 2   10
> 2   10
> the result would be
> hiid t1
> 1   20
> 2   20
> Hope you understand what i am trying to do here
>
> Regards,
> Raja
>
> Regards,
> Raja
>
>
>
> On 3 March 2014 17:05, Davide Sottara <dsotty at gmail.com
> <mailto:dsotty at gmail.com>> wrote:
>
>     Raja,
>     it is not clear what you are trying to achieve.
>
>     The rule might be well written or not, we don't know your
>     requirements.
>     If "id" is what it should be - an identifier - your accumulate
>     constraint
>     would match a single object for each Product (the original product
>     itself),
>     which looks suspicious.
>
>     I'll assume that you are doing a performance test and you are not
>     satisfied
>     with the overall execution time.
>     The question, however, is: how many facts are you inserting? Are
>     you firing
>     all rules after the insertion of each fact, or just once at the end?
>     Last, but not least, which version are you using?
>
>     Accumulate should be used carefully in combination with insertLogical,
>     since the former updates its result every time its fact collection
>     changes.
>     The latter will generate a new fact, keep track of its dependencies,
>     and replace it when the accumulation result itself changes.
>
>     Of course, if you were inserting 100 facts, the numbers would raise
>     a warning, but could be reasonable for several thousands, if not
>     hundred thousands. Citing one of your previous emails:
>     "I want to test my rules with 10,000 facts to 20,000,000 facts"
>
>     Best
>     Davide
>
>
>
>     On 03/03/2014 12:10 PM, Raja Sekhar wrote:
>>     Hi,
>>     I am using the following rule
>>     package com.demo;
>>     import com.sample.model.Product;
>>     import com.sample.model.Products;
>>     import java.util.List;
>>     import java.util.Set;
>>     dialect "mvel"
>>
>>     rule "Product_0"
>>     no-loop
>>     when
>>     Product(id: hlId) accumulate (e :Product(hlId == id), $sum1 :
>>     sum(e.t1) ) 
>>     then
>>      insertLogical("hid=" + id +",T1=" + $sum1);
>>     end
>>
>>     I am inserting facts (Product objects) into it and firing the rules.
>>     When i took performance stats from visual vm with JMX. got a time
>>     of 3832ms. I can you suggest if i am doing any thing wrong with this.
>>
>>     Regards,
>>     Raja Sekhar 
>>
>>     -- 
>>     Raja Sekhar Amirapu
>>     ------------------------------------------------------
>>     "If any anyone can do it, i can do it. If no one else can do it,
>>     i must do it"
>>
>>
>>     _______________________________________________
>>     rules-users mailing list
>>     rules-users at lists.jboss.org <mailto:rules-users at lists.jboss.org>
>>     https://lists.jboss.org/mailman/listinfo/rules-users
>
>
>     _______________________________________________
>     rules-users mailing list
>     rules-users at lists.jboss.org <mailto:rules-users at lists.jboss.org>
>     https://lists.jboss.org/mailman/listinfo/rules-users
>
>
>
>
> -- 
> Raja Sekhar Amirapu
> ------------------------------------------------------
> "If any anyone can do it, i can do it. If no one else can do it, i
> must do it"
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20140303/33735eed/attachment-0001.html 


More information about the rules-users mailing list