Search in sources :

Example 1 with BigIntegerLiteralExpr

use of org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr 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;
}
Also used : BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) BigDecimal(java.math.BigDecimal) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) Expression(com.github.javaparser.ast.expr.Expression) CoercedExpression(org.drools.modelcompiler.builder.generator.drlxparse.CoercedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) CastExpr(com.github.javaparser.ast.expr.CastExpr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BigInteger(java.math.BigInteger) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr)

Example 2 with BigIntegerLiteralExpr

use of org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr 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;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) CompiledExpressionResult(org.drools.mvelcompiler.CompiledExpressionResult) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ConstraintCompiler(org.drools.mvelcompiler.ConstraintCompiler) DrlxParseUtil.createConstraintCompiler(org.drools.modelcompiler.builder.generator.DrlxParseUtil.createConstraintCompiler) BigDecimal(java.math.BigDecimal) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 BigDecimal (java.math.BigDecimal)2 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)2 BigDecimalLiteralExpr (org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr)2 BigIntegerLiteralExpr (org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr)2 CastExpr (com.github.javaparser.ast.expr.CastExpr)1 DoubleLiteralExpr (com.github.javaparser.ast.expr.DoubleLiteralExpr)1 LiteralExpr (com.github.javaparser.ast.expr.LiteralExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 BigInteger (java.math.BigInteger)1 DrlxParseUtil.createConstraintCompiler (org.drools.modelcompiler.builder.generator.DrlxParseUtil.createConstraintCompiler)1 DrlxParseUtil.isThisExpression (org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression)1 CoercedExpression (org.drools.modelcompiler.builder.generator.drlxparse.CoercedExpression)1 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)1 CompiledExpressionResult (org.drools.mvelcompiler.CompiledExpressionResult)1 ConstraintCompiler (org.drools.mvelcompiler.ConstraintCompiler)1