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

[DROOLS-7214] non-executable-model doesn't react to bind-only Map pro… #4771

Merged
merged 3 commits into from
Oct 31, 2022
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 @@ -110,6 +110,8 @@
import static org.drools.compiler.rule.builder.util.PatternBuilderUtil.getNormalizeDate;
import static org.drools.compiler.rule.builder.util.PatternBuilderUtil.normalizeEmptyKeyword;
import static org.drools.compiler.rule.builder.util.PatternBuilderUtil.normalizeStringOperator;
import static org.drools.core.util.StringUtils.doesFirstPropHaveListMapAccessor;
import static org.drools.core.util.StringUtils.extractFirstIdentifier;
import static org.drools.core.util.StringUtils.isIdentifier;

/**
Expand Down Expand Up @@ -1402,8 +1404,12 @@ protected void buildRuleBindings(RuleBuildContext context,

declr.setReadAccessor(extractor);

if (!declr.isFromXpathChunk() && typeDeclaration != null && extractor instanceof ClassFieldReader) {
addFieldToPatternWatchlist(pattern, typeDeclaration, ((ClassFieldReader) extractor).getFieldName());
if (!declr.isFromXpathChunk() && typeDeclaration != null) {
if (extractor instanceof ClassFieldReader) {
addFieldToPatternWatchlist(pattern, typeDeclaration, ((ClassFieldReader) extractor).getFieldName());
} else if (doesFirstPropHaveListMapAccessor(fieldBindingDescr.getBindingField())) {
addFieldToPatternWatchlist(pattern, typeDeclaration, extractFirstIdentifier(fieldBindingDescr.getBindingField(), 0)); // extractor is MVELObjectClassFieldReader
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions drools-core/src/main/java/org/drools/core/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1377,4 +1377,22 @@ public static boolean equalsIgnoreSpaces(String s1, String s2) {
public static String uuid() {
return "x" + UUID.randomUUID().toString().replace( '-', 'x' );
}

public static boolean doesFirstPropHaveListMapAccessor(String expression) {
StringBuilder propertyNameBuilder = new StringBuilder();
int cursor = extractFirstIdentifier(expression, propertyNameBuilder, 0);
Character nextChar = lookAheadIgnoringSpaces(expression, cursor);
return nextChar != null && nextChar.equals('[');
}

public static Character lookAheadIgnoringSpaces(String expression, int cursor) {
while (cursor < expression.length()) {
char c = expression.charAt(cursor);
if (!Character.isWhitespace(c)) {
return c;
}
cursor++;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1636,4 +1636,96 @@ private void testVariousCasePropFact(String modifyStatement, String... expectedR

assertThat(results).containsExactly(expectedResults);
}

@Test
public void bindOnlyPropertyReacts() {
// DROOLS-7214
final String str =
"import " + Person.class.getCanonicalName() + ";\n" +
"dialect \"mvel\"\n" +
"rule R when\n" +
" $p : Person( name == \"Mario\", $age : age )\n" +
"then\n" +
" modify($p) { age = $age + 1 };\n" +
"end\n";

KieSession ksession = getKieSession(str);

Person p = new Person("Mario", 40);
ksession.insert(p);
int fired = ksession.fireAllRules(10); // intentional loop

assertThat(fired).isEqualTo(10);
}

@Test
public void bindOnlyMapPropertyReacts() {
// DROOLS-7214
final String str =
"import " + Person.class.getCanonicalName() + ";\n" +
"dialect \"mvel\"\n" +
"rule R when\n" +
" $p : Person( name == \"Mario\", $map : itemsString )\n" +
"then\n" +
" $p.itemsString[\"B\"] = \"itemB\";\n" +
" modify($p) { itemsString = $p.itemsString };\n" +
"end\n";

KieSession ksession = getKieSession(str);

Person p = new Person("Mario", 40);
p.getItemsString().put("A", "itemA");
ksession.insert(p);
int fired = ksession.fireAllRules(10); // intentional loop

assertThat(fired).isEqualTo(10);
}

@Test
public void bindOnlyMapPropertyWithAccessOperatorReacts() {
// DROOLS-7214
final String str =
"import " + Person.class.getCanonicalName() + ";\n" +
"dialect \"mvel\"\n" +
"rule R when\n" +
" $p : Person( name == \"Mario\", $mapDataA : itemsString[\"A\"] )\n" +
"then\n" +
" $p.itemsString[\"B\"] = \"itemB\";\n" +
" modify($p) { itemsString = $p.itemsString };\n" +
"end\n";

KieSession ksession = getKieSession(str);

Person p = new Person("Mario", 40);
p.getItemsString().put("A", "itemA");
ksession.insert(p);
int fired = ksession.fireAllRules(10); // intentional loop

assertThat(fired).isEqualTo(10);
}

@Test
public void bindOnlyListPropertyWithAccessOperatorReacts() {
// DROOLS-7214
final String str =
"import " + Person.class.getCanonicalName() + ";\n" +
"import " + Address.class.getCanonicalName() + ";\n" +
"dialect \"mvel\"\n" +
"rule R when\n" +
" $p : Person( name == \"Mario\", $listData0 : addresses[0] )\n" +
"then\n" +
" $p.addresses.add(new Address(\"C\"));\n" +
" modify($p) { addresses = $p.addresses };\n" +
"end\n";

KieSession ksession = getKieSession(str);

Person p = new Person("Mario", 40);
p.getAddresses().add(new Address("A"));
p.getAddresses().add(new Address("B"));
ksession.insert(p);
int fired = ksession.fireAllRules(10); // intentional loop

assertThat(fired).isEqualTo(10);
}
}
16 changes: 3 additions & 13 deletions drools-mvel/src/main/java/org/drools/mvel/MVELConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import static org.drools.core.util.StringUtils.codeAwareIndexOf;
import static org.drools.core.util.StringUtils.equalsIgnoreSpaces;
import static org.drools.core.util.StringUtils.extractFirstIdentifier;
import static org.drools.core.util.StringUtils.lookAheadIgnoringSpaces;
import static org.drools.core.util.StringUtils.skipBlanks;

public class MVELConstraint extends MutableTypeConstraint implements IndexableConstraint, AcceptsReadAccessor {
Expand Down Expand Up @@ -534,8 +535,8 @@ private int nextPropertyName(String expression, List<String> names, int cursor)
}

if (!isAccessor) {
String lookAhead = lookAheadIgnoringSpaces(expression, cursor);
boolean isMethodInvocation = lookAhead != null && lookAhead.equals("(");
Character lookAhead = lookAheadIgnoringSpaces(expression, cursor);
boolean isMethodInvocation = lookAhead != null && lookAhead.equals('(');
if (isMethodInvocation) {
return nextPropertyName(expression, names, cursor);
}
Expand All @@ -547,17 +548,6 @@ private int nextPropertyName(String expression, List<String> names, int cursor)
return skipOperator(expression, cursor);
}

private String lookAheadIgnoringSpaces(String expression, int cursor) {
while (cursor < expression.length()) {
char c = expression.charAt(cursor);
if (!Character.isWhitespace(c)) {
return "" + c;
}
cursor++;
}
return null;
}

private int skipOperator(String expression, int cursor) {
if (cursor < expression.length() && expression.charAt(cursor) == '.') {
while (cursor < expression.length() && Character.isWhitespace(expression.charAt(++cursor)));
Expand Down