JAVA - Expression parsing & evaluating library

后端 未结 4 2120
再見小時候
再見小時候 2021-02-09 01:04

I\'m looking for a JAVA library to parse & evaluate expression. I searched and tried some libraries like Apache\'s JEXL and Jeval, but they are not exactly what I need.

4条回答
  •  [愿得一人]
    2021-02-09 01:45

    You can try mXparser - it supports significant part of your requirements:

    1. It is based on double, so int is supported, additionally boolean is supported as true = 1 and false = 0. Unfortunately strings are not supported.

    Boolean example:

    import org.mariuszgromada.math.mxparser.*;
    ...
    ...
    Constant T = new Constant("T = 1");
    Constant F = new Constant("F = 0");
    Expression e = new Expression("T && (F || (F && T))", T, F);
    System.out.println(e.getExpressionString() + " = " + e.calculate());
    

    Result:

    T && (F || (F && T)) = 0.0
    
    1. mXparser has broad support for operators, functions, etc.. Check mXparser math collection. What is nice you can use help functionality inside the library.

    Example:

    import org.mariuszgromada.math.mxparser.*;
    ...
    ...
    mXparser.consolePrintHelp("operator");
    

    Result:

    Help content: 
    
        2. +                                 addition
        3. -                                 subtraction
        4. *                                 multiplication
        5. /                                 division
        6. ^                                 exponentiation
        7. !                                 factorial
        8. #                                 modulo function
        9. &                         logical conjunction (AND)
       10. &&                        logical conjunction (AND)
       11. /\                        logical conjunction (AND)
       12. ~&                        NAND - Sheffer stroke
       13. ~&&                       NAND - Sheffer stroke
       14. ~/\                       NAND - Sheffer stroke
       15. |                         logical disjunction (OR)
       16. ||                        logical disjunction (OR)
       17. \/                        logical disjunction (OR)
       18. ~|                        logical NOR
       19. ~||                       logical NOR
       20. ~\/                       logical NOR
       21. (+)                       exclusive or (XOR)
       22. -->                       implication (IMP)
       23. <--                       converse implication (CIMP)
       24. -/>                       material nonimplication (NIMP)
       25.       converse nonimplication (CNIMP)
       26. <->                       logical biconditional (EQV)
       27. ~                         negation
       28. ¬                         negation
      162. add                      (2.4) Summation operator add(a1,a2,a3,...,an)
      168. sum                      summation operator (SIGMA) sum(i, from, to, f(i,...))
      169. prod                     product operator (PI) prod(i, from, to, f(i,...))
      170. int                      definite integral operator ( int(f(x,...), x, a, b) )
      171. der                      derivative operator ( der(f(x,...), x) ) 
      172. der-                     left derivative operator ( der-(f(x,...), x) ) 
      173. der+                     right derivative operator ( der+(f(x,...), x) ) 
      174. dern                     n-th derivative operator ( dern(f(x,...), x) ) 
      175. diff                     forward difference operator
      176. difb                     backward difference operator
      177. avg                      (2.4) Average operator avg(i, from, to, f(i,...))
      178. vari                     (2.4) Bias-corrected sample variance operator vari(i, from, to, f(i,...))
      179. stdi                     (2.4) Bias-corrected sample standard deviation operator stdi(i, from, to, f(i,...))
      180. mini                     (2.4) Minimum value mini(i, from, to, f(i,...))
      181. maxi                     (2.4) Maximum value maxi(i, from, to, f(i,...))
      182. solve                    (4.0) f(x) = 0 equation solving, function root finding: solve( f(x,...), x, a, b )
      301. @~                        (4.0) Bitwise unary complement
      302. @&                        (4.0) Bitwise AND
      303. @^                        (4.0) Bitwise exclusive OR
      304. @|                        (4.0) Bitwise inclusive OR
      305. @<<                       (4.0) Signed left shift
      306. @>>                       (4.0) Signed right shift
    
    1. User defined variables and user defined constants are created without any special form.

    Example:

    import org.mariuszgromada.math.mxparser.*;
    ...
    ...
    Argument x = new Argument("x = 10");
    Constant y = new Constant("y = 2");
    Expression e = new Expression("x/y", x, y);
    System.out.println(e.getExpressionString() + " = " + e.calculate());
    

    Result:

    x/y = 5.0
    

    Additionally please check: a) Tutorial - User defined arguments, b) Tutorial - User defined constants.

    1. User defined functions are fully supported.

    Example 1 - body defined in run-time:

    import org.mariuszgromada.math.mxparser.*;
    ...
    ...
    Function f = new Function("f(x,y) = x*y");
    Expression e = new Expression("20-f(2,5)",f);
    System.out.println(e.getExpressionString() + " = " + e.calculate());
    

    Result 1

    20-f(2,5) = 10.0
    

    Example 2 - body extended via your own implementation:

    import org.mariuszgromada.math.mxparser.*;
    ...
    ...
    /*
     * Implementing FunctionExtension interface
     */
    public class Addition implements FunctionExtension {
       double x;
       double y;
       public Addition() {
          x = Double.NaN;
          y = Double.NaN;
       }
       public Addition(double x, double y) {
          this.x = x;
          this.y = y;
       }
       public int getParametersNumber() {
          return 2;
       }
       public void setParameterValue(int argumentIndex, double argumentValue) {
          if (argumentIndex == 0) x = argumentValue;
          if (argumentIndex == 1) y = argumentValue;
       }
       public double calculate(double... params) {
          return x+y;
       }
       public FunctionExtension clone() {
          return new Addition(x, y);
       }   
    }
    
    /*
    * Creating extended function
    */
    Function f = new Function("f", new Addition());
    mXparser.consolePrintln("f.calculate(1,2) = " + f.calculate(1,2) );
    /*
    * Using extended function in expression
    */
    Expression e = new Expression("f(2,3)", f);
    System.out.println(e.getExpressionString() + " = " + e.calculate() );
    

    Result 2:

    f.calculate(1,2) = 3.0
    f(2,3) = 5.0
    

    Additionally it is worth to follow the whole mXparser Tutorial.

    Found recently - in case you would like to try the syntax (and see the advanced use case) you can download the Scalar Calculator app that is powered by mXparser.

    Best regards

提交回复
热议问题