Search in sources :

Example 1 with MvelCompilerContext

use of org.drools.mvelcompiler.context.MvelCompilerContext 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 2 with MvelCompilerContext

use of org.drools.mvelcompiler.context.MvelCompilerContext 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 3 with MvelCompilerContext

use of org.drools.mvelcompiler.context.MvelCompilerContext in project drools by kiegroup.

the class CompilerTest method test.

default void test(Consumer<MvelCompilerContext> testFunction, String inputExpression, String expectedResult, Consumer<CompiledBlockResult> resultAssert) {
    Set<String> imports = new HashSet<>();
    imports.add("java.util.List");
    imports.add("java.util.ArrayList");
    imports.add("java.util.HashMap");
    imports.add("java.util.Map");
    imports.add("java.math.BigDecimal");
    imports.add("org.drools.Address");
    imports.add(Person.class.getCanonicalName());
    imports.add(Gender.class.getCanonicalName());
    TypeResolver typeResolver = new ClassTypeResolver(imports, this.getClass().getClassLoader());
    MvelCompilerContext mvelCompilerContext = new MvelCompilerContext(typeResolver);
    testFunction.accept(mvelCompilerContext);
    CompiledBlockResult compiled = new MvelCompiler(mvelCompilerContext).compileStatement(inputExpression);
    verifyBodyWithBetterDiff(expectedResult, compiled.resultAsString());
    resultAssert.accept(compiled);
}
Also used : MvelCompilerContext(org.drools.mvelcompiler.context.MvelCompilerContext) TypeResolver(org.drools.core.addon.TypeResolver) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) Gender(org.drools.Gender) Person(org.drools.Person) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) HashSet(java.util.HashSet)

Example 4 with MvelCompilerContext

use of org.drools.mvelcompiler.context.MvelCompilerContext in project drools by kiegroup.

the class ConstraintCompilerTest method testExpression.

public void testExpression(Consumer<MvelCompilerContext> testFunction, String inputExpression, String expectedResult, Consumer<CompiledExpressionResult> resultAssert) {
    Set<String> imports = new HashSet<>();
    imports.add("java.util.List");
    imports.add("java.util.ArrayList");
    imports.add("java.util.HashMap");
    imports.add("java.util.Map");
    imports.add("java.math.BigDecimal");
    imports.add("org.drools.Address");
    imports.add(Person.class.getCanonicalName());
    imports.add(Gender.class.getCanonicalName());
    TypeResolver typeResolver = new ClassTypeResolver(imports, this.getClass().getClassLoader());
    MvelCompilerContext mvelCompilerContext = new MvelCompilerContext(typeResolver);
    testFunction.accept(mvelCompilerContext);
    CompiledExpressionResult compiled = new ConstraintCompiler(mvelCompilerContext).compileExpression(inputExpression);
    verifyBodyWithBetterDiff(expectedResult, compiled.resultAsString());
    resultAssert.accept(compiled);
}
Also used : MvelCompilerContext(org.drools.mvelcompiler.context.MvelCompilerContext) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) TypeResolver(org.drools.core.addon.TypeResolver) Gender(org.drools.Gender) Person(org.drools.Person) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) HashSet(java.util.HashSet)

Example 5 with MvelCompilerContext

use of org.drools.mvelcompiler.context.MvelCompilerContext in project drools by kiegroup.

the class DrlxParseUtil method createConstraintCompiler.

public static ConstraintCompiler createConstraintCompiler(RuleContext context, Optional<Class<?>> originalPatternType) {
    MvelCompilerContext mvelCompilerContext = new MvelCompilerContext(context.getTypeResolver(), context.getCurrentScopeSuffix());
    List<DeclarationSpec> allDeclarations = new ArrayList<>(context.getAllDeclarations());
    originalPatternType.ifPresent(pt -> {
        allDeclarations.add(new DeclarationSpec(THIS_PLACEHOLDER, pt));
        mvelCompilerContext.setRootPatternPrefix(pt, THIS_PLACEHOLDER);
    });
    for (Map.Entry<String, Method> m : context.getPackageModel().getStaticMethods().entrySet()) {
        mvelCompilerContext.addStaticMethod(m.getKey(), m.getValue());
    }
    for (DeclarationSpec ds : allDeclarations) {
        mvelCompilerContext.addDeclaration(ds.getBindingId(), ds.getDeclarationClass());
    }
    return new ConstraintCompiler(mvelCompilerContext);
}
Also used : MvelCompilerContext(org.drools.mvelcompiler.context.MvelCompilerContext) ArrayList(java.util.ArrayList) MethodUtils.findMethod(org.drools.core.util.MethodUtils.findMethod) Method(java.lang.reflect.Method) ConstraintCompiler(org.drools.mvelcompiler.ConstraintCompiler) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Aggregations

MvelCompilerContext (org.drools.mvelcompiler.context.MvelCompilerContext)7 HashSet (java.util.HashSet)3 Map (java.util.Map)3 ClassTypeResolver (org.drools.core.addon.ClassTypeResolver)3 TypeResolver (org.drools.core.addon.TypeResolver)3 Node (com.github.javaparser.ast.Node)2 Expression (com.github.javaparser.ast.expr.Expression)2 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 DoStmt (com.github.javaparser.ast.stmt.DoStmt)2 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)2 ForEachStmt (com.github.javaparser.ast.stmt.ForEachStmt)2 ForStmt (com.github.javaparser.ast.stmt.ForStmt)2 IfStmt (com.github.javaparser.ast.stmt.IfStmt)2 SwitchEntry (com.github.javaparser.ast.stmt.SwitchEntry)2 SwitchStmt (com.github.javaparser.ast.stmt.SwitchStmt)2 WhileStmt (com.github.javaparser.ast.stmt.WhileStmt)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 Optional (java.util.Optional)2 Optional.of (java.util.Optional.of)2