This is an automated email from the ASF dual-hosted git repository.
tzimanyi pushed a commit to branch 8.40.x
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git
The following commit(s) were added to refs/heads/8.40.x by this push:
new 342dcb203e [8.40.x_incubator-kie-issues#988] Backport 968 and 978
(#5763)
342dcb203e is described below
commit 342dcb203e42c72c66c4ba7bed24b8895a244866
Author: Gabriele Cardosi <[email protected]>
AuthorDate: Wed Mar 6 17:31:20 2024 +0100
[8.40.x_incubator-kie-issues#988] Backport 968 and 978 (#5763)
* [incubator-kie-issues#968] Support ConditionalExpr in Drools executable
model (#5746)
Co-authored-by: Gabriele-Cardosi <[email protected]>
(cherry picked from commit 7b8a70f67ab2cfad8eec731d864e5e5903ab6596)
* [8.40.x_incubator-kie-issues#988] Fix cherry pick
* [8.40.x_incubator-kie-issues#988] Fix cherry pick
---------
Co-authored-by: Mario Fusco <[email protected]>
Co-authored-by: Gabriele-Cardosi <[email protected]>
---
.../execmodel/generator/OOPathExprGenerator.java | 2 +-
.../generator/drlxparse/ConstraintParser.java | 55 ++++++-------
.../generator/expressiontyper/ExpressionTyper.java | 5 ++
.../codegen/execmodel/ConditionalExprTest.java | 87 +++++++++++++++++++++
.../model/codegen/execmodel/ConstraintTest.java | 90 ++++++++++++++++++++++
5 files changed, 212 insertions(+), 27 deletions(-)
diff --git
a/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/OOPathExprGenerator.java
b/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/OOPathExprGenerator.java
index 9d6b00d56e..81ddb7aa6a 100644
---
a/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/OOPathExprGenerator.java
+++
b/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/OOPathExprGenerator.java
@@ -198,7 +198,7 @@ public class OOPathExprGenerator {
expr.setScope( patternExpr );
patternExpr = expr;
}
- if (singleDrlx.getExpr() != null && !(singleDrlx.getExpr()
instanceof NameExpr)) {
+ if (singleDrlx.getExpr() != null && singleDrlx.isPredicate()) {
MethodCallExpr expr =
expressionBuilder.buildExpressionWithIndexing( singleDrlx );
expr.setScope( patternExpr );
patternExpr = expr;
diff --git
a/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/drlxparse/ConstraintParser.java
b/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/drlxparse/ConstraintParser.java
index a0064a4c02..1ae89fcaa9 100644
---
a/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/drlxparse/ConstraintParser.java
+++
b/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/drlxparse/ConstraintParser.java
@@ -1,20 +1,21 @@
-/*
- * Copyright 2019 Red Hat, Inc. and/or its affiliates.
- *
- * Licensed 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
+/**
+ * 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
*
- * Unless required by applicable law or agreed to in writing, software
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * 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.
+ * 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.generator.drlxparse;
import java.lang.reflect.Type;
@@ -47,7 +48,6 @@ import com.github.javaparser.ast.expr.ThisExpr;
import com.github.javaparser.ast.expr.UnaryExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithArguments;
import com.github.javaparser.ast.nodeTypes.NodeWithOptionalScope;
-import org.drools.util.DateUtils;
import org.drools.model.Index;
import org.drools.model.codegen.execmodel.PackageModel;
import org.drools.model.codegen.execmodel.errors.ParseExpressionErrorResult;
@@ -72,6 +72,7 @@ import org.drools.mvel.parser.ast.expr.PointFreeExpr;
import org.drools.mvel.parser.printer.PrintUtil;
import org.drools.mvelcompiler.CompiledExpressionResult;
import org.drools.mvelcompiler.ConstraintCompiler;
+import org.drools.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -94,10 +95,6 @@ import static
org.drools.model.codegen.execmodel.generator.ConstraintUtil.GREATE
import static
org.drools.model.codegen.execmodel.generator.ConstraintUtil.GREATER_THAN_PREFIX;
import static
org.drools.model.codegen.execmodel.generator.ConstraintUtil.LESS_OR_EQUAL_PREFIX;
import static
org.drools.model.codegen.execmodel.generator.ConstraintUtil.LESS_THAN_PREFIX;
-import static
org.drools.model.codegen.execmodel.generator.expressiontyper.ExpressionTyper.convertArithmeticBinaryToMethodCall;
-import static
org.drools.model.codegen.execmodel.generator.expressiontyper.ExpressionTyper.getBinaryTypeAfterConversion;
-import static
org.drools.model.codegen.execmodel.generator.expressiontyper.ExpressionTyper.shouldConvertArithmeticBinaryToMethodCall;
-import static org.drools.util.StringUtils.lcFirstForBean;
import static
org.drools.model.codegen.execmodel.generator.DrlxParseUtil.THIS_PLACEHOLDER;
import static
org.drools.model.codegen.execmodel.generator.DrlxParseUtil.createConstraintCompiler;
import static
org.drools.model.codegen.execmodel.generator.DrlxParseUtil.getLiteralExpressionType;
@@ -107,9 +104,13 @@ import static
org.drools.model.codegen.execmodel.generator.DslMethodNames.NOT_CA
import static
org.drools.model.codegen.execmodel.generator.DslMethodNames.createDslTopLevelMethod;
import static
org.drools.model.codegen.execmodel.generator.drlxparse.MultipleDrlxParseSuccess.createMultipleDrlxParseSuccess;
import static
org.drools.model.codegen.execmodel.generator.drlxparse.SpecialComparisonCase.specialComparisonFactory;
+import static
org.drools.model.codegen.execmodel.generator.expressiontyper.ExpressionTyper.convertArithmeticBinaryToMethodCall;
+import static
org.drools.model.codegen.execmodel.generator.expressiontyper.ExpressionTyper.getBinaryTypeAfterConversion;
+import static
org.drools.model.codegen.execmodel.generator.expressiontyper.ExpressionTyper.shouldConvertArithmeticBinaryToMethodCall;
import static
org.drools.model.codegen.execmodel.generator.expressiontyper.FlattenScope.transformFullyQualifiedInlineCastExpr;
import static org.drools.mvel.parser.printer.PrintUtil.printNode;
import static org.drools.mvel.parser.utils.AstUtils.isLogicalOperator;
+import static org.drools.util.StringUtils.lcFirstForBean;
/**
* Parses the MVEL String Constraint and compiles it to a Java Expression
@@ -505,21 +506,23 @@ public class ConstraintParser {
Expression withThis = DrlxParseUtil.prepend(new
NameExpr(THIS_PLACEHOLDER), converted.getExpression());
if (hasBind) {
- return new SingleDrlxParseSuccess(patternType, bindingId, null,
converted.getType() )
+ return new SingleDrlxParseSuccess(patternType, bindingId,
withThis, converted.getType() )
.setLeft( new TypedExpression( withThis,
converted.getType() ) )
.addReactOnProperty(
lcFirstForBean(nameExpr.getNameAsString()) );
- } else if (context.hasDeclaration( expression )) {
+ }
+
+ if (context.hasDeclaration( expression )) {
Optional<DeclarationSpec> declarationSpec =
context.getDeclarationById(expression);
if (declarationSpec.isPresent()) {
return new SingleDrlxParseSuccess(patternType, bindingId,
context.getVarExpr(printNode(drlxExpr)),
declarationSpec.get().getDeclarationClass() ).setIsPredicate(true);
} else {
throw new IllegalArgumentException("Cannot find declaration
specification by specified expression " + expression + "!");
}
- } else {
- return new SingleDrlxParseSuccess(patternType, bindingId,
withThis, converted.getType() )
- .addReactOnProperty( nameExpr.getNameAsString() )
- .setIsPredicate(true);
}
+
+ return new SingleDrlxParseSuccess(patternType, bindingId, withThis,
converted.getType() )
+ .addReactOnProperty( nameExpr.getNameAsString() )
+ .setIsPredicate(true);
}
private DrlxParseResult parseFieldAccessExpr( FieldAccessExpr
fieldCallExpr, Class<?> patternType, String bindingId ) {
@@ -1017,7 +1020,7 @@ public class ConstraintParser {
List<BinaryExpr> binaryExprList =
methodCallExpr.findAll(BinaryExpr.class);
for (BinaryExpr binaryExpr : binaryExprList) {
Operator operator = binaryExpr.getOperator();
- boolean arithmeticExpr = ARITHMETIC_OPERATORS.contains(operator);
+ boolean arithmeticExpr = isArithmeticOperator(operator);
if (arithmeticExpr) {
final ExpressionTyperContext expressionTyperContext = new
ExpressionTyperContext();
final ExpressionTyper expressionTyper = new
ExpressionTyper(context, patternType, bindingId, isPositional,
expressionTyperContext);
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 e616ac1c5e..ce4e8f9133 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
@@ -43,6 +43,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;
@@ -379,6 +380,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
diff --git
a/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/ConstraintTest.java
b/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/ConstraintTest.java
new file mode 100644
index 0000000000..b3475e58c2
--- /dev/null
+++
b/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/ConstraintTest.java
@@ -0,0 +1,90 @@
+/**
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.math.BigDecimal;
+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 ConstraintTest extends BaseModelTest {
+
+ private static final String RULE_STRING = "package constraintexpression\n"
+
+ "\n" +
+ "import " + Person.class.getCanonicalName() + "\n" +
+ "import java.util.List; \n" +
+ "import java.math.BigDecimal; \n" +
+ "global List<BigDecimal> bigDecimalListGlobal; \n" +
+ "rule \"r1\"\n" +
+ "when \n" +
+ " $p : Person($amount: (money == null ?
BigDecimal.valueOf(100.0) : money))\n" +
+ "then \n" +
+ " System.out.println($amount); \n" +
+ " System.out.println($p); \n" +
+ " bigDecimalListGlobal.add($amount); \n " +
+ "end \n";
+
+ private KieSession ksession;
+
+ private List<BigDecimal> bigDecimalListGlobal;
+
+ public ConstraintTest(RUN_TYPE testRunType) {
+ super(testRunType);
+ }
+
+ @Before
+ public void setup() {
+ ksession = getKieSession(RULE_STRING);
+ bigDecimalListGlobal = new ArrayList<>();
+ ksession.setGlobal("bigDecimalListGlobal", bigDecimalListGlobal);
+ }
+
+ @Test
+ public void testConstraintWithMoney() {
+ try {
+ BigDecimal money = BigDecimal.valueOf(34.45);
+ Person person = new Person("", money);
+ ksession.insert(person);
+ int rulesFired = ksession.fireAllRules();
+ assertThat(rulesFired).isEqualTo(1);
+
assertThat(bigDecimalListGlobal).isNotEmpty().containsExactly(money);
+ } finally {
+ ksession.dispose();
+ }
+ }
+
+ @Test
+ public void testConstraintWithoutMoney() {
+ try {
+ Person person = new Person();
+ ksession.insert(person);
+ int rulesFired = ksession.fireAllRules();
+ assertThat(rulesFired).isEqualTo(1);
+
assertThat(bigDecimalListGlobal).isNotEmpty().containsExactly(BigDecimal.valueOf(100.0));
+ } finally {
+ ksession.dispose();
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]