use of org.mvel2.ParserConfiguration in project drools by kiegroup.
the class MVELConstraintBuilder method getMvelFieldValue.
@Override
public FieldValue getMvelFieldValue(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 FieldFactory.getInstance().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 MVELCompilationUnit method getCompiledExpression.
public Serializable getCompiledExpression(ParserConfiguration conf, Object evaluationContext) {
final ParserContext parserContext = new ParserContext(conf, evaluationContext);
if (MVELDebugHandler.isDebugMode()) {
parserContext.setDebugSymbols(true);
}
parserContext.setStrictTypeEnforcement(strictMode);
parserContext.setStrongTyping(strictMode);
parserContext.setIndexAllocation(true);
parserContext.addIndexedInput(inputIdentifiers);
String identifier = null;
String type = null;
try {
for (int i = 0, length = inputIdentifiers.length; i < length; i++) {
identifier = inputIdentifiers[i];
type = inputTypes[i];
Class<?> cls = loadClass(conf.getClassLoader(), inputTypes[i]);
parserContext.addInput(inputIdentifiers[i], cls);
}
} catch (ClassNotFoundException e) {
throw new RuntimeException("Unable to resolve class '" + type + "' for identifier '" + identifier);
}
parserContext.setSourceFile(name);
String[] varNames = parserContext.getIndexedVarNames();
ExecutableStatement stmt = (ExecutableStatement) compile(expression, parserContext);
Set<String> localNames = parserContext.getVariables().keySet();
parserContext.addIndexedLocals(localNames);
String[] locals = localNames.toArray(new String[localNames.size()]);
String[] allVars = new String[varNames.length + locals.length];
System.arraycopy(varNames, 0, allVars, 0, varNames.length);
System.arraycopy(locals, 0, allVars, varNames.length, locals.length);
this.varModel = new SimpleVariableSpaceModel(allVars);
this.allVarsLength = allVars.length;
return stmt;
}
use of org.mvel2.ParserConfiguration in project drools by kiegroup.
the class MVELConstraint method createMvelConditionEvaluator.
protected ConditionEvaluator createMvelConditionEvaluator(ReteEvaluator reteEvaluator) {
if (compilationUnit != null) {
MVELDialectRuntimeData data = getMVELDialectRuntimeData(reteEvaluator);
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(reteEvaluator), 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 drools by kiegroup.
the class MVELConstraintTestUtil method getParserConfiguration.
@Override
protected ParserConfiguration getParserConfiguration(ReteEvaluator reteEvaluator) {
ParserConfiguration parserConfiguration = new ParserConfiguration();
parserConfiguration.addImport(Cheese.class);
return parserConfiguration;
}
Aggregations