Search in sources :

Example 41 with Node

use of com.github.javaparser.ast.Node in project drools by kiegroup.

the class FlattenScopeTest method expr.

private Expression expr(String inputExpr) {
    Expression expr = DrlxParseUtil.parseExpression(inputExpr).getExpr();
    // This is because parsing doesn't set type arguments.
    expr.ifMethodCallExpr(m -> m.setTypeArguments(nodeList()));
    expr.getChildNodes().forEach((Node e) -> {
        if (e instanceof Expression) {
            ((Expression) e).ifMethodCallExpr(m -> m.setTypeArguments(nodeList()));
        }
    });
    return expr;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) Node(com.github.javaparser.ast.Node)

Example 42 with Node

use of com.github.javaparser.ast.Node in project drools by kiegroup.

the class FlattenScopeTest method flattenUnaryExpression.

@Test
public void flattenUnaryExpression() {
    List<Node> actual = flattenScope(MockTypeResolver.INSTANCE, expr("getMessageId"));
    List<Node> expected = Collections.singletonList(new NameExpr("getMessageId"));
    compareArrays(actual, expected);
}
Also used : Node(com.github.javaparser.ast.Node) NameExpr(com.github.javaparser.ast.expr.NameExpr) Test(org.junit.Test)

Example 43 with Node

use of com.github.javaparser.ast.Node in project drools by kiegroup.

the class ModifyCompiler method compile.

public CompiledBlockResult compile(String mvelBlock) {
    BlockStmt mvelExpression = MvelParser.parseBlock(mvelBlock);
    preprocessPhase.removeEmptyStmt(mvelExpression);
    Set<String> usedBindings = new HashSet<>();
    mvelExpression.findAll(ModifyStatement.class).forEach(s -> {
        Optional<Node> parentNode = s.getParentNode();
        PreprocessPhase.PreprocessPhaseResult invoke = preprocessPhase.invoke(s);
        usedBindings.addAll(invoke.getUsedBindings());
        parentNode.ifPresent(p -> {
            BlockStmt parentBlock = (BlockStmt) p;
            for (String modifiedFact : invoke.getUsedBindings()) {
                parentBlock.addStatement(new MethodCallExpr(null, "update", nodeList(new NameExpr(modifiedFact))));
            }
        });
        s.remove();
    });
    return new CompiledBlockResult(mvelExpression.getStatements()).setUsedBindings(usedBindings);
}
Also used : ModifyStatement(org.drools.mvel.parser.ast.expr.ModifyStatement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Node(com.github.javaparser.ast.Node) NameExpr(com.github.javaparser.ast.expr.NameExpr) HashSet(java.util.HashSet) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 44 with Node

use of com.github.javaparser.ast.Node in project drools by kiegroup.

the class StatementVisitor method compileMVEL.

private TypedExpression compileMVEL(Node n) {
    TypedExpression rhs = new RHSPhase(mvelCompilerContext).invoke(n);
    TypedExpression lhs = new LHSPhase(mvelCompilerContext, ofNullable(rhs)).invoke(n);
    Optional<TypedExpression> postProcessedRHS = new ReProcessRHSPhase(mvelCompilerContext).invoke(rhs, lhs);
    TypedExpression postProcessedLHS = postProcessedRHS.map(ppr -> new LHSPhase(mvelCompilerContext, of(ppr)).invoke(n)).orElse(lhs);
    return postProcessedLHS;
}
Also used : ForEachStmt(com.github.javaparser.ast.stmt.ForEachStmt) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) BlockStmtT(org.drools.mvelcompiler.ast.BlockStmtT) WhileStmt(com.github.javaparser.ast.stmt.WhileStmt) Optional.of(java.util.Optional.of) DoStmtT(org.drools.mvelcompiler.ast.DoStmtT) ForEachDowncastStmtT(org.drools.mvelcompiler.ast.ForEachDowncastStmtT) DoStmt(com.github.javaparser.ast.stmt.DoStmt) Declaration(org.drools.mvelcompiler.context.Declaration) WhileStmtT(org.drools.mvelcompiler.ast.WhileStmtT) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) Expression(com.github.javaparser.ast.expr.Expression) ForEachStmtT(org.drools.mvelcompiler.ast.ForEachStmtT) Node(com.github.javaparser.ast.Node) IfStmtT(org.drools.mvelcompiler.ast.IfStmtT) SwitchStmtT(org.drools.mvelcompiler.ast.SwitchStmtT) Optional.ofNullable(java.util.Optional.ofNullable) IfStmt(com.github.javaparser.ast.stmt.IfStmt) Collectors(java.util.stream.Collectors) PrintUtil(org.drools.mvel.parser.printer.PrintUtil) ForStmt(com.github.javaparser.ast.stmt.ForStmt) List(java.util.List) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) DrlGenericVisitor(org.drools.mvel.parser.ast.visitor.DrlGenericVisitor) ForStmtT(org.drools.mvelcompiler.ast.ForStmtT) Optional(java.util.Optional) MvelCompilerContext(org.drools.mvelcompiler.context.MvelCompilerContext) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) SwitchEntryT(org.drools.mvelcompiler.ast.SwitchEntryT) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression)

Example 45 with Node

use of com.github.javaparser.ast.Node in project drools by kiegroup.

the class MvelCompiler method compileStatement.

public CompiledBlockResult compileStatement(String mvelBlock) {
    BlockStmt mvelExpression = MvelParser.parseBlock(mvelBlock);
    preprocessPhase.removeEmptyStmt(mvelExpression);
    Set<String> allUsedBindings = new HashSet<>();
    List<String> modifyUsedBindings = mvelExpression.findAll(ModifyStatement.class).stream().flatMap(this::transformStatementWithPreprocessing).collect(toList());
    List<String> withUsedBindings = mvelExpression.findAll(WithStatement.class).stream().flatMap(this::transformStatementWithPreprocessing).collect(toList());
    allUsedBindings.addAll(modifyUsedBindings);
    allUsedBindings.addAll(withUsedBindings);
    // Entry point of the compiler
    TypedExpression compiledRoot = mvelExpression.accept(statementVisitor, null);
    allUsedBindings.addAll(mvelCompilerContext.getUsedBindings());
    Node javaRoot = compiledRoot.toJavaExpression();
    if (!(javaRoot instanceof BlockStmt)) {
        throw new MvelCompilerException("With a BlockStmt as a input I was expecting a BlockStmt output");
    }
    BlockStmt compiledBlockStatement = (BlockStmt) javaRoot;
    return new CompiledBlockResult(compiledBlockStatement.getStatements()).setUsedBindings(allUsedBindings);
}
Also used : ModifyStatement(org.drools.mvel.parser.ast.expr.ModifyStatement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Node(com.github.javaparser.ast.Node) WithStatement(org.drools.mvel.parser.ast.expr.WithStatement) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) HashSet(java.util.HashSet)

Aggregations

Node (com.github.javaparser.ast.Node)95 Test (org.junit.Test)24 Expression (com.github.javaparser.ast.expr.Expression)22 NodeList (com.github.javaparser.ast.NodeList)18 Comment (com.github.javaparser.ast.comments.Comment)13 CompilationUnit (com.github.javaparser.ast.CompilationUnit)12 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)12 NameExpr (com.github.javaparser.ast.expr.NameExpr)12 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 ArrayList (java.util.ArrayList)11 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)8 Collectors (java.util.stream.Collectors)8 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)7 LineComment (com.github.javaparser.ast.comments.LineComment)7 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)7 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)7 SimpleName (com.github.javaparser.ast.expr.SimpleName)7 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)7 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)6 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)6