Looking for an expression evaluator

前端 未结 7 1142
猫巷女王i
猫巷女王i 2020-12-08 21:19

I\'m looking for an evaluator for simple condition expressions. Expressions should include variables (read only), strings, numbers and some basic operators.

E.g. exp

相关标签:
7条回答
  • 2020-12-08 22:07

    Have you looked at MVEL? They provide a getting started guide and performance analysis.

    Here's one of their simple examples:

    // The compiled expression is serializable and can be cached for re-use.
    CompiledExpression compiled = MVEL.compileExpression("x * y"); 
    
    Map vars = new HashMap();
    vars.put("x", new Integer(5));
    vars.put("y", new Integer(10));
    
    // Executes the compiled expression
    Integer result = (Integer) MVEL.executeExpression(compiled, vars); 
    assert result.intValue() == 50; 
    

    Also (answering my own question) MVEL seems to provide some support for bytecode generation.

    Other alternatives, culling from the above answers and my own:

    • Java Expression Parser (JEP) -- and note there is an old version available for free
    • Apache Commons JEXL
    • With regard to Rhino, here's a dude who did some arithmetic evaluation in that context (looks messy)
    0 讨论(0)
提交回复
热议问题