Search in sources :

Example 1 with HalfPointFreeExpr

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

Aggregations

Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Optional.of (java.util.Optional.of)1 ClassUtils (org.drools.core.util.ClassUtils)1 ClassUtils.getter2property (org.drools.core.util.ClassUtils.getter2property)1 JavaParser (org.drools.javaparser.JavaParser)1 Node (org.drools.javaparser.ast.Node)1 NodeList (org.drools.javaparser.ast.NodeList)1 HalfBinaryExpr (org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr)1 HalfPointFreeExpr (org.drools.javaparser.ast.drlx.expr.HalfPointFreeExpr)1 InlineCastExpr (org.drools.javaparser.ast.drlx.expr.InlineCastExpr)1 NullSafeFieldAccessExpr (org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr)1 PointFreeExpr (org.drools.javaparser.ast.drlx.expr.PointFreeExpr)1 ArrayCreationExpr (org.drools.javaparser.ast.expr.ArrayCreationExpr)1 ArrayInitializerExpr (org.drools.javaparser.ast.expr.ArrayInitializerExpr)1 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)1 CastExpr (org.drools.javaparser.ast.expr.CastExpr)1 EnclosedExpr (org.drools.javaparser.ast.expr.EnclosedExpr)1