JEval

来源:互联网 发布:三角眼 整形 知乎 编辑:程序博客网 时间:2024/05/06 16:25

 

net.sourceforge.jeval
Class Evaluator

java.lang.Object  extended by net.sourceforge.jeval.Evaluator

public class Evaluator
extends java.lang.Object

This class is used to evaluate mathematical, string, Boolean and functional expressions. It is the main entry point into the JEval API.

The following types of expressions are supported:

  • mathematical Expression involving numbers. Numbers are treated as doubles, so resulting numbers will contain at least one decimal place.
  • string String can also be added together, compared, etc...
  • Boolean Expression that evaluate to true (1.0) and false (0.0).
  • functional Custom functions can be created or there are many Math and String functions that JEval supplies with this class.

The following operators are supported:

  • ( open parentheses
  • ) closed parentheses
  • + addition (for numbers and strings)
  • - subtration
  • * mutliplication
  • / division
  • % modulus
  • + unary plus
  • - unary minus
  • = equal (for numbers and strings)
  • != not equal (for numbers and strings)
  • < less than (for numbers and strings)
  • <= less than or equal (for numbers and strings)
  • > greater than (for numbers and strings)
  • >= greater than or equal (for numbers and strings)
  • && boolean and
  • || boolean or
  • ! boolean not

Allows for prebuilt and custom functions.

  • JEval already comes with many functions which represent most of the methods in the Math and String classes in the standard JDK.
  • Thirty-nine math and string functions come built in. See the net.sourceforge.jeval.functions.math and net.sourceforge.jeval.functions.string packages for details on these ready to use functions. You can choose to not load these functions if we you want to gain a small improvement in performance.
  • Functions must be followed by an open parentheses and a closed parentheses which contain any required parameters.
  • For more details on functions, see the Function class and the test classes.

Allows for variables.

  • Variable must be enclosed by a pound sign and open brace #{ and a closed brace }. i.e. expression = "#{a} + #{b}"
  • Two math variables come built in. The E and PI variables represent the same value as the Math.E and Math.PI constants in the standard Java SDK. You can choose not to load these variables.

Notes on expression parsing:

  • Spaces are ignored when parsing expressions.
  • The order of precedence used by this class is as follows from highest to lowest.
  • The expression is evaluated as one or more subexpressions. Subexpressions within open parentheses and closed parentheses are evaluated before other parts of the expression.
  • Inner most subexpression are evaluated first working outward.
  • Subexpressions at the same level are evaluated from left to right.
  • When evaluating expressions and subexpressions, operators are evaluated with the following precedence listed below.
  • Operators with with the same precedence are evaluated from left to right.
  • Once the expression is parsed, Variables are replaced with their values. The evaluator has its own internal variable map that it used to resolve variable values. All of the variable related methods on the evaluator refer to this internal map. You can choose to set you own variable resolver on your evaluator instance. IF you do this, then variables resolved by your resolver will override any variables in the evaluator's internal variable map.
  • Functions are then executed and replaced with their results. Function arguments are each inidividually evaluated as subexpressions that are comma separated. This gives you the ability to use nested functions in your expressions. You can choose not to evaluate function arguments as expressions and instead let the functions handle the arguments themselves. This in effect turns off nested expressions, unless you code nexted expression support into yours custom functions.
  • Once all variables and functions are resolved, then the parsed expression and subexpressions are evaluated according to operator precedence.

Operator precedence:

  • + unary plus, - unary minus, ! boolean not
  • * multiplication, / division, % modulus
  • + addition, - subtraction
  • < less than, <= less than or equal, > greater than, >= greater than or equal
  • = equal, != not equal
  • && boolean and
  • || boolean or

Function and variable names can not break any of the following rules:

  • can not start with a number
  • can not contain an operator (see the above list of operators)/li>
  • can not contain a quote character - single or double/li>
  • can not contain a brace character - open or closed/li>
  • can not contain one of the following special characters: #, ~ , ^ !

Other Notes:

  • This class is not thread safe.
  • Allows for the quote character (single or double) to be specified at run time. Quote characters are required for specifying string values.
  • Expressions can contain different types of expressions within the same expression. However, Numeric and string types can not be mixed in a left / right oeprand pair.
  • An expression can be parsed before being evaluated by calling the parse() method. This may save on response time if parsing takes more than a few seconds. However, parsing is usally very fast, so this is probably not needed.
  • Also, if an expression does not change, it will not be parsed each time the expression is evaluated. Therefore, variables values can change and the expression can be evaluataed again without having to re-parse the expression.
  • Nested functions calls are supported. Nested function support can be turned off to improve performance. Custom functions can be coded to handle nested calls instead if desired.
  • The string used to start variables, "#{", can not appear in an expression.
  • See the evaluate methods in this class, JUnit tests and samples for more details.

 

 

下载URL:http://jeval.sourceforge.net/

 

说明:

 

evaluator.putVariable("b", "'World'");

value的内容必须以"'"单引号开头和结尾。


原创粉丝点击