Search in sources :

Example 96 with ParserContext

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

Example 97 with ParserContext

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

Example 98 with ParserContext

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

Example 99 with ParserContext

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;
}
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 100 with ParserContext

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

Aggregations

ParserContext (org.mvel2.ParserContext)372 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)190 HashMap (java.util.HashMap)140 Serializable (java.io.Serializable)100 ParserConfiguration (org.mvel2.ParserConfiguration)86 Map (java.util.Map)74 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)67 LinkedHashMap (java.util.LinkedHashMap)66 CompiledExpression (org.mvel2.compiler.CompiledExpression)63 CompileException (org.mvel2.CompileException)53 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)31 Foo (org.mvel2.tests.core.res.Foo)27 List (java.util.List)26 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)24 MapObject (org.mvel2.tests.core.res.MapObject)23 ArrayList (java.util.ArrayList)22 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)19 IOException (java.io.IOException)16 MVEL.evalToBoolean (org.mvel2.MVEL.evalToBoolean)16 Debugger (org.mvel2.debug.Debugger)16