From f586c6f747ad5e0400cfb24aacd6803d78b45fdc Mon Sep 17 00:00:00 2001 From: Toshiya Kobayashi Date: Wed, 18 Oct 2023 15:42:50 +0900 Subject: [PATCH] [DROOLS-7295] Implement from collect (#39) --- .../main/antlr4/org/drools/parser/DRLLexer.g4 | 1 + .../main/antlr4/org/drools/parser/DRLParser.g4 | 17 +++++++++++------ .../java/org/drools/parser/DRLVisitorImpl.java | 14 +++++++++++--- .../org/drools/parser/MiscDRLParserTest.java | 4 ---- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLLexer.g4 b/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLLexer.g4 index 2bb35093aab6..35bfe12f992f 100644 --- a/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLLexer.g4 +++ b/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLLexer.g4 @@ -44,6 +44,7 @@ DRL_EXISTS : 'exists'; DRL_NOT : 'not'; DRL_IN : 'in'; DRL_FROM : 'from'; +DRL_COLLECT : 'collect'; DRL_MATCHES : 'matches'; DRL_MEMBEROF : 'memberOf'; DRL_ACCUMULATE : 'accumulate' | 'acc'; diff --git a/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLParser.g4 b/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLParser.g4 index d418d82da958..6069196f7c7f 100644 --- a/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLParser.g4 +++ b/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLParser.g4 @@ -53,10 +53,10 @@ lhsExpression : LPAREN lhsExpression RPAREN #lhsExpressionEnclosed | lhsExpression (DRL_OR lhsExpression)+ #lhsOr ; -// and is accepted for accumulate -lhsAndForAccumulate : lhsUnary (DRL_AND lhsUnary)* - | LPAREN DRL_AND lhsUnary+ RPAREN - ; +// lhsAnd is used as a label in lhsExpression rule. But some other rules also use it. +lhsAndDef : lhsUnary (DRL_AND lhsUnary)* + | LPAREN DRL_AND lhsUnary+ RPAREN + ; /* lhsUnary : ( lhsExists namedConsequence? @@ -298,6 +298,7 @@ patternFilter : label IDENTIFIER LPAREN expressionList RPAREN ; | fromExpression ) */ patternSource : fromAccumulate + | fromCollect | fromEntryPoint | fromExpression ; @@ -312,7 +313,7 @@ fromAccumulate := ACCUMULATE LEFT_PAREN lhsAnd (COMMA|SEMICOLON) | accumulateFunction ) RIGHT_PAREN */ -fromAccumulate : DRL_ACCUMULATE LPAREN lhsAndForAccumulate (COMMA|SEMI) +fromAccumulate : DRL_ACCUMULATE LPAREN lhsAndDef (COMMA|SEMI) ( DRL_INIT LPAREN initBlockStatements=blockStatements RPAREN COMMA DRL_ACTION LPAREN actionBlockStatements=blockStatements RPAREN COMMA ( DRL_REVERSE LPAREN reverseBlockStatements=blockStatements RPAREN COMMA)? DRL_RESULT LPAREN expression RPAREN | accumulateFunction ) @@ -326,6 +327,10 @@ accumulateFunction := label? ID parameters */ accumulateFunction : label? IDENTIFIER LPAREN drlExpression RPAREN; +// fromCollect := COLLECT LEFT_PAREN lhsPatternBind RIGHT_PAREN + +fromCollect : DRL_COLLECT LPAREN lhsPatternBind RPAREN ; + fromEntryPoint : DRL_ENTRY_POINT stringId ; /* @@ -363,7 +368,7 @@ lhsForall : DRL_FORALL LPAREN lhsPatternBind+ RPAREN ; * RIGHT_PAREN SEMICOLON? */ -lhsAccumulate : DRL_ACCUMULATE LPAREN lhsAndForAccumulate (COMMA|SEMI) +lhsAccumulate : DRL_ACCUMULATE LPAREN lhsAndDef (COMMA|SEMI) accumulateFunction (COMMA accumulateFunction)* (SEMI constraints)? RPAREN (SEMI)? diff --git a/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLVisitorImpl.java b/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLVisitorImpl.java index 1d33e4597541..783be414b49b 100644 --- a/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLVisitorImpl.java +++ b/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLVisitorImpl.java @@ -16,6 +16,7 @@ import org.drools.drl.ast.descr.AttributeDescr; import org.drools.drl.ast.descr.BaseDescr; import org.drools.drl.ast.descr.BehaviorDescr; +import org.drools.drl.ast.descr.CollectDescr; import org.drools.drl.ast.descr.EntryPointDescr; import org.drools.drl.ast.descr.EvalDescr; import org.drools.drl.ast.descr.ExistsDescr; @@ -314,7 +315,7 @@ public ForallDescr visitLhsForall(DRLParser.LhsForallContext ctx) { @Override public PatternDescr visitLhsAccumulate(DRLParser.LhsAccumulateContext ctx) { AccumulateDescr accumulateDescr = new AccumulateDescr(); - accumulateDescr.setInput(visitLhsAndForAccumulate(ctx.lhsAndForAccumulate())); + accumulateDescr.setInput(visitLhsAndDef(ctx.lhsAndDef())); // accumulate function for (DRLParser.AccumulateFunctionContext accumulateFunctionContext : ctx.accumulateFunction()) { @@ -346,10 +347,17 @@ public FromDescr visitFromExpression(DRLParser.FromExpressionContext ctx) { return fromDescr; } + @Override + public CollectDescr visitFromCollect(DRLParser.FromCollectContext ctx) { + CollectDescr collectDescr = new CollectDescr(); + collectDescr.setInputPattern((PatternDescr) visitLhsPatternBind(ctx.lhsPatternBind())); + return collectDescr; + } + @Override public AccumulateDescr visitFromAccumulate(DRLParser.FromAccumulateContext ctx) { AccumulateDescr accumulateDescr = new AccumulateDescr(); - accumulateDescr.setInput(visitLhsAndForAccumulate(ctx.lhsAndForAccumulate())); + accumulateDescr.setInput(visitLhsAndDef(ctx.lhsAndDef())); if (ctx.DRL_INIT() != null) { // inline custom accumulate accumulateDescr.setInitCode(getTextPreservingWhitespace(ctx.initBlockStatements)); @@ -469,7 +477,7 @@ private AndDescr createAndDescr(List descrList) { } @Override - public BaseDescr visitLhsAndForAccumulate(DRLParser.LhsAndForAccumulateContext ctx) { + public BaseDescr visitLhsAndDef(DRLParser.LhsAndDefContext ctx) { return createAndDescr(visitDescrChildren(ctx)); } diff --git a/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java b/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java index c25bb3274f62..93a98ed9eb1e 100644 --- a/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java +++ b/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java @@ -2047,7 +2047,6 @@ public void parse_AccumulateWithBindings() throws Exception { assertThat(pattern.getObjectType()).isEqualTo("Person"); } - @Disabled("Priority : High | Implement from collect") @Test public void parse_Collect() throws Exception { final PackageDescr pkg = parseAndGetPackageDescrFromFile( @@ -2310,7 +2309,6 @@ public void parse_Restrictions() throws Exception { assertThat(fieldConstr.getExpression()).isEqualTo("bar > 1 || == 1"); } - @Disabled("Priority : High | Implement semicolon delimiter | Implement from collect") @Test public void parse_Semicolon() throws Exception { final PackageDescr pkg = parseAndGetPackageDescrFromFile( @@ -2390,7 +2388,6 @@ public void parse_AccumulateExternalFunction() throws Exception { assertThat(pattern.getObjectType()).isEqualTo("Person"); } - @Disabled("Priority : High | Implement from collect") @Test public void parse_CollectWithNestedFrom() throws Exception { final PackageDescr pkg = parseAndGetPackageDescrFromFile( @@ -2412,7 +2409,6 @@ public void parse_CollectWithNestedFrom() throws Exception { assertThat(people.getObjectType()).isEqualTo("People"); } - @Disabled("Priority : High | Implement accumulate and Implement from collect") @Test public void parse_AccumulateWithNestedFrom() throws Exception { final PackageDescr pkg = parseAndGetPackageDescrFromFile(