use of org.mvel2.ParserConfiguration in project drools by kiegroup.
the class MVELExprAnalyzer method getExpressionType.
public static Class<?> getExpressionType(PackageBuildContext context, Map<String, Class<?>> declCls, RuleConditionElement source, String expression) {
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
ParserConfiguration conf = data.getParserConfiguration();
conf.setClassLoader(context.getKnowledgeBuilder().getRootClassLoader());
ParserContext pctx = new ParserContext(conf);
pctx.setStrongTyping(true);
pctx.setStrictTypeEnforcement(true);
for (Map.Entry<String, Class<?>> entry : declCls.entrySet()) {
pctx.addInput(entry.getKey(), entry.getValue());
}
for (Declaration decl : source.getOuterDeclarations().values()) {
pctx.addInput(decl.getBindingName(), decl.getDeclarationClass());
}
try {
return MVEL.analyze(expression, pctx);
} catch (Exception e) {
log.warn("Unable to parse expression: " + expression, e);
}
return null;
}
use of org.mvel2.ParserConfiguration in project drools by kiegroup.
the class MVELBeanCreator method createBean.
@Override
public <T> T createBean(ClassLoader cl, String type, QualifierModel qualifier) throws Exception {
if (qualifier != null) {
throw new IllegalArgumentException("Cannot use a qualifier without a CDI container");
}
ParserConfiguration config = new ParserConfiguration();
config.setClassLoader(cl);
ParserContext ctx = new ParserContext(config);
if (parameters != null) {
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
ctx.addVariable(entry.getKey(), entry.getValue().getClass());
}
}
Object compiledExpression = MVEL.compileExpression(type, ctx);
return (T) CoreComponentsBuilder.get().getMVELExecutor().executeExpression(compiledExpression, parameters);
}
use of org.mvel2.ParserConfiguration in project drools by kiegroup.
the class MVELConstraintBuilder method analyzeExpression.
@Override
public AnalysisResult analyzeExpression(Class<?> thisClass, String expr) {
ParserConfiguration conf = new ParserConfiguration();
conf.setClassLoader(thisClass.getClassLoader());
return analyzeExpression(expr, conf, new BoundIdentifiers(thisClass));
}
use of org.mvel2.ParserConfiguration 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.ParserConfiguration 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