use of org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr in project drools by kiegroup.
the class AbstractExpressionBuilder method narrowExpressionToType.
protected Expression narrowExpressionToType(TypedExpression right, java.lang.reflect.Type leftType) {
Expression expression = right.getExpression();
if (expression instanceof NullLiteralExpr) {
return expression;
}
if (leftType.equals(Double.class)) {
return new CastExpr(PrimitiveType.doubleType(), expression);
}
if (leftType.equals(Long.class)) {
if (right.getType().equals(Double.class) || right.getType().equals(double.class)) {
return new MethodCallExpr(expression, "longValue");
} else {
return new CastExpr(PrimitiveType.longType(), expression);
}
}
if (expression instanceof LiteralExpr) {
if (expression instanceof BigDecimalLiteralExpr) {
return toNewExpr(BigDecimal.class, toStringLiteral(((BigDecimalLiteralExpr) expression).asBigDecimal().toString()));
}
if (expression instanceof BigIntegerLiteralExpr) {
return toNewExpr(toRawClass(leftType), toStringLiteral(((BigIntegerLiteralExpr) expression).asBigInteger().toString()));
}
if (leftType.equals(BigDecimal.class)) {
String expressionString = stringValue(expression);
final BigDecimal bigDecimal = new BigDecimal(expressionString);
return toNewExpr(BigDecimal.class, toStringLiteral(bigDecimal.toString()));
}
if (leftType.equals(BigInteger.class)) {
String expressionString = stringValue(expression);
final BigInteger bigInteger = new BigDecimal(expressionString).toBigInteger();
return toNewExpr(BigInteger.class, toStringLiteral(bigInteger.toString()));
}
if (leftType.equals(float.class)) {
return new DoubleLiteralExpr(expression + "f");
}
}
if (expression instanceof NameExpr) {
if (leftType.equals(BigDecimal.class) && !right.getType().equals(BigDecimal.class)) {
return toNewExpr(BigDecimal.class, expression);
}
if (leftType.equals(BigInteger.class) && !right.getType().equals(BigInteger.class)) {
return toNewExpr(BigInteger.class, expression);
}
}
return expression;
}
use of org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr in project drools by kiegroup.
the class ConstraintParser method toBigDecimalExpression.
// TODO luca this logic should be moved in Constraint compiler?
private Expression toBigDecimalExpression(TypedExpression typedExpression) {
MethodCallExpr toBigDecimalMethod = new MethodCallExpr(null, "org.drools.modelcompiler.util.EvaluationUtil.toBigDecimal");
Expression arg = typedExpression.getExpression();
Optional<Class<?>> originalPatternType = typedExpression.getOriginalPatternType();
ConstraintCompiler constraintCompiler = createConstraintCompiler(context, originalPatternType);
CompiledExpressionResult compiledBlockResult = constraintCompiler.compileExpression(PrintUtil.printNode(arg));
arg = compiledBlockResult.getExpression();
if (arg.isEnclosedExpr()) {
arg = arg.asEnclosedExpr().getInner();
}
if (arg instanceof BigIntegerLiteralExpr) {
arg = new ObjectCreationExpr(null, toClassOrInterfaceType(BigInteger.class), NodeList.nodeList(new StringLiteralExpr(((BigIntegerLiteralExpr) arg).asBigInteger().toString())));
} else if (arg instanceof BigDecimalLiteralExpr) {
arg = new ObjectCreationExpr(null, toClassOrInterfaceType(BigDecimal.class), NodeList.nodeList(new StringLiteralExpr(((BigDecimalLiteralExpr) arg).asBigDecimal().toString())));
}
toBigDecimalMethod.addArgument(arg);
return toBigDecimalMethod;
}
Aggregations