Search in sources :

Example 11 with TypedExpression

use of org.drools.mvelcompiler.ast.TypedExpression in project drools by kiegroup.

the class ConstraintCompiler method compileExpression.

public CompiledExpressionResult compileExpression(Expression parsedExpression) {
    // Avoid processing the LHS as it's not present while compiling an expression
    TypedExpression compiled = new RHSPhase(mvelCompilerContext).invoke(parsedExpression);
    Expression expression = (Expression) compiled.toJavaExpression();
    return new CompiledExpressionResult(expression, compiled.getType()).setUsedBindings(mvelCompilerContext.getUsedBindings());
}
Also used : TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression)

Example 12 with TypedExpression

use of org.drools.mvelcompiler.ast.TypedExpression in project drools by kiegroup.

the class MethodCallExprVisitor method visit.

@Override
public TypedExpression visit(MethodCallExpr n, RHSPhase.Context arg) {
    Optional<TypedExpression> scope = n.getScope().map(s -> s.accept(this, arg));
    TypedExpression name = n.getName().accept(this, new RHSPhase.Context(scope.orElse(null)));
    final List<TypedExpression> arguments = new ArrayList<>(n.getArguments().size());
    for (Expression child : n.getArguments()) {
        TypedExpression a = child.accept(this, arg);
        arguments.add(a);
    }
    Class<?>[] argumentsTypes = parametersType(arguments);
    return parseMethodFromDeclaredFunction(n, arguments).orElseGet(() -> parseMethod(n, scope, name, arguments, argumentsTypes));
}
Also used : TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) Expression(com.github.javaparser.ast.expr.Expression) ArrayList(java.util.ArrayList) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression)

Example 13 with TypedExpression

use of org.drools.mvelcompiler.ast.TypedExpression in project drools by kiegroup.

the class LHSPhase method visit.

@Override
public TypedExpression visit(AssignExpr n, Void arg) {
    logPhase("AssignExpr {}", n);
    TypedExpression target = n.getTarget().accept(this, arg);
    Optional<TypedExpression> bigDecimalConversion = withBigDecimalConversion(n, target, rhsOrError());
    if (bigDecimalConversion.isPresent()) {
        return bigDecimalConversion.get();
    }
    if (target instanceof FieldToAccessorTExpr || target instanceof VariableDeclaratorTExpr || target instanceof MapPutExprT) {
        return target;
    }
    return new AssignExprT(n.getOperator(), target, rhsOrNull());
}
Also used : MapPutExprT(org.drools.mvelcompiler.ast.MapPutExprT) AssignExprT(org.drools.mvelcompiler.ast.AssignExprT) VariableDeclaratorTExpr(org.drools.mvelcompiler.ast.VariableDeclaratorTExpr) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) FieldToAccessorTExpr(org.drools.mvelcompiler.ast.FieldToAccessorTExpr)

Example 14 with TypedExpression

use of org.drools.mvelcompiler.ast.TypedExpression in project drools by kiegroup.

the class LHSPhase method visit.

@Override
public TypedExpression visit(ArrayAccessExpr n, Void arg) {
    if (parentIsExpressionStmt(n)) {
        return rhsOrError();
    }
    TypedExpression name = n.getName().accept(this, arg);
    Optional<Type> type = name.getType();
    if (type.filter(TypeUtils::isCollection).isPresent()) {
        Expression index = n.getIndex();
        if (index.isStringLiteralExpr() || index.isNameExpr()) {
            return new MapPutExprT(name, index, rhsOrNull(), name.getType());
        } else {
            return new ListAccessExprT(name, index, type.get());
        }
    }
    return new UnalteredTypedExpression(n, type.orElse(null));
}
Also used : MapPutExprT(org.drools.mvelcompiler.ast.MapPutExprT) Type(java.lang.reflect.Type) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) ListAccessExprT(org.drools.mvelcompiler.ast.ListAccessExprT) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression)

Example 15 with TypedExpression

use of org.drools.mvelcompiler.ast.TypedExpression in project drools by kiegroup.

the class LHSPhase method bigDecimalCompoundOperator.

/**
 *        Conversion of the compound operator applied to BigDecimal
 *        $p.salary += 50000B;
 *        $p.setSalary($p.getSalary().add(new BigDecimal(\"50000\")));
 */
private FieldToAccessorTExpr bigDecimalCompoundOperator(TypedExpression fieldAccessScope, String accessorName, Class<?> scopeType, AssignExpr.Operator parentOperator, Method setter) {
    String bigDecimalArithmeticMethod = BigDecimalArithmeticExprT.toBigDecimalMethod(parentOperator);
    Method optGetter = ofNullable(getAccessor(scopeType, accessorName)).orElseThrow(() -> new MvelCompilerException("No getter found but setter is present for accessor: " + accessorName));
    FieldToAccessorTExpr getterExpression = new FieldToAccessorTExpr(fieldAccessScope, optGetter, emptyList());
    TypedExpression argument = rhsOrError();
    if (argument.getType().filter(t -> t != BigDecimal.class).isPresent()) {
        argument = new BigDecimalConvertedExprT(argument);
    }
    BigDecimalArithmeticExprT bigDecimalArithmeticExprT = new BigDecimalArithmeticExprT(bigDecimalArithmeticMethod, getterExpression, argument);
    return new FieldToAccessorTExpr(fieldAccessScope, setter, singletonList(bigDecimalArithmeticExprT));
}
Also used : ArrayAccessExpr(com.github.javaparser.ast.expr.ArrayAccessExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) BinaryExprT(org.drools.mvelcompiler.ast.BinaryExprT) ExpressionStmtT(org.drools.mvelcompiler.ast.ExpressionStmtT) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) LoggerFactory(org.slf4j.LoggerFactory) ClassUtils.getAccessor(org.drools.core.util.ClassUtils.getAccessor) Collections.singletonList(java.util.Collections.singletonList) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) BigDecimal(java.math.BigDecimal) FieldToAccessorTExpr(org.drools.mvelcompiler.ast.FieldToAccessorTExpr) Declaration(org.drools.mvelcompiler.context.Declaration) Map(java.util.Map) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) Expression(com.github.javaparser.ast.expr.Expression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) ClassUtils.getSetter(org.drools.core.util.ClassUtils.getSetter) Method(java.lang.reflect.Method) ListAccessExprT(org.drools.mvelcompiler.ast.ListAccessExprT) Node(com.github.javaparser.ast.Node) VariableDeclaratorTExpr(org.drools.mvelcompiler.ast.VariableDeclaratorTExpr) SimpleNameTExpr(org.drools.mvelcompiler.ast.SimpleNameTExpr) Logger(org.slf4j.Logger) AssignExprT(org.drools.mvelcompiler.ast.AssignExprT) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) List(java.util.List) Type(java.lang.reflect.Type) BigDecimalConvertedExprT(org.drools.mvelcompiler.ast.BigDecimalConvertedExprT) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) DrlGenericVisitor(org.drools.mvel.parser.ast.visitor.DrlGenericVisitor) MapPutExprT(org.drools.mvelcompiler.ast.MapPutExprT) Optional(java.util.Optional) MvelCompilerContext(org.drools.mvelcompiler.context.MvelCompilerContext) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) BigDecimalArithmeticExprT(org.drools.mvelcompiler.ast.BigDecimalArithmeticExprT) Collections(java.util.Collections) TypeUtils(org.drools.mvelcompiler.util.TypeUtils) BigDecimalConvertedExprT(org.drools.mvelcompiler.ast.BigDecimalConvertedExprT) BigDecimalArithmeticExprT(org.drools.mvelcompiler.ast.BigDecimalArithmeticExprT) Method(java.lang.reflect.Method) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) FieldToAccessorTExpr(org.drools.mvelcompiler.ast.FieldToAccessorTExpr)

Aggregations

TypedExpression (org.drools.mvelcompiler.ast.TypedExpression)21 UnalteredTypedExpression (org.drools.mvelcompiler.ast.UnalteredTypedExpression)18 Expression (com.github.javaparser.ast.expr.Expression)8 Node (com.github.javaparser.ast.Node)4 Type (java.lang.reflect.Type)4 Optional (java.util.Optional)4 FieldToAccessorTExpr (org.drools.mvelcompiler.ast.FieldToAccessorTExpr)4 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)3 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 Optional.ofNullable (java.util.Optional.ofNullable)3 DrlGenericVisitor (org.drools.mvel.parser.ast.visitor.DrlGenericVisitor)3 DoStmtT (org.drools.mvelcompiler.ast.DoStmtT)3 ForEachStmtT (org.drools.mvelcompiler.ast.ForEachStmtT)3 MapPutExprT (org.drools.mvelcompiler.ast.MapPutExprT)3 WhileStmtT (org.drools.mvelcompiler.ast.WhileStmtT)3 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)2 DoStmt (com.github.javaparser.ast.stmt.DoStmt)2 ForEachStmt (com.github.javaparser.ast.stmt.ForEachStmt)2