This is an automated email from the ASF dual-hosted git repository.
tkobayas pushed a commit to branch dev-new-parser
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git
The following commit(s) were added to refs/heads/dev-new-parser by this push:
new 8b143c31cb [incubator-kie-drools-5912] [new-parser] Corner-case
combinations of not, or, and exists (#5914)
8b143c31cb is described below
commit 8b143c31cb3dabcccb129ac4cf541b3787173e23
Author: Toshiya Kobayashi <[email protected]>
AuthorDate: Fri May 10 10:34:40 2024 +0900
[incubator-kie-drools-5912] [new-parser] Corner-case combinations of not,
or, and exists (#5914)
---
.../drl/parser/antlr4/MiscDRLParserTest.java | 44 ++++++++++++++++++++++
.../org/drools/drl/parser/antlr4/DRLParser.g4 | 8 ++--
2 files changed, 48 insertions(+), 4 deletions(-)
diff --git
a/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java
b/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java
index 899d7902d3..e2a039f709 100644
---
a/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java
+++
b/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java
@@ -4778,4 +4778,48 @@ class MiscDRLParserTest {
assertThat(accumulateFunction.getFunction()).isEqualTo("min");
assertThat(accumulateFunction.getParams()).containsExactly("$p.getAge()");
}
+
+ @Test
+ void existsOrNot() {
+ final String text =
+ "rule R\n" +
+ "when\n" +
+ " exists(not(Integer()) or not(Double()))\n" +
+ "then\n" +
+ "end";
+ RuleDescr rule = parseAndGetFirstRuleDescr(text);
+
assertThat(rule.getLhs().getDescrs().get(0)).isInstanceOfSatisfying(ExistsDescr.class,
existsDescr -> {
+
assertThat(existsDescr.getDescrs().get(0)).isInstanceOfSatisfying(OrDescr.class,
orDescr -> {
+ assertThat(orDescr.getDescrs()).hasSize(2);
+
assertThat(orDescr.getDescrs().get(0)).isInstanceOfSatisfying(NotDescr.class,
notDescr -> {
+
assertThat(notDescr.getDescrs().get(0)).isInstanceOfSatisfying(PatternDescr.class,
patternDescr -> {
+
assertThat(patternDescr.getObjectType()).isEqualTo("Integer");
+ });
+ });
+
assertThat(orDescr.getDescrs().get(1)).isInstanceOfSatisfying(NotDescr.class,
notDescr -> {
+
assertThat(notDescr.getDescrs().get(0)).isInstanceOfSatisfying(PatternDescr.class,
patternDescr -> {
+
assertThat(patternDescr.getObjectType()).isEqualTo("Double");
+ });
+ });
+ });
+ });
+ }
+
+ @Test
+ void nestedNot() {
+ final String text =
+ "rule R\n" +
+ "when\n" +
+ " not ( not ( Cheese() ) )\n" +
+ "then\n" +
+ "end";
+ RuleDescr rule = parseAndGetFirstRuleDescr(text);
+
assertThat(rule.getLhs().getDescrs().get(0)).isInstanceOfSatisfying(NotDescr.class,
notDescr -> {
+
assertThat(notDescr.getDescrs().get(0)).isInstanceOfSatisfying(NotDescr.class,
nestedNotDescr -> {
+
assertThat(nestedNotDescr.getDescrs().get(0)).isInstanceOfSatisfying(PatternDescr.class,
patternDescr -> {
+
assertThat(patternDescr.getObjectType()).isEqualTo("Cheese");
+ });
+ });
+ });
+ }
}
diff --git
a/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLParser.g4
b/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLParser.g4
index 2416b09cfb..bf0d2f41ad 100644
---
a/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLParser.g4
+++
b/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLParser.g4
@@ -105,9 +105,9 @@ queryLhs : lhsExpression* ;
lhsExpression : LPAREN lhsExpression RPAREN
#lhsExpressionEnclosed
| lhsUnary
#lhsUnarySingle
- | LPAREN DRL_AND lhsExpression+ RPAREN #lhsAnd
+ | DRL_AND lhsExpression+ #lhsAnd
| lhsExpression (DRL_AND lhsExpression)+ #lhsAnd
- | LPAREN DRL_OR lhsExpression+ RPAREN #lhsOr
+ | DRL_OR lhsExpression+ #lhsOr
| lhsExpression (DRL_OR lhsExpression)+ #lhsOr
;
@@ -373,7 +373,7 @@ fromWindow : DRL_WINDOW drlIdentifier ;
)
*/
// Use lhsExpression instead of lhsOr because lhsExpression has good enough
structure
-lhsExists : DRL_EXISTS ( lhsPatternBind | lhsExpression ) ;
+lhsExists : DRL_EXISTS ( LPAREN lhsExpression RPAREN | lhsPatternBind ) ;
/*
lhsNot := NOT
@@ -383,7 +383,7 @@ lhsExists : DRL_EXISTS ( lhsPatternBind | lhsExpression ) ;
)
*/
// Use lhsExpression instead of lhsOr because lhsExpression has good enough
structure
-lhsNot : DRL_NOT ( lhsPatternBind | lhsExpression ) ;
+lhsNot : DRL_NOT ( LPAREN lhsExpression RPAREN | lhsPatternBind ) ;
/**
* lhsEval := EVAL LEFT_PAREN conditionalExpression RIGHT_PAREN
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]