Search in sources :

Example 21 with EnclosedExpr

use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.

the class AccumulateInline method parseResultMethod.

private void parseResultMethod() {
    // <result expression>: this is a semantic expression in the selected dialect that is executed after all source objects are iterated.
    MethodDeclaration resultMethod = getMethodFromTemplateClass("getResult");
    Type returnExpressionType = toClassOrInterfaceType(java.lang.Object.class);
    Expression returnExpression = StaticJavaParser.parseExpression(accumulateDescr.getResultCode());
    if (returnExpression instanceof NameExpr) {
        returnExpression = new EnclosedExpr(returnExpression);
    }
    rescopeNamesToNewScope(getDataNameExpr(), contextFieldNames, returnExpression);
    resultMethod.getBody().orElseThrow(InvalidInlineTemplateException::new).addStatement(new ReturnStmt(returnExpression));
    MethodDeclaration getResultTypeMethod = getMethodFromTemplateClass("getResultType");
    getResultTypeMethod.getBody().orElseThrow(InvalidInlineTemplateException::new).addStatement(new ReturnStmt(new ClassExpr(returnExpressionType)));
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 22 with EnclosedExpr

use of com.github.javaparser.ast.expr.EnclosedExpr 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)

Example 23 with EnclosedExpr

use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.

the class AstUtils method transformHalfBinaryArg.

private static Expression transformHalfBinaryArg(TokenRange tokenRange, Expression scope, Expression name, Expression expr, boolean nullSafe) {
    if (expr instanceof HalfBinaryExpr) {
        Expression left = scope == null ? name : (nullSafe ? new NullSafeFieldAccessExpr(scope, null, name.asNameExpr().getName()) : new FieldAccessExpr(scope, null, name.asNameExpr().getName()));
        return new BinaryExpr(tokenRange, left, ((HalfBinaryExpr) expr).getRight(), ((HalfBinaryExpr) expr).getOperator().toBinaryExprOperator());
    }
    if (expr instanceof EnclosedExpr) {
        return transformHalfBinaryArg(tokenRange, scope, name, ((EnclosedExpr) expr).getInner(), nullSafe);
    }
    if (expr instanceof BinaryExpr) {
        BinaryExpr binary = (BinaryExpr) expr;
        Expression rewrittenLeft = transformHalfBinaryArg(tokenRange, scope, name, binary.getLeft(), nullSafe);
        Expression rewrittenRight = binary.getRight() instanceof HalfBinaryExpr && !(binary.getLeft() instanceof EnclosedExpr) ? binary.getRight() : transformHalfBinaryArg(tokenRange, scope, name, binary.getRight(), nullSafe);
        rewrittenRight.setParentNode(rewrittenLeft);
        return new BinaryExpr(tokenRange, rewrittenLeft, rewrittenRight, binary.getOperator());
    }
    throw new IllegalStateException();
}
Also used : HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) NullSafeFieldAccessExpr(org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) NullSafeFieldAccessExpr(org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr)

Example 24 with EnclosedExpr

use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.

the class DrlxParseUtil method findRootNodeViaScopeRec.

private static RemoveRootNodeResult findRootNodeViaScopeRec(Expression expr, LinkedList<Expression> acc) {
    if (expr.isArrayAccessExpr()) {
        throw new RuntimeException("This doesn't work on arrayAccessExpr convert them to a method call");
    }
    if (expr instanceof EnclosedExpr) {
        return findRootNodeViaScopeRec(expr.asEnclosedExpr().getInner(), acc);
    } else if (expr instanceof CastExpr) {
        return findRootNodeViaScopeRec(expr.asCastExpr().getExpression(), acc);
    } else if (expr instanceof ThisExpr) {
        return new RemoveRootNodeResult(Optional.of(expr), expr, expr);
    } else if (expr instanceof NodeWithTraversableScope) {
        final NodeWithTraversableScope exprWithScope = (NodeWithTraversableScope) expr;
        return exprWithScope.traverseScope().flatMap((Expression scope) -> {
            if (isDslTopLevelNamespace(scope)) {
                return empty();
            }
            Expression sanitizedExpr = DrlxParseUtil.transformDrlNameExprToNameExpr(expr);
            acc.addLast(sanitizedExpr.clone());
            return of(findRootNodeViaScopeRec(scope, acc));
        }).orElse(new RemoveRootNodeResult(Optional.of(expr), expr, acc.isEmpty() ? expr : acc.getLast()));
    } else if (expr instanceof NameExpr) {
        if (!acc.isEmpty() && acc.getLast() instanceof NodeWithOptionalScope<?>) {
            ((NodeWithOptionalScope<?>) acc.getLast()).setScope(null);
            for (ListIterator<Expression> iterator = acc.listIterator(); iterator.hasNext(); ) {
                Expression e = iterator.next();
                if (e instanceof NodeWithOptionalScope) {
                    NodeWithOptionalScope<?> node = (NodeWithOptionalScope<?>) e;
                    if (iterator.hasNext()) {
                        node.setScope(acc.get(iterator.nextIndex()));
                    }
                }
            }
            return new RemoveRootNodeResult(Optional.of(expr), acc.getFirst(), acc.getLast());
        } else {
            return new RemoveRootNodeResult(Optional.of(expr), expr, expr);
        }
    }
    return new RemoveRootNodeResult(empty(), expr, expr);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) CastExpr(com.github.javaparser.ast.expr.CastExpr) NodeWithTraversableScope(com.github.javaparser.ast.nodeTypes.NodeWithTraversableScope) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) NodeWithOptionalScope(com.github.javaparser.ast.nodeTypes.NodeWithOptionalScope) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) ThisExpr(com.github.javaparser.ast.expr.ThisExpr)

Example 25 with EnclosedExpr

use of com.github.javaparser.ast.expr.EnclosedExpr in project drools by kiegroup.

the class ConstraintUtil method normalizeConstraint.

/**
 * Swap left and right operands in a constraint when a fact property is located on the right side.
 *
 * e.g. Person(20 < age) should be normalized to Person(age > 20)
 *
 * @param drlxParseResult
 * @return Normalized <code>DrlxParseResult</code>
 */
public static DrlxParseResult normalizeConstraint(DrlxParseResult drlxParseResult) {
    if (!ENABLE_NORMALIZE) {
        return drlxParseResult;
    }
    if (drlxParseResult instanceof SingleDrlxParseSuccess) {
        // Create a copy
        SingleDrlxParseSuccess drlx = new SingleDrlxParseSuccess((SingleDrlxParseSuccess) drlxParseResult);
        Expression expr = drlx.getExpr();
        if (expr == null) {
            return drlxParseResult;
        }
        if (expr instanceof MethodCallExpr) {
            processTopLevelExpression(drlx, (MethodCallExpr) expr);
        } else if (expr instanceof EnclosedExpr) {
            Expression inner = stripEnclosedExpr((EnclosedExpr) expr);
            if (inner instanceof MethodCallExpr) {
                processTopLevelExpression(drlx, (MethodCallExpr) inner);
            } else {
                processExpression(expr);
            }
        } else {
            processExpression(expr);
        }
        return drlx;
    }
    return drlxParseResult;
}
Also used : SingleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.SingleDrlxParseSuccess) Expression(com.github.javaparser.ast.expr.Expression) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) DrlxParseUtil.stripEnclosedExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)25 Expression (com.github.javaparser.ast.expr.Expression)22 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)12 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)9 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)9 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)8 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)8 CastExpr (com.github.javaparser.ast.expr.CastExpr)7 NameExpr (com.github.javaparser.ast.expr.NameExpr)7 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)7 Node (com.github.javaparser.ast.Node)6 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)6 UnaryExpr (com.github.javaparser.ast.expr.UnaryExpr)6 ArrayList (java.util.ArrayList)6 DrlxParseUtil.stripEnclosedExpr (org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr)6 ParseExpressionErrorResult (org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult)5 SimpleName (com.github.javaparser.ast.expr.SimpleName)4 ThisExpr (com.github.javaparser.ast.expr.ThisExpr)4 Optional (java.util.Optional)4 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)4