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 7b8a70f67a [incubator-kie-issues#968] Support ConditionalExpr in 
Drools executable model (#5746)
7b8a70f67a is described below

commit 7b8a70f67ab2cfad8eec731d864e5e5903ab6596
Author: Gabriele Cardosi <[email protected]>
AuthorDate: Tue Feb 27 10:48:44 2024 +0100

    [incubator-kie-issues#968] Support ConditionalExpr in Drools executable 
model (#5746)
    
    Co-authored-by: Gabriele-Cardosi <[email protected]>
---
 .../generator/expressiontyper/ExpressionTyper.java |  5 ++
 .../codegen/execmodel/ConditionalExprTest.java     | 87 ++++++++++++++++++++++
 2 files changed, 92 insertions(+)

diff --git 
a/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/expressiontyper/ExpressionTyper.java
 
b/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/expressiontyper/ExpressionTyper.java
index 2aad35aba3..79db23e1c1 100644
--- 
a/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/expressiontyper/ExpressionTyper.java
+++ 
b/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/expressiontyper/ExpressionTyper.java
@@ -44,6 +44,7 @@ import com.github.javaparser.ast.expr.BinaryExpr.Operator;
 import com.github.javaparser.ast.expr.CastExpr;
 import com.github.javaparser.ast.expr.CharLiteralExpr;
 import com.github.javaparser.ast.expr.ClassExpr;
+import com.github.javaparser.ast.expr.ConditionalExpr;
 import com.github.javaparser.ast.expr.DoubleLiteralExpr;
 import com.github.javaparser.ast.expr.EnclosedExpr;
 import com.github.javaparser.ast.expr.Expression;
@@ -380,6 +381,10 @@ public class ExpressionTyper {
             return of(new TypedExpression(drlxExpr, type));
         }
 
+        if (drlxExpr instanceof ConditionalExpr) {
+            return of(new TypedExpression(drlxExpr, Boolean.class));
+        }
+
         if (drlxExpr.isAssignExpr()) {
             AssignExpr assignExpr = drlxExpr.asAssignExpr();
 
diff --git 
a/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/ConditionalExprTest.java
 
b/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/ConditionalExprTest.java
new file mode 100644
index 0000000000..89f89561c7
--- /dev/null
+++ 
b/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/ConditionalExprTest.java
@@ -0,0 +1,87 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.drools.model.codegen.execmodel;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.drools.model.codegen.execmodel.domain.Person;
+import org.junit.Before;
+import org.junit.Test;
+import org.kie.api.runtime.KieSession;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ConditionalExprTest extends BaseModelTest {
+
+    private static final String RULE_STRING = "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 ? true : 
false))\n" +
+            "then \n" +
+            "    System.out.println($booleanVariable); \n" +
+            "    System.out.println($p); \n" +
+            "    booleanListGlobal.add($booleanVariable); \n " +
+            "end \n";
+
+    private KieSession ksession;
+    private List<Boolean> booleanListGlobal;
+
+    public ConditionalExprTest(RUN_TYPE testRunType) {
+        super(testRunType);
+    }
+
+    @Before
+    public void setup() {
+        ksession = getKieSession(RULE_STRING);
+        booleanListGlobal = new ArrayList<>();
+        ksession.setGlobal("booleanListGlobal", booleanListGlobal);
+    }
+
+    @Test
+    public void testConditionalExpressionWithNamedPerson() {
+        try {
+            Person person = new Person("someName");
+            ksession.insert(person);
+            int rulesFired = ksession.fireAllRules();
+            assertThat(rulesFired).isEqualTo(1);
+            
assertThat(booleanListGlobal).isNotEmpty().containsExactly(Boolean.TRUE);
+        } finally {
+            ksession.dispose();
+        }
+    }
+
+    @Test
+    public void testConditionalExpressionWithUnnamedPerson() {
+        try {
+            Person person = new Person();
+            ksession.insert(person);
+            int rulesFired = ksession.fireAllRules();
+            assertThat(rulesFired).isEqualTo(1);
+            
assertThat(booleanListGlobal).isNotEmpty().containsExactly(Boolean.FALSE);
+        } finally {
+            ksession.dispose();
+        }
+    }
+
+}
\ No newline at end of file


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

Reply via email to