use of org.mvel2.ParserConfiguration in project drools by kiegroup.
the class QueryElementBuilder method getParserContext.
private ParserContext getParserContext(RuleBuildContext context) {
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
ParserConfiguration conf = data.getParserConfiguration();
conf.setClassLoader(context.getKnowledgeBuilder().getRootClassLoader());
return new ParserContext(conf);
}
use of org.mvel2.ParserConfiguration in project drools by kiegroup.
the class PatternBuilder method getFieldValue.
private FieldValue getFieldValue(RuleBuildContext context, ValueType vtype, String value) {
try {
MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
MVEL.COMPILER_OPT_ALLOW_RESOLVE_INNERCLASSES_WITH_DOTNOTATION = true;
MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS = true;
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
ParserConfiguration pconf = data.getParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
Object o = MVELSafeHelper.getEvaluator().executeExpression(MVEL.compileExpression(value, pctx));
if (o != null && vtype == null) {
// was a compilation problem else where, so guess valuetype so we can continue
vtype = ValueType.determineValueType(o.getClass());
}
return context.getCompilerFactory().getFieldFactory().getFieldValue(o, vtype);
} catch (final Exception e) {
// we will fallback to regular preducates, so don't raise an error
}
return null;
}
use of org.mvel2.ParserConfiguration in project drools by kiegroup.
the class MvelConstraint method createMvelConditionEvaluator.
protected ConditionEvaluator createMvelConditionEvaluator(InternalWorkingMemory workingMemory) {
if (compilationUnit != null) {
MVELDialectRuntimeData data = getMVELDialectRuntimeData(workingMemory);
ExecutableStatement statement = (ExecutableStatement) compilationUnit.getCompiledExpression(data, evaluationContext);
ParserConfiguration configuration = statement instanceof CompiledExpression ? ((CompiledExpression) statement).getParserConfiguration() : data.getParserConfiguration();
return new MvelConditionEvaluator(compilationUnit, configuration, statement, declarations, operators, getAccessedClass());
} else {
return new MvelConditionEvaluator(getParserConfiguration(workingMemory), expression, declarations, operators, getAccessedClass());
}
}
use of org.mvel2.ParserConfiguration in project drools by kiegroup.
the class MVELDialectRuntimeData method getParserConfiguration.
public ParserConfiguration getParserConfiguration() {
if (parserConfiguration == null) {
ClassLoader packageClassLoader = getPackageClassLoader();
String key = null;
Object value = null;
try {
// First replace fields and method tokens with actual instances
for (Entry<String, Object> entry : this.imports.entrySet()) {
key = entry.getKey();
value = entry.getValue();
if (entry.getValue() instanceof String) {
String str = (String) value;
// @TODO MVEL doesn't yet support importing of fields
if (str.startsWith("m:")) {
Class cls = packageClassLoader.loadClass(str.substring(2));
for (Method method : cls.getDeclaredMethods()) {
if (method.getName().equals(key)) {
entry.setValue(method);
break;
}
}
} else {
Class cls = packageClassLoader.loadClass(str);
entry.setValue(cls);
}
}
}
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Unable to resolve method of field: " + key + " - " + value, e);
}
final ParserConfiguration conf = new ParserConfiguration();
conf.setImports(this.imports);
conf.setPackageImports(this.packageImports);
conf.setClassLoader(packageClassLoader);
this.parserConfiguration = conf;
}
return this.parserConfiguration;
}
use of org.mvel2.ParserConfiguration in project jbpm by kiegroup.
the class MVELObjectModelResolver method getInstance.
@Override
public Object getInstance(ObjectModel model, ClassLoader cl, Map<String, Object> contextParams) {
Object instance = null;
InternalRuntimeManager manager = null;
if (contextParams.containsKey("runtimeManager")) {
manager = (InternalRuntimeManager) contextParams.get("runtimeManager");
instance = manager.getCacheManager().get(model.getIdentifier());
if (instance != null) {
return instance;
}
}
ParserConfiguration config = new ParserConfiguration();
config.setClassLoader(cl);
ParserContext ctx = new ParserContext(config);
if (contextParams != null) {
for (Map.Entry<String, Object> entry : contextParams.entrySet()) {
ctx.addVariable(entry.getKey(), entry.getValue().getClass());
}
}
Object compiledExpression = MVEL.compileExpression(model.getIdentifier(), ctx);
instance = MVELSafeHelper.getEvaluator().executeExpression(compiledExpression, contextParams);
if (manager != null && instance instanceof Cacheable) {
manager.getCacheManager().add(model.getIdentifier(), instance);
}
return instance;
}
Aggregations