Search in sources :

Example 16 with MapVariableResolverFactory

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

the class AbstractTest method _test.

protected static Object _test(String ex) {
    ExpressionCompiler compiler = new ExpressionCompiler(ex);
    StringAppender failErrors = new StringAppender();
    CompiledExpression compiled = compiler.compile();
    Object first = null, second = null, third = null, fourth = null, fifth = null, sixth = null, seventh = null, eighth = null;
    System.out.println(DebugTools.decompile((Serializable) compiled));
    if (!Boolean.getBoolean("mvel2.disable.jit")) {
        setDefaultOptimizer("ASM");
        try {
            first = executeExpression(compiled, new Base(), createTestMap());
        } catch (Exception e) {
            failErrors.append("\nFIRST TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
            CharArrayWriter writer = new CharArrayWriter();
            e.printStackTrace(new PrintWriter(writer));
            failErrors.append(writer.toCharArray());
        }
        try {
            second = executeExpression(compiled, new Base(), createTestMap());
        } catch (Exception e) {
            failErrors.append("\nSECOND TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
            CharArrayWriter writer = new CharArrayWriter();
            e.printStackTrace(new PrintWriter(writer));
            failErrors.append(writer.toCharArray());
        }
    }
    try {
        third = MVEL.eval(ex, new Base(), createTestMap());
    } catch (Exception e) {
        failErrors.append("\nTHIRD TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (first != null && !first.getClass().isArray()) {
        if (!first.equals(second)) {
            System.out.println(failErrors.toString());
            throw new AssertionError("Different result from test 1 and 2 (Compiled Re-Run / JIT) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
        }
        if (!first.equals(third)) {
            if (failErrors != null)
                System.out.println(failErrors.toString());
            throw new AssertionError("Different result from test 1 and 3 (Compiled to Interpreted) [first: " + valueOf(first) + " (" + (first != null ? first.getClass().getName() : null) + "); third: " + valueOf(third) + " (" + (third != null ? third.getClass().getName() : "null") + ")]");
        }
    }
    setDefaultOptimizer("reflective");
    Serializable compiled2 = compileExpression(ex);
    try {
        fourth = executeExpression(compiled2, new Base(), createTestMap());
    } catch (Exception e) {
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nFOURTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    try {
        fifth = executeExpression(compiled2, new Base(), createTestMap());
    } catch (Exception e) {
        e.printStackTrace();
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nFIFTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (fourth != null && !fourth.getClass().isArray()) {
        if (!fourth.equals(fifth)) {
            throw new AssertionError("Different result from test 4 and 5 (Compiled Re-Run X2) [fourth: " + valueOf(fourth) + "; fifth: " + valueOf(fifth) + "]");
        }
    }
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("unittest");
    ctx.setDebugSymbols(true);
    ExpressionCompiler debuggingCompiler = new ExpressionCompiler(ex);
    // debuggingCompiler.setDebugSymbols(true);
    CompiledExpression compiledD = debuggingCompiler.compile(ctx);
    try {
        sixth = executeExpression(compiledD, new Base(), createTestMap());
    } catch (Exception e) {
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nSIXTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (sixth != null && !sixth.getClass().isArray()) {
        if (!fifth.equals(sixth)) {
            System.out.println("Payload 1 -- No Symbols: ");
            System.out.println(decompile(compiled));
            System.out.println();
            System.out.println("Payload 2 -- With Symbols: ");
            System.out.println(decompile(compiledD));
            System.out.println();
            throw new AssertionError("Different result from test 5 and 6 (Compiled to Compiled+DebuggingSymbols) [first: " + valueOf(fifth) + "; second: " + valueOf(sixth) + "]");
        }
    }
    try {
        seventh = executeExpression(compiledD, new Base(), createTestMap());
    } catch (Exception e) {
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nSEVENTH TEST: { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (seventh != null && !seventh.getClass().isArray()) {
        if (!seventh.equals(sixth)) {
            throw new AssertionError("Different result from test 4 and 5 (Compiled Re-Run / Reflective) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
        }
    }
    try {
        Serializable xx = serializationTest(compiledD);
        AbstractParser.resetParserContext();
        eighth = executeExpression(xx, new Base(), new MapVariableResolverFactory(createTestMap()));
    } catch (Exception e) {
        if (failErrors == null)
            failErrors = new StringAppender();
        failErrors.append("\nEIGHTH TEST (Serializability): { " + ex + " }: EXCEPTION REPORT: \n\n");
        CharArrayWriter writer = new CharArrayWriter();
        e.printStackTrace(new PrintWriter(writer));
        failErrors.append(writer.toCharArray());
    }
    if (eighth != null && !eighth.getClass().isArray()) {
        if (!eighth.equals(seventh)) {
            throw new AssertionError("Different result from test 7 and 8 (Compiled Re-Run / Reflective) [first: " + valueOf(first) + "; second: " + valueOf(second) + "]");
        }
    }
    if (failErrors.length() > 0) {
        System.out.println(decompile(compiledD));
        throw new AssertionError("Detailed Failure Report:\n" + failErrors.toString());
    }
    return fourth;
}
Also used : StringAppender(org.mvel2.util.StringAppender) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression) Base(org.mvel2.tests.core.res.Base)

Example 17 with MapVariableResolverFactory

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

the class CoreConfidenceTests method testCheeseConstructor.

// public void testClassImportViaFactory() {
// MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
// ClassImportResolverFactory classes = new ClassImportResolverFactory();
// classes.addClass(HashMap.class);
// 
// ResolverTools.appendFactory(mvf, classes);
// 
// assertTrue(executeExpression(compileExpression("HashMap map = new HashMap()",
// classes.getImportedClasses()),
// mvf) instanceof HashMap);
// }
// 
// public void testSataticClassImportViaFactory() {
// MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
// ClassImportResolverFactory classes = new ClassImportResolverFactory();
// classes.addClass(Person.class);
// 
// ResolverTools.appendFactory(mvf,
// classes);
// 
// assertEquals("tom",
// executeExpression(compileExpression("p = new Person('tom'); return p.name;",
// classes.getImportedClasses()),
// mvf));
// }
public void testCheeseConstructor() {
    MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
    ClassImportResolverFactory classes = new ClassImportResolverFactory();
    classes.addClass(Cheese.class);
    ResolverTools.appendFactory(mvf, classes);
    assertTrue(executeExpression(compileExpression("cheese = new Cheese(\"cheddar\", 15);", classes.getImportedClasses()), mvf) instanceof Cheese);
}
Also used : MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ClassImportResolverFactory(org.mvel2.integration.impl.ClassImportResolverFactory)

Example 18 with MapVariableResolverFactory

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

the class IndexedVariablesTests method testVariableInjection1.

public void testVariableInjection1() {
    String[] varNames = { "x", "y", "z" };
    Object[] values = { 10, 20, 30 };
    String expr = "foo = -1; res = x + y + z;\n" + "if (x > 9) {\n" + "   res = z - y - x;\n" + "   int k = 5;\n" + "   foo = k;" + "}; \n" + "for (i = 0; i < 5000; i++) { foo++; }; foo;";
    ParserContext ctx = ParserContext.create();
    ctx.addIndexedInput(varNames);
    ctx.setIndexAllocation(true);
    SharedVariableSpaceModel model = VariableSpaceCompiler.compileShared(expr, ctx, values);
    Serializable indexCompile = MVEL.compileExpression(expr, ctx);
    Serializable dynamicCompile = MVEL.compileExpression(expr, ParserContext.create());
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("x", 10);
    map.put("y", 20);
    map.put("z", 30);
    assertEquals(MVEL.executeExpression(dynamicCompile, map), MVEL.executeExpression(indexCompile, model.createFactory()));
// 
// for (int x = 0; x < 10; x++) {
// long tm = System.currentTimeMillis();
// for (int i = 0; i < 10000; i++) {
// MVEL.executeExpression(indexCompile, model.createFactory());
// }
// tm = System.currentTimeMillis() - tm;
// System.out.println("(StaticInjection (ms): " + tm + ")");
// 
// tm = System.currentTimeMillis();
// Map<String, Object> map = new HashMap<String, Object>();
// map.put("x", 10);
// map.put("y", 20);
// map.put("z", 30);
// 
// MapVariableResolverFactory factory = new MapVariableResolverFactory(map);
// for (int i = 0; i < 10000; i++) {
// MVEL.executeExpression(dynamicCompile, factory);
// }
// tm = System.currentTimeMillis() - tm;
// System.out.println("(MapInjection    (ms): " + tm + ")");
// }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) SharedVariableSpaceModel(org.mvel2.util.SharedVariableSpaceModel) ParserContext(org.mvel2.ParserContext)

Example 19 with MapVariableResolverFactory

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

the class ForNode method getReducedValueAccelerated.

public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
    VariableResolverFactory ctxFactory = indexAlloc ? factory : new MapVariableResolverFactory(new HashMap<String, Object>(1), factory);
    Object v;
    for (initializer.getValue(ctx, thisValue, ctxFactory); (Boolean) condition.getValue(ctx, thisValue, ctxFactory); after.getValue(ctx, thisValue, ctxFactory)) {
        v = compiledBlock.getValue(ctx, thisValue, ctxFactory);
        if (ctxFactory.tiltFlag())
            return v;
    }
    return null;
}
Also used : VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Example 20 with MapVariableResolverFactory

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

the class CompiledForEachNode 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.executeExpression(ce[i], ctx, factory)) instanceof Iterable) {
            iters[i] = ((Iterable) o).iterator();
        } else if (o instanceof Object[]) {
            iters[i] = new ArrayIterator((Object[]) o);
        } else if (o instanceof Integer) {
            iters[i] = new CountIterator((Integer) 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.executeExpression(cSepExpr, ctx, factory)));
                        break;
                    }
                }
            }
        } else
            break;
    }
    return next != null ? next.eval(runtime, appender, ctx, factory) : null;
}
Also used : CountIterator(org.mvel2.templates.util.CountIterator) HashMap(java.util.HashMap) TemplateRuntimeError(org.mvel2.templates.TemplateRuntimeError) Iterator(java.util.Iterator) ArrayIterator(org.mvel2.templates.util.ArrayIterator) CountIterator(org.mvel2.templates.util.CountIterator) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ArrayIterator(org.mvel2.templates.util.ArrayIterator)

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