Search in sources :

Example 21 with ParserConfiguration

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;
}
Also used : MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) Declaration(org.drools.core.rule.Declaration) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) Map(java.util.Map) CompileException(org.mvel2.CompileException) RecognitionException(org.antlr.runtime.RecognitionException) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 22 with ParserConfiguration

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);
}
Also used : ParserContext(org.mvel2.ParserContext) Map(java.util.Map) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 23 with ParserConfiguration

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));
}
Also used : ParserConfiguration(org.mvel2.ParserConfiguration) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 24 with ParserConfiguration

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;
}
Also used : EvaluatorWrapper(org.drools.compiler.rule.builder.EvaluatorWrapper) IOException(java.io.IOException) MVELAnalysisResult(org.drools.mvel.builder.MVELAnalysisResult) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 25 with ParserConfiguration

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);
}
Also used : ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

ParserConfiguration (org.mvel2.ParserConfiguration)95 ParserContext (org.mvel2.ParserContext)88 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)48 HashMap (java.util.HashMap)43 LinkedHashMap (java.util.LinkedHashMap)30 Map (java.util.Map)27 Serializable (java.io.Serializable)12 CompileException (org.mvel2.CompileException)9 MVEL.evalToBoolean (org.mvel2.MVEL.evalToBoolean)9 IOException (java.io.IOException)7 MapObject (org.mvel2.tests.core.res.MapObject)7 ParserConfiguration (org.mule.mvel2.ParserConfiguration)6 Method (java.lang.reflect.Method)5 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)5 MVELExpressionLanguageContext (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext)5 RecognitionException (org.antlr.runtime.RecognitionException)4 PropertyAccessException (org.mvel2.PropertyAccessException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3