Skip to content

Commit

Permalink
[DROOLS-2996] fix property reactivity introspection when a method inv…
Browse files Browse the repository at this point in the history
…ocation is on the right side of a constraint
  • Loading branch information
mariofusco committed Oct 1, 2018
1 parent a230836 commit c49666e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
Expand Up @@ -443,12 +443,16 @@ private String getPropertyNameFromSimpleExpression(String simpleExpression) {
propertyNameBuilder = new StringBuilder();
extractFirstIdentifier(simpleExpression, propertyNameBuilder, cursor);
propertyName = propertyNameBuilder.toString();
} else if (propertyName.equals("null") || propertyName.equals("true") || propertyName.equals("false")) {
propertyNameBuilder = new StringBuilder();
extractFirstIdentifier(simpleExpression, propertyNameBuilder, cursor);
propertyName = propertyNameBuilder.toString();
}

if (propertyName.startsWith("is") || propertyName.startsWith("get")) {
int exprPos = simpleExpression.indexOf(propertyName);
int propNameEnd = exprPos + propertyName.length();
if (simpleExpression.length() > propNameEnd + 2 && simpleExpression.charAt(propNameEnd) == '(') {
if (simpleExpression.length() > propNameEnd + 1 && simpleExpression.charAt(propNameEnd) == '(') {
propertyName = getter2property(propertyName);
}
}
Expand Down
Expand Up @@ -925,15 +925,17 @@ public static String extractFirstIdentifier(String string, int start) {
}

public static int extractFirstIdentifier(String string, StringBuilder builder, int start) {
boolean isQuoted = false;
boolean started = false;
int i = start;
for (; i < string.length(); i++) {
char ch = string.charAt(i);
if (Character.isJavaIdentifierStart(ch)) {
if (!isQuoted && Character.isJavaIdentifierStart(ch)) {
builder.append(ch);
started = true;
}
else if (started && Character.isJavaIdentifierPart(ch)) {
} else if (ch == '"' || ch == '\'') {
isQuoted = !isQuoted;
} else if (started && Character.isJavaIdentifierPart(ch)) {
builder.append(ch);
} else if (started) {
break;
Expand Down
Expand Up @@ -332,14 +332,24 @@ private TypedExpressionResult toTypedExpressionFromMethodCallOrField(Expression
firstNode = firstChild;
}

if(originalTypeCursor != null && originalTypeCursor.equals(Object.class)) {
if (originalTypeCursor != null && originalTypeCursor.equals(Object.class)) {
// try infer type from the declarations
final Optional<DeclarationSpec> declarationById = ruleContext.getDeclarationById(firstChild.toString());
originalTypeCursor = declarationById.map(d -> (java.lang.reflect.Type)d.getDeclarationClass()).orElse(originalTypeCursor);
}

final Optional<TypedExpressionCursor> teCursor = processFirstNode(drlxExpr, childrenNodes, firstNode, isInLineCast, originalTypeCursor);

if (firstNode instanceof MethodCallExpr) {
MethodCallExpr firstMethod = ( MethodCallExpr ) firstNode;
if (firstMethod.getArguments().isEmpty()) {
String firstProp = getter2property(firstMethod.getNameAsString());
if (firstProp != null) {
context.addReactOnProperties( firstProp );
}
}
}

Expression previous;
java.lang.reflect.Type typeCursor;
if(!teCursor.isPresent()) {
Expand Down
Expand Up @@ -29,6 +29,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class PropertyReactivityTest extends BaseModelTest {

Expand Down Expand Up @@ -306,4 +307,49 @@ public void testPROnAtomic() {
ksession.insert(new AtomicInteger(0));
assertEquals( 3, ksession.fireAllRules() );
}

@Test(timeout = 10000L)
public void testPropertyReactivityWith2Rules() {
checkPropertyReactivityWith2Rules( "age == 41\n" );
}

@Test(timeout = 10000L)
public void testPropertyReactivityWith2RulesUsingAccessor() {
checkPropertyReactivityWith2Rules( "getAge() == 41\n" );
}

@Test(timeout = 10000L)
public void testPropertyReactivityWith2RulesLiteralFirst() {
checkPropertyReactivityWith2Rules( "41 == age\n" );
}

@Test(timeout = 10000L)
public void testPropertyReactivityWith2RulesLiteralFirstUsingAccessor() {
checkPropertyReactivityWith2Rules( "41 == getAge()\n" );
}

private void checkPropertyReactivityWith2Rules( String constraint ) {
final String str =
"import " + Person.class.getCanonicalName() + ";\n" +
"\n" +
"rule R1 when\n" +
" $p : Person( age == 40 )\n" +
"then\n" +
" modify($p) { setAge( $p.getAge()+1 ) };\n" +
"end\n" +
"rule R2 when\n" +
" $p : Person( " + constraint + " )\n" +
"then\n" +
" modify($p) { setEmployed( true ) };\n" +
"end\n";

KieSession ksession = getKieSession( str );

Person p = new Person( "Mario", 40 );
ksession.insert( p );
ksession.fireAllRules();

assertEquals( 41, p.getAge() );
assertTrue( p.getEmployed() );
}
}

0 comments on commit c49666e

Please sign in to comment.