[rules-users] accumulate / collect behaviour question - another stupid question

Edson Tirelli tirelli at post.com
Thu Feb 22 13:38:22 EST 2007


   Michael,

   If I understood what you said :), I think you are right.
   Accumulate is easier to understand from an example.

$total : Double() from accumulate( Cheese( type == 'brie', $price : price )
             init( double total = 0; )
             action( total += $price; )
             result( new Double( total ) ) )

    If you have the above as part of a rule, and you have the following 
facts in your working memory:

Cheese( "stilton", 10 )
Cheese( "stilton", 3 )
Cheese( "brie", 4 )
Cheese( "brie", 12 )
Cheese( "brie", 5 )
Cheese( "gouda", 15 )
Cheese( "gouda", 13 )
Cheese( "muzzarela", 9 )

    First of all, the accumulate CE will match your facts, so given the 
constraint (type == 'brie'), 3 facts will match. Then, the accumulate CE 
will execute the init() block once, execute action for each match (3 
times in this case), and finally will execute result() returning its 
value ( new Double( 21 ) ) in this case.
 
    I hope it is a bit more clear. Feel free to continue asking...

    BTW, accumulate is a flexible and powerfull CE, but it is also a 
really heavy CE from a performance point of view, if you modify facts 
that he matches (Cheese, in this case).

    []s
    Edson

Anstis, Michael (M.) wrote:

>I think you have misunderstood (or at least one of us has!)
>
>The "transaction consistency" rule consists of four patterns:-
>1 - Transaction ( $id : id) pattern. 
>2 - The "$credit : Double() from..." pattern
>3 - The "$debit : Double() from..." pattern 
>4 - eval( ! $credits.equals( $debits ) )
>
>The rule will only be activated when all patterns match. IMO, I think of
>"accumulate" as a nested rule: the "action" will fire when the pattern
>following the "accumulate (..." matches. Therefore pattern 2 above is
>matched when the "accumulate" returns - i.e. it is always matched. The
>same for patterns 1 and 3. Pattern 4 is only matched when the credits
>and debits don't equal.
>
>So for 100 Transaction facts and 10 TransactionEntry facts per each of
>10 unique id's the rule will be activated once per pattern 4 match.
>Pattern 1 always matches; pattern 2 always matches and pattern 3 always
>matches. Although pattern matches 100 times, pattern 2 matches 10 times
>per id and pattern 3 matches 10 times per id. I think it is different to
>consider pattern matches versus rule activations.
>
>I hope this helps!??! - or I'm completely wrong ;-)
>
>Cheers,
>
>Mike
>
>
>-----Original Message-----
>From: rules-users-bounces at lists.jboss.org
>[mailto:rules-users-bounces at lists.jboss.org] On Behalf Of Olenin,
>Vladimir (MOH)
>Sent: 22 February 2007 14:52
>To: Rules Users List
>Subject: RE: [rules-users] accumulate / collect behaviour question -
>another stupid question
>
>  
>
>>So, you are right: action will be executed 10 times for each id and
>>    
>>
>eval 
>  
>
>>will be executed 1 time for each id.
>>    
>>
>
>Hmm... So, the 'action' part of the _rule_ (NOT of the accumulate
>statement)
>will be called 100 times? How should the rule be modified for the action
>to
>be called only 10 times? (once per each unique transaction id).
>Basically
>the idea is to do smth if all debit operations are not balanced with
>credit
>operations within the same transaction id....
>
>Or have I misunderstood
>
>
>
>
>
>-----Original Message-----
>From: rules-users-bounces at lists.jboss.org
>[mailto:rules-users-bounces at lists.jboss.org] On Behalf Of Edson Tirelli
>Sent: 21 February 2007 17:19
>To: Rules Users List
>Subject: Re: [rules-users] accumulate / collect behaviour question -
>another
>stupid question
>
>  Vlad,
>
>Inside an accumulate CE, the action code will be executed once for each 
>match of the given pattern (TransactionEntry in this case).
>Eval CEs are executed once for each tuple that matches all previous 
>patterns.
>So, you are right: action will be executed 10 times for each id and eval
>
>will be executed 1 time for each id.
>
>To understand better how forward rule engines work, there are some books
>
>and articles listed in JBoss Rules project page. I can personally 
>recomend "Expert Systems..." by Gary Riley.
>
>And don't worry about asking. Usually people ask a lot in the begining 
>and then when they feel confident, they start to answer more than ask.
>:)
>
>[]s
>Edson
>
>Olenin, Vladimir (MOH) wrote:
>
>  
>
>>Hi,
>>
>>Edson quoted the following sample rule some time ago:
>>
>>rule "transaction consistency"
>>
>>when
>>
>>Transaction( $id : id )
>>
>>$credits: Double( )
>>
>>from accumulate( TransactionEntry( id == $id, operation == "credit", 
>>$amount : amount ),
>>
>>init( double balance = 0 ),
>>
>>action( balance += $amount ),
>>
>>result( new Double( balance ) ) );
>>
>>$debits: Double( )
>>
>>from accumulate( TransactionEntry( id == $id, operation == "debit", 
>>$amount : amount ),
>>
>>init( double balance = 0 ),
>>
>>action( balance -= $amount ),
>>
>>result( new Double( balance ) ) );
>>
>>eval( ! $credits.equals( $debits ) ) then
>>
>>// inconsistency for transaction $id
>>
>>End
>>
>>If there are 100 transactions overall and only 10 unique transaction 
>>ids (eg, id = 1..10), so that we have on average 10 Transaction facts 
>>per id,
>>
>>- how many times will 'eval' be called?
>>
>>- how many times will the action be called, assuming 'eval' always 
>>returns 'true'?
>>
>>I guess the answer for the above should be '10 times', once per each 
>>id? What if I put another eval statement like "eval($id == 1)" after 
>>existing 'eval'? Would that be evaluated 100 times (for all 
>>Transaction facts) or only 10 times (once per unique id)?
>>
>>I think I'm missing quite significant piece on how tuples are selected
>>    
>>
>
>  
>
>>in DROOLS and how 'eval' and new 'from accumulate' features work....
>>    
>>
>Any 
>  
>
>>pointers would be greatly appreciated! (would some general literature 
>>on rule engines help to clear up this types of questions?...)
>>
>>Thanks,
>>
>>Vlad
>>
>>PS: sorry for posting so many questions and answering so few.....
>>
>>-----------------------------------------------------------------------
>>    
>>
>-
>  
>
>>_______________________________________________
>>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





More information about the rules-users mailing list