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
  • Loading branch information
tkobayas committed May 2, 2022
1 parent 4b2784c commit 123be0c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 10 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 @@ -91,7 +90,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 @@ -269,8 +267,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 @@ -836,9 +839,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
@@ -1,7 +1,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 @@ -371,6 +374,9 @@ public BdHolder(BigDecimal bd1, BigDecimal bd2) {
this.bd2 = bd2;
}

public BdHolder() {
}

public BigDecimal getBd1() {
return bd1;
}
Expand Down Expand Up @@ -459,4 +465,30 @@ private void testBigDecimalArithmeticOperation(String pattern, String bd1, Strin

assertEquals(1, ksession.fireAllRules());
}

@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 @@ -25,6 +25,7 @@
import org.drools.core.addon.ClassTypeResolver;
import org.drools.core.addon.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 @@ -39,7 +40,7 @@ public class ConstraintParserTest {

@Before
public void setup() {
PackageModel packageModel = new PackageModel(new ReleaseIdImpl("org.kie.test", "constraint-parser-test", "1.0.0"), "org.kie.test", null, null, null);
PackageModel packageModel = new PackageModel(new ReleaseIdImpl("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 @@ -98,9 +99,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 @@ -23,12 +23,17 @@
*/
package org.drools.mvel.parser.ast.expr;

import java.math.BigDecimal;
import java.math.BigInteger;

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;
Expand Down Expand Up @@ -109,4 +114,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 123be0c

Please sign in to comment.