Skip to content

Commit

Permalink
[incubator-kie-drools-5906] [new-parser] unification in accumulate (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas committed Oct 2, 2024
1 parent 1757266 commit 2c25d36
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2c25d36

Please sign in to comment.