Search code examples
mvel

How do I create MVEL evaluation context?


I want to sequentially parse MVEL as follows:

HashMap myData = new HashMap(){{ put("x", 1); }}

eval("y = 2", ...) // assign value to y
...
eval("x + y", myData, ...) // expect 3

Is this possible? If so, how?


Solution

  • Just share the variable resolver across expression evaluations:

    VariableResolverFactory vars = new MapVariableResolverFactory(new HashMap(){{ put("x", 1); }});

    MVEL.eval("y = 2", vars): MVEL.eval("x + y", vars);