Search in sources :

Example 1 with WithStatement

use of org.drools.mvel.parser.ast.expr.WithStatement in project drools by kiegroup.

the class PreprocessPhase method withPreprocessor.

private PreprocessPhaseResult withPreprocessor(WithStatement withStatement) {
    PreprocessPhaseResult result = new StatementResult();
    Deque<Statement> allNewStatements = new ArrayDeque<>();
    Optional<Expression> initScope = addTypeToInitialization(withStatement, allNewStatements);
    final Expression scope = initScope.orElse(withStatement.getWithObject());
    withStatement.findAll(AssignExpr.class).replaceAll(assignExpr -> assignToFieldAccess(result, scope, assignExpr));
    // Do not use findAll as we should only process top level expressions
    withStatement.getExpressions().replaceAll(e -> addScopeToMethodCallExpr(result, scope, e));
    NodeList<Statement> bodyStatements = wrapToExpressionStmt(withStatement.getExpressions());
    allNewStatements.addAll(bodyStatements);
    // delete modify statement and add the new statements to its children
    Node parentNode = withStatement.getParentNode().orElseThrow(() -> new MvelCompilerException("A parent node is expected here"));
    // See RuleChainingTest.testRuleChainingWithLogicalInserts
    if (parentNode instanceof BlockStmt) {
        BlockStmt parentBlock = (BlockStmt) parentNode;
        Iterator<Statement> newStatementsReversed = allNewStatements.descendingIterator();
        while (newStatementsReversed.hasNext()) {
            parentBlock.getStatements().addAfter(newStatementsReversed.next(), withStatement);
        }
        withStatement.remove();
    } else {
        throw new MvelCompilerException("Expecting a BlockStmt as a parent");
    }
    return result;
}
Also used : WithStatement(org.drools.mvel.parser.ast.expr.WithStatement) ModifyStatement(org.drools.mvel.parser.ast.expr.ModifyStatement) Statement(com.github.javaparser.ast.stmt.Statement) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) Node(com.github.javaparser.ast.Node) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ArrayDeque(java.util.ArrayDeque) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) Expression(com.github.javaparser.ast.expr.Expression)

Aggregations

Node (com.github.javaparser.ast.Node)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 Statement (com.github.javaparser.ast.stmt.Statement)1 ArrayDeque (java.util.ArrayDeque)1 ModifyStatement (org.drools.mvel.parser.ast.expr.ModifyStatement)1 WithStatement (org.drools.mvel.parser.ast.expr.WithStatement)1 PrintUtil.printNode (org.drools.mvel.parser.printer.PrintUtil.printNode)1