Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kie-issues#941] Ignore constraint in parentheses that is bound to a variable #5750

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,26 @@ public boolean isPredicate() {
return this.isPredicate;
}

/*
* This method finds out, if the parse result is a predicate enclosed in parentheses, bound to a variable.
* Example: Person($booleanVariable: (name != null))
* This shouldn't apply to any other form of predicate. So e.g.
* Person($booleanVariable: (name != null) == "someName") should be properly generated as a constraint.
* After discussions, to align the executable model behaviour with the old non-executable model,
* such predicate is not generated as a rule constraint, and just bound to a variable. This behaviour needs more
* discussions to revisit this behaviour.
*/
private boolean isEnclosedPredicateBoundToVariable() {
final TypedExpression boundExpr = getBoundExpr();
return boundExpr != null
&& boundExpr.getExpression() instanceof EnclosedExpr
&& getExprBinding() != null
&& !getLeft().getExpression().equals(boundExpr.getExpression())
&& !getRight().getExpression().equals(boundExpr.getExpression());
}

public SingleDrlxParseSuccess setIsPredicate(boolean predicate) {
this.isPredicate = predicate;
this.isPredicate = predicate && !isEnclosedPredicateBoundToVariable();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,39 @@ public void testConstraintExpression() {
ksession.dispose();
}
}

/**
* This test checks that a rule is not fired, when a binding is
* enclosed in parentheses. This is intentional behaviour, agreed in discussions,
* which may be revised in the future.
*/
@Test
public void testIgnoreConstraintInParentheses() {
String str = "package constraintexpression\n" +
"\n" +
"import " + Person.class.getCanonicalName() + "\n" +
"import java.util.List; \n" +
"global List<Boolean> booleanListGlobal; \n" +
"rule \"r1\"\n" +
"when \n" +
" $p : Person($booleanVariable: (name == null))\n" +
"then \n" +
" System.out.println($booleanVariable); \n" +
" System.out.println($p); \n" +
" booleanListGlobal.add($booleanVariable); \n " +
"end \n";

KieSession ksession = getKieSession(str);
try {
final List<Boolean> booleanListGlobal = new ArrayList<>();
ksession.setGlobal("booleanListGlobal", booleanListGlobal);
Person person = new Person("someName");
ksession.insert(person);
int rulesFired = ksession.fireAllRules();
assertThat(rulesFired).isEqualTo(1);
assertThat(booleanListGlobal).isNotEmpty().containsExactly(Boolean.FALSE);
} finally {
ksession.dispose();
}
}
}
Loading