This is an automated email from the ASF dual-hosted git repository.

mariofusco pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git


The following commit(s) were added to refs/heads/main by this push:
     new f9166188de [kie-issues#941] Ignore constraint in parentheses that is 
bound to a variable (#5750)
f9166188de is described below

commit f9166188dece23f9ca364fc979c9d152709f3e5c
Author: Tibor Zimányi <[email protected]>
AuthorDate: Wed Feb 28 08:49:09 2024 +0100

    [kie-issues#941] Ignore constraint in parentheses that is bound to a 
variable (#5750)
    
    * Add a test case
    
    * Ignore a predicate when it is enclosed and bound to a variable.
    
    * Move the isEnclosedPredicateBoundToVariable to the SingleDrlxParseSuccess
    
    * Fix typos
---
 .../drlxparse/SingleDrlxParseSuccess.java          | 20 ++++++++++++-
 .../model/codegen/execmodel/BindingTest.java       | 35 ++++++++++++++++++++++
 2 files changed, 54 insertions(+), 1 deletion(-)

diff --git 
a/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/drlxparse/SingleDrlxParseSuccess.java
 
b/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/drlxparse/SingleDrlxParseSuccess.java
index d0557abdb5..fa85133a62 100644
--- 
a/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/drlxparse/SingleDrlxParseSuccess.java
+++ 
b/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/drlxparse/SingleDrlxParseSuccess.java
@@ -357,8 +357,26 @@ public class SingleDrlxParseSuccess extends 
AbstractDrlxParseSuccess {
         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;
     }
 
diff --git 
a/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/BindingTest.java
 
b/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/BindingTest.java
index 83e0d3411d..9512691161 100644
--- 
a/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/BindingTest.java
+++ 
b/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/BindingTest.java
@@ -505,4 +505,39 @@ public class BindingTest extends BaseModelTest {
             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();
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to