I'm trying to run a condition on an array of elements, where each element contains multiple params. I want to evaluate a dynamic condition (as in input from the user) where I want to check if:
(($person1: PersonClass(getName() == "tom") from $persons) || ($person2: PersonClass(getAddress() == "x") from $persons) && ($person3: PersonClass(getCarType() == "aa" from $persons)) && PersonClass($person1 == $person2, $person2 = $person3) from $persons)
I got the error:
unable to resolve method using strict-mode: PersonClass.$person1()]
[Near : {... $person1 == $person2 ....}]
The problem is that Drools evaluates the first group (the || of name equals tom and address equals x, and if true, it continues to the next section. while if the first group is true, this chosen person doesn't need to have a car of type "aa" (it can be any other person from the $persons list, even if its name is not Tom or its address is not "x")
How do I enforce Drools to check the attribute of the entire condition on the same person object?
I'm using latest version of Drools (9.44.0.Final)
You run all your checks at the same time on the same object.
Your example is a little complicated, so I'm going to use a simpler one. Let's say we have an array of Fruit objects which each have some properties -- name, color, price.
Consider this when clause:
This condition will match a fruit that is both red and costs less than $1.00. Considering the objects in working memory listed above, it will only match the strawberry (first item). It will not match the tomato, because the price condition isn't met; and it will not match the blueberry because the price is right but the color is no good.
Compare that to:
This rule will match the first check (color == blue) against blueberry. Then it will match the second check against the only fruit that costs more than $2.00 -- tomato. Tomatoes are not blue, but the two conditions are not being applied to the same fruit because they're in disjoint checks.
Circling back to your rule, a good rule of thumb is that if you find yourself writing "or" conditions, it's very likely that you should instead have two rules.
First, the Drools error is not about the
||at all. It's about the fact that syntactically, this condition makes no sense at all:I can't even begin to try to figure out what you're trying to do here unfortunately.
As a completely wild guess, if you're looking to see if there is a person named Tom with address X OR a person with address X with address AA, then you should write two rules like this: