javadroolsrule-enginedrools-fusion

Drools - Check if there are multiple events with different properties in wm


I need advice on writing a rule for Drools to check distinct values.

I have an event as such:

public class Event {
    int propertyA;
    String propertyB;
}

and I would like to check if there are Events in the working memory so that their propertyA values are the same, but they have 4+ distinct propertyB values.

For example: If Event would have been a user, and propertyA would have been its id and propertyB a tshirt color, I would be asking: If there's a user who wore tshirts with more than or equal to (>=) 4 different colors.

How can I implement this rule? Thanks!


Solution

  • Locate one Event with a certain A and then collect the set of the Bs from all events with this A.

    $e: Event( $a: propertyA )   // $e ist the last of a group of As
    not Event( this after $e, propertyA == $a ) 
    accumulate( Event( propertyA == $a, $b: propertyB );
                $set: collectSet( $b );
                $set.size() >= 4 )