Search in sources :

Example 1 with ObjectCreationExpr

use of org.drools.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.

the class Consequence method executeScriptCall.

private MethodCallExpr executeScriptCall(RuleDescr ruleDescr, MethodCallExpr onCall) {
    MethodCallExpr executeCall = new MethodCallExpr(onCall, EXECUTESCRIPT_CALL);
    executeCall.addArgument(new StringLiteralExpr("mvel"));
    ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
    objectCreationExpr.setType(StringBuilder.class.getCanonicalName());
    Expression mvelSB = objectCreationExpr;
    for (String i : packageModel.getImports()) {
        if (i.equals(packageModel.getName() + ".*")) {
            // skip same-package star import.
            continue;
        }
        MethodCallExpr appendCall = new MethodCallExpr(mvelSB, "append");
        StringLiteralExpr importAsStringLiteral = new StringLiteralExpr();
        // use the setter method in order for the string literal be properly escaped.
        importAsStringLiteral.setString("import " + i + ";\n");
        appendCall.addArgument(importAsStringLiteral);
        mvelSB = appendCall;
    }
    StringLiteralExpr mvelScriptBodyStringLiteral = new StringLiteralExpr();
    // use the setter method in order for the string literal be properly escaped.
    mvelScriptBodyStringLiteral.setString(ruleDescr.getConsequence().toString());
    MethodCallExpr appendCall = new MethodCallExpr(mvelSB, "append");
    appendCall.addArgument(mvelScriptBodyStringLiteral);
    executeCall.addArgument(new MethodCallExpr(appendCall, "toString"));
    return executeCall;
}
Also used : ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) Expression(org.drools.javaparser.ast.expr.Expression) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) 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 ObjectCreationExpr (org.drools.javaparser.ast.expr.ObjectCreationExpr)1 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)1