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

Edson Tirelli tirelli at post.com
Wed Feb 21 17:19:04 EST 2007


  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