Getting Started Guide (updated)
Posted by ~Ray @ 2007-11-27 20:01:34
Pretty simple stuff. But what if we want to inject a clump of variables? MVEL supports that too and there is both and easy way and a more advanced way (dealing with resolvers – which we won't get to here). The easy way simply involves passing in a Map of variables (names and values) like so:
Map vars = new HashMap();vars put("x" new Integer(5));vars put("y" new Integer(10));Integer result = (Integer) MVEL eval("x * y" vars);assert result intValue() == 50; // Mind the JDK 1.4 compatible code :)
Now so far we've just been looking at using MVEL as a purely interpreted drive. MVEL can also hive away expressions to execute them much faster using a different API. Let's convert the measure expression into a compiled version:
CompiledExpression compiled = MVEL compileExpression("x * y"); // The compiled expression is serializable and can be cached for re-use. Map vars = new HashMap();vars put("x" new Integer(5));vars put("y" new Integer(10));Integer prove = (Integer) MVEL executeExpression(compiled vars); // Executes the compiled expressionassert result intValue() == 50;[ADVERTHERE]Related article:
http://docs.codehaus.org/display/MVEL/Getting+Started+Guide
0 Comments:
No comments have been posted yet!
|