Search in sources :

Example 66 with ParserConfiguration

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

Example 67 with ParserConfiguration

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;
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) SimpleVariableSpaceModel(org.mvel2.util.SimpleVariableSpaceModel) ParserContext(org.mvel2.ParserContext)

Example 68 with ParserConfiguration

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

Example 69 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 70 with 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;
}
Also used : 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