[rules-users] Use of helper class to simulate "accumulate"

Jeevan Tambuluri jeevan_tambuluri at symantec.com
Tue Feb 6 20:47:40 EST 2007


Thanks a bunch, Edson! This makes sense now - the use of salience makes 
it all clear...

Jeevan



Edson Tirelli 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
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>  
>>
>
>





More information about the rules-users mailing list