Skip to content

Commit

Permalink
[DROOLS-4658] Executable model compilation fails with ClassCastExcept…
Browse files Browse the repository at this point in the history
…ion when using accumulate and additional constraint (apache#2618)

* Test

* Recursively find old pattern

* Refactor
  • Loading branch information
lucamolteni authored and mariofusco committed Oct 24, 2019
1 parent 12a9343 commit a47901f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Expand Up @@ -61,7 +61,9 @@ protected void processNewBinding(MethodCallExpr accumulateDSL) {
}

private void composeTwoBindings(MethodCallExpr newBindingExpression, MethodCallExpr pattern) {
pattern.getParentNode().ifPresent(oldBindExpression -> {
Optional<Node> oldBinding = findOldBinding(pattern);

oldBinding.ifPresent(oldBindExpression -> {
MethodCallExpr oldBind = (MethodCallExpr) oldBindExpression;

LambdaExpr oldBindLambda = (LambdaExpr) oldBind.getArgument(1);
Expand All @@ -77,6 +79,20 @@ private void composeTwoBindings(MethodCallExpr newBindingExpression, MethodCallE
});
}

// Navigate to the first parent that is a Binding Expression
private static Optional<Node> findOldBinding(Node pattern) {

Optional<Node> parentNodeBindExpression = pattern.getParentNode().filter(parent -> {
boolean isMethodCallExpr = parent instanceof MethodCallExpr;
return isMethodCallExpr && ((MethodCallExpr) parent).getNameAsString().equals(BIND_CALL);
});

return parentNodeBindExpression
.map(Optional::of)
.orElseGet(() -> pattern.getParentNode().flatMap(AccumulateVisitorPatternDSL::findOldBinding));
}


private void addBindAsLastChainCall(MethodCallExpr newBindingExpression, MethodCallExpr pattern) {
final Optional<Node> optParent = pattern.getParentNode();
newBindingExpression.setScope(pattern);
Expand Down
Expand Up @@ -1139,6 +1139,33 @@ public void testAccumulateWithMaxCalendar() {
Assertions.assertThat(ksession.fireAllRules()).isEqualTo(1);
}

@Test
public void testAccumulateWithMaxCalendarAndConstraint() {
String str =
"import " + Customer.class.getCanonicalName() + ";\n" +
"import " + StockTick.class.getCanonicalName() + ";\n" +
"rule AccumulateMaxDate\n" +
" dialect \"java\"\n" +
" when\n" +
" $customer : Customer( code == \"RHT\" )\n" +
" $max1 : Number() from accumulate(\n" +
" StockTick( company == $customer.code\n" +
" , $time : dueDate);\n" +
" max($time.getTime().getTime()))\n" +
"then\n" +
"end\n";

KieSession ksession = getKieSession(str);

StockTick st = new StockTick("RHT");
st.setDueDate(Calendar.getInstance());
Customer c = new Customer();
c.setCode("RHT");
ksession.insert(st);
ksession.insert(c);
Assertions.assertThat(ksession.fireAllRules()).isEqualTo(1);
}

@Test
public void testNoBinding() {

Expand Down

0 comments on commit a47901f

Please sign in to comment.