Search in sources :

Example 1 with EmptyStmt

use of org.drools.javaparser.ast.stmt.EmptyStmt in project drools by kiegroup.

the class Consequence method rewriteConsequenceBlock.

private String rewriteConsequenceBlock(String consequence) {
    int modifyPos = StringUtils.indexOfOutOfQuotes(consequence, "modify");
    if (modifyPos < 0) {
        return consequence;
    }
    int lastCopiedEnd = 0;
    StringBuilder sb = new StringBuilder();
    sb.append(consequence.substring(lastCopiedEnd, modifyPos));
    lastCopiedEnd = modifyPos + 1;
    for (; modifyPos >= 0; modifyPos = StringUtils.indexOfOutOfQuotes(consequence, "modify", modifyPos + 6)) {
        int declStart = consequence.indexOf('(', modifyPos + 6);
        int declEnd = consequence.indexOf(')', declStart + 1);
        if (declEnd < 0) {
            continue;
        }
        String decl = consequence.substring(declStart + 1, declEnd).trim();
        if (!context.getDeclarationById(decl).isPresent()) {
            continue;
        }
        int blockStart = consequence.indexOf('{', declEnd + 1);
        int blockEnd = consequence.indexOf('}', blockStart + 1);
        if (blockEnd < 0) {
            continue;
        }
        if (lastCopiedEnd < modifyPos) {
            sb.append(consequence.substring(lastCopiedEnd, modifyPos));
        }
        NameExpr declAsNameExpr = new NameExpr(decl);
        String originalBlock = consequence.substring(blockStart + 1, blockEnd).trim();
        BlockStmt modifyBlock = JavaParser.parseBlock("{" + originalBlock + ";}");
        List<MethodCallExpr> originalMethodCalls = modifyBlock.findAll(MethodCallExpr.class);
        for (MethodCallExpr mc : originalMethodCalls) {
            Expression mcWithScope = org.drools.modelcompiler.builder.generator.DrlxParseUtil.prepend(declAsNameExpr, mc);
            modifyBlock.replace(mc, mcWithScope);
        }
        for (Statement n : modifyBlock.getStatements()) {
            if (!(n instanceof EmptyStmt)) {
                sb.append(n);
            }
        }
        sb.append("update(").append(decl).append(");\n");
        lastCopiedEnd = blockEnd + 1;
    }
    if (lastCopiedEnd < consequence.length()) {
        sb.append(consequence.substring(lastCopiedEnd));
    }
    return sb.toString();
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) Statement(org.drools.javaparser.ast.stmt.Statement) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) NameExpr(org.drools.javaparser.ast.expr.NameExpr) EmptyStmt(org.drools.javaparser.ast.stmt.EmptyStmt) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

JavaParser.parseExpression (org.drools.javaparser.JavaParser.parseExpression)1 Expression (org.drools.javaparser.ast.expr.Expression)1 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)1 NameExpr (org.drools.javaparser.ast.expr.NameExpr)1 BlockStmt (org.drools.javaparser.ast.stmt.BlockStmt)1 EmptyStmt (org.drools.javaparser.ast.stmt.EmptyStmt)1 Statement (org.drools.javaparser.ast.stmt.Statement)1