Search in sources :

Example 21 with MapVariableResolverFactory

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

the class CoreConfidenceTests method testMVEL190.

public void testMVEL190() {
    ParserContext context = new ParserContext();
    context.addImport(Ship.class);
    context.addImport(MapObject.class);
    context.addInput("obj", MapObject.class);
    Object compiled = MVEL.compileExpression("((Ship) obj).getName()", context);
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("obj", new Ship());
    VariableResolverFactory varsResolver = new MapVariableResolverFactory(vars);
    System.out.println(executeExpression(compiled, varsResolver));
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Example 22 with MapVariableResolverFactory

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

the class CoreConfidenceTests method testThisReferenceMapVirtualObjects.

// interpreted
public void testThisReferenceMapVirtualObjects() {
    Map<String, String> map = new HashMap<String, String>();
    map.put("foo", "bar");
    VariableResolverFactory factory = new MapVariableResolverFactory(new HashMap<String, Object>());
    factory.createVariable("this", map);
    assertEquals(true, eval("this.foo == 'bar'", map, factory));
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Example 23 with MapVariableResolverFactory

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

the class CoreConfidenceTests method testThisReferenceMapVirtualObjects2.

// compiled - asm
public void testThisReferenceMapVirtualObjects2() {
    // Create our root Map object
    Map<String, String> map = new HashMap<String, String>();
    map.put("foo", "bar");
    VariableResolverFactory factory = new MapVariableResolverFactory(new HashMap<String, Object>());
    factory.createVariable("this", map);
    if (!Boolean.getBoolean("mvel2.disable.jit"))
        OptimizerFactory.setDefaultOptimizer("ASM");
    // Run test
    assertEquals(true, executeExpression(compileExpression("this.foo == 'bar'"), map, factory));
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Example 24 with MapVariableResolverFactory

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

the class DebuggerTests method testBreakpoints5.

public void testBreakpoints5() {
    OptimizerFactory.setDefaultOptimizer("ASM");
    String expression = "System.out.println('foo');\r\n" + "a = new Foo244();\r\n" + "a.name = 'bar';\r\n" + "foo.happy();\r\n" + "System.out.println( 'name:' + a.name );               \r\n" + "System.out.println( 'name:' + a.name );         \r\n" + "System.out.println( 'name:' + a.name );     \r\n" + "return a.name;";
    Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
    Map<String, Macro> macros = new HashMap<String, Macro>();
    expression = parseMacros(expression, macros);
    ExpressionCompiler compiler = new ExpressionCompiler(expression);
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test2.mv");
    ctx.setDebugSymbols(true);
    ctx.addImport("Foo244", Foo.class);
    ctx.setInterceptors(interceptors);
    CompiledExpression compiled = compiler.compile(ctx);
    System.out.println("\nExpression:------------");
    System.out.println(expression);
    System.out.println("------------");
    System.out.println(DebugTools.decompile(compiled));
    MVELRuntime.registerBreakpoint("test2.mv", 1);
    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 Debugger.STEP_OVER;
        }
    };
    MVELRuntime.setThreadDebugger(testDebugger);
    System.out.println("\n==RUN==\n");
    assertEquals("bar", MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
    assertTrue("did not break at line 1", breaked.contains(1));
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) HashMap(java.util.HashMap) Macro(org.mvel2.Macro) CompiledExpression(org.mvel2.compiler.CompiledExpression) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) Interceptor(org.mvel2.integration.Interceptor) HashSet(java.util.HashSet)

Example 25 with MapVariableResolverFactory

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

the class DebuggerTests method testBreakpoints.

public void testBreakpoints() {
    ExpressionCompiler compiler = new ExpressionCompiler("a = 5;\nb = 5;\n\nif (a == b) {\n\nSystem.out.println('Good');\nreturn a + b;\n}\n");
    System.out.println("-------\n" + compiler.getExpression() + "\n-------\n");
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    CompiledExpression compiled = compiler.compile(ctx);
    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)

Aggregations

MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)35 HashMap (java.util.HashMap)19 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)10 ParserContext (org.mvel2.ParserContext)8 CompiledExpression (org.mvel2.compiler.CompiledExpression)8 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)8 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Debugger (org.mvel2.debug.Debugger)6 Frame (org.mvel2.debug.Frame)6 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)6 HashSet (java.util.HashSet)5 CompiledTemplate (org.mvel2.templates.CompiledTemplate)5 TemplateRegistry (org.mvel2.templates.TemplateRegistry)5 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)5 Serializable (java.io.Serializable)3 List (java.util.List)3 Map (java.util.Map)3 ClassObjectType (org.drools.core.base.ClassObjectType)3 Declaration (org.drools.core.rule.Declaration)3