Search in sources :

Example 6 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)

Example 7 with MvelCompilerContext

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

the class DrlxParseUtil method createMvelCompiler.

public static MvelCompiler createMvelCompiler(RuleContext context) {
    MvelCompilerContext mvelCompilerContext = new MvelCompilerContext(context.getTypeResolver(), context.getCurrentScopeSuffix());
    for (DeclarationSpec ds : context.getAllDeclarations()) {
        mvelCompilerContext.addDeclaration(ds.getBindingId(), ds.getDeclarationClass());
    }
    for (Map.Entry<String, Method> m : context.getPackageModel().getStaticMethods().entrySet()) {
        mvelCompilerContext.addStaticMethod(m.getKey(), m.getValue());
    }
    for (MethodDeclaration m : context.getPackageModel().getFunctions()) {
        List<String> parametersType = m.getParameters().stream().map(Parameter::getType).map(Type::asString).collect(toList());
        mvelCompilerContext.addDeclaredFunction(m.getNameAsString(), m.getTypeAsString(), parametersType);
    }
    return new MvelCompiler(mvelCompilerContext);
}
Also used : MvelCompiler(org.drools.mvelcompiler.MvelCompiler) MvelCompilerContext(org.drools.mvelcompiler.context.MvelCompilerContext) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Parameter(com.github.javaparser.ast.body.Parameter) MethodUtils.findMethod(org.drools.core.util.MethodUtils.findMethod) Method(java.lang.reflect.Method) 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