Skip to content

Commit

Permalink
[DROOLS-6936] BigDecimalLiteral with binding in mvel dialect causes C…
Browse files Browse the repository at this point in the history
…lassCastException in executable model build (#4352) (#4380)
  • Loading branch information
tkobayas authored May 12, 2022
1 parent 93c3434 commit 9c8331c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BigDecimal> 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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> imports = new HashSet<>();
imports.add("org.drools.modelcompiler.domain.Person");
TypeResolver typeResolver = new ClassTypeResolver(imports, getClass().getClassLoader());
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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())));
}
}

0 comments on commit 9c8331c

Please sign in to comment.