Skip to content

Instantly share code, notes, and snippets.

@lucamolteni
Created February 5, 2020 10:08
Show Gist options
  • Select an option

  • Save lucamolteni/489536ef67dc775c6f630ee2bd1ef75a to your computer and use it in GitHub Desktop.

Select an option

Save lucamolteni/489536ef67dc775c6f630ee2bd1ef75a to your computer and use it in GitHub Desktop.
package org.drools.modelcompiler.builder.generator.visitor.accumulate;
import java.util.List;
import java.util.Optional;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.printer.PrettyPrinter;
import com.github.javaparser.printer.PrettyPrinterConfiguration;
import org.drools.modelcompiler.builder.generator.DrlxParseUtil;
public class ProvaJP {
public static void main(String[] args) {
PrettyPrinterConfiguration prettyPrinterConfiguration = new PrettyPrinterConfiguration();
PrettyPrinter prettyPrinter = new PrettyPrinter(prettyPrinterConfiguration);
Expression dsl = StaticJavaParser.parseExpression("accumulate(and(" +
" expr(), " +
" bind().as().reactOn()" +
" ), " +
" accFunction())");
System.out.println(prettyPrinter.print(dsl));
System.out.println("-------------------------");
MethodCallExpr input = new MethodCallExpr(null, "input");
findBind(dsl).forEach(n -> {
n.replace(input);
int a = 0;
});
System.out.println(prettyPrinter.print(dsl));
}
private static List<MethodCallExpr> findBind(Expression dsl) {
return dsl.findAll(MethodCallExpr.class, mc -> DrlxParseUtil.findRootNodeViaScope(mc)
.filter(l -> l.isMethodCallExpr() && l.asMethodCallExpr().getNameAsString().equals("bind")).isPresent());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment