Search in sources :

Example 1 with ThisExpr

use of org.drools.javaparser.ast.expr.ThisExpr 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 ThisExpr

use of org.drools.javaparser.ast.expr.ThisExpr in project drools by kiegroup.

the class ExpressionTyper method toTypedExpressionRec.

private Optional<TypedExpression> toTypedExpressionRec(Expression drlxExpr) {
    Class<?> typeCursor = patternType;
    if (drlxExpr instanceof EnclosedExpr) {
        drlxExpr = ((EnclosedExpr) drlxExpr).getInner();
    }
    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()));
    } else 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()));
        }));
    } else if (drlxExpr instanceof HalfBinaryExpr) {
        final Expression binaryExpr = trasformHalfBinaryToBinary(drlxExpr);
        return toTypedExpressionRec(binaryExpr);
    } else if (drlxExpr instanceof LiteralExpr) {
        return of(new TypedExpression(drlxExpr, getLiteralExpressionType((LiteralExpr) drlxExpr)));
    } else if (drlxExpr instanceof ThisExpr) {
        return of(new TypedExpression(new NameExpr("_this"), patternType));
    } else if (drlxExpr instanceof CastExpr) {
        CastExpr castExpr = (CastExpr) drlxExpr;
        toTypedExpressionRec(castExpr.getExpression());
        return of(new TypedExpression(castExpr, getClassFromContext(ruleContext.getTypeResolver(), castExpr.getType().asString())));
    } else if (drlxExpr instanceof NameExpr) {
        String name = drlxExpr.toString();
        Optional<DeclarationSpec> decl = ruleContext.getDeclarationById(name);
        if (decl.isPresent()) {
            // then drlxExpr is a single NameExpr referring to a binding, e.g.: "$p1".
            context.addUsedDeclarations(name);
            return of(new TypedExpression(drlxExpr, decl.get().getDeclarationClass()));
        }
        if (ruleContext.getQueryParameters().stream().anyMatch(qp -> qp.getName().equals(name))) {
            // then drlxExpr is a single NameExpr referring to a query parameter, e.g.: "$p1".
            context.addUsedDeclarations(name);
            return of(new TypedExpression(drlxExpr));
        } else if (packageModel.getGlobals().containsKey(name)) {
            Expression plusThis = new NameExpr(name);
            context.addUsedDeclarations(name);
            return of(new TypedExpression(plusThis, packageModel.getGlobals().get(name)));
        } else {
            TypedExpression expression;
            try {
                expression = nameExprToMethodCallExpr(name, typeCursor, null);
            } catch (IllegalArgumentException e) {
                if (isPositional || ruleContext.getQueryName().isPresent()) {
                    String unificationVariable = ruleContext.getOrCreateUnificationId(name);
                    expression = new TypedExpression(unificationVariable, typeCursor, name);
                    return of(expression);
                }
                return Optional.empty();
            }
            context.addReactOnProperties(name);
            Expression plusThis = prepend(new NameExpr("_this"), expression.getExpression());
            return of(new TypedExpression(plusThis, expression.getType(), name));
        }
    } else if (drlxExpr instanceof FieldAccessExpr || drlxExpr instanceof MethodCallExpr) {
        return toTypedExpressionFromMethodCallOrField(drlxExpr).getTypedExpression();
    } else if (drlxExpr instanceof PointFreeExpr) {
        final PointFreeExpr pointFreeExpr = (PointFreeExpr) drlxExpr;
        Optional<TypedExpression> optLeft = toTypedExpressionRec(pointFreeExpr.getLeft());
        OperatorSpec opSpec = getOperatorSpec(drlxExpr, pointFreeExpr.getRight(), pointFreeExpr.getOperator());
        return optLeft.map(left -> new TypedExpression(opSpec.getExpression(ruleContext, pointFreeExpr, left), left.getType()).setStatic(opSpec.isStatic()).setLeft(left));
    } else if (drlxExpr instanceof HalfPointFreeExpr) {
        final HalfPointFreeExpr halfPointFreeExpr = (HalfPointFreeExpr) drlxExpr;
        Expression parentLeft = findLeftLeafOfNameExpr(halfPointFreeExpr.getParentNode().orElseThrow(UnsupportedOperationException::new));
        Optional<TypedExpression> optLeft = toTypedExpressionRec(parentLeft);
        OperatorSpec opSpec = getOperatorSpec(drlxExpr, halfPointFreeExpr.getRight(), halfPointFreeExpr.getOperator());
        final PointFreeExpr transformedToPointFree = new PointFreeExpr(halfPointFreeExpr.getTokenRange().get(), 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), left.getType()).setStatic(opSpec.isStatic()).setLeft(left));
    }
    throw new UnsupportedOperationException();
}
Also used : ArrayInitializerExpr(org.drools.javaparser.ast.expr.ArrayInitializerExpr) Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseUtil.findRootNodeViaParent(org.drools.modelcompiler.builder.generator.DrlxParseUtil.findRootNodeViaParent) ThisExpr(org.drools.javaparser.ast.expr.ThisExpr) DrlxParseUtil.prepend(org.drools.modelcompiler.builder.generator.DrlxParseUtil.prepend) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) DrlxParseUtil.getClassFromType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getClassFromType) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) TemporalOperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.TemporalOperatorSpec) Optional.of(java.util.Optional.of) JavaParser(org.drools.javaparser.JavaParser) LoggerFactory(org.slf4j.LoggerFactory) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) ClassUtils.getter2property(org.drools.core.util.ClassUtils.getter2property) ParseExpressionErrorResult(org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult) CustomOperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.CustomOperatorSpec) ClassUtil(org.drools.modelcompiler.util.ClassUtil) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) PrintUtil.toDrlx(org.drools.javaparser.printer.PrintUtil.toDrlx) DrlxParseUtil.getClassFromContext(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getClassFromContext) OperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.OperatorSpec) Method(java.lang.reflect.Method) PackageModel(org.drools.modelcompiler.builder.PackageModel) ArrayCreationExpr(org.drools.javaparser.ast.expr.ArrayCreationExpr) InstanceOfExpr(org.drools.javaparser.ast.expr.InstanceOfExpr) DrlxParseUtil.getExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getExpressionType) PointFreeExpr(org.drools.javaparser.ast.drlx.expr.PointFreeExpr) HalfPointFreeExpr(org.drools.javaparser.ast.drlx.expr.HalfPointFreeExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) List(java.util.List) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) Optional(java.util.Optional) DrlxParseUtil.replaceAllHalfBinaryChildren(org.drools.modelcompiler.builder.generator.DrlxParseUtil.replaceAllHalfBinaryChildren) CastExpr(org.drools.javaparser.ast.expr.CastExpr) NullLiteralExpr(org.drools.javaparser.ast.expr.NullLiteralExpr) ArrayList(java.util.ArrayList) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) InlineCastExpr(org.drools.javaparser.ast.drlx.expr.InlineCastExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) Logger(org.slf4j.Logger) ClassUtils(org.drools.core.util.ClassUtils) ReferenceType(org.drools.javaparser.ast.type.ReferenceType) PrintUtil(org.drools.javaparser.printer.PrintUtil) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) Type(org.drools.javaparser.ast.type.Type) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) NodeList(org.drools.javaparser.ast.NodeList) DrlxParseUtil.trasformHalfBinaryToBinary(org.drools.modelcompiler.builder.generator.DrlxParseUtil.trasformHalfBinaryToBinary) ModelGenerator(org.drools.modelcompiler.builder.generator.ModelGenerator) SimpleName(org.drools.javaparser.ast.expr.SimpleName) Node(org.drools.javaparser.ast.Node) NameExpr(org.drools.javaparser.ast.expr.NameExpr) TemporalOperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.TemporalOperatorSpec) CustomOperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.CustomOperatorSpec) OperatorSpec(org.drools.modelcompiler.builder.generator.operatorspec.OperatorSpec) CastExpr(org.drools.javaparser.ast.expr.CastExpr) InlineCastExpr(org.drools.javaparser.ast.drlx.expr.InlineCastExpr) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) NullLiteralExpr(org.drools.javaparser.ast.expr.NullLiteralExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) ThisExpr(org.drools.javaparser.ast.expr.ThisExpr) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) Optional(java.util.Optional) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) PointFreeExpr(org.drools.javaparser.ast.drlx.expr.PointFreeExpr) HalfPointFreeExpr(org.drools.javaparser.ast.drlx.expr.HalfPointFreeExpr) HalfPointFreeExpr(org.drools.javaparser.ast.drlx.expr.HalfPointFreeExpr) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 3 with ThisExpr

use of org.drools.javaparser.ast.expr.ThisExpr in project drools by kiegroup.

the class ExpressionTyper method processFirstNode.

private Optional<TypedExpressionCursor> processFirstNode(Expression drlxExpr, List<Node> childNodes, Node firstNode, boolean isInLineCast, Class<?> originalTypeCursor) {
    final Optional<TypedExpressionCursor> result;
    if (firstNode instanceof ThisExpr || (firstNode instanceof NameExpr && firstNode.toString().equals(bindingId))) {
        result = of(thisExpr(drlxExpr, childNodes, isInLineCast, originalTypeCursor));
    } else if (firstNode instanceof NameExpr) {
        result = nameExpr(drlxExpr, (NameExpr) firstNode, isInLineCast, originalTypeCursor);
    } else if (firstNode instanceof FieldAccessExpr && ((FieldAccessExpr) firstNode).getScope() instanceof ThisExpr) {
        result = of(fieldAccessExpr((FieldAccessExpr) firstNode, originalTypeCursor));
    } else if (firstNode instanceof MethodCallExpr) {
        result = of(methodCallExpr((MethodCallExpr) firstNode, originalTypeCursor, new NameExpr("_this")));
    } else if (firstNode instanceof StringLiteralExpr) {
        result = of(stringLiteralExpr((StringLiteralExpr) firstNode));
    } else if (firstNode instanceof EnclosedExpr) {
        result = processFirstNode(drlxExpr, childNodes, ((EnclosedExpr) firstNode).getInner(), isInLineCast, originalTypeCursor);
    } else if (firstNode instanceof CastExpr) {
        result = castExpr((CastExpr) firstNode, drlxExpr, childNodes, isInLineCast, originalTypeCursor);
    } else if (firstNode instanceof ArrayCreationExpr) {
        result = of(arrayCreationExpr(((ArrayCreationExpr) firstNode)));
    } else if (firstNode instanceof BinaryExpr) {
        result = of(binaryExpr((BinaryExpr) firstNode));
    } else {
        result = of(new TypedExpressionCursor((Expression) firstNode, getExpressionType(ruleContext, ruleContext.getTypeResolver(), (Expression) firstNode, context.getUsedDeclarations())));
    }
    result.ifPresent(te -> {
        if (drlxExpr instanceof NullSafeFieldAccessExpr) {
            extractPrefixExpressions((NullSafeFieldAccessExpr) drlxExpr, te.expressionCursor);
        }
    });
    return result.map(te -> {
        if (isInLineCast) {
            Expression exprWithInlineCast = addCastToExpression(te.typeCursor, te.expressionCursor, isInLineCast);
            return new TypedExpressionCursor(exprWithInlineCast, te.typeCursor);
        } else {
            return te;
        }
    });
}
Also used : BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) CastExpr(org.drools.javaparser.ast.expr.CastExpr) InlineCastExpr(org.drools.javaparser.ast.drlx.expr.InlineCastExpr) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) ArrayCreationExpr(org.drools.javaparser.ast.expr.ArrayCreationExpr) ThisExpr(org.drools.javaparser.ast.expr.ThisExpr) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)3 CastExpr (org.drools.javaparser.ast.expr.CastExpr)3 EnclosedExpr (org.drools.javaparser.ast.expr.EnclosedExpr)3 Expression (org.drools.javaparser.ast.expr.Expression)3 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)3 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)3 NameExpr (org.drools.javaparser.ast.expr.NameExpr)3 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)3 ThisExpr (org.drools.javaparser.ast.expr.ThisExpr)3 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 NodeList (org.drools.javaparser.ast.NodeList)2 HalfBinaryExpr (org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr)2 InlineCastExpr (org.drools.javaparser.ast.drlx.expr.InlineCastExpr)2 PointFreeExpr (org.drools.javaparser.ast.drlx.expr.PointFreeExpr)2 LiteralExpr (org.drools.javaparser.ast.expr.LiteralExpr)2 UnaryExpr (org.drools.javaparser.ast.expr.UnaryExpr)2 PrintUtil.toDrlx (org.drools.javaparser.printer.PrintUtil.toDrlx)2