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;
}
Aggregations