Search in sources :

Example 1 with ParserConfiguration

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

Example 2 with ParserConfiguration

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;
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) ParserContext(org.mvel2.ParserContext) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 3 with ParserConfiguration

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());
    }
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) ExecutableStatement(org.mvel2.compiler.ExecutableStatement) CompiledExpression(org.mvel2.compiler.CompiledExpression) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 4 with ParserConfiguration

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;
}
Also used : Method(java.lang.reflect.Method) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 5 with 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;
}
Also used : InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) Cacheable(org.kie.internal.runtime.Cacheable) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) 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