public class Expression
extends java.lang.Object
BigDecimal result = null; Expression expression = new Expression("1+1/3"); result = expression.eval(): expression.setPrecision(2); result = expression.eval(): result = new Expression("(3.4 + -4.1)/2").eval(); result = new Expression("SQRT(a^2 + b^2").with("a","2.4").and("b","9.253").eval(); BigDecimal a = new BigDecimal("2.4"); BigDecimal b = new BigDecimal("9.235"); result = new Expression("SQRT(a^2 + b^2").with("a",a).and("b",b).eval(); result = new Expression("2.4/PI").setPrecision(128).setRoundingMode(RoundingMode.UP).eval(); result = new Expression("random() > 0.5").eval(); result = new Expression("not(x<7 || sqrt(max(x,9)) <= 3))").with("x","22.9").eval();
Mathematical Operators | |
---|---|
Operator | Description |
+ | Additive operator |
- | Subtraction operator |
* | Multiplication operator |
/ | Division operator |
% | Remainder operator (Modulo) |
^ | Power operator |
Boolean Operators* | |
---|---|
Operator | Description |
= | Equals |
== | Equals |
!= | Not equals |
<> | Not equals |
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
&& | Boolean and |
|| | Boolean or |
Function* | Description |
---|---|
NOT(expression) | Boolean negation, 1 (means true) if the expression is not zero |
IF(condition,value_if_true,value_if_false) | Returns one value if the condition evaluates to true or the other if it evaluates to false |
RANDOM() | Produces a random number between 0 and 1 |
MIN(e1,e2) | Returns the smaller of both expressions |
MAX(e1,e2) | Returns the bigger of both expressions |
ABS(expression) | Returns the absolute (non-negative) value of the expression |
ROUND(expression,precision) | Rounds a value to a certain number of digits, uses the current rounding mode |
FLOOR(expression) | Rounds the value down to the nearest integer |
CEILING(expression) | Rounds the value up to the nearest integer |
LOG(expression) | Returns the natural logarithm (base e) of an expression |
LOG10(expression) | Returns the common logarithm (base 10) of an expression |
SQRT(expression) | Returns the square root of an expression |
SIN(expression) | Returns the trigonometric sine of an angle (in degrees) |
COS(expression) | Returns the trigonometric cosine of an angle (in degrees) |
TAN(expression) | Returns the trigonometric tangens of an angle (in degrees) |
ASIN(expression) | Returns the angle of asin (in degrees) |
ACOS(expression) | Returns the angle of acos (in degrees) |
ATAN(expression) | Returns the angle of atan (in degrees) |
SINH(expression) | Returns the hyperbolic sine of a value |
COSH(expression) | Returns the hyperbolic cosine of a value |
TANH(expression) | Returns the hyperbolic tangens of a value |
RAD(expression) | Converts an angle measured in degrees to an approximately equivalent angle measured in radians |
DEG(expression) | Converts an angle measured in radians to an approximately equivalent angle measured in degrees |
Constant | Description |
---|---|
PI | The value of PI, exact to 100 digits |
TRUE | The value one |
FALSE | The value zero |
Expression e = new Expression("2.1234 >> 2"); e.addOperator(e.new Operator(">>", 30, true) { @Override public BigDecimal eval(BigDecimal v1, BigDecimal v2) { return v1.movePointRight(v2.toBigInteger().intValue()); } }); e.eval(); // returns 212.34
Expression e = new Expression("2 * average(12,4,8)"); e.addFunction(e.new Function("average", 3) { @Override public BigDecimal eval(ListThe software is licensed under the MIT Open Source license (see LICENSE file).parameters) { BigDecimal sum = parameters.get(0).add(parameters.get(1)).add(parameters.get(2)); return sum.divide(new BigDecimal(3)); } }); e.eval(); // returns 16
Modifier and Type | Class and Description |
---|---|
class |
Expression.Operator
Abstract definition of a supported operator.
|
Modifier and Type | Field and Description |
---|---|
static MXLogger |
EXPLOGGER |
static java.lang.String |
GLOBAL_VARS |
static java.lang.String |
LOCAL_VARS |
static java.math.BigDecimal |
PI
Definition of PI as a constant, can be used in expressions as variable.
|
Constructor and Description |
---|
Expression(java.lang.String expression,
ExpressionContext context)
Creates a new expression instance from an expression string.
|
Modifier and Type | Method and Description |
---|---|
Function |
addFunction(Function function)
Adds a function to the list of supported functions
|
LazyFunction |
addLazyFunction(LazyFunction function)
Adds a lazy function function to the list of supported functions
|
Expression.Operator |
addOperator(Expression.Operator operator)
Adds an operator to the list of supported operators.
|
Expression |
and(java.lang.String variable,
java.math.BigDecimal value)
Sets a variable value.
|
Expression |
and(java.lang.String variable,
java.lang.String value)
Sets a variable value.
|
void |
cacheValue(java.lang.String key,
java.lang.Double value) |
java.math.BigDecimal |
eval() |
java.lang.Object |
evalObjectValue() |
java.util.Set<java.lang.String> |
findAttributesInExpression() |
java.lang.String |
getAttrFormulaAttrName() |
java.lang.Double |
getCachedValue(java.lang.String key) |
java.util.Map<java.lang.String,java.lang.Object> |
getCalculatedValue() |
java.lang.Object |
getCalculatedValue(java.lang.Object key) |
java.util.Map<java.lang.String,java.lang.Exception> |
getErrorDataMap() |
java.util.Iterator<java.lang.String> |
getExpressionTokenizer()
Get an iterator for this expression, allows iterating over an expression
token by token.
|
java.util.Map<java.lang.String,java.lang.Double> |
getExpValue() |
java.util.Map<java.lang.String,java.lang.Double> |
getExpValueMap() |
java.lang.Object |
getGlobalVar(java.lang.String varName) |
java.lang.Integer |
getLineNum() |
java.lang.Object |
getLocalVar(java.lang.String varName) |
java.math.MathContext |
getMathContext() |
java.lang.Object |
getMetaData(java.lang.String key) |
java.util.Map<java.lang.String,java.lang.Object> |
getMetaDataMap() |
ObjectFormulaInfo |
getObjectFormulaInfo() |
java.util.Map<java.lang.String,java.lang.Object> |
getScriptContext() |
ExpressionContext |
getVariables() |
boolean |
hasCachedValue(java.lang.String key) |
boolean |
hasGlobalVar(java.lang.String varName) |
boolean |
hasLocalVar(java.lang.String varName) |
boolean |
isMBRMode() |
boolean |
isVar(java.lang.String key) |
java.lang.Object |
resolveVar(java.lang.String varName) |
void |
setAttrFormulaAttrName(java.lang.String attrName) |
void |
setErrorData(java.lang.String key,
java.lang.Exception value) |
void |
setExpValue(java.lang.String key,
java.lang.Double value) |
void |
setGlobalVar(java.lang.String varName,
java.lang.Object value) |
void |
setLineNum(int lineNum) |
void |
setLocalVar(java.lang.String varName,
java.lang.Object value) |
void |
setMetaData(java.lang.String key,
java.lang.Object value) |
void |
setObjectFormulaInfo(ObjectFormulaInfo objectFormulaInfo) |
Expression |
setPrecision(int precision)
Sets the precision for expression evaluation.
|
Expression |
setRoundingMode(java.math.RoundingMode roundingMode)
Sets the rounding mode for expression evaluation.
|
void |
setScriptContext(java.util.Map<java.lang.String,java.lang.Object> context) |
Expression |
setVariable(java.lang.String variable,
java.math.BigDecimal value)
Sets a variable value.
|
Expression |
setVariable(java.lang.String variable,
java.lang.String value)
Sets a variable value.
|
boolean |
shouldEval() |
java.lang.String |
toRPN()
Get a string representation of the RPN (Reverse Polish Notation) for this
expression.
|
void |
validateAttributesInExpression() |
java.math.BigDecimal |
validateEval()
Evaluates the expression.
|
Expression |
with(java.lang.String variable,
java.math.BigDecimal value)
Sets a variable value.
|
Expression |
with(java.lang.String variable,
java.lang.String value)
Sets a variable value.
|
public static final java.math.BigDecimal PI
public static final java.lang.String GLOBAL_VARS
public static final java.lang.String LOCAL_VARS
public static final MXLogger EXPLOGGER
public Expression(java.lang.String expression, ExpressionContext context)
expression
- The expression. E.g. "2.4*sin(3)/(2-4)"
or
"sin(y)>0 & max(z, 3)>3"
public ExpressionContext getVariables()
public void setLineNum(int lineNum)
public java.lang.Integer getLineNum()
public boolean isMBRMode()
public void setScriptContext(java.util.Map<java.lang.String,java.lang.Object> context)
public java.util.Map<java.lang.String,java.lang.Object> getScriptContext()
public void setGlobalVar(java.lang.String varName, java.lang.Object value)
public java.lang.Object getGlobalVar(java.lang.String varName)
public boolean hasGlobalVar(java.lang.String varName)
public void setLocalVar(java.lang.String varName, java.lang.Object value)
public java.lang.Object getLocalVar(java.lang.String varName)
public boolean hasLocalVar(java.lang.String varName)
public java.lang.Object resolveVar(java.lang.String varName)
public java.util.Set<java.lang.String> findAttributesInExpression()
public void validateAttributesInExpression()
public java.math.BigDecimal validateEval() throws MXException
MXException
public java.math.BigDecimal eval() throws MXException
MXException
public java.lang.Object evalObjectValue() throws MXException
MXException
public Expression setPrecision(int precision)
precision
- The new precision.public Expression setRoundingMode(java.math.RoundingMode roundingMode)
roundingMode
- The new rounding mode.public java.math.MathContext getMathContext()
public Expression.Operator addOperator(Expression.Operator operator)
operator
- The operator to add.null
if
there was none.public Function addFunction(Function function)
function
- The function to add.null
if
there was none.public LazyFunction addLazyFunction(LazyFunction function)
function
- The function to add.null
if
there was none.public Expression setVariable(java.lang.String variable, java.math.BigDecimal value)
variable
- The variable name.value
- The variable value.public Expression setVariable(java.lang.String variable, java.lang.String value)
variable
- The variable to set.value
- The variable value.public Expression with(java.lang.String variable, java.math.BigDecimal value)
variable
- The variable to set.value
- The variable value.public Expression and(java.lang.String variable, java.lang.String value)
variable
- The variable to set.value
- The variable value.public Expression and(java.lang.String variable, java.math.BigDecimal value)
variable
- The variable to set.value
- The variable value.public Expression with(java.lang.String variable, java.lang.String value)
variable
- The variable to set.value
- The variable value.public java.util.Iterator<java.lang.String> getExpressionTokenizer()
public java.lang.String toRPN() throws MXException
MXException
public java.util.Map<java.lang.String,java.lang.Object> getCalculatedValue()
public java.util.Map<java.lang.String,java.lang.Double> getExpValue()
public java.lang.Object getCalculatedValue(java.lang.Object key)
public void cacheValue(java.lang.String key, java.lang.Double value)
public java.lang.Double getCachedValue(java.lang.String key)
public boolean hasCachedValue(java.lang.String key)
public void setAttrFormulaAttrName(java.lang.String attrName)
public java.lang.String getAttrFormulaAttrName()
public ObjectFormulaInfo getObjectFormulaInfo()
public void setObjectFormulaInfo(ObjectFormulaInfo objectFormulaInfo)
public void setExpValue(java.lang.String key, java.lang.Double value)
public java.util.Map<java.lang.String,java.lang.Double> getExpValueMap()
public java.lang.Object getMetaData(java.lang.String key)
public java.util.Map<java.lang.String,java.lang.Object> getMetaDataMap()
public void setMetaData(java.lang.String key, java.lang.Object value)
public java.util.Map<java.lang.String,java.lang.Exception> getErrorDataMap()
public void setErrorData(java.lang.String key, java.lang.Exception value)
public boolean isVar(java.lang.String key)
public boolean shouldEval()