use of org.mvel2.ParserContext in project drools by kiegroup.
the class MVELTest method test1.
@Test
public void test1() {
final ParserContext pc = new ParserContext();
pc.addInput("x", String.class);
pc.setStrongTyping(true);
final Object o = MVEL.compileExpression("x.startsWith('d')", pc);
final Map vars = new HashMap();
vars.put("x", "d");
MVEL.executeExpression(o, vars);
System.out.println(o);
}
use of org.mvel2.ParserContext in project drools by kiegroup.
the class MVELObjectClassFieldReader method doCompile.
public static void doCompile(MVELClassFieldReader target, MVELDialectRuntimeData runtimeData, Object evaluationContext) {
Class cls;
try {
cls = runtimeData.getRootClassLoader().loadClass(target.getClassName());
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Unable to compile as Class could not be found '" + target.getClassName() + "'");
}
ParserContext context = new ParserContext(runtimeData.getParserConfiguration(), evaluationContext);
context.addInput("this", cls);
context.setStrongTyping(target.isTypeSafe());
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;
ExecutableStatement mvelExpression = (ExecutableStatement) MVEL.compileExpression(target.getExpression(), context);
Class returnType = mvelExpression.getKnownEgressType();
target.setExecutableStatement(mvelExpression);
target.setFieldType(returnType);
target.setValueType(ValueType.determineValueType(returnType));
target.setEvaluationContext(evaluationContext);
}
use of org.mvel2.ParserContext in project drools by kiegroup.
the class MVELExpressionEvaluator method compileAndExecute.
protected Object compileAndExecute(String rawExpression, Map<String, Object> params) {
ParserContext ctx = new ParserContext(this.config);
for (Map.Entry<String, Object> entry : params.entrySet()) {
Class<?> type = entry.getValue() != null ? entry.getValue().getClass() : null;
ctx.addVariable(entry.getKey(), type);
}
String expression = cleanExpression(rawExpression);
Object compiledExpression = MVEL.compileExpression(expression, ctx);
return evaluator.executeExpression(compiledExpression, params);
}
use of org.mvel2.ParserContext in project drools by kiegroup.
the class MVELConstraintBuilder method analyzeExpression.
private static MVELAnalysisResult analyzeExpression(String expr, ParserConfiguration conf, BoundIdentifiers availableIdentifiers) {
if (expr.trim().length() <= 0) {
MVELAnalysisResult result = analyze((Set<String>) Collections.EMPTY_SET, availableIdentifiers);
result.setMvelVariables(new HashMap<String, Class<?>>());
result.setTypesafe(true);
return result;
}
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;
// first compilation is for verification only
final ParserContext parserContext1 = new ParserContext(conf);
if (availableIdentifiers.getThisClass() != null) {
parserContext1.addInput("this", availableIdentifiers.getThisClass());
}
if (availableIdentifiers.getOperators() != null) {
for (Map.Entry<String, EvaluatorWrapper> opEntry : availableIdentifiers.getOperators().entrySet()) {
parserContext1.addInput(opEntry.getKey(), opEntry.getValue().getClass());
}
}
parserContext1.setStrictTypeEnforcement(false);
parserContext1.setStrongTyping(false);
Class<?> returnType;
try {
returnType = MVEL.analyze(expr, parserContext1);
} catch (Exception e) {
return null;
}
Set<String> requiredInputs = new HashSet<>(parserContext1.getInputs().keySet());
Map<String, Class> variables = parserContext1.getVariables();
// MVEL includes direct fields of context object in non-strict mode. so we need to strip those
if (availableIdentifiers.getThisClass() != null) {
requiredInputs.removeIf(s -> PropertyTools.getFieldOrAccessor(availableIdentifiers.getThisClass(), s) != null);
}
// now, set the required input types and compile again
final ParserContext parserContext2 = new ParserContext(conf);
parserContext2.setStrictTypeEnforcement(true);
parserContext2.setStrongTyping(true);
for (String input : requiredInputs) {
if ("this".equals(input)) {
continue;
}
if (WM_ARGUMENT.equals(input)) {
parserContext2.addInput(input, InternalWorkingMemory.class);
continue;
}
Class<?> cls = availableIdentifiers.resolveType(input);
if (cls == null) {
if (input.equals("rule")) {
cls = Rule.class;
}
}
if (cls != null) {
parserContext2.addInput(input, cls);
}
}
if (availableIdentifiers.getThisClass() != null) {
parserContext2.addInput("this", availableIdentifiers.getThisClass());
}
try {
returnType = MVEL.analyze(expr, parserContext2);
} catch (Exception e) {
return null;
}
requiredInputs = new HashSet<>();
requiredInputs.addAll(parserContext2.getInputs().keySet());
requiredInputs.addAll(variables.keySet());
variables = parserContext2.getVariables();
MVELAnalysisResult result = analyze(requiredInputs, availableIdentifiers);
result.setReturnType(returnType);
result.setMvelVariables((Map<String, Class<?>>) (Map) variables);
result.setTypesafe(true);
return result;
}
use of org.mvel2.ParserContext in project drools by kiegroup.
the class MVELCoreComponentsBuilder method getParserContext.
static ParserContext getParserContext(DialectRuntimeData data, ClassLoader classLoader) {
ParserConfiguration conf = ((MVELDialectRuntimeData) data).getParserConfiguration();
conf.setClassLoader(classLoader);
return new ParserContext(conf);
}
Aggregations