Search in sources :

Example 1 with CoercedExpression

use of org.drools.modelcompiler.builder.generator.drlxparse.CoercedExpression in project drools by kiegroup.

the class NativeOperatorSpec method getExpression.

public Expression getExpression(RuleContext context, PointFreeExpr pointFreeExpr, TypedExpression left, ExpressionTyper expressionTyper) {
    MethodCallExpr methodCallExpr = createDslTopLevelMethod(EVAL_CALL);
    String opName = pointFreeExpr.getOperator().asString();
    Operator operator = addOperatorArgument(context, methodCallExpr, opName);
    if (operator != null && !operator.isCompatibleWithType(left.getRawClass())) {
        context.addCompilationError(new InvalidExpressionErrorResult("Cannot use contains on class " + left.getRawClass() + " in expression '" + PrintUtil.printNode(pointFreeExpr) + "'"));
    }
    methodCallExpr.addArgument(left.getExpression());
    for (Expression rightExpr : pointFreeExpr.getRight()) {
        final Optional<TypedExpression> optionalRight = expressionTyper.toTypedExpression(rightExpr).getTypedExpression();
        optionalRight.ifPresent(right -> {
            final TypedExpression coercedRight;
            if (operator != null && operator.requiresCoercion()) {
                final CoercedExpression.CoercedExpressionResult coerced = new CoercedExpression(left, right, false).coerce();
                coercedRight = getCoercedRightExpression(context.getPackageModel(), coerced);
            } else {
                coercedRight = right;
            }
            methodCallExpr.addArgument(coercedRight.getExpression());
        });
    }
    return pointFreeExpr.isNegated() ? new UnaryExpr(methodCallExpr, UnaryExpr.Operator.LOGICAL_COMPLEMENT) : methodCallExpr;
}
Also used : Operator(org.drools.model.functions.Operator) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ConstraintParser.getCoercedRightExpression(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser.getCoercedRightExpression) CoercedExpression(org.drools.modelcompiler.builder.generator.drlxparse.CoercedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) CoercedExpression(org.drools.modelcompiler.builder.generator.drlxparse.CoercedExpression)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 UnaryExpr (com.github.javaparser.ast.expr.UnaryExpr)1 Operator (org.drools.model.functions.Operator)1 InvalidExpressionErrorResult (org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult)1 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)1 CoercedExpression (org.drools.modelcompiler.builder.generator.drlxparse.CoercedExpression)1 ConstraintParser.getCoercedRightExpression (org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser.getCoercedRightExpression)1