This is an automated email from the ASF dual-hosted git repository.
Rikkola pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie.git
The following commit(s) were added to refs/heads/main by this push:
new 641c714ae86 [in[incubator-kie#7311] Drools: Consequence with an
indexed method-call should work (#7312)
641c714ae86 is described below
commit 641c714ae86c0ed16e7189120a61a0bb86614841
Author: Toni Rikkola <[email protected]>
AuthorDate: Wed Nov 25 17:07:25 2026 +0200
[in[incubator-kie#7311] Drools: Consequence with an indexed method-call
should work (#7312)
Consequence.rewriteChannels() called asNameExpr() on every array-access
scope before checking it was 'channels', and hasDroolsScope() went through
DrlxParseUtil.findRootNodeViaScope(), which rejects array access. An expression
such as Color.values()[1].name() therefore failed rule compilation with an
exception on the executable model. Guard the channels rewrite with isNameExpr()
and resolve the drools scope root through array access.
Assisted-by: IBM Bob
---
.../codegen/execmodel/generator/Consequence.java | 63 ++++-
.../org/drools/mvel/integrationtests/EnumTest.java | 285 +++++++++++++++++++++
2 files changed, 345 insertions(+), 3 deletions(-)
diff --git
a/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/Consequence.java
b/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/Consequence.java
index a15816f350e..e00082cfc85 100644
---
a/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/Consequence.java
+++
b/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/Consequence.java
@@ -250,7 +250,7 @@ public class Consequence {
.map(Optional::get)
.filter(sc -> sc instanceof ArrayAccessExpr)
.map(aae -> (ArrayAccessExpr)aae)
- .filter(aae ->
aae.getName().asNameExpr().getNameAsString().equals("channels"))
+ .filter(aae -> aae.getName().isNameExpr() &&
aae.getName().asNameExpr().getNameAsString().equals("channels"))
.forEach(aae -> {
String channelName =
aae.getIndex().asStringLiteralExpr().asString();
MethodCallExpr mce = new MethodCallExpr(new
NameExpr("drools"), GET_CHANNEL_CALL);
@@ -409,7 +409,12 @@ public class Consequence {
if ( !initializedBitmaskFields.contains( updatedVar ) ) {
Set<String> modifiedProps =
findModifiedProperties(methodCallExprs, updatedVar, updatedClass );
-
modifiedProps.addAll(findModifiedPropertiesFromAssignment(assignExprs,
updatedVar));
+ Set<String> assignedProps =
findModifiedPropertiesFromAssignment(assignExprs, updatedVar);
+ if (modifiedProps == null || assignedProps == null) {
+ modifiedProps = null;
+ } else {
+ modifiedProps.addAll(assignedProps);
+ }
MethodCallExpr bitMaskCreation =
createBitMaskInitialization( updatedClass, modifiedProps );
AssignExpr bitMaskAssign =
createBitMaskField(updatedVar, bitMaskCreation);
if (!DrlxParseUtil.hasDuplicateExpr(ruleBlock,
bitMaskAssign)) {
@@ -467,6 +472,14 @@ public class Consequence {
if (!isDirectExpression(methodCall)) {
continue; // don't evaluate a method which is a part of other
expression
}
+ if (hasArrayAccessInScope(methodCall)) {
+ // DrlxParseUtil.findRemoveRootNodeViaScope() rejects array
access in the scope chain
+ if (isNameExprWithName(findRootThroughArrayAccess(methodCall),
updatedVar)) {
+ // can't tell which property is modified, so the mask has
to be all set
+ return null;
+ }
+ continue; // unrelated to the updated fact
+ }
DrlxParseUtil.RemoveRootNodeResult removeRootNodeViaScope =
DrlxParseUtil.findRemoveRootNodeViaScope(methodCall);
Optional<Expression> root = removeRootNodeViaScope.getRootNode()
.filter(s -> isNameExprWithName(s, updatedVar));
@@ -506,7 +519,7 @@ public class Consequence {
}
} else {
// if we were unable to detect the property the mask has
to be all set, so avoid the rest of the cycle
- return new HashSet<>();
+ return null;
}
}
}
@@ -517,6 +530,12 @@ public class Consequence {
Set<String> modifiedProps = new HashSet<>();
for (AssignExpr assignExpr : assignExprs) {
Expression target = assignExpr.getTarget();
+ if (hasArrayAccessInScope(target)) {
+ if (isNameExprWithName(findRootThroughArrayAccess(target),
updatedVar)) {
+ return null;
+ }
+ continue;
+ }
if (target instanceof FieldAccessExpr) {
FieldAccessExpr fieldAccessExpr = (FieldAccessExpr)target;
Expression scope = fieldAccessExpr.getScope();
@@ -537,11 +556,49 @@ public class Consequence {
}
private static boolean hasDroolsScope( MethodCallExpr mce ) {
+ if (hasArrayAccessInScope(mce)) {
+ // DrlxParseUtil.findRootNodeViaScope() rejects array access in
the scope chain
+ return isNameExprWithName(findRootThroughArrayAccess(mce),
"drools");
+ }
return DrlxParseUtil.findRootNodeViaScope(mce)
.filter(s -> isNameExprWithName(s, "drools"))
.isPresent();
}
+ private static boolean hasArrayAccessInScope( Expression expr ) {
+ for (Expression current = expr; current != null; current =
nextInScopeChain(current).orElse(null)) {
+ if (current.isArrayAccessExpr()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static Expression findRootThroughArrayAccess( Expression expr ) {
+ Expression root = expr;
+ Optional<Expression> next;
+ while ((next = nextInScopeChain(root)).isPresent()) {
+ root = next.get();
+ }
+ return root;
+ }
+
+ // One step towards the root of a scope chain, looking through array
access, parentheses and casts
+ private static Optional<Expression> nextInScopeChain( Expression expr ) {
+ if (expr.isArrayAccessExpr()) {
+ return Optional.of(expr.asArrayAccessExpr().getName());
+ } else if (expr.isMethodCallExpr()) {
+ return expr.asMethodCallExpr().getScope();
+ } else if (expr.isFieldAccessExpr()) {
+ return Optional.of(expr.asFieldAccessExpr().getScope());
+ } else if (expr.isEnclosedExpr()) {
+ return Optional.of(expr.asEnclosedExpr().getInner());
+ } else if (expr.isCastExpr()) {
+ return Optional.of(expr.asCastExpr().getExpression());
+ }
+ return Optional.empty();
+ }
+
private boolean isDataStoreScope(Expression scope) {
return scope.isNameExpr() &&
context.getTypedDeclarationById(scope.asNameExpr().getNameAsString())
.map(TypedDeclarationSpec::getDeclarationClass)
diff --git
a/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/integrationtests/EnumTest.java
b/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/integrationtests/EnumTest.java
index 959cc00b58e..8b1ff660118 100644
---
a/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/integrationtests/EnumTest.java
+++
b/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/integrationtests/EnumTest.java
@@ -28,6 +28,7 @@ import
org.drools.testcoverage.common.util.KieBaseTestConfiguration;
import org.drools.testcoverage.common.util.KieBaseUtil;
import org.drools.testcoverage.common.util.KieUtil;
import org.drools.testcoverage.common.util.TestParametersUtil2;
+import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.kie.api.KieBase;
@@ -43,6 +44,20 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class EnumTest {
+ @org.kie.api.definition.type.PropertyReactive
+ public static class FieldHolder {
+ public String name;
+ public StringBuilder[] items;
+
+ public StringBuilder[] getItems() {
+ return items;
+ }
+
+ public void setItems(StringBuilder[] items) {
+ this.items = items;
+ }
+ }
+
public static Stream<KieBaseTestConfiguration> parameters() {
return
TestParametersUtil2.getKieBaseCloudConfigurations(true).stream();
}
@@ -67,6 +82,275 @@ public class EnumTest {
ksession.dispose();
}
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void testIndexedEnumValuesInConsequence(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ final String drl =
+ "package org.test;\n" +
+ "global java.util.List list;\n" +
+ "declare enum Color\n" +
+ " RED, GREEN, BLUE;\n" +
+ "end\n" +
+ "rule R when\n" +
+ "then\n" +
+ " list.add( Color.values()[1].name() );\n" +
+ "end\n";
+
+ KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test",
kieBaseTestConfiguration, drl);
+ KieSession ksession = kbase.newKieSession();
+ final List<Object> list = new java.util.ArrayList<>();
+ ksession.setGlobal("list", list);
+
+ ksession.fireAllRules();
+
+ assertThat(list)
+ .as("values()[1].name() must evaluate like Java, on every
KieBase configuration")
+ .containsExactly("GREEN");
+ ksession.dispose();
+ }
+
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void
testIndexedEnumValuesStatementBeforeUpdateOfPropertyReactiveFact(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ final String drl =
+ "package org.test;\n" +
+ "global java.util.List list;\n" +
+ "declare enum Color\n" +
+ " RED, GREEN, BLUE;\n" +
+ "end\n" +
+ "declare P\n" +
+ " @propertyReactive\n" +
+ " name : String\n" +
+ "end\n" +
+ "rule Init when\n" +
+ "then\n" +
+ " insert( new P() );\n" +
+ "end\n" +
+ "rule R no-loop when\n" +
+ " $p : P()\n" +
+ "then\n" +
+ " Color.values()[1].name();\n" +
+ " update( $p );\n" +
+ " list.add( \"fired\" );\n" +
+ "end\n";
+
+ KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test",
kieBaseTestConfiguration, drl);
+ KieSession ksession = kbase.newKieSession();
+ final List<Object> list = new java.util.ArrayList<>();
+ ksession.setGlobal("list", list);
+
+ ksession.fireAllRules();
+
+ assertThat(list)
+ .as("a standalone values()[1].name() before update() of a
property-reactive fact must not break rule compilation")
+ .containsExactly("fired");
+ ksession.dispose();
+ }
+
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void
testIndexedCallRootedAtUpdatedFactBeforeUpdateOfPropertyReactiveFact(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ final String drl =
+ "package org.test;\n" +
+ "global java.util.List list;\n" +
+ "declare P\n" +
+ " @propertyReactive\n" +
+ " items : String[]\n" +
+ "end\n" +
+ "rule Init when\n" +
+ "then\n" +
+ " P p = new P();\n" +
+ " p.setItems( new String[] { \"a\" } );\n" +
+ " insert( p );\n" +
+ "end\n" +
+ "rule R no-loop when\n" +
+ " $p : P()\n" +
+ "then\n" +
+ " $p.getItems()[0].length();\n" +
+ " update( $p );\n" +
+ " list.add( \"fired\" );\n" +
+ "end\n";
+
+ KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test",
kieBaseTestConfiguration, drl);
+ KieSession ksession = kbase.newKieSession();
+ final List<Object> list = new java.util.ArrayList<>();
+ ksession.setGlobal("list", list);
+
+ ksession.fireAllRules();
+
+ assertThat(list)
+ .as("an indexed call rooted at the updated fact must not break
rule compilation")
+ .containsExactly("fired");
+ ksession.dispose();
+ }
+
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void
testCastEnclosedIndexedCallRootedAtUpdatedFactWidensMask(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ Assumptions.assumeTrue(kieBaseTestConfiguration.isExecutableModel());
+ final String drl =
+ "package org.test;\n" +
+ "global java.util.List list;\n" +
+ "declare P\n" +
+ " @propertyReactive\n" +
+ " name : String\n" +
+ " items : StringBuilder[]\n" +
+ "end\n" +
+ "rule Init salience 100 when\n" +
+ "then\n" +
+ " P p = new P();\n" +
+ " p.setItems( new StringBuilder[] { new StringBuilder() }
);\n" +
+ " insert( p );\n" +
+ "end\n" +
+ "rule ListenOnItems salience 50 when\n" +
+ " P( items != null )\n" +
+ "then\n" +
+ " list.add( \"items\" );\n" +
+ "end\n" +
+ "rule Modify no-loop salience 0 when\n" +
+ " $p : P()\n" +
+ "then\n" +
+ " ((P) $p).getItems()[0].append( \"x\" );\n" +
+ " $p.setName( \"n\" );\n" +
+ " update( $p );\n" +
+ "end\n";
+
+ KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test",
kieBaseTestConfiguration, drl);
+ KieSession ksession = kbase.newKieSession();
+ final List<Object> list = new java.util.ArrayList<>();
+ ksession.setGlobal("list", list);
+
+ ksession.fireAllRules();
+
+ assertThat(list)
+ .as("an indexed call rooted at the updated fact through a
cast/parentheses must widen the "
+ + "update mask, so the rule listening on 'items' is
re-evaluated by update()")
+ .containsExactly("items", "items");
+ ksession.dispose();
+ }
+
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void
testIndexedCallRootedAtUpdatedFactAfterSetterDiscardsCollectedProps(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ final String drl =
+ "package org.test;\n" +
+ "global java.util.List list;\n" +
+ "declare P\n" +
+ " @propertyReactive\n" +
+ " name : String\n" +
+ " items : StringBuilder[]\n" +
+ "end\n" +
+ "rule Init salience 100 when\n" +
+ "then\n" +
+ " P p = new P();\n" +
+ " p.setItems( new StringBuilder[] { new StringBuilder() }
);\n" +
+ " insert( p );\n" +
+ "end\n" +
+ "rule ListenOnItems salience 50 when\n" +
+ " P( items != null )\n" +
+ "then\n" +
+ " list.add( \"items\" );\n" +
+ "end\n" +
+ "rule Modify no-loop salience 0 when\n" +
+ " $p : P()\n" +
+ "then\n" +
+ " $p.setName( \"n\" );\n" +
+ " $p.getItems()[0].append( \"x\" );\n" +
+ " update( $p );\n" +
+ "end\n";
+
+ KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test",
kieBaseTestConfiguration, drl);
+ KieSession ksession = kbase.newKieSession();
+ final List<Object> list = new java.util.ArrayList<>();
+ ksession.setGlobal("list", list);
+
+ ksession.fireAllRules();
+
+ assertThat(list)
+ .as("an unresolvable indexed call after a setter must discard
the already collected "
+ + "properties and widen the update mask to all-set")
+ .containsExactly("items", "items");
+ ksession.dispose();
+ }
+
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void
testEnclosedIndexedCallBeforeUpdateOfPropertyReactiveFact(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ final String drl =
+ "package org.test;\n" +
+ "global java.util.List list;\n" +
+ "declare P\n" +
+ " @propertyReactive\n" +
+ " items : String[]\n" +
+ "end\n" +
+ "rule Init when\n" +
+ "then\n" +
+ " P p = new P();\n" +
+ " p.setItems( new String[] { \"a\" } );\n" +
+ " insert( p );\n" +
+ "end\n" +
+ "rule R no-loop when\n" +
+ " $p : P()\n" +
+ "then\n" +
+ " ($p.getItems()[0]).length();\n" +
+ " update( $p );\n" +
+ " list.add( \"fired\" );\n" +
+ "end\n";
+
+ KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test",
kieBaseTestConfiguration, drl);
+ KieSession ksession = kbase.newKieSession();
+ final List<Object> list = new java.util.ArrayList<>();
+ ksession.setGlobal("list", list);
+
+ ksession.fireAllRules();
+
+ assertThat(list)
+ .as("an enclosed indexed call before update() of a
property-reactive fact must not break rule compilation")
+ .containsExactly("fired");
+ ksession.dispose();
+ }
+
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void
testIndexedCallRootedAtUpdatedFactWithFieldAssignmentWidensMask(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ Assumptions.assumeTrue(kieBaseTestConfiguration.isExecutableModel());
+ final String drl =
+ "package org.test;\n" +
+ "import " + FieldHolder.class.getCanonicalName() + ";\n" +
+ "global java.util.List list;\n" +
+ "rule Init salience 100 when\n" +
+ "then\n" +
+ " FieldHolder h = new FieldHolder();\n" +
+ " h.setItems( new StringBuilder[] { new StringBuilder() }
);\n" +
+ " insert( h );\n" +
+ "end\n" +
+ "rule ListenOnItems salience 50 when\n" +
+ " FieldHolder( items != null )\n" +
+ "then\n" +
+ " list.add( \"items\" );\n" +
+ "end\n" +
+ "rule Modify no-loop salience 0 when\n" +
+ " $p : FieldHolder()\n" +
+ "then\n" +
+ " $p.getItems()[0].append( \"x\" );\n" +
+ " $p.name = \"n\";\n" +
+ " update( $p );\n" +
+ "end\n";
+
+ KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test",
kieBaseTestConfiguration, drl);
+ KieSession ksession = kbase.newKieSession();
+ final List<Object> list = new java.util.ArrayList<>();
+ ksession.setGlobal("list", list);
+
+ ksession.fireAllRules();
+
+ assertThat(list)
+ .as("an unresolvable indexed call combined with a direct field
assignment must keep the "
+ + "update mask all-set, so the rule listening on
'items' is re-evaluated by update()")
+ .containsExactly("items", "items");
+ ksession.dispose();
+ }
+
@ParameterizedTest(name = "KieBase type={0}")
@MethodSource("parameters")
public void
testCrossEnumReferenceWithMisspelledConstant(KieBaseTestConfiguration
kieBaseTestConfiguration) {
@@ -184,5 +468,6 @@ public class EnumTest {
assertThat(rules).isEqualTo(1);
ksession.dispose();
}
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]