Search in sources :

Example 21 with MapVariableResolverFactory

use of org.mule.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.

the class DebuggerTests method testBreakpoints.

public void testBreakpoints() {
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    ExpressionCompiler compiler = new ExpressionCompiler("a = 5;\nb = 5;\n\nif (a == b) {\n\nSystem.out.println('Good');\nreturn a + b;\n}\n", ctx);
    System.out.println("-------\n" + compiler.getExpression() + "\n-------\n");
    CompiledExpression compiled = compiler.compile();
    MVELRuntime.registerBreakpoint("test.mv", 7);
    final Set<Integer> breaked = new HashSet<Integer>();
    Debugger testDebugger = new Debugger() {

        public int onBreak(Frame frame) {
            System.out.println("Breakpoint [source:" + frame.getSourceName() + "; line:" + frame.getLineNumber() + "]");
            breaked.add(frame.getLineNumber());
            return 0;
        }
    };
    MVELRuntime.setThreadDebugger(testDebugger);
    assertEquals(10, MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
    assertTrue("did not break at line 7", breaked.contains(7));
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression) HashSet(java.util.HashSet)

Example 22 with MapVariableResolverFactory

use of org.mule.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.

the class FunctionsTest method testFunctionReuse.

public void testFunctionReuse() {
    VariableResolverFactory functionFactory = new MapVariableResolverFactory();
    MVEL.eval("def foo() { \"foo\"; }; def bar() { \"bar\" };", functionFactory);
    VariableResolverFactory myVarFactory = new MapVariableResolverFactory();
    myVarFactory.setNextFactory(functionFactory);
    Serializable s = MVEL.compileExpression("foo() + bar();");
    assertEquals("foobar", MVEL.executeExpression(s, myVarFactory));
}
Also used : Serializable(java.io.Serializable) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Example 23 with MapVariableResolverFactory

use of org.mule.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.

the class FunctionsTest method testMVEL225.

public void testMVEL225() {
    Serializable compileExpression = MVEL.compileExpression("def f() { int a=1;a++;return a; }; f();");
    MapVariableResolverFactory factory = new MapVariableResolverFactory(new HashMap<String, Object>());
    assertEquals(2, MVEL.executeExpression(compileExpression, factory));
}
Also used : Serializable(java.io.Serializable) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Example 24 with MapVariableResolverFactory

use of org.mule.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.

the class ForEachNode method eval.

public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
    Iterator[] iters = new Iterator[item.length];
    Object o;
    for (int i = 0; i < iters.length; i++) {
        if ((o = MVEL.eval(expression[i], ctx, factory)) instanceof Iterable) {
            iters[i] = ((Iterable) o).iterator();
        } else if (o instanceof Object[]) {
            iters[i] = new ArrayIterator((Object[]) o);
        } else {
            throw new TemplateRuntimeError("cannot iterate object type: " + o.getClass().getName());
        }
    }
    Map<String, Object> locals = new HashMap<String, Object>();
    MapVariableResolverFactory localFactory = new MapVariableResolverFactory(locals, factory);
    int iterate = iters.length;
    while (true) {
        for (int i = 0; i < iters.length; i++) {
            if (!iters[i].hasNext()) {
                iterate--;
                locals.put(item[i], "");
            } else {
                locals.put(item[i], iters[i].next());
            }
        }
        if (iterate != 0) {
            nestedNode.eval(runtime, appender, ctx, localFactory);
            if (sepExpr != null) {
                for (Iterator it : iters) {
                    if (it.hasNext()) {
                        appender.append(String.valueOf(MVEL.eval(sepExpr, ctx, factory)));
                        break;
                    }
                }
            }
        } else
            break;
    }
    return next != null ? next.eval(runtime, appender, ctx, factory) : null;
}
Also used : TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) HashMap(java.util.HashMap) Iterator(java.util.Iterator) ArrayIterator(org.mvel2.templates.util.ArrayIterator) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ArrayIterator(org.mvel2.templates.util.ArrayIterator)

Example 25 with MapVariableResolverFactory

use of org.mule.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mvel.

the class SharedFuncLib method eval.

public <T> T eval(String formula, Map<String, Object> context, Class<T> toType) {
    VariableResolverFactory myVariableResolverFactory = new MapVariableResolverFactory();
    myVariableResolverFactory.setNextFactory(functionFactory);
    return MVEL.eval(formula, context, myVariableResolverFactory, toType);
}
Also used : VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Aggregations

MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)62 HashMap (java.util.HashMap)31 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)20 ParserContext (org.mvel2.ParserContext)18 CompiledExpression (org.mvel2.compiler.CompiledExpression)16 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)16 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)13 Debugger (org.mvel2.debug.Debugger)12 Frame (org.mvel2.debug.Frame)12 HashSet (java.util.HashSet)10 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)7 TemplateRegistry (org.mvel2.templates.TemplateRegistry)7 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Serializable (java.io.Serializable)6 LinkedHashMap (java.util.LinkedHashMap)6 CompiledTemplate (org.mvel2.templates.CompiledTemplate)6 MapObject (org.mvel2.tests.core.res.MapObject)6 Interceptor (org.mvel2.integration.Interceptor)5 IndexedVariableResolverFactory (org.mvel2.integration.impl.IndexedVariableResolverFactory)5