Search in sources :

Example 36 with Expression

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

Example 37 with Expression

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

the class ExpressionTyper method nameExpr.

private Optional<TypedExpressionCursor> nameExpr(Expression drlxExpr, NameExpr firstNode, boolean isInLineCast, Class<?> originalTypeCursor) {
    Optional<TypedExpressionCursor> teCursor;
    String firstName = firstNode.getName().getIdentifier();
    Optional<DeclarationSpec> declarationById = ruleContext.getDeclarationById(firstName);
    if (declarationById.isPresent()) {
        // do NOT append any reactOnProperties.
        // because reactOnProperties is referring only to the properties of the type of the pattern, not other declarations properites.
        context.addUsedDeclarations(firstName);
        final Class<?> typeCursor;
        if (!isInLineCast) {
            typeCursor = declarationById.get().getDeclarationClass();
        } else {
            typeCursor = originalTypeCursor;
        }
        teCursor = of(new TypedExpressionCursor(new NameExpr(firstName), typeCursor));
    } else if (packageModel.getGlobals().containsKey(firstName)) {
        context.addUsedDeclarations(firstName);
        return of(new TypedExpressionCursor(new NameExpr(firstName), packageModel.getGlobals().get(firstName)));
    } else {
        final Class<?> typeCursor;
        // In OOPath a declaration is based on a position rather than a name.
        // Only an OOPath chunk can have a backreference expression
        Optional<DeclarationSpec> backReference = Optional.empty();
        if (firstNode.getBackReferencesCount() > 0) {
            List<DeclarationSpec> ooPathDeclarations = ruleContext.getOOPathDeclarations();
            DeclarationSpec backReferenceDeclaration = ooPathDeclarations.get(ooPathDeclarations.size() - 1 - firstNode.getBackReferencesCount());
            typeCursor = backReferenceDeclaration.getDeclarationClass();
            backReference = of(backReferenceDeclaration);
            context.addUsedDeclarations(backReferenceDeclaration.getBindingId());
        } else {
            typeCursor = originalTypeCursor;
        }
        Method firstAccessor = ClassUtils.getAccessor((!isInLineCast) ? typeCursor : patternType, firstName);
        if (firstAccessor != null) {
            // Hack to review - if a property is upper case it's probably not a react on property
            if (!"".equals(firstName) && Character.isLowerCase(firstName.charAt(0))) {
                context.addReactOnProperties(firstName);
            }
            final Class<?> typeOfFirstAccessor;
            if (!isInLineCast) {
                typeOfFirstAccessor = firstAccessor.getReturnType();
            } else {
                typeOfFirstAccessor = typeCursor;
            }
            NameExpr thisAccessor = new NameExpr("_this");
            final NameExpr scope = backReference.map(d -> new NameExpr(d.getBindingId())).orElse(thisAccessor);
            teCursor = of(new TypedExpressionCursor(new MethodCallExpr(scope, firstAccessor.getName()), typeOfFirstAccessor));
        } else {
            try {
                Class<?> resolvedType = ruleContext.getTypeResolver().resolveType(firstName);
                return of(new TypedExpressionCursor(null, resolvedType));
            } catch (ClassNotFoundException e) {
            // ignore
            }
            final Optional<Node> rootNode = findRootNodeViaParent(drlxExpr);
            rootNode.ifPresent(n -> {
                // In the error messages HalfBinary are transformed to Binary
                Node withHalfBinaryReplaced = replaceAllHalfBinaryChildren(n);
                ruleContext.addCompilationError(new ParseExpressionErrorResult((Expression) withHalfBinaryReplaced));
            });
            teCursor = Optional.empty();
        }
    }
    return teCursor;
}
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) Optional(java.util.Optional) Node(org.drools.javaparser.ast.Node) NameExpr(org.drools.javaparser.ast.expr.NameExpr) Method(java.lang.reflect.Method) ParseExpressionErrorResult(org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.drools.javaparser.ast.NodeList) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 38 with Expression

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

the class ExpressionTyper method toTypedExpressionFromMethodCallOrField.

private TypedExpressionResult toTypedExpressionFromMethodCallOrField(Expression drlxExpr) {
    final List<Node> childrenNodes = flattenScope(drlxExpr);
    final Node firstChild = childrenNodes.get(0);
    boolean isInLineCast = firstChild instanceof InlineCastExpr;
    final Class<?> originalTypeCursor;
    final Node firstNode;
    if (isInLineCast) {
        InlineCastExpr inlineCast = (InlineCastExpr) firstChild;
        originalTypeCursor = originalTypeCursorFromInlineCast(inlineCast);
        firstNode = inlineCast.getExpression();
    } else {
        originalTypeCursor = patternType;
        firstNode = firstChild;
    }
    final Optional<TypedExpressionCursor> teCursor = processFirstNode(drlxExpr, childrenNodes, firstNode, isInLineCast, originalTypeCursor);
    Expression previous;
    Class<?> typeCursor;
    if (!teCursor.isPresent()) {
        return new TypedExpressionResult(Optional.empty(), context);
    } else {
        previous = teCursor.get().expressionCursor;
        typeCursor = teCursor.get().typeCursor;
    }
    List<Node> childrenWithoutFirst = childrenNodes.subList(1, childrenNodes.size());
    for (Node part : childrenWithoutFirst) {
        if (typeCursor.isEnum()) {
            previous = drlxExpr;
        } else if (part instanceof SimpleName) {
            String field = part.toString();
            TypedExpression expression = nameExprToMethodCallExpr(field, typeCursor, previous);
            typeCursor = expression.getType();
            previous = expression.getExpression();
        } else if (part instanceof MethodCallExpr) {
            TypedExpressionCursor typedExpr = methodCallExpr((MethodCallExpr) part, typeCursor, previous);
            typeCursor = typedExpr.typeCursor;
            previous = typedExpr.expressionCursor;
        } else if (part instanceof InlineCastExpr && ((InlineCastExpr) part).getExpression() instanceof FieldAccessExpr) {
            InlineCastExpr inlineCastExprPart = (InlineCastExpr) part;
            final FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) inlineCastExprPart.getExpression();
            final TypedExpression toMethodCallExpr = nameExprToMethodCallExpr(fieldAccessExpr.getNameAsString(), typeCursor, previous);
            final Class<?> castClass = getClassFromType(ruleContext.getTypeResolver(), inlineCastExprPart.getType());
            previous = addCastToExpression(castClass, toMethodCallExpr.getExpression(), false);
        } else {
            throw new UnsupportedOperationException();
        }
    }
    return new TypedExpressionResult(of(new TypedExpression().setExpression(previous).setType(typeCursor)), context);
}
Also used : Node(org.drools.javaparser.ast.Node) SimpleName(org.drools.javaparser.ast.expr.SimpleName) InlineCastExpr(org.drools.javaparser.ast.drlx.expr.InlineCastExpr) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 39 with Expression

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

the class CustomOperatorSpec method getExpression.

public Expression getExpression(RuleContext context, PointFreeExpr pointFreeExpr, TypedExpression left) {
    MethodCallExpr methodCallExpr = new MethodCallExpr(null, "eval");
    String opName = pointFreeExpr.getOperator().asString();
    Operator operator = Operator.Register.getOperator(opName);
    try {
        // if the operator has an INSTANCE field avoid the operator lookup at runtime
        operator.getClass().getField("INSTANCE");
        methodCallExpr.addArgument(operator.getClass().getCanonicalName() + ".INSTANCE");
    } catch (NoSuchFieldException e) {
        methodCallExpr.addArgument(new StringLiteralExpr(opName));
    }
    methodCallExpr.addArgument(left.getExpression());
    for (Expression rightExpr : pointFreeExpr.getRight()) {
        if (rightExpr instanceof LiteralExpr) {
            methodCallExpr.addArgument(rightExpr);
        } else {
            TypedExpression typedExpression = DrlxParseUtil.toMethodCallWithClassCheck(context, rightExpr, null, null, context.getTypeResolver());
            methodCallExpr.addArgument(typedExpression.getExpression());
        }
    }
    return pointFreeExpr.isNegated() ? new UnaryExpr(methodCallExpr, UnaryExpr.Operator.LOGICAL_COMPLEMENT) : methodCallExpr;
}
Also used : Operator(org.drools.model.functions.Operator) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Expression(org.drools.javaparser.ast.expr.Expression) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 40 with Expression

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

the class EvalVisitor method visit.

public void visit(EvalDescr descr) {
    String expression = descr.getContent().toString();
    DrlxParseResult drlxParseResult = new ConstraintParser(context, packageModel).drlxParse(null, null, expression);
    drlxParseResult.accept(drlxParseSuccess -> {
        Expression rewriteExprAsLambdaWithoutThisParam = DrlxParseUtil.generateLambdaWithoutParameters(drlxParseSuccess.getUsedDeclarations(), drlxParseSuccess.getExpr(), true);
        // rewrites the DrlxParserResult expr as directly the lambda to use
        drlxParseSuccess.setExpr(rewriteExprAsLambdaWithoutThisParam);
        drlxParseSuccess.setStatic(true);
        new FlowExpressionBuilder(context).processExpression(drlxParseSuccess);
    });
}
Also used : ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) FlowExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.FlowExpressionBuilder)

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