Search in sources :

Example 1 with MvelCompiler

use of org.drools.mvelcompiler.MvelCompiler in project drools by kiegroup.

the class Evaluator method compileWithMvelCompiler.

private static CompiledResult compileWithMvelCompiler(Object compiledExpression, Map<String, Object> vars, ClassLoader classLoader) {
    String expressionString = compiledExpression.toString();
    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");
    TypeResolver classTypeResolver = new ClassTypeResolver(imports, classLoader);
    MvelCompilerContext context = new MvelCompilerContext(classTypeResolver);
    for (Map.Entry<String, Object> o : vars.entrySet()) {
        Object value1 = o.getValue();
        if (value1 != null) {
            context.addDeclaration(o.getKey(), value1.getClass());
        }
    }
    MvelCompiler mvelCompiler = new MvelCompiler(context);
    ConstraintCompiler constraintCompiler = new ConstraintCompiler(context);
    if (isAStatement(expressionString)) {
        String expressionStringWithBraces = String.format("{%s}", expressionString);
        return mvelCompiler.compileStatement(expressionStringWithBraces);
    } else {
        return constraintCompiler.compileExpression(expressionString);
    }
}
Also used : MvelCompiler(org.drools.mvelcompiler.MvelCompiler) MvelCompilerContext(org.drools.mvelcompiler.context.MvelCompilerContext) TypeResolver(org.drools.core.addon.TypeResolver) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) ConstraintCompiler(org.drools.mvelcompiler.ConstraintCompiler) ClassTypeResolver(org.drools.core.addon.ClassTypeResolver) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with MvelCompiler

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

Map (java.util.Map)2 MvelCompiler (org.drools.mvelcompiler.MvelCompiler)2 MvelCompilerContext (org.drools.mvelcompiler.context.MvelCompilerContext)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 Parameter (com.github.javaparser.ast.body.Parameter)1 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ClassTypeResolver (org.drools.core.addon.ClassTypeResolver)1 TypeResolver (org.drools.core.addon.TypeResolver)1 MethodUtils.findMethod (org.drools.core.util.MethodUtils.findMethod)1 ConstraintCompiler (org.drools.mvelcompiler.ConstraintCompiler)1