Search in sources :

Example 1 with SharedVariableSpaceModel

use of org.mvel2.util.SharedVariableSpaceModel in project mvel by mvel.

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 2 with SharedVariableSpaceModel

use of org.mvel2.util.SharedVariableSpaceModel in project mvel by mikebrock.

the class VariableSpaceCompiler method compileShared.

public static SharedVariableSpaceModel compileShared(String expr, ParserContext pCtx, Object[] vars) {
    String[] varNames = pCtx.getIndexedVarNames();
    ParserContext analysisContext = ParserContext.create();
    analysisContext.setIndexAllocation(true);
    MVEL.analysisCompile(expr, analysisContext);
    Set<String> localNames = analysisContext.getVariables().keySet();
    pCtx.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);
    return new SharedVariableSpaceModel(allVars, vars);
}
Also used : ParserContext(org.mvel2.ParserContext)

Example 3 with SharedVariableSpaceModel

use of org.mvel2.util.SharedVariableSpaceModel in project mvel by mvel.

the class IntegrationTests method testIndexedVariableFactory.

public void testIndexedVariableFactory() {
    ParserContext ctx = ParserContext.create();
    String[] vars = { "a", "b" };
    Object[] vals = { "foo", "bar" };
    ctx.setIndexAllocation(true);
    ctx.addIndexedInput(vars);
    String expr = "def myfunc(z) { a + b + z }; myfunc('poop');";
    SharedVariableSpaceModel model = VariableSpaceCompiler.compileShared(expr, ctx, vals);
    Serializable s = MVEL.compileExpression(expr, ctx);
    // VariableResolverFactory locals = new CachingMapVariableResolverFactory(new HashMap<String, Object>());
    // VariableResolverFactory injected = new IndexedVariableResolverFactory(vars, vals, locals);
    assertEquals("foobarpoop", MVEL.executeExpression(s, model.createFactory()));
}
Also used : Serializable(java.io.Serializable) SharedVariableSpaceModel(org.mvel2.util.SharedVariableSpaceModel) ParserContext(org.mvel2.ParserContext)

Example 4 with SharedVariableSpaceModel

use of org.mvel2.util.SharedVariableSpaceModel in project mvel by mvel.

the class VariableSpaceCompiler method compileShared.

public static SharedVariableSpaceModel compileShared(String expr, ParserContext pCtx, Object[] vars) {
    String[] varNames = pCtx.getIndexedVarNames();
    ParserContext analysisContext = ParserContext.create();
    analysisContext.setIndexAllocation(true);
    MVEL.analysisCompile(expr, analysisContext);
    Set<String> localNames = analysisContext.getVariables().keySet();
    pCtx.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);
    return new SharedVariableSpaceModel(allVars, vars);
}
Also used : ParserContext(org.mvel2.ParserContext)

Example 5 with SharedVariableSpaceModel

use of org.mvel2.util.SharedVariableSpaceModel in project mvel by mikebrock.

the class IntegrationTests method testIndexedVariableFactory.

public void testIndexedVariableFactory() {
    ParserContext ctx = ParserContext.create();
    String[] vars = { "a", "b" };
    Object[] vals = { "foo", "bar" };
    ctx.setIndexAllocation(true);
    ctx.addIndexedInput(vars);
    String expr = "def myfunc(z) { a + b + z }; myfunc('poop');";
    SharedVariableSpaceModel model = VariableSpaceCompiler.compileShared(expr, ctx, vals);
    Serializable s = MVEL.compileExpression(expr, ctx);
    // VariableResolverFactory locals = new CachingMapVariableResolverFactory(new HashMap<String, Object>());
    // VariableResolverFactory injected = new IndexedVariableResolverFactory(vars, vals, locals);
    assertEquals("foobarpoop", MVEL.executeExpression(s, model.createFactory()));
}
Also used : Serializable(java.io.Serializable) SharedVariableSpaceModel(org.mvel2.util.SharedVariableSpaceModel) ParserContext(org.mvel2.ParserContext)

Aggregations

ParserContext (org.mvel2.ParserContext)6 Serializable (java.io.Serializable)4 SharedVariableSpaceModel (org.mvel2.util.SharedVariableSpaceModel)4 HashMap (java.util.HashMap)2