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 94802923bba..899d7902d3a 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 @@ -4751,4 +4751,31 @@ void andWithMethodCallWithArg() { ExprConstraintDescr exprConstraintDescr = parseAndGetFirstConstraintDescr(text); assertThat(exprConstraintDescr.getExpression()).isEqualTo("value == 10 && someMethod(value) == 4"); } + + @Test + void unificationInAccumulateRule() { + final String text = + "rule R\n" + + "when\n" + + " MyFact($i : currentValue)\n" + + " accumulate( $p : Person( name == \"John\" ),\n" + + " $i := min( $p.getAge() ) )\n" + + "then\n" + + "end"; + RuleDescr rule = parseAndGetFirstRuleDescr(text); + assertThat(rule.getLhs().getDescrs().size()).isEqualTo(2); + + final PatternDescr outPattern = (PatternDescr) rule.getLhs().getDescrs().get(1); + AccumulateDescr accumulateDescr = (AccumulateDescr) outPattern.getSource(); + assertThat(accumulateDescr.getInputPattern()).isInstanceOfSatisfying(PatternDescr.class, patternDescr -> { + assertThat(patternDescr.getIdentifier()).isEqualTo("$p"); + assertThat(patternDescr.getObjectType()).isEqualTo("Person"); + }); + + AccumulateDescr.AccumulateFunctionCallDescr accumulateFunction = accumulateDescr.getFunctions().get(0); + assertThat(accumulateFunction.getBind()).isEqualTo("$i"); + assertThat(accumulateFunction.isUnification()).isTrue(); + assertThat(accumulateFunction.getFunction()).isEqualTo("min"); + assertThat(accumulateFunction.getParams()).containsExactly("$p.getAge()"); + } } 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 6c37402f10f..2416b09cfbf 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 @@ -351,7 +351,7 @@ blockStatements : drlBlockStatement* ; /* accumulateFunction := label? ID parameters */ -accumulateFunction : label? drlIdentifier conditionalExpressions ; +accumulateFunction : (label|unif)? drlIdentifier conditionalExpressions ; // parameters := LEFT_PAREN (conditionalExpression (COMMA conditionalExpression)* )? RIGHT_PAREN conditionalExpressions : LPAREN (conditionalExpression (COMMA conditionalExpression)* )? RPAREN ; diff --git a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java index 1faa36574e8..8212a7f64c4 100644 --- a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java +++ b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java @@ -803,12 +803,19 @@ public AccumulateDescr visitFromAccumulate(DRLParser.FromAccumulateContext ctx) @Override public AccumulateDescr.AccumulateFunctionCallDescr visitAccumulateFunction(DRLParser.AccumulateFunctionContext ctx) { String function = ctx.drlIdentifier().getText(); - String bind = ctx.label() == null ? null : ctx.label().drlIdentifier().getText(); + String bind = null; + boolean unify = false; + if (ctx.label() != null) { + bind = ctx.label().drlIdentifier().getText(); + } else if (ctx.unif() != null) { + bind = ctx.unif().drlIdentifier().getText(); + unify = true; + } String[] params = ctx.conditionalExpressions().conditionalExpression().stream() .map(RuleContext::getText) .toArray(String[]::new); - return new AccumulateDescr.AccumulateFunctionCallDescr(function, bind, false, params); + return new AccumulateDescr.AccumulateFunctionCallDescr(function, bind, unify, params); } @Override