Search in sources :

Example 1 with TypedExpressionResult

use of org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult in project drools by kiegroup.

the class ConstraintParser method getDrlxParseResult.

private DrlxParseResult getDrlxParseResult(Class<?> patternType, String bindingId, String expression, DrlxExpression drlx, boolean isPositional) {
    Expression drlxExpr = drlx.getExpr();
    boolean isEnclosed = false;
    while (drlxExpr instanceof EnclosedExpr) {
        drlxExpr = ((EnclosedExpr) drlxExpr).getInner();
        isEnclosed = true;
    }
    if (drlxExpr instanceof MethodCallExpr && !((MethodCallExpr) drlxExpr).getScope().isPresent() && ((MethodCallExpr) drlxExpr).getNameAsString().equals("eval")) {
        drlxExpr = ((MethodCallExpr) drlxExpr).getArgument(0);
    }
    String exprId;
    if (GENERATE_EXPR_ID) {
        exprId = context.getExprId(patternType, expression);
    }
    if (drlxExpr instanceof BinaryExpr) {
        BinaryExpr binaryExpr = (BinaryExpr) drlxExpr;
        BinaryExpr.Operator operator = binaryExpr.getOperator();
        IndexUtil.ConstraintType decodeConstraintType = DrlxParseUtil.toConstraintType(operator);
        final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
        final ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, isPositional, expressionTyperContext);
        TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(binaryExpr.getLeft());
        Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
        if (!optLeft.isPresent()) {
            return new DrlxParseFail();
        }
        List<String> usedDeclarationsOnLeft = drlx.getBind() == null ? null : new ArrayList<>(expressionTyperContext.getUsedDeclarations());
        TypedExpressionResult rightExpressionResult = expressionTyper.toTypedExpression(binaryExpr.getRight());
        Optional<TypedExpression> optRight = rightExpressionResult.getTypedExpression();
        if (!optRight.isPresent()) {
            context.addCompilationError(new ParseExpressionErrorResult(drlxExpr));
            return new DrlxParseFail();
        }
        TypedExpression left = optLeft.get();
        TypedExpression right = optRight.get();
        Expression combo;
        if (left.isPrimitive()) {
            if (!right.getType().isPrimitive() && !Number.class.isAssignableFrom(right.getType()) && !Boolean.class.isAssignableFrom(right.getType()) && !String.class.isAssignableFrom(right.getType())) {
                context.addCompilationError(new InvalidExpressionErrorResult("Comparison operation requires compatible types. Found " + left.getType() + " and " + right.getType()));
                return new DrlxParseFail();
            }
            if (right.getExpression() instanceof StringLiteralExpr) {
                right.setExpression(new IntegerLiteralExpr(((StringLiteralExpr) right.getExpression()).asString()));
            } else if (right.getExpression() instanceof LiteralStringValueExpr) {
                right.setExpression(coerceLiteralExprToType((LiteralStringValueExpr) right.getExpression(), left.getType()));
            }
            combo = new BinaryExpr(left.getExpression(), right.getExpression(), operator);
        } else {
            coerceRightExpression(left, right);
            switch(operator) {
                case EQUALS:
                case NOT_EQUALS:
                    combo = getEqualityExpression(left, right, operator);
                    break;
                default:
                    if (left.getExpression() == null || right.getExpression() == null) {
                        context.addCompilationError(new ParseExpressionErrorResult(drlxExpr));
                        return new DrlxParseFail();
                    }
                    combo = handleSpecialComparisonCases(operator, left, right);
            }
        }
        for (Expression e : leftTypedExpressionResult.getPrefixExpressions()) {
            combo = new BinaryExpr(e, combo, BinaryExpr.Operator.AND);
        }
        boolean isBetaNode = false;
        if (right.getExpression() instanceof BinaryExpr) {
            if (((BinaryExpr) right.getExpression()).getRight() instanceof MethodCallExpr) {
                isBetaNode = true;
            }
        } else if (right.getExpression() instanceof NameExpr) {
            isBetaNode = true;
        }
        if (isEnclosed) {
            combo = new EnclosedExpr(combo);
        }
        return new DrlxParseSuccess(patternType, exprId, bindingId, combo, left.getType()).setDecodeConstraintType(decodeConstraintType).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setUsedDeclarationsOnLeft(usedDeclarationsOnLeft).setReactOnProperties(expressionTyperContext.getReactOnProperties()).setLeft(left).setRight(right).setBetaNode(isBetaNode);
    }
    if (drlxExpr instanceof UnaryExpr) {
        UnaryExpr unaryExpr = (UnaryExpr) drlxExpr;
        TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, patternType, bindingId, isPositional).toTypedExpression(unaryExpr);
        return typedExpressionResult.getTypedExpression().<DrlxParseResult>map(left -> {
            return new DrlxParseSuccess(patternType, exprId, bindingId, left.getExpression(), left.getType()).setUsedDeclarations(typedExpressionResult.getUsedDeclarations()).setReactOnProperties(typedExpressionResult.getReactOnProperties()).setLeft(left);
        }).orElse(new DrlxParseFail());
    }
    if (drlxExpr instanceof PointFreeExpr) {
        PointFreeExpr pointFreeExpr = (PointFreeExpr) drlxExpr;
        TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, patternType, bindingId, isPositional).toTypedExpression(pointFreeExpr);
        final Optional<TypedExpression> optTypedExpression = typedExpressionResult.getTypedExpression();
        return optTypedExpression.<DrlxParseResult>map(typedExpression -> {
            final Expression returnExpression = typedExpression.getExpression();
            final Class<?> returnType = typedExpression.getType();
            return new DrlxParseSuccess(patternType, exprId, bindingId, returnExpression, returnType).setUsedDeclarations(typedExpressionResult.getUsedDeclarations()).setReactOnProperties(typedExpressionResult.getReactOnProperties()).setLeft(typedExpression.getLeft()).setStatic(typedExpression.isStatic()).setValidExpression(true);
        }).orElse(new DrlxParseFail());
    }
    if (drlxExpr instanceof MethodCallExpr) {
        MethodCallExpr methodCallExpr = (MethodCallExpr) drlxExpr;
        // when the methodCallExpr will be placed in the model/DSL, any parameter being a "this" need to be implemented as _this by convention.
        List<ThisExpr> rewriteThisExprs = recurseCollectArguments(methodCallExpr).stream().filter(ThisExpr.class::isInstance).map(ThisExpr.class::cast).collect(Collectors.toList());
        for (ThisExpr t : rewriteThisExprs) {
            methodCallExpr.replace(t, new NameExpr("_this"));
        }
        Optional<MethodDeclaration> functionCall = packageModel.getFunctions().stream().filter(m -> m.getName().equals(methodCallExpr.getName())).findFirst();
        if (functionCall.isPresent()) {
            Class<?> returnType = DrlxParseUtil.getClassFromContext(context.getTypeResolver(), functionCall.get().getType().asString());
            NodeList<Expression> arguments = methodCallExpr.getArguments();
            List<String> usedDeclarations = new ArrayList<>();
            for (Expression arg : arguments) {
                if (arg instanceof NameExpr && !arg.toString().equals("_this")) {
                    usedDeclarations.add(arg.toString());
                } else if (arg instanceof MethodCallExpr) {
                    TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, null, bindingId, isPositional).toTypedExpression(arg);
                    usedDeclarations.addAll(typedExpressionResult.getUsedDeclarations());
                }
            }
            return new DrlxParseSuccess(patternType, exprId, bindingId, methodCallExpr, returnType).setUsedDeclarations(usedDeclarations);
        } else if (methodCallExpr.getScope().isPresent() && methodCallExpr.getScope().get() instanceof StringLiteralExpr) {
            TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, String.class, bindingId, isPositional).toTypedExpression(methodCallExpr);
            Optional<TypedExpression> optConverted = typedExpressionResult.getTypedExpression();
            return optConverted.<DrlxParseResult>map(converted -> {
                return new DrlxParseSuccess(String.class, exprId, bindingId, converted.getExpression(), converted.getType()).setLeft(converted).setUsedDeclarations(typedExpressionResult.getUsedDeclarations());
            }).orElse(new DrlxParseFail());
        } else if (patternType != null) {
            NameExpr _this = new NameExpr("_this");
            TypedExpression converted = DrlxParseUtil.toMethodCallWithClassCheck(context, methodCallExpr, bindingId, patternType, context.getTypeResolver());
            Expression withThis = DrlxParseUtil.prepend(_this, converted.getExpression());
            return new DrlxParseSuccess(patternType, exprId, bindingId, withThis, converted.getType()).setLeft(converted);
        } else {
            return new DrlxParseSuccess(patternType, exprId, bindingId, methodCallExpr, null);
        }
    }
    if (drlxExpr instanceof FieldAccessExpr) {
        FieldAccessExpr fieldCallExpr = (FieldAccessExpr) drlxExpr;
        NameExpr _this = new NameExpr("_this");
        TypedExpression converted = DrlxParseUtil.toMethodCallWithClassCheck(context, fieldCallExpr, bindingId, patternType, context.getTypeResolver());
        Expression withThis = DrlxParseUtil.prepend(_this, converted.getExpression());
        return new DrlxParseSuccess(patternType, exprId, bindingId, withThis, converted.getType()).setLeft(converted);
    }
    if (drlxExpr instanceof NameExpr) {
        NameExpr nameExpr = (NameExpr) drlxExpr;
        NameExpr _this = new NameExpr("_this");
        TypedExpression converted = DrlxParseUtil.toMethodCallWithClassCheck(context, nameExpr, bindingId, patternType, context.getTypeResolver());
        Expression withThis = DrlxParseUtil.prepend(_this, converted.getExpression());
        if (drlx.getBind() != null) {
            return new DrlxParseSuccess(patternType, exprId, bindingId, null, converted.getType()).setLeft(new TypedExpression(withThis, converted.getType())).addReactOnProperty(lcFirst(nameExpr.getNameAsString()));
        } else {
            return new DrlxParseSuccess(patternType, exprId, bindingId, withThis, converted.getType()).addReactOnProperty(nameExpr.getNameAsString());
        }
    }
    if (drlxExpr instanceof OOPathExpr) {
        return new DrlxParseSuccess(patternType, exprId, bindingId, drlxExpr, null);
    }
    if (drlxExpr instanceof LiteralExpr) {
        return new DrlxParseSuccess(patternType, exprId, bindingId, drlxExpr, getLiteralExpressionType(((LiteralExpr) drlxExpr)));
    }
    // TODO
    throw new UnsupportedOperationException("Unknown expression: " + toDrlx(drlxExpr));
}
Also used : IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) Expression(org.drools.javaparser.ast.expr.Expression) ThisExpr(org.drools.javaparser.ast.expr.ThisExpr) ClassUtil.toNonPrimitiveType(org.drools.modelcompiler.util.ClassUtil.toNonPrimitiveType) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) CastExpr(org.drools.javaparser.ast.expr.CastExpr) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ArrayList(java.util.ArrayList) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) BigDecimal(java.math.BigDecimal) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) ParseExpressionErrorResult(org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult) ClassUtil(org.drools.modelcompiler.util.ClassUtil) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) LiteralStringValueExpr(org.drools.javaparser.ast.expr.LiteralStringValueExpr) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) PrintUtil.toDrlx(org.drools.javaparser.printer.PrintUtil.toDrlx) GREATER(org.drools.javaparser.ast.expr.BinaryExpr.Operator.GREATER) PackageModel(org.drools.modelcompiler.builder.PackageModel) LESS(org.drools.javaparser.ast.expr.BinaryExpr.Operator.LESS) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) DrlxExpression(org.drools.javaparser.ast.drlx.expr.DrlxExpression) JavaParserUtil.toJavaParserType(org.drools.modelcompiler.util.JavaParserUtil.toJavaParserType) PointFreeExpr(org.drools.javaparser.ast.drlx.expr.PointFreeExpr) LESS_EQUALS(org.drools.javaparser.ast.expr.BinaryExpr.Operator.LESS_EQUALS) Collectors(java.util.stream.Collectors) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) IndexUtil(org.drools.core.util.index.IndexUtil) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) OOPathExpr(org.drools.javaparser.ast.drlx.OOPathExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) NodeList(org.drools.javaparser.ast.NodeList) StringUtils.lcFirst(org.drools.core.util.StringUtils.lcFirst) NodeWithArguments(org.drools.javaparser.ast.nodeTypes.NodeWithArguments) NodeWithOptionalScope(org.drools.javaparser.ast.nodeTypes.NodeWithOptionalScope) Optional(java.util.Optional) DrlxParseUtil.coerceLiteralExprToType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.coerceLiteralExprToType) GREATER_EQUALS(org.drools.javaparser.ast.expr.BinaryExpr.Operator.GREATER_EQUALS) DrlxParseUtil.isPrimitiveExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isPrimitiveExpression) IndexUtil(org.drools.core.util.index.IndexUtil) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) ArrayList(java.util.ArrayList) ParseExpressionErrorResult(org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) OOPathExpr(org.drools.javaparser.ast.drlx.OOPathExpr) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) ThisExpr(org.drools.javaparser.ast.expr.ThisExpr) Optional(java.util.Optional) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) PointFreeExpr(org.drools.javaparser.ast.drlx.expr.PointFreeExpr) LiteralStringValueExpr(org.drools.javaparser.ast.expr.LiteralStringValueExpr) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) Expression(org.drools.javaparser.ast.expr.Expression) DrlxExpression(org.drools.javaparser.ast.drlx.expr.DrlxExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseUtil.isPrimitiveExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isPrimitiveExpression) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 2 with TypedExpressionResult

use of org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult in project drools by kiegroup.

the class ConstraintParser method parseFunctionInEval.

private DrlxParseResult parseFunctionInEval(MethodCallExpr methodCallExpr, Class<?> patternType, String bindingId, boolean isPositional, Optional<MethodDeclaration> functionCall) {
    // when the methodCallExpr will be placed in the model/DSL, any parameter being a "this" need to be implemented as _this by convention.
    List<ThisExpr> rewriteThisExprs = recurseCollectArguments(methodCallExpr).stream().filter(ThisExpr.class::isInstance).map(ThisExpr.class::cast).collect(Collectors.toList());
    for (ThisExpr t : rewriteThisExprs) {
        methodCallExpr.replace(t, new NameExpr(THIS_PLACEHOLDER));
    }
    if (functionCall.isPresent()) {
        Class<?> returnType = DrlxParseUtil.getClassFromContext(context.getTypeResolver(), functionCall.get().getType().asString());
        NodeList<Expression> arguments = methodCallExpr.getArguments();
        List<String> usedDeclarations = new ArrayList<>();
        for (Expression arg : arguments) {
            String argString = printNode(arg);
            if (arg instanceof DrlNameExpr && !argString.equals(THIS_PLACEHOLDER)) {
                usedDeclarations.add(argString);
            } else if (arg instanceof CastExpr) {
                String s = printNode(((CastExpr) arg).getExpression());
                usedDeclarations.add(s);
            } else if (arg instanceof MethodCallExpr) {
                TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, null, bindingId, isPositional).toTypedExpression(arg);
                usedDeclarations.addAll(typedExpressionResult.getUsedDeclarations());
            }
        }
        return new SingleDrlxParseSuccess(patternType, bindingId, methodCallExpr, returnType).setUsedDeclarations(usedDeclarations).setIsPredicate(isBooleanBoxedUnboxed(returnType));
    } else {
        throw new IllegalArgumentException("Specified function call is not present!");
    }
}
Also used : DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ArrayList(java.util.ArrayList) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) FlattenScope.transformFullyQualifiedInlineCastExpr(org.drools.modelcompiler.builder.generator.expressiontyper.FlattenScope.transformFullyQualifiedInlineCastExpr) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) CastExpr(com.github.javaparser.ast.expr.CastExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 3 with TypedExpressionResult

use of org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult in project drools by kiegroup.

the class ConstraintParser method compileToJavaRecursive.

private DrlxParseResult compileToJavaRecursive(Class<?> patternType, String bindingId, ConstraintExpression constraint, Expression drlxExpr, boolean hasBind, boolean isPositional) {
    boolean isEnclosed = false;
    SimpleName bind = null;
    if (drlxExpr instanceof FullyQualifiedInlineCastExpr) {
        drlxExpr = transformFullyQualifiedInlineCastExpr(context.getTypeResolver(), (FullyQualifiedInlineCastExpr) drlxExpr);
    }
    while (drlxExpr instanceof EnclosedExpr) {
        drlxExpr = ((EnclosedExpr) drlxExpr).getInner();
        isEnclosed = true;
    }
    if (drlxExpr instanceof DrlxExpression) {
        bind = ((DrlxExpression) drlxExpr).getBind();
        drlxExpr = ((DrlxExpression) drlxExpr).getExpr();
    }
    if (drlxExpr instanceof MethodCallExpr && !((MethodCallExpr) drlxExpr).getScope().isPresent() && ((MethodCallExpr) drlxExpr).getNameAsString().equals("eval")) {
        drlxExpr = ((MethodCallExpr) drlxExpr).getArgument(0);
    }
    if (drlxExpr instanceof BinaryExpr) {
        DrlxParseResult result = parseBinaryExpr((BinaryExpr) drlxExpr, patternType, bindingId, constraint, drlxExpr, hasBind, isPositional, isEnclosed);
        if (result instanceof SingleDrlxParseSuccess && bind != null) {
            ((SingleDrlxParseSuccess) result).setExprBinding(bind.asString());
        }
        return result;
    }
    if (drlxExpr instanceof UnaryExpr) {
        return parseUnaryExpr((UnaryExpr) drlxExpr, patternType, bindingId, constraint, drlxExpr, hasBind, isPositional);
    }
    if (drlxExpr instanceof PointFreeExpr) {
        return parsePointFreeExpr((PointFreeExpr) drlxExpr, patternType, bindingId, isPositional);
    }
    if (patternType == null && drlxExpr instanceof MethodCallExpr) {
        MethodCallExpr methodCallExpr = (MethodCallExpr) drlxExpr;
        Optional<MethodDeclaration> functionCall = packageModel.getFunctions().stream().filter(m -> m.getName().equals(methodCallExpr.getName())).findFirst();
        if (functionCall.isPresent()) {
            return parseFunctionInEval(methodCallExpr, patternType, bindingId, isPositional, functionCall);
        }
    }
    if (drlxExpr instanceof FieldAccessExpr) {
        return parseFieldAccessExpr((FieldAccessExpr) drlxExpr, patternType, bindingId);
    }
    String expression = constraint.getExpression();
    if (drlxExpr instanceof DrlNameExpr) {
        return parseNameExpr((DrlNameExpr) drlxExpr, patternType, bindingId, drlxExpr, hasBind, expression);
    }
    if (drlxExpr instanceof OOPathExpr) {
        return parseOOPathExpr((OOPathExpr) drlxExpr, patternType, bindingId, drlxExpr, hasBind, expression);
    }
    if (drlxExpr instanceof LiteralExpr) {
        Class<?> literalExpressionType = getLiteralExpressionType(((LiteralExpr) drlxExpr));
        return new SingleDrlxParseSuccess(patternType, bindingId, drlxExpr, literalExpressionType).setIsPredicate(isBooleanBoxedUnboxed(literalExpressionType));
    }
    if (patternType != null) {
        ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
        ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, isPositional, expressionTyperContext);
        TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(drlxExpr);
        Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
        if (!optLeft.isPresent()) {
            return new DrlxParseFail();
        }
        TypedExpression left = optLeft.get();
        Expression combo = left.getExpression();
        Type exprType = left.getType();
        boolean isPredicate = isBooleanBoxedUnboxed(exprType);
        if (isPredicate) {
            combo = combineExpressions(leftTypedExpressionResult, combo);
        }
        return new SingleDrlxParseSuccess(patternType, bindingId, combo, exprType).setReactOnProperties(expressionTyperContext.getReactOnProperties()).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setImplicitCastExpression(expressionTyperContext.getInlineCastExpression()).setNullSafeExpressions(expressionTyperContext.getNullSafeExpressions()).setIsPredicate(isPredicate);
    } else {
        final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
        final ExpressionTyper expressionTyper = new ExpressionTyper(context, null, bindingId, isPositional, expressionTyperContext);
        TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(drlxExpr);
        Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
        if (!optLeft.isPresent()) {
            return new DrlxParseFail();
        }
        TypedExpression left = optLeft.get();
        return new SingleDrlxParseSuccess(null, bindingId, drlxExpr, left.getType()).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setIsPredicate(true);
    }
}
Also used : HalfPointFreeExpr(org.drools.mvel.parser.ast.expr.HalfPointFreeExpr) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) Optional.of(java.util.Optional.of) PLUS(com.github.javaparser.ast.expr.BinaryExpr.Operator.PLUS) LESS(com.github.javaparser.ast.expr.BinaryExpr.Operator.LESS) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) OR(com.github.javaparser.ast.expr.BinaryExpr.Operator.OR) SpecialComparisonCase.specialComparisonFactory(org.drools.modelcompiler.builder.generator.drlxparse.SpecialComparisonCase.specialComparisonFactory) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) BigDecimal(java.math.BigDecimal) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) ParseExpressionErrorResult(org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult) EQUALS(com.github.javaparser.ast.expr.BinaryExpr.Operator.EQUALS) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) CompiledExpressionResult(org.drools.mvelcompiler.CompiledExpressionResult) GREATER_EQUALS(com.github.javaparser.ast.expr.BinaryExpr.Operator.GREATER_EQUALS) NOT_EQUALS(com.github.javaparser.ast.expr.BinaryExpr.Operator.NOT_EQUALS) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) DateUtils(org.drools.core.util.DateUtils) Arrays.asList(java.util.Arrays.asList) REMAINDER(com.github.javaparser.ast.expr.BinaryExpr.Operator.REMAINDER) BigInteger(java.math.BigInteger) Expression(com.github.javaparser.ast.expr.Expression) FlattenScope.transformFullyQualifiedInlineCastExpr(org.drools.modelcompiler.builder.generator.expressiontyper.FlattenScope.transformFullyQualifiedInlineCastExpr) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) OOPathExpr(org.drools.mvel.parser.ast.expr.OOPathExpr) DrlxParseUtil.stripEnclosedExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr) PackageModel(org.drools.modelcompiler.builder.PackageModel) NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) ConstraintCompiler(org.drools.mvelcompiler.ConstraintCompiler) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) MultipleDrlxParseSuccess.createMultipleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.MultipleDrlxParseSuccess.createMultipleDrlxParseSuccess) THIS_PLACEHOLDER(org.drools.modelcompiler.builder.generator.DrlxParseUtil.THIS_PLACEHOLDER) Index(org.drools.model.Index) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) List(java.util.List) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Type(java.lang.reflect.Type) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) AND(com.github.javaparser.ast.expr.BinaryExpr.Operator.AND) Optional(java.util.Optional) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) NodeWithArguments(com.github.javaparser.ast.nodeTypes.NodeWithArguments) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) MINUS(com.github.javaparser.ast.expr.BinaryExpr.Operator.MINUS) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) StringUtils.lcFirstForBean(org.drools.core.util.StringUtils.lcFirstForBean) CastExpr(com.github.javaparser.ast.expr.CastExpr) ArrayList(java.util.ArrayList) PointFreeExpr(org.drools.mvel.parser.ast.expr.PointFreeExpr) NodeWithOptionalScope(com.github.javaparser.ast.nodeTypes.NodeWithOptionalScope) BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) VariableUsedInBindingError(org.drools.modelcompiler.builder.errors.VariableUsedInBindingError) MULTIPLY(com.github.javaparser.ast.expr.BinaryExpr.Operator.MULTIPLY) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) DrlxParseUtil.createConstraintCompiler(org.drools.modelcompiler.builder.generator.DrlxParseUtil.createConstraintCompiler) NameExpr(com.github.javaparser.ast.expr.NameExpr) LESS_EQUALS(com.github.javaparser.ast.expr.BinaryExpr.Operator.LESS_EQUALS) PrintUtil(org.drools.mvel.parser.printer.PrintUtil) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) DIVIDE(com.github.javaparser.ast.expr.BinaryExpr.Operator.DIVIDE) DrlxParseUtil.isBooleanBoxedUnboxed(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isBooleanBoxedUnboxed) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ModelGenerator(org.drools.modelcompiler.builder.generator.ModelGenerator) Collections(java.util.Collections) GREATER(com.github.javaparser.ast.expr.BinaryExpr.Operator.GREATER) SimpleName(com.github.javaparser.ast.expr.SimpleName) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) OOPathExpr(org.drools.mvel.parser.ast.expr.OOPathExpr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) HalfPointFreeExpr(org.drools.mvel.parser.ast.expr.HalfPointFreeExpr) PointFreeExpr(org.drools.mvel.parser.ast.expr.PointFreeExpr) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) Type(java.lang.reflect.Type) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) DrlxParseUtil.stripEnclosedExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) FlattenScope.transformFullyQualifiedInlineCastExpr(org.drools.modelcompiler.builder.generator.expressiontyper.FlattenScope.transformFullyQualifiedInlineCastExpr) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 4 with TypedExpressionResult

use of org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult in project drools by kiegroup.

the class ExpressionTyperTest method pointFreeTest.

@Test
public void pointFreeTest() {
    final PointFreeExpr expression = new PointFreeExpr(null, new NameExpr("name"), NodeList.nodeList(new StringLiteralExpr("[A-Z]")), new SimpleName("matches"), false, null, null, null, null);
    TypedExpressionResult typedExpressionResult = new ExpressionTyper(ruleContext, Person.class, null, true).toTypedExpression(expression);
    final TypedExpression actual = typedExpressionResult.getTypedExpression().get();
    final TypedExpression expected = typedResult("D.eval(org.drools.model.operators.MatchesOperator.INSTANCE, _this.getName(), \"[A-Z]\")", String.class);
    assertEquals(expected, actual);
}
Also used : SimpleName(com.github.javaparser.ast.expr.SimpleName) PointFreeExpr(org.drools.mvel.parser.ast.expr.PointFreeExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 5 with TypedExpressionResult

use of org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult in project drools by kiegroup.

the class ToTypedExpressionTest method pointFreeTest.

@Test
public void pointFreeTest() {
    final PointFreeExpr expression = new PointFreeExpr(null, new NameExpr("name"), NodeList.nodeList(new StringLiteralExpr("[A-Z]")), new SimpleName("matches"), false, null, null, null, null);
    TypedExpressionResult typedExpressionResult = new ExpressionTyper(ruleContext, Person.class, null, true).toTypedExpression(expression);
    final TypedExpression actual = typedExpressionResult.getTypedExpression().get();
    final TypedExpression expected = typedResult("eval(org.drools.model.operators.MatchesOperator.INSTANCE, _this.getName(), \"[A-Z]\")", String.class);
    assertEquals(expected, actual);
}
Also used : SimpleName(org.drools.javaparser.ast.expr.SimpleName) PointFreeExpr(org.drools.javaparser.ast.drlx.expr.PointFreeExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Aggregations

ExpressionTyper (org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper)11 TypedExpressionResult (org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult)11 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)8 Expression (com.github.javaparser.ast.expr.Expression)6 NameExpr (com.github.javaparser.ast.expr.NameExpr)5 ExpressionTyperContext (org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext)5 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)5 ArrayList (java.util.ArrayList)4 ParseExpressionErrorResult (org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult)4 DrlxParseUtil.getLiteralExpressionType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType)4 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)4 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)3 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)3 PointFreeExpr (org.drools.mvel.parser.ast.expr.PointFreeExpr)3 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)2 CastExpr (com.github.javaparser.ast.expr.CastExpr)2 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)2 SimpleName (com.github.javaparser.ast.expr.SimpleName)2 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)2 ThisExpr (com.github.javaparser.ast.expr.ThisExpr)2