Created
January 28, 2019 15:47
-
-
Save lucamolteni/6273bf17dc705aae896f549b7aae69b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = declStart > 0 && consequence.indexOf(')', declStart + 1) >= 0 ? StringUtils.findEndOfMethodArgsIndex(consequence, declStart) : -1; | |
if (declEnd < 0) { | |
continue; | |
} | |
String decl = consequence.substring(declStart + 1, declEnd).trim(); | |
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)); | |
} | |
Expression declAsExpr = JavaParser.parseExpression( decl ); | |
if (decl.indexOf( '(' ) >= 0) { | |
declAsExpr = new EnclosedExpr( declAsExpr ); | |
} | |
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(declAsExpr, 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; | |
modifyPos = lastCopiedEnd - 6; | |
} | |
if (lastCopiedEnd < consequence.length()) { | |
sb.append(consequence.substring(lastCopiedEnd)); | |
} | |
return sb.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment