Search in sources :

Example 1 with FullyQualifiedInlineCastExpr

use of org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr 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 2 with FullyQualifiedInlineCastExpr

use of org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr in project drools by kiegroup.

the class ExpressionTyper method toTypedExpressionRec.

private Optional<TypedExpression> toTypedExpressionRec(Expression drlxExpr) {
    Class<?> typeCursor = patternType;
    if (drlxExpr instanceof FullyQualifiedInlineCastExpr) {
        return toTypedExpressionRec(transformFullyQualifiedInlineCastExpr(ruleContext.getTypeResolver(), (FullyQualifiedInlineCastExpr) drlxExpr));
    }
    if (drlxExpr instanceof EnclosedExpr) {
        Expression inner = ((EnclosedExpr) drlxExpr).getInner();
        Optional<TypedExpression> typedExpression = toTypedExpressionRec(inner);
        return typedExpression.map(t -> t.cloneWithNewExpression(new EnclosedExpr(t.getExpression())));
    }
    if (drlxExpr instanceof MethodCallExpr) {
        MethodCallExpr methodExpr = (MethodCallExpr) drlxExpr;
        Expression expr = methodExpr;
        if (isEval(methodExpr.getNameAsString(), methodExpr.getScope(), methodExpr.getArguments())) {
            expr = methodExpr.getArgument(0);
        }
        drlxExpr = expr;
    }
    if (drlxExpr instanceof NullSafeMethodCallExpr) {
        NullSafeMethodCallExpr methodExpr = (NullSafeMethodCallExpr) drlxExpr;
        Expression expr = methodExpr;
        if (isEval(methodExpr.getNameAsString(), methodExpr.getScope(), methodExpr.getArguments())) {
            expr = methodExpr.getArgument(0);
        }
        drlxExpr = expr;
    }
    if (drlxExpr instanceof UnaryExpr) {
        UnaryExpr unaryExpr = (UnaryExpr) drlxExpr;
        Optional<TypedExpression> optTypedExpr = toTypedExpressionRec(unaryExpr.getExpression());
        return optTypedExpr.map(typedExpr -> new TypedExpression(new UnaryExpr(typedExpr.getExpression(), unaryExpr.getOperator()), typedExpr.getType()));
    }
    if (drlxExpr instanceof BinaryExpr) {
        BinaryExpr binaryExpr = (BinaryExpr) drlxExpr;
        BinaryExpr.Operator operator = binaryExpr.getOperator();
        Optional<TypedExpression> optLeft = toTypedExpressionRec(binaryExpr.getLeft());
        Optional<TypedExpression> optRight = toTypedExpressionRec(binaryExpr.getRight());
        return optLeft.flatMap(left -> optRight.flatMap(right -> {
            final BinaryExpr combo = new BinaryExpr(left.getExpression(), right.getExpression(), operator);
            return of(new TypedExpression(combo, left.getType()));
        }));
    }
    if (drlxExpr instanceof HalfBinaryExpr) {
        final Expression binaryExpr = trasformHalfBinaryToBinary(drlxExpr);
        if (binaryExpr instanceof BinaryExpr && ((BinaryExpr) binaryExpr).getLeft() == drlxExpr) {
            throw new CannotTypeExpressionException("left leaf is the same : drlxExpr = " + drlxExpr + ", originalExpression = " + context.getOriginalExpression());
        }
        return toTypedExpressionRec(binaryExpr);
    }
    if (drlxExpr instanceof LiteralExpr) {
        drlxExpr = normalizeDigit(drlxExpr);
        return of(new TypedExpression(drlxExpr, getLiteralExpressionType((LiteralExpr) drlxExpr)));
    }
    if (drlxExpr instanceof ThisExpr || (drlxExpr instanceof NameExpr && THIS_PLACEHOLDER.equals(printNode(drlxExpr)))) {
        return of(new TypedExpression(new NameExpr(THIS_PLACEHOLDER), patternType));
    }
    if (drlxExpr instanceof CastExpr) {
        CastExpr castExpr = (CastExpr) drlxExpr;
        Optional<TypedExpression> optTypedExpr = toTypedExpressionRec(castExpr.getExpression());
        return optTypedExpr.map(typedExpr -> new TypedExpression(new CastExpr(castExpr.getType(), typedExpr.getExpression()), getClassFromContext(ruleContext.getTypeResolver(), castExpr.getType().asString())));
    }
    if (drlxExpr instanceof NameExpr) {
        return nameExpr(((NameExpr) drlxExpr).getNameAsString(), typeCursor);
    }
    if (drlxExpr instanceof FieldAccessExpr || drlxExpr instanceof MethodCallExpr || drlxExpr instanceof ObjectCreationExpr || drlxExpr instanceof NullSafeFieldAccessExpr || drlxExpr instanceof NullSafeMethodCallExpr || drlxExpr instanceof MapCreationLiteralExpression || drlxExpr instanceof ListCreationLiteralExpression) {
        return toTypedExpressionFromMethodCallOrField(drlxExpr).getTypedExpression();
    }
    if (drlxExpr instanceof PointFreeExpr) {
        final PointFreeExpr pointFreeExpr = (PointFreeExpr) drlxExpr;
        Optional<TypedExpression> optLeft = toTypedExpressionRec(pointFreeExpr.getLeft());
        Optional<TypedExpression> optRight = pointFreeExpr.getRight().size() == 1 ? toTypedExpressionRec(pointFreeExpr.getRight().get(0)) : Optional.empty();
        OperatorSpec opSpec = getOperatorSpec(pointFreeExpr.getRight(), pointFreeExpr.getOperator());
        return optLeft.map(left -> new TypedExpression(opSpec.getExpression(ruleContext, pointFreeExpr, left, this), left.getType()).setStatic(opSpec.isStatic()).setLeft(left).setRight(optRight.orElse(null)));
    }
    if (drlxExpr instanceof HalfPointFreeExpr) {
        final HalfPointFreeExpr halfPointFreeExpr = (HalfPointFreeExpr) drlxExpr;
        Expression parentLeft = findLeftLeafOfNameExprTraversingParent(halfPointFreeExpr);
        if (parentLeft == halfPointFreeExpr) {
            throw new CannotTypeExpressionException("left leaf is the same : halfPointFreeExpr = " + halfPointFreeExpr + ", originalExpression = " + context.getOriginalExpression());
        }
        Optional<TypedExpression> optLeft = toTypedExpressionRec(parentLeft);
        OperatorSpec opSpec = getOperatorSpec(halfPointFreeExpr.getRight(), halfPointFreeExpr.getOperator());
        final PointFreeExpr transformedToPointFree = new PointFreeExpr(halfPointFreeExpr.getTokenRange().orElseThrow(() -> new IllegalStateException("Token range is not present!")), parentLeft, halfPointFreeExpr.getRight(), halfPointFreeExpr.getOperator(), halfPointFreeExpr.isNegated(), halfPointFreeExpr.getArg1(), halfPointFreeExpr.getArg2(), halfPointFreeExpr.getArg3(), halfPointFreeExpr.getArg4());
        return optLeft.map(left -> new TypedExpression(opSpec.getExpression(ruleContext, transformedToPointFree, left, this), left.getType()).setStatic(opSpec.isStatic()).setLeft(left));
    }
    if (drlxExpr instanceof ArrayAccessExpr) {
        final ArrayAccessExpr arrayAccessExpr = (ArrayAccessExpr) drlxExpr;
        if (Map.class.isAssignableFrom(typeCursor)) {
            return createMapAccessExpression(arrayAccessExpr.getIndex(), arrayAccessExpr.getName() instanceof ThisExpr ? new NameExpr(THIS_PLACEHOLDER) : arrayAccessExpr.getName(), Map.class);
        } else if (arrayAccessExpr.getName() instanceof FieldAccessExpr) {
            Optional<TypedExpression> typedExpression = toTypedExpressionFromMethodCallOrField(drlxExpr).getTypedExpression();
            typedExpression.ifPresent(te -> {
                final Expression originalExpression = te.getExpression();
                DrlxParseUtil.removeRootNode(originalExpression);
            });
            return typedExpression;
        } else {
            String name = printNode(drlxExpr.asArrayAccessExpr().getName());
            final Optional<TypedExpression> nameExpr = nameExpr(name, typeCursor);
            Expression indexExpr = toTypedExpressionFromMethodCallOrField(arrayAccessExpr.getIndex()).getTypedExpression().orElseThrow(() -> new NoSuchElementException("TypedExpressionResult doesn't contain TypedExpression!")).getExpression();
            return nameExpr.flatMap(te -> transformToArrayOrMapExpressionWithType(indexExpr, te));
        }
    }
    if (drlxExpr instanceof InstanceOfExpr) {
        InstanceOfExpr instanceOfExpr = (InstanceOfExpr) drlxExpr;
        ruleContext.addInlineCastType(printNode(instanceOfExpr.getExpression()), instanceOfExpr.getType());
        return toTypedExpressionRec(instanceOfExpr.getExpression()).map(e -> new TypedExpression(new InstanceOfExpr(e.getExpression(), instanceOfExpr.getType()), boolean.class));
    }
    if (drlxExpr instanceof ClassExpr) {
        return of(new TypedExpression(drlxExpr, Class.class));
    }
    if (drlxExpr instanceof InlineCastExpr) {
        return toTypedExpressionFromMethodCallOrField(drlxExpr).getTypedExpression();
    }
    if (drlxExpr instanceof OOPathExpr) {
        Class<?> type = patternType;
        for (OOPathChunk chunk : ((OOPathExpr) drlxExpr).getChunks()) {
            final String fieldName = chunk.getField().toString();
            final TypedExpression callExpr = DrlxParseUtil.nameExprToMethodCallExpr(fieldName, type, null, ruleContext);
            if (callExpr == null) {
                return empty();
            }
            Class<?> fieldType = (chunk.getInlineCast() != null) ? DrlxParseUtil.getClassFromContext(ruleContext.getTypeResolver(), chunk.getInlineCast().toString()) : callExpr.getRawClass();
            if (!chunk.isSingleValue() && Iterable.class.isAssignableFrom(fieldType) || isDataSource(fieldType)) {
                type = extractGenericType(type, ((MethodCallExpr) callExpr.getExpression()).getName().toString());
            } else {
                type = fieldType;
            }
        }
        return of(new TypedExpression(drlxExpr, type));
    }
    if (drlxExpr.isAssignExpr()) {
        AssignExpr assignExpr = drlxExpr.asAssignExpr();
        final Expression rightSide = assignExpr.getValue();
        return toTypedExpressionRec(rightSide).map(e -> {
            final AssignExpr newExpression = new AssignExpr(assignExpr.getTarget(), e.getExpression(), assignExpr.getOperator());
            return new TypedExpression(newExpression, e.getType());
        });
    }
    throw new UnsupportedOperationException();
}
Also used : ArrayAccessExpr(com.github.javaparser.ast.expr.ArrayAccessExpr) Arrays(java.util.Arrays) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) HalfPointFreeExpr(org.drools.mvel.parser.ast.expr.HalfPointFreeExpr) DrlxParseUtil.findRootNodeViaParent(org.drools.modelcompiler.builder.generator.DrlxParseUtil.findRootNodeViaParent) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) Optional.of(java.util.Optional.of) InstanceOfExpr(com.github.javaparser.ast.expr.InstanceOfExpr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) ParseExpressionErrorResult(org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult) FlattenScope.flattenScope(org.drools.modelcompiler.builder.generator.expressiontyper.FlattenScope.flattenScope) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) Type(com.github.javaparser.ast.type.Type) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) FlattenScope.transformFullyQualifiedInlineCastExpr(org.drools.modelcompiler.builder.generator.expressiontyper.FlattenScope.transformFullyQualifiedInlineCastExpr) ClassUtil.toRawClass(org.drools.modelcompiler.util.ClassUtil.toRawClass) DrlxParseUtil.getClassFromContext(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getClassFromContext) OperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.OperatorSpec) OOPathExpr(org.drools.mvel.parser.ast.expr.OOPathExpr) PackageModel(org.drools.modelcompiler.builder.PackageModel) SimpleName(com.github.javaparser.ast.expr.SimpleName) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr) DrlxParseUtil.getExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getExpressionType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) MapCreationLiteralExpressionKeyValuePair(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpressionKeyValuePair) MethodUtils(org.drools.core.util.MethodUtils) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) Optional.empty(java.util.Optional.empty) DrlxParseUtil.replaceAllHalfBinaryChildren(org.drools.modelcompiler.builder.generator.DrlxParseUtil.replaceAllHalfBinaryChildren) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) CastExpr(com.github.javaparser.ast.expr.CastExpr) ArrayList(java.util.ArrayList) NullSafeMethodCallExpr(org.drools.mvel.parser.ast.expr.NullSafeMethodCallExpr) DrlxParseUtil.transformDrlNameExprToNameExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.transformDrlNameExprToNameExpr) ListCreationLiteralExpressionElement(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpressionElement) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) TypeVariable(java.lang.reflect.TypeVariable) NativeOperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.NativeOperatorSpec) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) InlineCastExpr(org.drools.mvel.parser.ast.expr.InlineCastExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) Field(java.lang.reflect.Field) InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) ParameterizedType(java.lang.reflect.ParameterizedType) ModelGenerator(org.drools.modelcompiler.builder.generator.ModelGenerator) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.prepend(org.drools.modelcompiler.builder.generator.DrlxParseUtil.prepend) DrlxParseUtil.getClassFromType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getClassFromType) TypeResolver(org.drools.core.addon.TypeResolver) TemporalOperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.TemporalOperatorSpec) LoggerFactory(org.slf4j.LoggerFactory) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ClassUtils.getter2property(org.drools.core.util.ClassUtils.getter2property) DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) CustomOperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.CustomOperatorSpec) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) Method(java.lang.reflect.Method) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) ReferenceType(com.github.javaparser.ast.type.ReferenceType) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) OOPathChunk(org.drools.mvel.parser.ast.expr.OOPathChunk) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) DrlxParseUtil.safeResolveType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.safeResolveType) THIS_PLACEHOLDER(org.drools.modelcompiler.builder.generator.DrlxParseUtil.THIS_PLACEHOLDER) 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) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) MvelParser.parseType(org.drools.mvel.parser.MvelParser.parseType) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) DrlxParseUtil.toStringLiteral(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toStringLiteral) NodeWithArguments(com.github.javaparser.ast.nodeTypes.NodeWithArguments) HashMap(java.util.HashMap) ClassUtils.extractGenericType(org.drools.core.util.ClassUtils.extractGenericType) PointFreeExpr(org.drools.mvel.parser.ast.expr.PointFreeExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) ClassUtil.getTypeArgument(org.drools.modelcompiler.util.ClassUtil.getTypeArgument) RuleUnitUtil.isDataSource(org.kie.internal.ruleunit.RuleUnitUtil.isDataSource) NoSuchElementException(java.util.NoSuchElementException) Logger(org.slf4j.Logger) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) NullSafeFieldAccessExpr(org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr) PrintUtil(org.drools.mvel.parser.printer.PrintUtil) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) DrlxParseUtil.trasformHalfBinaryToBinary(org.drools.modelcompiler.builder.generator.DrlxParseUtil.trasformHalfBinaryToBinary) BigDecimalArgumentCoercion(org.drools.mvelcompiler.util.BigDecimalArgumentCoercion) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) DrlxParseUtil.transformDrlNameExprToNameExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.transformDrlNameExprToNameExpr) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) OperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.OperatorSpec) NativeOperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.NativeOperatorSpec) TemporalOperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.TemporalOperatorSpec) CustomOperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.CustomOperatorSpec) OOPathExpr(org.drools.mvel.parser.ast.expr.OOPathExpr) OOPathChunk(org.drools.mvel.parser.ast.expr.OOPathChunk) FlattenScope.transformFullyQualifiedInlineCastExpr(org.drools.modelcompiler.builder.generator.expressiontyper.FlattenScope.transformFullyQualifiedInlineCastExpr) CastExpr(com.github.javaparser.ast.expr.CastExpr) InlineCastExpr(org.drools.mvel.parser.ast.expr.InlineCastExpr) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) NullSafeFieldAccessExpr(org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) Optional(java.util.Optional) ArrayAccessExpr(com.github.javaparser.ast.expr.ArrayAccessExpr) 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) FlattenScope.transformFullyQualifiedInlineCastExpr(org.drools.modelcompiler.builder.generator.expressiontyper.FlattenScope.transformFullyQualifiedInlineCastExpr) InlineCastExpr(org.drools.mvel.parser.ast.expr.InlineCastExpr) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) HalfPointFreeExpr(org.drools.mvel.parser.ast.expr.HalfPointFreeExpr) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) InstanceOfExpr(com.github.javaparser.ast.expr.InstanceOfExpr) NullSafeFieldAccessExpr(org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) Expression(com.github.javaparser.ast.expr.Expression) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ClassUtil.toRawClass(org.drools.modelcompiler.util.ClassUtil.toRawClass) NullSafeMethodCallExpr(org.drools.mvel.parser.ast.expr.NullSafeMethodCallExpr) 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) NoSuchElementException(java.util.NoSuchElementException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) NullSafeMethodCallExpr(org.drools.mvel.parser.ast.expr.NullSafeMethodCallExpr) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr)

Example 3 with FullyQualifiedInlineCastExpr

use of org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr in project drools by kiegroup.

the class FlattenScope method transformFullyQualifiedInlineCastExpr.

public static Expression transformFullyQualifiedInlineCastExpr(TypeResolver typeResolver, FullyQualifiedInlineCastExpr fqInlineCastExpr) {
    final Deque<String> expressionNamesWithoutType = new ArrayDeque<>();
    String className = findClassName(fqInlineCastExpr.getName(), typeResolver, expressionNamesWithoutType);
    Expression scope = fqInlineCastExpr.getScope();
    if (scope instanceof FullyQualifiedInlineCastExpr) {
        scope = transformFullyQualifiedInlineCastExpr(typeResolver, (FullyQualifiedInlineCastExpr) scope);
    }
    Expression expr = new InlineCastExpr(toClassOrInterfaceType(className), scope);
    // last element is the one with the actual arguments and need a special case
    if (!expressionNamesWithoutType.isEmpty()) {
        String lastElement = expressionNamesWithoutType.removeLast();
        // the others will be considered FieldAccessExpr
        for (String s : expressionNamesWithoutType) {
            expr = new FieldAccessExpr(expr, s);
        }
        if (fqInlineCastExpr.hasArguments()) {
            expr = new MethodCallExpr(expr, lastElement, fqInlineCastExpr.getArguments());
        } else {
            expr = new FieldAccessExpr(expr, lastElement);
        }
    }
    return expr;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) InlineCastExpr(org.drools.mvel.parser.ast.expr.InlineCastExpr) NullSafeFieldAccessExpr(org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ArrayDeque(java.util.ArrayDeque) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) NullSafeMethodCallExpr(org.drools.mvel.parser.ast.expr.NullSafeMethodCallExpr)

Example 4 with FullyQualifiedInlineCastExpr

use of org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr in project drools by kiegroup.

the class FlattenScope method flattenScope.

public static List<Node> flattenScope(TypeResolver typeResolver, Expression expressionWithScope) {
    List<Node> res = new ArrayList<>();
    if (expressionWithScope instanceof FullyQualifiedInlineCastExpr) {
        res.addAll(flattenScope(typeResolver, transformFullyQualifiedInlineCastExpr(typeResolver, (FullyQualifiedInlineCastExpr) expressionWithScope)));
    } else if (expressionWithScope instanceof FieldAccessExpr) {
        FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) expressionWithScope;
        res.addAll(flattenScope(typeResolver, fieldAccessExpr.getScope()));
        res.add(fieldAccessExpr.getName());
    } else if (expressionWithScope instanceof NullSafeFieldAccessExpr) {
        NullSafeFieldAccessExpr fieldAccessExpr = (NullSafeFieldAccessExpr) expressionWithScope;
        res.addAll(flattenScope(typeResolver, fieldAccessExpr.getScope()));
        res.add(fieldAccessExpr.getName());
    } else if (expressionWithScope instanceof MethodCallExpr) {
        MethodCallExpr methodCallExpr = (MethodCallExpr) expressionWithScope;
        if (methodCallExpr.getScope().isPresent()) {
            Expression scope = methodCallExpr.getScope().get();
            if (isFullyQualifiedClassName(typeResolver, scope)) {
                res.add(scope);
            } else {
                res.addAll(flattenScope(typeResolver, scope));
            }
        }
        res.add(methodCallExpr);
    } else if (expressionWithScope instanceof NullSafeMethodCallExpr) {
        NullSafeMethodCallExpr methodCallExpr = (NullSafeMethodCallExpr) expressionWithScope;
        if (methodCallExpr.getScope().isPresent()) {
            res.addAll(flattenScope(typeResolver, methodCallExpr.getScope().orElseThrow(() -> new IllegalStateException("Scope expression is not present!"))));
        }
        res.add(methodCallExpr);
    } else if (expressionWithScope instanceof InlineCastExpr && ((InlineCastExpr) expressionWithScope).getExpression() instanceof FieldAccessExpr) {
        InlineCastExpr inlineCastExpr = (InlineCastExpr) expressionWithScope;
        Expression internalScope = ((FieldAccessExpr) inlineCastExpr.getExpression()).getScope();
        res.addAll(flattenScope(typeResolver, internalScope));
        res.add(expressionWithScope);
    } else if (expressionWithScope instanceof ArrayAccessExpr) {
        ArrayAccessExpr arrayAccessExpr = (ArrayAccessExpr) expressionWithScope;
        res.addAll(flattenScope(typeResolver, arrayAccessExpr.getName()));
        res.add(arrayAccessExpr);
    } else if (expressionWithScope instanceof EnclosedExpr) {
        res.addAll(flattenScope(typeResolver, ((EnclosedExpr) expressionWithScope).getInner()));
    } else {
        res.add(expressionWithScope);
    }
    return res;
}
Also used : ArrayAccessExpr(com.github.javaparser.ast.expr.ArrayAccessExpr) Node(com.github.javaparser.ast.Node) ArrayList(java.util.ArrayList) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) InlineCastExpr(org.drools.mvel.parser.ast.expr.InlineCastExpr) NullSafeFieldAccessExpr(org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr) Expression(com.github.javaparser.ast.expr.Expression) NullSafeFieldAccessExpr(org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) NullSafeMethodCallExpr(org.drools.mvel.parser.ast.expr.NullSafeMethodCallExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) NullSafeMethodCallExpr(org.drools.mvel.parser.ast.expr.NullSafeMethodCallExpr)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)4 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)4 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)4 FullyQualifiedInlineCastExpr (org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr)4 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)3 ArrayList (java.util.ArrayList)3 InlineCastExpr (org.drools.mvel.parser.ast.expr.InlineCastExpr)3 NullSafeFieldAccessExpr (org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr)3 NullSafeMethodCallExpr (org.drools.mvel.parser.ast.expr.NullSafeMethodCallExpr)3 NodeList (com.github.javaparser.ast.NodeList)2 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)2 CastExpr (com.github.javaparser.ast.expr.CastExpr)2 LiteralExpr (com.github.javaparser.ast.expr.LiteralExpr)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)2 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)2 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)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 UnaryExpr (com.github.javaparser.ast.expr.UnaryExpr)2