Search in sources :

Example 6 with TypedExpression

use of org.drools.mvelcompiler.ast.TypedExpression 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 7 with TypedExpression

use of org.drools.mvelcompiler.ast.TypedExpression in project drools by kiegroup.

the class StatementVisitor method visit.

@Override
public TypedExpression visit(WhileStmt n, Void arg) {
    TypedExpression typedCondition = new RHSPhase(mvelCompilerContext).invoke(n.getCondition());
    TypedExpression typedThen = n.getBody().accept(this, arg);
    return new WhileStmtT(typedCondition, typedThen);
}
Also used : WhileStmtT(org.drools.mvelcompiler.ast.WhileStmtT) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression)

Example 8 with TypedExpression

use of org.drools.mvelcompiler.ast.TypedExpression in project drools by kiegroup.

the class StatementVisitor method visit.

@Override
public TypedExpression visit(SwitchStmt n, Void arg) {
    TypedExpression typedSelector = new RHSPhase(mvelCompilerContext).invoke(n.getSelector());
    List<TypedExpression> typedEntries = n.getEntries().stream().map(e -> e.accept(this, arg)).collect(Collectors.toList());
    return new SwitchStmtT(typedSelector, typedEntries);
}
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) SwitchStmtT(org.drools.mvelcompiler.ast.SwitchStmtT) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression)

Example 9 with TypedExpression

use of org.drools.mvelcompiler.ast.TypedExpression in project drools by kiegroup.

the class StatementVisitor method visit.

@Override
public TypedExpression visit(ForEachStmt n, Void arg) {
    Expression iterable = n.getIterable();
    Optional<TypedExpression> convertedToDowncastStmt = iterable.toNameExpr().map(PrintUtil::printNode).flatMap(mvelCompilerContext::findDeclarations).filter(this::isDeclarationIterable).map(d -> toForEachDowncastStmtT(n, arg));
    if (convertedToDowncastStmt.isPresent()) {
        return convertedToDowncastStmt.get();
    }
    TypedExpression variableDeclarationExpr = new LHSPhase(mvelCompilerContext, Optional.empty()).invoke(n.getVariable());
    TypedExpression typedIterable = new RHSPhase(mvelCompilerContext).invoke(n.getIterable());
    TypedExpression body = n.getBody().accept(this, arg);
    return new ForEachStmtT(variableDeclarationExpr, typedIterable, body);
}
Also used : UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) ForEachStmtT(org.drools.mvelcompiler.ast.ForEachStmtT)

Example 10 with TypedExpression

use of org.drools.mvelcompiler.ast.TypedExpression 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

TypedExpression (org.drools.mvelcompiler.ast.TypedExpression)21 UnalteredTypedExpression (org.drools.mvelcompiler.ast.UnalteredTypedExpression)18 Expression (com.github.javaparser.ast.expr.Expression)8 Node (com.github.javaparser.ast.Node)4 Type (java.lang.reflect.Type)4 Optional (java.util.Optional)4 FieldToAccessorTExpr (org.drools.mvelcompiler.ast.FieldToAccessorTExpr)4 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)3 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 Optional.ofNullable (java.util.Optional.ofNullable)3 DrlGenericVisitor (org.drools.mvel.parser.ast.visitor.DrlGenericVisitor)3 DoStmtT (org.drools.mvelcompiler.ast.DoStmtT)3 ForEachStmtT (org.drools.mvelcompiler.ast.ForEachStmtT)3 MapPutExprT (org.drools.mvelcompiler.ast.MapPutExprT)3 WhileStmtT (org.drools.mvelcompiler.ast.WhileStmtT)3 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)2 DoStmt (com.github.javaparser.ast.stmt.DoStmt)2 ForEachStmt (com.github.javaparser.ast.stmt.ForEachStmt)2