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