Pattern could not parse constrains with '||' (OR expression) into MVELConstraint

81 views
Skip to first unread message

Leo Lin

unread,
Apr 18, 2024, 5:42:39 PMApr 18
to Drools Usage
Hello,
I'm using Drools 8.33.0.Final. I have code to parse RuleImpl and extract all Fields those are being used in the facts.
I found that Pattern and MVELConstraint have pretty different behaviors between '&&' expression and '||' expression.
For example, in the rule:

import com.MockObjectWithProjections
rule "test"
when
  obj : MockObjectWithProjections(value1 == 1 || value2 == 2)
  then
  end

When I'm looking at kieBase.getKiePackages(), the obj is represented as a Pattern and it has only one MVELConstraint
* MVELConstraint (value1 == 1|| value2 == 2), constrainType = UNKNOWEN

However, if it is obj : MockObjectWithProjections(value1 == 1 && value2 == 2),
The pattern has 2 constraints:
* MVELConstraint (value1 == 1), constraintType = EQUAL
* MVELConstraint (value2 == 2), constraintType = EQUAL


The constraints are built by iterating deserialized object ConstraintConnectiveDescr descr, which is in a tree structure. However, this iteration only iterates the first layer of descr. All '&&' condition could be expanded into the first layer list but '||' could not. This caused that '||' expression could not parsed by MVELConstraint and the constraintType is UNKNOWN.

My code use constrains to find out which fields are being used.
Since '||' (double pipe) expression could not be built in MVELConstraint correctly, may I ask if there is any recommendation on how to find which fields are being used?

Thanks,
Leo

Toshiya Kobayashi

unread,
Apr 18, 2024, 10:20:23 PMApr 18
to Drools Usage
Hi Leo,

(value1 == 1 || value2 == 2) is not decomposed into finer constraints, so MVELConstraint (value1 == 1 || value2 == 2) is the expected result. Drools will let Mvel the expression execute.


> My code use constrains to find out which fields are being used.

I think parsing `MVELConstraint.expression` (-> value1 == 1 || value2 == 2) by yourself is the only way to detect the property names.

Regards,
Toshiya

2024年4月19日金曜日 6:42:39 UTC+9 zjli...@gmail.com:
Message has been deleted

Leo Lin

unread,
Apr 19, 2024, 12:15:08 PMApr 19
to Drools Usage
Hi Toshiya,
Thank you for your quick response.
May I ask if you have any recommendation on how to parse ` `MVELConstraint.expression` (-> value1 == 1 || value2 == 2)`?
String parsing seems burdensome as we need to consider different cases(e.g, 3 and more fields, or multiple parenthesis) and then map them back to the object.fields().
May I ask if Drools has any library to support the parsing? Or if we could transform it into other type of Constraints which has a finer Constraint?

Thanks,
Leo

Toshiya Kobayashi

unread,
Apr 21, 2024, 10:19:01 PMApr 21
to Drools Usage
A) If you just want to collect property names, simple String parsing may be enough. You would just read from start to end and pick java identifiers, ignoring String/char literals (surrounded by " or ') and skipping other chars (e.g. operators, parenthesis).

B) Drools internally uses antlr to parse DRL. You can make use of the parser class, which actually decomposes such a "||" constraint into finer elements at the first stage.

You can try like this:
```
DrlExprParser exprParser = new DrlExprParser(LanguageLevelOption.DRL6);
ConstraintConnectiveDescr constraintConnectiveDescr = exprParser.parse("value1 == 1 || value2 == 2");
List<BaseDescr> childDescrs = constraintConnectiveDescr.getDescrs();
...
```
Note that DrlExprParser is an internal class, so it may be changed in the future. If you want to write your own parser class, https://github.com/kiegroup/drools/blob/main/drools-drl/drools-drl-parser/src/main/resources/org/drools/drl/parser/DRL6Expressions.g is the antlr parser definition.

Regards,
Toshiya

2024年4月20日土曜日 1:15:08 UTC+9 zjli...@gmail.com:

Leo Lin

unread,
Apr 22, 2024, 12:29:04 AMApr 22
to Drools Usage
Thank you for answering the question! The answers are pretty informative and helpful!
I will try to leverage DrlExprParser on parsing.

Thanks,
Leo

Luca Molteni

unread,
Apr 22, 2024, 6:04:00 AMApr 22
to drools...@googlegroups.com


> On 22 Apr 2024, at 06:29, Leo Lin <zjli...@gmail.com> wrote:
>
> Thank you for answering the question! The answers are pretty informative and helpful!
> I will try to leverage DrlExprParser on parsing.
>
> Thanks,
> Leo


If you want to get into Drools parsing, we kind of had to parse everything to build the executable model, so by taking a look at the internals of the executable model compiler you’ll find something you might need.

For example while compiling a MVEL expression with the MVEL to Java compiler you can find the used properties in this field

https://github.com/apache/incubator-kie-drools/blob/18047e93c81c3502cd759fbe3e8ce0c8f2e796f4/drools-model/drools-mvel-compiler/src/main/java/org/drools/mvelcompiler/PreprocessPhase.java#L62

Luca

Leo Lin

unread,
Apr 22, 2024, 6:02:19 PMApr 22
to Drools Usage
Thank you Luca!
Reply all
Reply to author
Forward
0 new messages