Drools rule to validate a collection with constrained composite values

I want to check if the collection contains not three elements. In java I would do

!(collection.contains("s1") && collection.contains("s2") && collection.contains("s3"))

How can I do this with drooling? I searched for two hours and tried everything, but could not find a solution to this "simple" problem. I found the “Compound Value Constraint” that I definitely need, but it doesn’t work for collections and the “contains” operator.

I would be grateful for your answers.

Nathanael

+3
source share
2 answers

This does the Java code:

Collection( this not contains "s1" ||
            this not contains "s2" ||
            this not contains "s3")
+2
source

I think you can use the java rule dialect.
 The following sample may help you.

global java.util.ArrayList responseList

rule "checkCollectionRule"
    dialect "java"
    salience -1

    when

        eval(ifContains(responseList, val1, val2.....))

    then

        responseList.add(new Boolean("true"));

end


function Boolean ifContains(List responseList, String val1, String val2,....) {
    return (responseList.contains("s1") && responseList.contains("s2") && responseList.contains("s3"));

}

.  .

0

Source: https://habr.com/ru/post/1783195/


All Articles