Search in sources :

Example 1 with SimpleVariableSpaceModel

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

the class IndexedVariablesTests method testVariableInjection3.

public void testVariableInjection3() {
    String[] varNames = { "x", "y", "z" };
    Object[] values = { 10, 20, 30 };
    String expr = "def add(a,b) { a + b }; foo = -1; res = x + y + z;\n" + "if (x > 9) {\n" + "   res = z - y - x;\n" + "   int k = 5;\n" + "   foo = add(5,10);" + "}; \n" + "for (i = 0; i < 100000; i++) { foo++; }; foo;";
    ParserContext ctx = ParserContext.create();
    ctx.addIndexedInput(varNames);
    ctx.setIndexAllocation(true);
    SimpleVariableSpaceModel model = VariableSpaceCompiler.compile(expr, ctx);
    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(values)));
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) SimpleVariableSpaceModel(org.mvel2.util.SimpleVariableSpaceModel) ParserContext(org.mvel2.ParserContext)

Example 2 with SimpleVariableSpaceModel

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

the class VariableSpaceCompiler method compile.

public static SimpleVariableSpaceModel compile(String expr, ParserContext pCtx) {
    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 SimpleVariableSpaceModel(allVars);
}
Also used : ParserContext(org.mvel2.ParserContext)

Example 3 with SimpleVariableSpaceModel

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

the class IndexedVariablesTests method testVariableInjection2.

public void testVariableInjection2() {
    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 < 100000; i++) { foo++; }; foo;";
    ParserContext ctx = ParserContext.create();
    ctx.addIndexedInput(varNames);
    ctx.setIndexAllocation(true);
    SimpleVariableSpaceModel model = VariableSpaceCompiler.compile(expr, ctx);
    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(values)));
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) SimpleVariableSpaceModel(org.mvel2.util.SimpleVariableSpaceModel) ParserContext(org.mvel2.ParserContext)

Example 4 with SimpleVariableSpaceModel

use of org.mvel2.util.SimpleVariableSpaceModel in project drools by kiegroup.

the class MVELCompilationUnit method getCompiledExpression.

public Serializable getCompiledExpression(MVELDialectRuntimeData runtimeData, Object evaluationContext) {
    ParserConfiguration conf = runtimeData.getParserConfiguration();
    final ParserContext parserContext = new ParserContext(conf, evaluationContext);
    if (MVELDebugHandler.isDebugMode()) {
        parserContext.setDebugSymbols(true);
    }
    parserContext.setStrictTypeEnforcement(strictMode);
    parserContext.setStrongTyping(strictMode);
    parserContext.setIndexAllocation(true);
    if (INTERCEPTORS != null) {
        parserContext.setInterceptors(INTERCEPTORS);
    }
    parserContext.addIndexedInput(inputIdentifiers);
    String identifier = null;
    String type = null;
    try {
        for (int i = 0, length = inputIdentifiers.length; i < length; i++) {
            identifier = inputIdentifiers[i];
            type = inputTypes[i];
            Class<?> cls = loadClass(runtimeData.getPackageClassLoader(), inputTypes[i]);
            parserContext.addInput(inputIdentifiers[i], cls);
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Unable to resolve class '" + type + "' for identifier '" + identifier);
    }
    parserContext.setSourceFile(name);
    String[] varNames = parserContext.getIndexedVarNames();
    ExecutableStatement stmt = (ExecutableStatement) compile(expression, parserContext);
    Set<String> localNames = parserContext.getVariables().keySet();
    parserContext.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);
    this.varModel = new SimpleVariableSpaceModel(allVars);
    this.allVarsLength = allVars.length;
    return stmt;
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) SimpleVariableSpaceModel(org.mvel2.util.SimpleVariableSpaceModel) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

ParserContext (org.mvel2.ParserContext)4 SimpleVariableSpaceModel (org.mvel2.util.SimpleVariableSpaceModel)3 Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2 ParserConfiguration (org.mvel2.ParserConfiguration)1 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)1