Skip to content

Commit

Permalink
[incubator-kie-drools-5945] [new-parser] Broken named consequences in…
Browse files Browse the repository at this point in the history
…side OR (apache#5950)

* Tests for incubator-kie-drools-5945

* [incubator-kie-drools-5945] [new-parser] Broken named consequences inside OR

---------

Co-authored-by: Jiří Locker <[email protected]>
  • Loading branch information
tkobayas and yurloc committed Jun 11, 2024
1 parent 5411e12 commit 04c54cd
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4657,6 +4657,98 @@ void namedConsequenceOrWithBindVariables() {
assertThat(namedConsequenceDescr.getName()).isEqualTo("FoundMarkOrMario");
}

@Test
void testNamedConsequencesInsideOR1() {
final String text =
"import org.drools.mvel.compiler.Cheese;\n " +
"global java.util.List results;\n" +
"\n" +
"rule R1 when\n" +
" ( $a: Cheese ( type == \"stilton\" ) do[t1]\n" +
" or\n" +
" $b: Cheese ( type == \"gorgonzola\" ) )\n" +
" $c: Cheese ( type == \"cheddar\" )\n" +
"then\n" +
" results.add( $c.getType() );\n" +
"then[t1]\n" +
" results.add( $a.getType() );\n" +
"end\n";
PackageDescr packageDescr = parseAndGetPackageDescr(text);
RuleDescr ruleDescr = packageDescr.getRules().get(0);

assertThat(ruleDescr.getLhs().getDescrs()).hasSize(2);
assertThat(ruleDescr.getLhs().getDescrs()).first().isInstanceOfSatisfying(OrDescr.class, stiltonOrGorgonzola -> {
assertThat(stiltonOrGorgonzola.getDescrs()).hasSize(2);
assertThat(stiltonOrGorgonzola.getDescrs()).first().isInstanceOfSatisfying(AndDescr.class, andDescr -> {
assertThat(andDescr.getDescrs()).hasSize(2);
assertThat(andDescr.getDescrs()).first().isInstanceOfSatisfying(PatternDescr.class, stilton -> {
assertThat(stilton.getIdentifier()).isEqualTo("$a");
assertThat(stilton.getObjectType()).isEqualTo("Cheese");
assertThat(stilton.getConstraint().toString()).contains("stilton");
});
assertThat(andDescr.getDescrs()).last().isInstanceOfSatisfying(NamedConsequenceDescr.class, namedConsequenceDescr -> {
assertThat(namedConsequenceDescr.getName()).isEqualTo("t1");
});
});
assertThat(stiltonOrGorgonzola.getDescrs()).last().isInstanceOfSatisfying(PatternDescr.class, gorgonzola -> {
assertThat(gorgonzola.getIdentifier()).isEqualTo("$b");
assertThat(gorgonzola.getObjectType()).isEqualTo("Cheese");
assertThat(gorgonzola.getConstraint().toString()).contains("gorgonzola");
});
});
assertThat(ruleDescr.getLhs().getDescrs()).last().isInstanceOfSatisfying(PatternDescr.class, cheddar -> {
assertThat(cheddar.getIdentifier()).isEqualTo("$c");
assertThat(cheddar.getObjectType()).isEqualTo("Cheese");
assertThat(cheddar.getConstraint().toString()).contains("cheddar");
});
}

@Test
void testNamedConsequencesInsideOR2() {
final String text =
"import org.drools.mvel.compiler.Cheese;\n " +
"global java.util.List results;\n" +
"\n" +
"rule R1 when\n" +
" ( $a: Cheese ( type == \"stilton\" )\n" +
" or\n" +
" $b: Cheese ( type == \"gorgonzola\" ) do[t1] )\n" +
" $c: Cheese ( type == \"cheddar\" )\n" +
"then\n" +
" results.add( $c.getType() );\n" +
"then[t1]\n" +
" results.add( $b.getType() );\n" +
"end\n";
PackageDescr packageDescr = parseAndGetPackageDescr(text);
RuleDescr ruleDescr = packageDescr.getRules().get(0);

assertThat(ruleDescr.getLhs().getDescrs()).hasSize(2);
assertThat(ruleDescr.getLhs().getDescrs()).first().isInstanceOfSatisfying(OrDescr.class, stiltonOrGorgonzola -> {
assertThat(stiltonOrGorgonzola.getDescrs()).hasSize(2);
assertThat(stiltonOrGorgonzola.getDescrs()).first().isInstanceOfSatisfying(PatternDescr.class, stilton -> {
assertThat(stilton.getIdentifier()).isEqualTo("$a");
assertThat(stilton.getObjectType()).isEqualTo("Cheese");
assertThat(stilton.getConstraint().toString()).contains("stilton");
});
assertThat(stiltonOrGorgonzola.getDescrs()).last().isInstanceOfSatisfying(AndDescr.class, andDescr -> {
assertThat(andDescr.getDescrs()).hasSize(2);
assertThat(andDescr.getDescrs()).first().isInstanceOfSatisfying(PatternDescr.class, gorgonzola -> {
assertThat(gorgonzola.getIdentifier()).isEqualTo("$b");
assertThat(gorgonzola.getObjectType()).isEqualTo("Cheese");
assertThat(gorgonzola.getConstraint().toString()).contains("gorgonzola");
});
assertThat(andDescr.getDescrs()).last().isInstanceOfSatisfying(NamedConsequenceDescr.class, namedConsequenceDescr -> {
assertThat(namedConsequenceDescr.getName()).isEqualTo("t1");
});
});
});
assertThat(ruleDescr.getLhs().getDescrs()).last().isInstanceOfSatisfying(PatternDescr.class, cheddar -> {
assertThat(cheddar.getIdentifier()).isEqualTo("$c");
assertThat(cheddar.getObjectType()).isEqualTo("Cheese");
assertThat(cheddar.getConstraint().toString()).contains("cheddar");
});
}

@Test
public void queryComplexLhs() {
final String text = "query isContainedIn(String x, String y)\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1078,8 +1078,19 @@ public BaseDescr visitLhsAndDef(DRLParser.LhsAndDefContext ctx) {
}

@Override
public List<BaseDescr> visitLhsUnary(DRLParser.LhsUnaryContext ctx) {
return visitDescrChildren(ctx); // lhsUnary may have consequenceInvocation, so not always a single child
public BaseDescr visitLhsUnary(DRLParser.LhsUnaryContext ctx) {
List<BaseDescr> children = visitDescrChildren(ctx);
if (children.size() > 1) {
// lhsUnary may have multiple children e.g. consequenceInvocation, connect with AND
AndDescr andDescr = BaseDescrFactory.builder(new AndDescr())
.withParserRuleContext(ctx)
.build();
children.forEach(andDescr::addDescr);
return andDescr;
} else {
// size == 1. children never be empty
return children.get(0);
}
}

/**
Expand Down

0 comments on commit 04c54cd

Please sign in to comment.