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

[incubator-kie-drools-5957] executable model fails to handle property… #5963

Merged
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 @@ -1181,14 +1181,19 @@ private TypedExpressionCursor thisExpr(Expression drlxExpr, List<Node> childNode
TypedExpressionCursor teCursor;
if (childNodes.size() > 1 && !isInLineCast) {
SimpleName fieldName = null;
if (childNodes.get(1) instanceof NameExpr) {
fieldName = (( NameExpr ) childNodes.get(1 )).getName();
} else if (childNodes.get(1) instanceof SimpleName) {
fieldName = ( SimpleName ) childNodes.get(1 );
Node secondNode = childNodes.get(1);
if (secondNode instanceof NameExpr nameExpr) {
fieldName = nameExpr.getName();
} else if (secondNode instanceof SimpleName simpleName) {
fieldName = simpleName;
}
if (fieldName != null) {
context.addReactOnProperties( getFieldName(drlxExpr, fieldName ) );
}

if (secondNode instanceof MethodCallExpr methodCallExpr) {
addReactOnProperty(methodCallExpr.getNameAsString(), methodCallExpr.getArguments());
}
}
teCursor = new TypedExpressionCursor(new NameExpr(THIS_PLACEHOLDER), originalTypeCursor);
return teCursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,46 @@ public void testComplexSetterArgument() {
assertThat(me.getLikes()).isEqualTo("street1city1");
}

@Test
public void thisWithGetter() {
String str =
"import " + Person.class.getCanonicalName() + ";" +
"rule R \n" +
"when\n" +
" $p: Person(this.getAddress() != null)\n" +
"then\n" +
" modify($p) { setLikes(\"Cheese\") };\n" +
"end";

KieSession ksession = getKieSession( str );

Person me = new Person( "Mario", 40 );
me.setAddress(new Address("street1", 2, "city1"));
ksession.insert( me );

assertThat(ksession.fireAllRules(10)).as("should not loop").isEqualTo(1);
}

@Test
public void nullSafeDereferencing() {
String str =
"import " + Person.class.getCanonicalName() + ";" +
"rule R \n" +
"when\n" +
" $p: Person(address!.street == \"street1\")\n" +
"then\n" +
" modify($p) { setLikes(\"Cheese\") };\n" +
"end";

KieSession ksession = getKieSession( str );

Person me = new Person( "Mario", 40 );
me.setAddress(new Address("street1", 2, "city1"));
ksession.insert( me );

assertThat(ksession.fireAllRules(10)).as("should not loop").isEqualTo(1);
}

@Test
public void testNestedPropInRHS() throws Exception {
// Property Reactivity for "owner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ public void coercionInMethodArgument() {
assertThat(typedExpression.getExpression().toString()).isEqualTo(expected);
}

@Test
public void thisWithGetterReactivity() {
Expression expression = DrlxParseUtil.parseExpression("_this.getAge() > 20").getExpr();
TypedExpressionResult result = new ExpressionTyper(ruleContext, Person.class, null, true).toTypedExpression(expression);
assertThat(result.getReactOnProperties()).containsExactly("age");
}

private TypedExpression toTypedExpression(String inputExpression, Class<?> patternType, TypedDeclarationSpec... declarations) {

for(TypedDeclarationSpec d : declarations) {
Expand Down
Loading