diff --git a/drools-model/drools-model-compiler/src/main/java/org/drools/modelcompiler/builder/generator/drlxparse/ConstraintParser.java b/drools-model/drools-model-compiler/src/main/java/org/drools/modelcompiler/builder/generator/drlxparse/ConstraintParser.java index 7ee2b71927e..67ba97a140e 100644 --- a/drools-model/drools-model-compiler/src/main/java/org/drools/modelcompiler/builder/generator/drlxparse/ConstraintParser.java +++ b/drools-model/drools-model-compiler/src/main/java/org/drools/modelcompiler/builder/generator/drlxparse/ConstraintParser.java @@ -37,7 +37,6 @@ import com.github.javaparser.ast.expr.MethodCallExpr; import com.github.javaparser.ast.expr.NameExpr; import com.github.javaparser.ast.expr.NullLiteralExpr; -import com.github.javaparser.ast.expr.ObjectCreationExpr; import com.github.javaparser.ast.expr.SimpleName; import com.github.javaparser.ast.expr.StringLiteralExpr; import com.github.javaparser.ast.expr.ThisExpr; @@ -95,7 +94,6 @@ import static org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType; import static org.drools.modelcompiler.builder.generator.DrlxParseUtil.isBooleanBoxedUnboxed; import static org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr; -import static org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType; import static org.drools.modelcompiler.builder.generator.drlxparse.MultipleDrlxParseSuccess.createMultipleDrlxParseSuccess; import static org.drools.modelcompiler.builder.generator.drlxparse.SpecialComparisonCase.specialComparisonFactory; import static org.drools.modelcompiler.builder.generator.expressiontyper.FlattenScope.transformFullyQualifiedInlineCastExpr; @@ -274,8 +272,13 @@ private DrlxParseResult compileToJavaRecursive(Class patternType, return parseOOPathExpr( (OOPathExpr) drlxExpr, patternType, bindingId, drlxExpr, hasBind, expression); } - if (drlxExpr instanceof LiteralExpr ) { + if (drlxExpr instanceof LiteralExpr) { Class literalExpressionType = getLiteralExpressionType(((LiteralExpr) drlxExpr)); + if (drlxExpr instanceof BigIntegerLiteralExpr) { + drlxExpr = ((BigIntegerLiteralExpr) drlxExpr).convertToObjectCreationExpr(); + } else if (drlxExpr instanceof BigDecimalLiteralExpr) { + drlxExpr = ((BigDecimalLiteralExpr) drlxExpr).convertToObjectCreationExpr(); + } return new SingleDrlxParseSuccess(patternType, bindingId, drlxExpr, literalExpressionType) .setIsPredicate(isBooleanBoxedUnboxed(literalExpressionType)); } @@ -837,9 +840,9 @@ public static Expression toBigDecimalExpression( TypedExpression typedExpression arg = arg.asEnclosedExpr().getInner(); } if (arg instanceof BigIntegerLiteralExpr) { - arg = new ObjectCreationExpr(null, toClassOrInterfaceType(BigInteger.class), NodeList.nodeList( new StringLiteralExpr(((BigIntegerLiteralExpr) arg).asBigInteger().toString()) )); + arg = ((BigIntegerLiteralExpr) arg).convertToObjectCreationExpr(); } else if (arg instanceof BigDecimalLiteralExpr ) { - arg = new ObjectCreationExpr(null, toClassOrInterfaceType(BigDecimal.class), NodeList.nodeList( new StringLiteralExpr(((BigDecimalLiteralExpr) arg).asBigDecimal().toString()) )); + arg = ((BigDecimalLiteralExpr) arg).convertToObjectCreationExpr(); } toBigDecimalMethod.addArgument( arg ); return toBigDecimalMethod; diff --git a/drools-model/drools-model-compiler/src/test/java/org/drools/modelcompiler/bigdecimaltest/BigDecimalTest.java b/drools-model/drools-model-compiler/src/test/java/org/drools/modelcompiler/bigdecimaltest/BigDecimalTest.java index e2a1bd22ee9..fc99575ef11 100644 --- a/drools-model/drools-model-compiler/src/test/java/org/drools/modelcompiler/bigdecimaltest/BigDecimalTest.java +++ b/drools-model/drools-model-compiler/src/test/java/org/drools/modelcompiler/bigdecimaltest/BigDecimalTest.java @@ -18,7 +18,10 @@ package org.drools.modelcompiler.bigdecimaltest; import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import org.assertj.core.api.Assertions; import org.drools.modelcompiler.BaseModelTest; import org.junit.Test; import org.kie.api.runtime.KieSession; @@ -522,4 +525,30 @@ public void testBigDecimalLiteralRhsNegative() { assertEquals(new BigDecimal("-10.5"), holder.getBd1()); } + + @Test + public void testBigDecimalLiteralWithBinding() { + // DROOLS-6936 + String str = + "package org.drools.modelcompiler.bigdecimals\n" + + "import " + BdHolder.class.getCanonicalName() + ";\n" + + "global java.util.List result;\n" + + "rule R1 dialect \"mvel\"\n" + + "when\n" + + " $holder : BdHolder($bd1 : bd1, $zero : 0B)\n" + + "then\n" + + " result.add($zero);\n" + + "end"; + + KieSession ksession = getKieSession(str); + List result = new ArrayList<>(); + ksession.setGlobal("result", result); + + BdHolder holder = new BdHolder(); + holder.setBd1(new BigDecimal("10")); + ksession.insert(holder); + ksession.fireAllRules(); + + Assertions.assertThat(result).containsExactly(new BigDecimal("0")); + } } diff --git a/drools-model/drools-model-compiler/src/test/java/org/drools/modelcompiler/builder/generator/drlxparse/ConstraintParserTest.java b/drools-model/drools-model-compiler/src/test/java/org/drools/modelcompiler/builder/generator/drlxparse/ConstraintParserTest.java index fd20b3f2b77..2a52f33cbb8 100644 --- a/drools-model/drools-model-compiler/src/test/java/org/drools/modelcompiler/builder/generator/drlxparse/ConstraintParserTest.java +++ b/drools-model/drools-model-compiler/src/test/java/org/drools/modelcompiler/builder/generator/drlxparse/ConstraintParserTest.java @@ -24,6 +24,7 @@ import org.drools.util.ClassTypeResolver; import org.drools.util.TypeResolver; import org.drools.modelcompiler.builder.PackageModel; +import org.drools.modelcompiler.builder.generator.DRLIdGenerator; import org.drools.modelcompiler.builder.generator.RuleContext; import org.drools.modelcompiler.domain.Person; import org.junit.Before; @@ -38,7 +39,7 @@ public class ConstraintParserTest { @Before public void setup() { - PackageModel packageModel = new PackageModel("org.kie.test:constraint-parser-test:1.0.0", "org.kie.test", null, null, null); + PackageModel packageModel = new PackageModel("org.kie.test:constraint-parser-test:1.0.0", "org.kie.test", null, null, new DRLIdGenerator()); Set imports = new HashSet<>(); imports.add("org.drools.modelcompiler.domain.Person"); TypeResolver typeResolver = new ClassTypeResolver(imports, getClass().getClassLoader()); @@ -97,9 +98,21 @@ public void testImplicitCastExpressionWithOr() { public void testMultiplyStringIntWithBindVariableCompareToBigDecimal() { SingleDrlxParseSuccess result = (SingleDrlxParseSuccess) parser.drlxParse(Person.class, "$p", "money == likes * 10"); // assuming likes contains number String - System.out.println(result.getExpr().toString()); - assertEquals("org.drools.modelcompiler.util.EvaluationUtil.equals(org.drools.modelcompiler.util.EvaluationUtil.toBigDecimal(_this.getMoney()), org.drools.modelcompiler.util.EvaluationUtil.toBigDecimal(Double.valueOf(_this.getLikes()) * 10))", result.getExpr().toString()); } + + @Test + public void testBigDecimalLiteralWithBindVariable() { + SingleDrlxParseSuccess result = (SingleDrlxParseSuccess) parser.drlxParse(Person.class, "$p", "$bd : 10.3B"); + + assertEquals("new java.math.BigDecimal(\"10.3\")", result.getExpr().toString()); + } + + @Test + public void testBigIntegerLiteralWithBindVariable() { + SingleDrlxParseSuccess result = (SingleDrlxParseSuccess) parser.drlxParse(Person.class, "$p", "$bi : 10I"); + + assertEquals("new java.math.BigInteger(\"10\")", result.getExpr().toString()); + } } diff --git a/drools-model/drools-mvel-parser/src/main/java/org/drools/mvel/parser/ast/expr/BigDecimalLiteralExpr.java b/drools-model/drools-mvel-parser/src/main/java/org/drools/mvel/parser/ast/expr/BigDecimalLiteralExpr.java index 17387f76709..80a99aced8a 100644 --- a/drools-model/drools-mvel-parser/src/main/java/org/drools/mvel/parser/ast/expr/BigDecimalLiteralExpr.java +++ b/drools-model/drools-mvel-parser/src/main/java/org/drools/mvel/parser/ast/expr/BigDecimalLiteralExpr.java @@ -28,14 +28,18 @@ import com.github.javaparser.TokenRange; import com.github.javaparser.ast.AllFieldsConstructor; import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.expr.LiteralStringValueExpr; +import com.github.javaparser.ast.expr.ObjectCreationExpr; +import com.github.javaparser.ast.expr.StringLiteralExpr; +import com.github.javaparser.ast.type.ClassOrInterfaceType; import com.github.javaparser.ast.visitor.CloneVisitor; -import org.drools.mvel.parser.ast.visitor.DrlGenericVisitor; -import org.drools.mvel.parser.ast.visitor.DrlVoidVisitor; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; import com.github.javaparser.metamodel.JavaParserMetaModel; import com.github.javaparser.metamodel.LongLiteralExprMetaModel; +import org.drools.mvel.parser.ast.visitor.DrlGenericVisitor; +import org.drools.mvel.parser.ast.visitor.DrlVoidVisitor; public final class BigDecimalLiteralExpr extends LiteralStringValueExpr { @@ -108,4 +112,8 @@ public BigDecimalLiteralExpr clone() { public LongLiteralExprMetaModel getMetaModel() { return JavaParserMetaModel.longLiteralExprMetaModel; } + + public ObjectCreationExpr convertToObjectCreationExpr() { + return new ObjectCreationExpr(null, new ClassOrInterfaceType(null, BigDecimal.class.getCanonicalName()), NodeList.nodeList(new StringLiteralExpr(getValue()))); + } } diff --git a/drools-model/drools-mvel-parser/src/main/java/org/drools/mvel/parser/ast/expr/BigIntegerLiteralExpr.java b/drools-model/drools-mvel-parser/src/main/java/org/drools/mvel/parser/ast/expr/BigIntegerLiteralExpr.java index f126155e77d..392bd568e8a 100644 --- a/drools-model/drools-mvel-parser/src/main/java/org/drools/mvel/parser/ast/expr/BigIntegerLiteralExpr.java +++ b/drools-model/drools-mvel-parser/src/main/java/org/drools/mvel/parser/ast/expr/BigIntegerLiteralExpr.java @@ -28,14 +28,18 @@ import com.github.javaparser.TokenRange; import com.github.javaparser.ast.AllFieldsConstructor; import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.expr.LiteralStringValueExpr; +import com.github.javaparser.ast.expr.ObjectCreationExpr; +import com.github.javaparser.ast.expr.StringLiteralExpr; +import com.github.javaparser.ast.type.ClassOrInterfaceType; import com.github.javaparser.ast.visitor.CloneVisitor; -import org.drools.mvel.parser.ast.visitor.DrlGenericVisitor; -import org.drools.mvel.parser.ast.visitor.DrlVoidVisitor; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; import com.github.javaparser.metamodel.JavaParserMetaModel; import com.github.javaparser.metamodel.LongLiteralExprMetaModel; +import org.drools.mvel.parser.ast.visitor.DrlGenericVisitor; +import org.drools.mvel.parser.ast.visitor.DrlVoidVisitor; public final class BigIntegerLiteralExpr extends LiteralStringValueExpr { @@ -109,4 +113,8 @@ public BigIntegerLiteralExpr clone() { public LongLiteralExprMetaModel getMetaModel() { return JavaParserMetaModel.longLiteralExprMetaModel; } + + public ObjectCreationExpr convertToObjectCreationExpr() { + return new ObjectCreationExpr(null, new ClassOrInterfaceType(null, BigInteger.class.getCanonicalName()), NodeList.nodeList(new StringLiteralExpr(getValue()))); + } }