Search code examples
javadroolsrule-engine

How to write Drools rule to validate several values in an array or list


I have an array with a Collection or an array of values. I want to write a rule of this form:

rule "listRule"

when 
$first: from list1() and $first!= "a" and $first!="b"
$second: from list1() and $second!="c" and $second != "z"
then
System.out.println(" this works!")

end

The objective is to be able to evaluate if a list or an array has 2 or more objects($first, $second and so on) that each satisfy a different condition of theirs. It can be an array or list.


Solution

  • Inserting multiple String[] or List<String> objects is almost certainly a bad idea because you won't be able to identify these objects. But here's how:

    rule "listRule"
    when
        $list: ArrayList()
        $s1: String( toString != "a" && != "b" ) from $list
        $s2: String( this != $s1, toString !="c" && != "z" ) from $list
    then
        System.out.println( "Strings: " + $s1 + " - " + $s2 );
    end