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);
}
}
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);
}
Aggregations