Looking for an expression evaluator

前端 未结 7 1141
猫巷女王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 21:48

    Why don't you use Rhino? It's a JavaScript engine already present inside the JDK.

    It can evaluate whatever you like to write in JS.. take a look here

    0 讨论(0)
  • 2020-12-08 21:51

    What about SPEL (Spring Expression Lang); http://static.springsource.org/spring/docs/3.0.x/reference/expressions.html

    0 讨论(0)
  • 2020-12-08 21:52

    This simple recursive descent parser evaluates constants as named functions having no parameters.

    0 讨论(0)
  • 2020-12-08 21:53

    Here is 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());
    
    0 讨论(0)
  • 2020-12-08 22:00

    Sounds like JEXL might work well for you. Check out its syntax reference.

    0 讨论(0)
  • 2020-12-08 22:02

    A very simple and easy to use alternative with a lot of built in excel functions for string, date and number formatting.

    The library also allows easy addition of custom functions. A lot of examples available on the git page. A simple example using variables

      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));
    
    0 讨论(0)
提交回复
热议问题