Boolean Expression Evaluation in Java

前端 未结 10 1315
-上瘾入骨i
-上瘾入骨i 2020-12-05 16:26

I\'m looking for a relatively simpler (when compared with writing a parser) way to evaluate boolean expressions in Java, and I do not want to use the JEP library.

I

相关标签:
10条回答
  • 2020-12-05 16:53

    Here is the latest resources for expression evaluation framework

    The information page is at http://expressionoasis.vedantatree.com/

    0 讨论(0)
  • 2020-12-05 16:58

    There is a API available at http://lts.online.fr/dev/java/math.evaluator/

    Example:

    MathEvaluator m = new MathEvaluator("-5-6/(-2) + sqr(15+x)");
    m.addVariable("x", 15.1d);
    System.out.println( m.getValue() );
    
    0 讨论(0)
  • 2020-12-05 16:59

    You could use the scripting engine in Java6 and the choose any of the popular scripting languages like Scala, Ruby, Python, Groovy, and Javascript. Than all you have to do is make sure the expression you want to evaluate is in the right language. Groovy is probably the easiest and will integrate best.

    I have used this method successfully for a feature offering capabilities much like a formula / calculated column in a popular spreadsheet application.

    0 讨论(0)
  • 2020-12-05 17:02

    Use Apache Commons Jexl; which is exactly designed for such requirement.

    http://commons.apache.org/jexl/

    0 讨论(0)
提交回复
热议问题