Skip to content

Commit

Permalink
[incubator-kie-drools-5936] [new-parser] || and && should be allowed …
Browse files Browse the repository at this point in the history
…as alternatives to infix or and and (apache#5966)
  • Loading branch information
tkobayas committed Oct 11, 2024
1 parent 4dffe1e commit 47d0617
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5135,11 +5135,85 @@ void accumulateEmptyChunks() {
"end";
RuleDescr rule = parseAndGetFirstRuleDescr(text);

final PatternDescr outPattern = (PatternDescr) rule.getLhs().getDescrs().get( 0 );
final PatternDescr outPattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
final AccumulateDescr accumulateDescr = (AccumulateDescr) outPattern.getSource();
assertThat(accumulateDescr.getInitCode()).isEmpty();
assertThat(accumulateDescr.getActionCode()).isEmpty();
assertThat(accumulateDescr.getReverseCode()).isNull();
assertThat(accumulateDescr.getResultCode()).isEqualTo( "null");
assertThat(accumulateDescr.getResultCode()).isEqualTo("null");
}

@Test
void doublePipeInfixOr() {
final String text =
"rule R\n" +
"when\n" +
" Person()\n" +
" ||\n" +
" Address()\n" +
"then\n" +
"end";
RuleDescr rule = parseAndGetFirstRuleDescr(text);
assertThat(rule.getLhs().getDescrs().get(0)).isInstanceOfSatisfying(OrDescr.class, orDescr -> {
assertThat(orDescr.getDescrs().get(0)).isInstanceOfSatisfying(PatternDescr.class, patternDescr -> {
assertThat(patternDescr.getObjectType()).isEqualTo("Person");
});
assertThat(orDescr.getDescrs().get(1)).isInstanceOfSatisfying(PatternDescr.class, patternDescr -> {
assertThat(patternDescr.getObjectType()).isEqualTo("Address");
});
});
}

@Test
void doubleAmpersandInfixAnd() {
final String text =
"rule R\n" +
"when\n" +
" Person()\n" +
" &&\n" +
" Address()\n" +
"then\n" +
"end";
RuleDescr rule = parseAndGetFirstRuleDescr(text);
assertThat(rule.getLhs().getDescrs().get(0)).isInstanceOfSatisfying(PatternDescr.class, patternDescr -> {
assertThat(patternDescr.getObjectType()).isEqualTo("Person");
});
assertThat(rule.getLhs().getDescrs().get(1)).isInstanceOfSatisfying(PatternDescr.class, patternDescr -> {
assertThat(patternDescr.getObjectType()).isEqualTo("Address");
});
}

@Test
void doubleAmpersandInfixAndInAccumulate() {
final String text =
"rule R\n" +
"when\n" +
" accumulate( FactA($a : value) && FactB($b : value);\n" +
" $avg : average($a + $b))\n" +
"then\n" +
"end";
RuleDescr rule = parseAndGetFirstRuleDescr(text);
final PatternDescr outPattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
AccumulateDescr accumulateDescr = (AccumulateDescr) outPattern.getSource();
assertThat(accumulateDescr.getInput()).isInstanceOfSatisfying(AndDescr.class, andDescr -> {
assertThat(andDescr.getDescrs()).hasSize(2);
assertThat(andDescr.getDescrs().get(0)).isInstanceOfSatisfying(PatternDescr.class, patternDescr -> {
assertThat(patternDescr.getObjectType()).isEqualTo("FactA");
assertThat(patternDescr.getConstraint().getDescrs().get(0)).isInstanceOfSatisfying(ExprConstraintDescr.class, exprConstraintDescr -> {
assertThat(exprConstraintDescr.getExpression()).isEqualTo("$a : value");
});
});
assertThat(andDescr.getDescrs().get(1)).isInstanceOfSatisfying(PatternDescr.class, patternDescr -> {
assertThat(patternDescr.getObjectType()).isEqualTo("FactB");
assertThat(patternDescr.getConstraint().getDescrs().get(0)).isInstanceOfSatisfying(ExprConstraintDescr.class, exprConstraintDescr -> {
assertThat(exprConstraintDescr.getExpression()).isEqualTo("$b : value");
});
});
});

AccumulateDescr.AccumulateFunctionCallDescr accumulateFunction = accumulateDescr.getFunctions().get(0);
assertThat(accumulateFunction.getBind()).isEqualTo("$avg");
assertThat(accumulateFunction.getFunction()).isEqualTo("average");
assertThat(accumulateFunction.getParams()).containsExactly("$a + $b");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,16 @@ lhs : DRL_WHEN lhsExpression* ;
queryLhs : lhsExpression* ;

lhsExpression : LPAREN lhsExpression RPAREN #lhsExpressionEnclosed
| lhsUnary #lhsUnarySingle
| DRL_AND drlAnnotation* lhsExpression+ #lhsAnd
| lhsExpression (DRL_AND drlAnnotation* lhsExpression)+ #lhsAnd
| DRL_OR drlAnnotation* lhsExpression+ #lhsOr
| lhsExpression (DRL_OR drlAnnotation* lhsExpression)+ #lhsOr
| lhsExpression ((DRL_OR|OR) drlAnnotation* lhsExpression)+ #lhsOr
| DRL_AND drlAnnotation* lhsExpression+ #lhsAnd
| lhsExpression ((DRL_AND|AND) drlAnnotation* lhsExpression)+ #lhsAnd
| lhsUnary #lhsUnarySingle
;

// lhsAnd is used as a label in lhsExpression rule. But some other rules explicitly use the def, so lhsAndDef is declared.
lhsAndDef : LPAREN lhsAndDef RPAREN
| lhsUnary (DRL_AND lhsUnary)*
| lhsUnary ((DRL_AND|AND) lhsUnary)*
| LPAREN DRL_AND lhsUnary+ RPAREN
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public static String getTextPreservingWhitespace(ParserRuleContext ctx) {
* Get text from List of ParserRuleContext's CharStream without trimming whitespace
*/
public static String getTextPreservingWhitespace(List<? extends ParserRuleContext> ctx) {
if (ctx == null) {
return "";
}
return ctx.stream().map(Antlr4ParserStringUtils::getTextPreservingWhitespace).collect(Collectors.joining());
}

Expand All @@ -65,6 +68,9 @@ public static String getTextPreservingWhitespace(List<? extends ParserRuleContex
* Unlike getTextPreservingWhitespace, this method reflects Lexer normalizeString
*/
public static String getTokenTextPreservingWhitespace(ParserRuleContext ctx, TokenStream tokenStream) {
if (ctx == null) {
return "";
}
return tokenStream.getText(ctx.start, ctx.stop);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ public AccumulateDescr.AccumulateFunctionCallDescr visitAccumulateFunction(DRLPa
}

String[] params = ctx.conditionalExpressions().conditionalExpression().stream()
.map(RuleContext::getText)
.map(Antlr4ParserStringUtils::getTextPreservingWhitespace)
.toArray(String[]::new);
return new AccumulateDescr.AccumulateFunctionCallDescr(function, bind, unify, params);
}
Expand Down

0 comments on commit 47d0617

Please sign in to comment.