Boolean Expression Evaluation in Java

前端 未结 10 1314
-上瘾入骨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:37

    I found the libraries listed here too complicated for my needs. I ended up using Fscript: http://fscript.sourceforge.net/

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

    You could try this library https://github.com/Shy-Ta/expression-evaluator-demo - the read me has a fair number of examples. The library uses java and groovy.

    In addition to supporting this use case, it also supports a lot of other excel like functions. Also, it is very simple to add new functions as demonstrated in the example.

          ExpressionsEvaluator evalExpr = ExpressionsFactory.create("(x > 4 || x < 8 && p > 6)");  
          Map<String, Object> variables = new HashMap<String, Object>();  
          variables.put("x", 100);  
          variables.put("p", 10);
          evalExpr.eval();
    
    0 讨论(0)
  • 2020-12-05 16:41

    try Janino http://docs.codehaus.org/display/JANINO/Home It is very simple to use eg (taken from http://docs.codehaus.org/display/JANINO/Basic):

    // Compile the expression once; relatively slow.
    ExpressionEvaluator ee = new ExpressionEvaluator(
        "c > d ? c : d",                     // expression
        int.class,                           // expressionType
        new String[] { "c", "d" },           // parameterNames
        new Class[] { int.class, int.class } // parameterTypes
    );
    
    // Evaluate it with varying parameter values; very fast.
    Integer res = (Integer) ee.evaluate(
        new Object[] {          // parameterValues
            new Integer(10),
            new Integer(11),
        }
    );
    System.out.println("res = " + res);
    
    0 讨论(0)
  • 2020-12-05 16:45

    Try http://code.google.com/p/xpressionengine/ for open source implementation

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

    Using jexl (http://commons.apache.org/jexl/), you can accomplish this like this

        JexlEngine jexl = new JexlEngine();
        jexl.setSilent(true);
        jexl.setLenient(true);
    
        Expression expression = jexl.createExpression("(a || b && (c && d))");
        JexlContext jexlContext = new MapContext();
    
        //b and c and d should pass
        jexlContext.set("b",true);
        jexlContext.set("c",true);
        jexlContext.set("d",true);
    
        assertTrue((Boolean)expression.evaluate(jexlContext));
    
        jexlContext = new MapContext();
    
        //b and c and NOT d should be false
        jexlContext.set("b",true);
        jexlContext.set("c",true);
    
        //note this works without setting d to false on the context
        //because null evaluates to false
    
        assertFalse((Boolean)expression.evaluate(jexlContext));
    
    0 讨论(0)
  • 2020-12-05 16:48

    JUEL provides an implementation of Java's Unified Expression Language without being explicitly tied to JSP. Here's its Quick Start guide, expression evaluation (#3 on that page) is the part you're interested in.

    Alternatively, Spring 3.0 provides its own (though somewhat similar) expression language. This option only makes sense if you're already using Spring, though - I wouldn't pull it in just for EL.

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