Search code examples
javarulesdroolsdecision-treerule-engine

Check all elements of a list (Drools Expert)


I'm trying to write rules in Drools Expert. In the when part of the rule, I check some properties of an Application object. This object contains a List and I would like to check if a bunch of rules apply to all objects of SomeOtherType in this list. The rule should fire only when the constraints are valid for ALL objects in that list.

rule "Application eligible"
    when
        app : Application(
               some constrains
               & write some constraints for all objects in app.getList() (a method
               that returns a List<SomeOtherType> object)
        )
    then 
        // application is eligible
end

Solution

  • Insert all your SomeOtherType instances into the working memory too if you haven't already. Then try something like this if you want to check that all SomeOtherType's have the color RED:

    rule "Application eligible"
    when
        $app : Application()
        forall( $x : SomeOtherType( application == $app ) 
                SomeOtherType( this == $x, color == RED ) )
    then 
        // application is eligible
    end