Search in sources :

Example 6 with Expression

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

the class ExpressionTyper method castExpr.

private Optional<TypedExpressionCursor> castExpr(CastExpr firstNode, Expression drlxExpr, List<Node> childNodes, boolean isInLineCast, Class<?> originalTypeCursor) {
    try {
        Type type = firstNode.getType();
        Class<?> typeClass = ruleContext.getTypeResolver().resolveType(type.toString());
        Optional<TypedExpressionCursor> result = processFirstNode(drlxExpr, childNodes, firstNode.getExpression(), isInLineCast, originalTypeCursor);
        return result.map(te -> {
            Expression exprWithInlineCast = addCastToExpression(type, te.expressionCursor, isInLineCast);
            return new TypedExpressionCursor(exprWithInlineCast, typeClass);
        });
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
Also used : DrlxParseUtil.getClassFromType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getClassFromType) DrlxParseUtil.getExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getExpressionType) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) ReferenceType(org.drools.javaparser.ast.type.ReferenceType) Type(org.drools.javaparser.ast.type.Type) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Example 7 with Expression

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

the class ExpressionTyper method methodCallExpr.

private TypedExpressionCursor methodCallExpr(MethodCallExpr methodCallExpr, Class<?> originalTypeCursor, Expression scope) {
    methodCallExpr.setScope(scope);
    Class[] argsType = new Class[methodCallExpr.getArguments().size()];
    for (int i = 0; i < methodCallExpr.getArguments().size(); i++) {
        Expression arg = methodCallExpr.getArgument(i);
        TypedExpressionResult typedArg = toTypedExpressionFromMethodCallOrField(arg);
        TypedExpression typedExpr = typedArg.getTypedExpression().get();
        argsType[i] = typedExpr.getType();
        methodCallExpr.setArgument(i, typedExpr.getExpression());
    }
    Class<?> type = ClassUtil.findMethod(originalTypeCursor, methodCallExpr.getNameAsString(), argsType).getReturnType();
    return new TypedExpressionCursor(methodCallExpr, type);
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Example 8 with Expression

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

the class ExpressionTyper method flattenScope.

private static List<Node> flattenScope(Expression expressionWithScope) {
    List<Node> res = new ArrayList<>();
    if (expressionWithScope instanceof FieldAccessExpr) {
        FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) expressionWithScope;
        res.addAll(flattenScope(fieldAccessExpr.getScope()));
        res.add(fieldAccessExpr.getName());
    } else if (expressionWithScope instanceof MethodCallExpr) {
        MethodCallExpr methodCallExpr = (MethodCallExpr) expressionWithScope;
        if (methodCallExpr.getScope().isPresent()) {
            res.addAll(flattenScope(methodCallExpr.getScope().get()));
        }
        res.add(methodCallExpr.setScope(null));
    } else if (expressionWithScope instanceof InlineCastExpr && ((InlineCastExpr) expressionWithScope).getExpression() instanceof FieldAccessExpr) {
        InlineCastExpr inlineCastExpr = (InlineCastExpr) expressionWithScope;
        Expression internalScope = ((FieldAccessExpr) inlineCastExpr.getExpression()).getScope();
        res.addAll(flattenScope((internalScope)));
        res.add(expressionWithScope);
    } else {
        res.add(expressionWithScope);
    }
    return res;
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Node(org.drools.javaparser.ast.Node) ArrayList(java.util.ArrayList) InlineCastExpr(org.drools.javaparser.ast.drlx.expr.InlineCastExpr) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 9 with Expression

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

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

the class FromVisitor method createFromCall.

private Expression createFromCall(String expression, Optional<String> optContainsBinding, String bindingId) {
    MethodCallExpr fromCall = new MethodCallExpr(null, FROM_CALL);
    fromCall.addArgument(new NameExpr(toVar(bindingId)));
    if (optContainsBinding.isPresent()) {
        DeclarationSpec declarationSpec = ruleContext.getDeclarationById(bindingId).orElseThrow(RuntimeException::new);
        Class<?> clazz = declarationSpec.getDeclarationClass();
        DrlxParseResult drlxParseResult = new ConstraintParser(ruleContext, packageModel).drlxParse(clazz, bindingId, expression);
        drlxParseResult.accept(drlxParseSuccess -> {
            Expression parsedExpression = drlxParseSuccess.getExpr();
            Expression exprArg = generateLambdaWithoutParameters(drlxParseSuccess.getUsedDeclarations(), parsedExpression);
            fromCall.addArgument(exprArg);
        });
    }
    return fromCall;
}
Also used : DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) Expression(org.drools.javaparser.ast.expr.Expression) NameExpr(org.drools.javaparser.ast.expr.NameExpr) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

Expression (org.drools.javaparser.ast.expr.Expression)48 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)32 NameExpr (org.drools.javaparser.ast.expr.NameExpr)25 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)19 List (java.util.List)14 ArrayList (java.util.ArrayList)13 Optional (java.util.Optional)13 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)13 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)11 EnclosedExpr (org.drools.javaparser.ast.expr.EnclosedExpr)11 PackageModel (org.drools.modelcompiler.builder.PackageModel)11 RuleContext (org.drools.modelcompiler.builder.generator.RuleContext)11 Node (org.drools.javaparser.ast.Node)10 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)10 Collection (java.util.Collection)9 Parameter (org.drools.javaparser.ast.body.Parameter)9 LambdaExpr (org.drools.javaparser.ast.expr.LambdaExpr)9 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)9 DrlxParseUtil.toVar (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar)8 JavaParser (org.drools.javaparser.JavaParser)7