Java 寻找表达式评估器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4010674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Looking for an expression evaluator
提问by Stroboskop
I'm looking for an evaluator for simple condition expressions. Expressions should include variables (read only), strings, numbers and some basic operators.
我正在寻找一个简单条件表达式的评估器。表达式应包括变量(只读)、字符串、数字和一些基本运算符。
E.g. expressions something like this:
例如像这样的表达式:
${a} == "Peter" && ( ${b} == null || ${c} > 10 )
So far i implemented a rather "magical" parser that returns an AST that i can evaluate, but i can't believe that i'm the first one to solve that problem.
到目前为止,我实现了一个相当“神奇”的解析器,它返回一个我可以评估的 AST,但我不敢相信我是第一个解决这个问题的人。
What existing code could i use instead?
我可以改用什么现有代码?
采纳答案by NightWolf
What about SPEL (Spring Expression Lang); http://static.springsource.org/spring/docs/3.0.x/reference/expressions.html
SPEL(Spring Expression Lang)呢?http://static.springsource.org/spring/docs/3.0.x/reference/expressions.html
回答by Hyman
回答by trashgod
This simple recursive descent parserevaluates constants as named functions having no parameters.
这个简单的递归下降解析器将常量评估为没有参数的命名函数。
回答by Mike Daniels
Sounds like JEXLmight work well for you. Check out its syntax reference.
回答by andersoj
Have you looked at MVEL? They provide a getting started guideand 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.
另外(回答我自己的问题)MVEL 似乎为字节码生成提供了一些支持。
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 dudewho did some arithmetic evaluation in that context (looks messy)
回答by Scorpion
A very simple and easy to use alternativewith a lot of built in excel functions for string, date and number formatting.
一个非常简单且易于使用的替代方案,具有许多用于字符串、日期和数字格式的内置 excel 函数。
The library also allows easy addition of custom functions. A lot of examples available on the git page. A simple example using variables
该库还允许轻松添加自定义功能。git 页面上提供了很多示例。使用变量的简单示例
ExpressionsEvaluator evalExpr = ExpressionsFactory.create("LEFT(City, 3)");
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("City", "New York");
assertEquals("New", evalExpr.eval(variables));
回答by Skyost
Hereis a little library I've worked on that supports expression evaluation (including variables, strings, boolean, etc...).
这是我研究过的一个支持表达式求值(包括变量、字符串、布尔值等)的小库。
A little example :
一个小例子:
String expression = "EXP(var)";
ExpressionEvaluator evaluator = new ExpressionEvaluator();
evaluator.putVariable(new Variable("var", VariableType.NUMBER, new BigDecimal(20)));
System.out.println("Value of exp(var) : " + evaluator.evaluate(expression).getValue());