use of org.drools.javaparser.ast.stmt.Statement in project drools by kiegroup.
the class AccumulateVisitor method visitAccInlineCustomCode.
/**
* By design this legacy accumulate (with inline custome code) visitor supports only with 1-and-only binding in the accumulate code/expressions.
*/
protected void visitAccInlineCustomCode(RuleContext context2, AccumulateDescr descr, MethodCallExpr accumulateDSL, PatternDescr basePattern, PatternDescr inputDescr) {
context.pushExprPointer(accumulateDSL::addArgument);
final MethodCallExpr functionDSL = new MethodCallExpr(null, "accFunction");
String code = null;
try {
code = new String(IoUtils.readBytesFromInputStream(this.getClass().getResourceAsStream("/AccumulateInlineFunction.java")));
} catch (IOException e1) {
e1.printStackTrace();
throw new RuntimeException("Unable to locate template.");
}
String targetClassName = StringUtil.toId(context2.getRuleDescr().getName()) + "Accumulate" + descr.getLine();
code = code.replaceAll("AccumulateInlineFunction", targetClassName);
CompilationUnit templateCU = JavaParser.parse(code);
ClassOrInterfaceDeclaration templateClass = templateCU.getClassByName(targetClassName).orElseThrow(() -> new RuntimeException("Template did not contain expected type definition."));
ClassOrInterfaceDeclaration templateContextClass = templateClass.getMembers().stream().filter(m -> m instanceof ClassOrInterfaceDeclaration && ((ClassOrInterfaceDeclaration) m).getNameAsString().equals("ContextData")).map(ClassOrInterfaceDeclaration.class::cast).findFirst().orElseThrow(() -> new RuntimeException("Template did not contain expected type definition."));
List<String> contextFieldNames = new ArrayList<>();
MethodDeclaration initMethod = templateClass.getMethodsByName("init").get(0);
BlockStmt initBlock = JavaParser.parseBlock("{" + descr.getInitCode() + "}");
for (Statement stmt : initBlock.getStatements()) {
if (stmt instanceof ExpressionStmt && ((ExpressionStmt) stmt).getExpression() instanceof VariableDeclarationExpr) {
VariableDeclarationExpr vdExpr = (VariableDeclarationExpr) ((ExpressionStmt) stmt).getExpression();
for (VariableDeclarator vd : vdExpr.getVariables()) {
contextFieldNames.add(vd.getNameAsString());
templateContextClass.addField(vd.getType(), vd.getNameAsString(), Modifier.PUBLIC);
if (vd.getInitializer().isPresent()) {
Expression initializer = vd.getInitializer().get();
Expression target = new FieldAccessExpr(new NameExpr("data"), vd.getNameAsString());
Statement initStmt = new ExpressionStmt(new AssignExpr(target, initializer, AssignExpr.Operator.ASSIGN));
initMethod.getBody().get().addStatement(initStmt);
}
}
} else {
// add as-is.
initMethod.getBody().get().addStatement(stmt);
}
}
Type singleAccumulateType = JavaParser.parseType("java.lang.Object");
MethodDeclaration accumulateMethod = templateClass.getMethodsByName("accumulate").get(0);
BlockStmt actionBlock = JavaParser.parseBlock("{" + descr.getActionCode() + "}");
Collection<String> allNamesInActionBlock = collectNamesInBlock(context2, actionBlock);
if (allNamesInActionBlock.size() == 1) {
String nameExpr = allNamesInActionBlock.iterator().next();
accumulateMethod.getParameter(1).setName(nameExpr);
singleAccumulateType = context2.getDeclarationById(nameExpr).get().getType();
} else {
new LegacyAccumulate(context, descr, basePattern).build();
return;
}
writeAccumulateMethod(contextFieldNames, singleAccumulateType, accumulateMethod, actionBlock);
// <result expression>: this is a semantic expression in the selected dialect that is executed after all source objects are iterated.
MethodDeclaration resultMethod = templateClass.getMethodsByName("getResult").get(0);
Type returnExpressionType = JavaParser.parseType("java.lang.Object");
Expression returnExpression = JavaParser.parseExpression(descr.getResultCode());
if (returnExpression instanceof NameExpr) {
returnExpression = new EnclosedExpr(returnExpression);
}
rescopeNamesToNewScope(new NameExpr("data"), contextFieldNames, returnExpression);
resultMethod.getBody().get().addStatement(new ReturnStmt(returnExpression));
MethodDeclaration getResultTypeMethod = templateClass.getMethodsByName("getResultType").get(0);
getResultTypeMethod.getBody().get().addStatement(new ReturnStmt(new ClassExpr(returnExpressionType)));
if (descr.getReverseCode() != null) {
MethodDeclaration supportsReverseMethod = templateClass.getMethodsByName("supportsReverse").get(0);
supportsReverseMethod.getBody().get().addStatement(JavaParser.parseStatement("return true;"));
MethodDeclaration reverseMethod = templateClass.getMethodsByName("reverse").get(0);
BlockStmt reverseBlock = JavaParser.parseBlock("{" + descr.getReverseCode() + "}");
Collection<String> allNamesInReverseBlock = collectNamesInBlock(context2, reverseBlock);
if (allNamesInReverseBlock.size() == 1) {
reverseMethod.getParameter(1).setName(allNamesInReverseBlock.iterator().next());
} else {
new LegacyAccumulate(context, descr, basePattern).build();
return;
}
writeAccumulateMethod(contextFieldNames, singleAccumulateType, reverseMethod, reverseBlock);
} else {
MethodDeclaration supportsReverseMethod = templateClass.getMethodsByName("supportsReverse").get(0);
supportsReverseMethod.getBody().get().addStatement(JavaParser.parseStatement("return false;"));
MethodDeclaration reverseMethod = templateClass.getMethodsByName("reverse").get(0);
reverseMethod.getBody().get().addStatement(JavaParser.parseStatement("throw new UnsupportedOperationException(\"This function does not support reverse.\");"));
}
// add resulting accumulator class into the package model
this.packageModel.addGeneratedPOJO(templateClass);
functionDSL.addArgument(new ClassExpr(JavaParser.parseType(targetClassName)));
functionDSL.addArgument(new NameExpr(toVar(inputDescr.getIdentifier())));
final String bindingId = basePattern.getIdentifier();
final MethodCallExpr asDSL = new MethodCallExpr(functionDSL, "as");
asDSL.addArgument(new NameExpr(toVar(bindingId)));
accumulateDSL.addArgument(asDSL);
context.popExprPointer();
}
use of org.drools.javaparser.ast.stmt.Statement 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();
}
use of org.drools.javaparser.ast.stmt.Statement in project drools by kiegroup.
the class POJOGenerator method generateHashCodeMethod.
private static MethodDeclaration generateHashCodeMethod(List<Statement> hashCodeFieldStatement) {
final Statement header = parseStatement("int result = super.hashCode();");
NodeList<Statement> hashCodeStatements = nodeList(header);
hashCodeStatements.addAll(hashCodeFieldStatement);
hashCodeStatements.add(parseStatement("return result;"));
final Type returnType = JavaParser.parseType(int.class.getSimpleName());
final MethodDeclaration equals = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC), returnType, HASH_CODE);
equals.addAnnotation("Override");
equals.setBody(new BlockStmt(hashCodeStatements));
return equals;
}
use of org.drools.javaparser.ast.stmt.Statement in project drools by kiegroup.
the class POJOGenerator method generateEqualsMethod.
private static MethodDeclaration generateEqualsMethod(String generatedClassName, List<Statement> equalsFieldStatement) {
NodeList<Statement> equalsStatements = nodeList(referenceEquals, classCheckEquals);
equalsStatements.add(classCastStatement(generatedClassName));
equalsStatements.add(parseStatement("if ( !super.equals( o ) ) return false;"));
equalsStatements.addAll(equalsFieldStatement);
equalsStatements.add(parseStatement("return true;"));
final Type returnType = JavaParser.parseType(boolean.class.getSimpleName());
final MethodDeclaration equals = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC), returnType, EQUALS);
equals.addParameter(Object.class, "o");
equals.addAnnotation("Override");
equals.setBody(new BlockStmt(equalsStatements));
return equals;
}
Aggregations