2012-08-14 59 views
6

Tôi đang tìm thư viện JAVA để phân tích cú pháp & đánh giá biểu thức. Tôi đã tìm kiếm và thử một số thư viện như JEXL và Jeval của Apache, nhưng chúng không chính xác là những gì tôi cần.JAVA - Thư viện phân tích và đánh giá biểu thức

yêu cầu của tôi:

  1. Hỗ trợ tất cả các loại giá trị (ví dụ int, double, boolean, String, vv)
  2. Hỗ trợ tất cả các toán học & toán tử logic được biết đến (+, -, *, <, < =, vv.)
  3. Biến hỗ trợ (không có bất kỳ ký hiệu đặc biệt nào - ví dụ trong biến Jeval phải được viết như # {a} - không đủ tốt cho tôi)
  4. Hỗ trợ chức năng tùy chỉnh - với thực thi và xác thực loại

Bất kỳ đề xuất nào?

+1

Bạn đã xem xét công cụ Java Script tích hợp chưa? – EJP

Trả lời

0

Dưới đây sẽ là một vài giải pháp workaround mà bạn có thể lựa chọn, nếu bạn không tìm thấy một thư viện đánh giá biểu hiện Java thực tế:

  • Đánh giá biểu thức bằng cách sử dụng XPath.
    • Ưu điểm: XPath biết khai thác hợp lý, và bạn có thể thực hiện các biến và các chức năng tùy chỉnh sử dụng phần mở rộng Xalan của
    • Nhược điểm: XPath có loại ít hơn Java
  • Đánh giá biểu thức của bạn sử dụng JavaScript.
    • Ưu điểm: Javascript rất linh hoạt và sẽ vẫn phù hợp khi yêu cầu của bạn bị thắt chặt. Bạn có thể thực hiện các biến và các chức năng tùy chỉnh sử dụng Javascript cũng
    • Nhược điểm: javascript có loại ít hơn Java
  • Đánh giá biểu thức bằng cách sử dụng ngôn ngữ biểu thức JSP của (ví dụ với JUEL)
3

Giống như đề nghị, bạn có thể sử dụng JavaScript. Nhưng bạn cũng có thể xem Spring EL có hỗ trợ cho các yêu cầu của bạn.

5

Hãy thử Janino. Đó là một trình biên dịch trong bộ nhớ thời gian chạy có thể được sử dụng như một bộ đánh giá biểu thức. Có lẽ đó là điều phù hợp với bạn.

+0

Đó thực sự là một thư viện ấn tượng, ít nhất là từ những gì họ yêu cầu để có thể làm –

1

Bạn có thể thử mXparser - nó hỗ trợ một phần đáng kể yêu cầu của bạn:

  1. Nó dựa trên đôi, vì vậy int được hỗ trợ, bổ sung boolean được hỗ trợ là đúng = 1 và sai = 0. Thật không may chuỗi không được hỗ trợ.

dụ Boolean:

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()); 

Kết quả:

T && (F || (F && T)) = 0.0 
  1. mXparser đã ủng hộ rộng rãi cho các nhà khai thác, chức năng, vv .. Kiểm tra mXparser math collection . Điều gì là tốt đẹp, bạn có thể sử dụng chức năng trợ giúp bên trong thư viện.

Ví dụ:

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

Kết quả:

Help content: 

    2. +     <Operator>    addition 
    3. -     <Operator>    subtraction 
    4. *     <Operator>    multiplication 
    5./     <Operator>    division 
    6.^     <Operator>    exponentiation 
    7. !     <Operator>    factorial 
    8. #     <Operator>    modulo function 
    9. &     <Boolean Operator>  logical conjunction (AND) 
    10. &&     <Boolean Operator>  logical conjunction (AND) 
    11. /\     <Boolean Operator>  logical conjunction (AND) 
    12. ~&     <Boolean Operator>  NAND - Sheffer stroke 
    13. ~&&     <Boolean Operator>  NAND - Sheffer stroke 
    14. ~/\     <Boolean Operator>  NAND - Sheffer stroke 
    15. |     <Boolean Operator>  logical disjunction (OR) 
    16. ||     <Boolean Operator>  logical disjunction (OR) 
    17. \/     <Boolean Operator>  logical disjunction (OR) 
    18. ~|     <Boolean Operator>  logical NOR 
    19. ~||     <Boolean Operator>  logical NOR 
    20. ~\/     <Boolean Operator>  logical NOR 
    21. (+)     <Boolean Operator>  exclusive or (XOR) 
    22. -->     <Boolean Operator>  implication (IMP) 
    23. <--     <Boolean Operator>  converse implication (CIMP) 
    24. -/>     <Boolean Operator>  material nonimplication (NIMP) 
    25. </-     <Boolean Operator>  converse nonimplication (CNIMP) 
    26. <->     <Boolean Operator>  logical biconditional (EQV) 
    27. ~     <Boolean Operator>  negation 
    28. ¬     <Boolean Operator>  negation 
    162. add     <Variadic Function>  (2.4) Summation operator add(a1,a2,a3,...,an) 
    168. sum     <Calculus Operator>  summation operator (SIGMA) sum(i, from, to, f(i,...)) 
    169. prod    <Calculus Operator>  product operator (PI) prod(i, from, to, f(i,...)) 
    170. int     <Calculus Operator>  definite integral operator (int(f(x,...), x, a, b)) 
    171. der     <Calculus Operator>  derivative operator (der(f(x,...), x)) 
    172. der-    <Calculus Operator>  left derivative operator (der-(f(x,...), x)) 
    173. der+    <Calculus Operator>  right derivative operator (der+(f(x,...), x)) 
    174. dern    <Calculus Operator>  n-th derivative operator (dern(f(x,...), x)) 
    175. diff    <Calculus Operator>  forward difference operator 
    176. difb    <Calculus Operator>  backward difference operator 
    177. avg     <Calculus Operator>  (2.4) Average operator avg(i, from, to, f(i,...)) 
    178. vari    <Calculus Operator>  (2.4) Bias-corrected sample variance operator vari(i, from, to, f(i,...)) 
    179. stdi    <Calculus Operator>  (2.4) Bias-corrected sample standard deviation operator stdi(i, from, to, f(i,...)) 
    180. mini    <Calculus Operator>  (2.4) Minimum value mini(i, from, to, f(i,...)) 
    181. maxi    <Calculus Operator>  (2.4) Maximum value maxi(i, from, to, f(i,...)) 
    182. solve    <Calculus Operator>  (4.0) f(x) = 0 equation solving, function root finding: solve(f(x,...), x, a, b) 
    301. @~     <Bitwise Operator>  (4.0) Bitwise unary complement 
    302. @&     <Bitwise Operator>  (4.0) Bitwise AND 
    303. @^     <Bitwise Operator>  (4.0) Bitwise exclusive OR 
    304. @|     <Bitwise Operator>  (4.0) Bitwise inclusive OR 
    305. @<<     <Bitwise Operator>  (4.0) Signed left shift 
    306. @>>     <Bitwise Operator>  (4.0) Signed right shift 
  1. Người dùng xác định các biếnhằng người dùng định nghĩa được tạo ra mà không cần bất kỳ hình thức đặc biệt .

Ví dụ:

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()); 

Kết quả:

x/y = 5.0 

Ngoài ra hãy kiểm tra: a) Tutorial - User defined arguments, b) Tutorial - User defined constants.

  1. Chức năng do người dùng xác định được hỗ trợ đầy đủ.

Ví dụ 1 - Cơ quan quy định tại thời gian chạy:

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()); 

Kết quả 1

20-f(2,5) = 10.0 

Ví dụ 2 - cơ thể mở rộng thông qua thực hiện của riêng bạn:

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()); 

quả 2:

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

Ngoài ra bạn nên tuân theo toàn bộ sốmXparser Tutorial.

Trân trọng

Các vấn đề liên quan