Class Jep
- java.lang.Object
-
- net.sf.basedb.util.jep.Jep
-
public class Jep extends Object
Utility class for parsing mathematical expressions. This class uses the Java Mathematical Expression Parser package from Singular Systems to parse mathematical expressions.Create a new
JEP
expression with thenewJep(String, JepFunction[])
method. If the expression contains variables set values for those with theJEP.setVarValue(String, Object)
method. Then, evaluate the expression using theJEP.getValue()
method.You can also convert the expression to a Query API
Expression
object with thejepToExpression(JEP)
method. The conversion doesn't support all methods since the Query API doesn't have support for them.See the JEP homepage for more information about JEP and all supported functions/operators. See
jepToExpression(JEP)
andjepToRestriction(JEP)
for functions/operators supported by the conversion methods.- Version:
- 2.0
- Author:
- Nicklas
- See Also:
- Java Mathematical Expression Parser package
- Last modified
- $Date: 2015-05-12 11:27:08 +0200 (ti, 12 maj 2015) $
-
-
Field Summary
Fields Modifier and Type Field Description private static Enumeration<String,String>
functions
private static Field
jepErrorList
private static Enumeration<String,String>
operators
-
Constructor Summary
Constructors Constructor Description Jep()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static void
addFunctions(JEP jep, JepFunction... functions)
static void
clearErrorList(JEP jep)
This is a workaround for a bug in the JEP error handling.static Expression
formulaToExpression(String formula, JepFunction... functions)
Convert JEP expression to a Query APIExpression
.static Restriction
formulaToRestriction(String formula, JepFunction... functions)
Convert JEP expression to a Query APIRestriction
.static Enumeration<String,String>
getFunctions()
Get a list of built-in functions supported by thejepToExpression(JEP)
converter.static Enumeration<String,String>
getOperators()
Get a list of built-in operators supported by thejepToExpression(JEP)
andjepToRestriction(JEP)
converter.static Expression
jepToExpression(JEP jep)
Convert a JEP expression to a Query APIExpression
.static Restriction
jepToRestriction(JEP jep)
Convert a JEP expression to a Query APIRestriction
.static JEP
newJep(String formula, JepFunction... functions)
Create a new JEP expression.static Expression
nodeToExpression(Node node)
Convert a node with it's children to an expression.static int
nodeToInt(Node node)
Convert a node to an integer value.static Restriction
nodeToRestriction(Node node)
Convert a node with it's children to a restriction.static String
nodeToString(Node node)
Convert a node to a string value.
-
-
-
Field Detail
-
functions
private static volatile Enumeration<String,String> functions
-
operators
private static volatile Enumeration<String,String> operators
-
jepErrorList
private static Field jepErrorList
-
-
Method Detail
-
newJep
public static JEP newJep(String formula, JepFunction... functions) throws BaseException
Create a new JEP expression. Evaluate the expression using theJEP.getValue()
method. Set values for variables with theJEP.setVarValue(String, Object)
.- Parameters:
formula
- The expression to parsefunctions
- Extra functions that are required to parse the expression- Returns:
- A
JEP
object - Throws:
BaseException
- If the expression couldn't be parsed
-
addFunctions
private static void addFunctions(JEP jep, JepFunction... functions)
-
getFunctions
public static Enumeration<String,String> getFunctions()
Get a list of built-in functions supported by thejepToExpression(JEP)
converter. More functions can be registered when creating the JEP expression (seenewJep(String, JepFunction[])
).- Returns:
- An enumeration with the name of the function in the key and a description in the value part
-
getOperators
public static Enumeration<String,String> getOperators()
Get a list of built-in operators supported by thejepToExpression(JEP)
andjepToRestriction(JEP)
converter. It is not possible to register more operators.- Returns:
- An enumeration with the symbol of the operator in the key and a description in the value part
- Since:
- 2.4
-
jepToExpression
public static Expression jepToExpression(JEP jep) throws BaseException
Convert a JEP expression to a Query APIExpression
. The Query API only supports a subset of the functions/operators that JEP supports. Supported operators are:+
= This is a optional function that must be registered when creating the JEP object.Expressions.add(Expression, Expression)
-Expressions.subtract(Expression, Expression)
Expressions.multiply(Expression, Expression)
/Expressions.divide(Expression, Expression)
-Expressions.negate(Expression)
logExpressions.log10(Expression)
log2Expressions.log2(Expression)
lnExpressions.ln(Expression)
sqrtExpressions.sqrt(Expression)
absExpressions.abs(Expression)
expExpressions.exp(Expression)
rawDynamic.rawData(String)
* chDynamic.column(VirtualColumn)
* rawChDynamic.column(VirtualColumn)
* numberExpressions.integer(int)
orExpressions.aFloat(float)
stringExpressions.parameter(String, Object)
variableExpressions.parameter(String)
**
= The NULL string is converted to a NULL value- Parameters:
jep
- The JEP object- Returns:
- An
Expression
object - Throws:
BaseException
- If the JEP expression can't be converted- See Also:
newJep(String, JepFunction[])
,getFunctions()
-
formulaToExpression
public static Expression formulaToExpression(String formula, JepFunction... functions) throws BaseException
Convert JEP expression to a Query APIExpression
. This method is equivalent to:jepToExpression(newJep(formula, functions))
.- Throws:
BaseException
- See Also:
jepToExpression(JEP)
-
jepToRestriction
public static Restriction jepToRestriction(JEP jep) throws BaseException
Convert a JEP expression to a Query APIRestriction
. The Query API only supports a subset of the functions/operators that JEP supports. Supported functions/operators are:==
= This is a optional function that must be registered when creating the JEP object.Restrictions.eq(Expression, Expression)
!=Restrictions.neq(Expression, Expression)
>Restrictions.gt(Expression, Expression)
>=Restrictions.gteq(Expression, Expression)
<Restrictions.lt(Expression, Expression)
<=Restrictions.lteq(Expression, Expression)
>Restrictions.gt(Expression, Expression)
&&Restrictions.and(Restriction[])
||Restrictions.or(Restriction[])
!Restrictions.not(Restriction)
All operators supported by the
jepToExpression(JEP)
are of course also supported in the appropriate places.- Parameters:
jep
- The JEP object- Returns:
- A
Restriction
object - Throws:
BaseException
- If the JEP expression can't be converted- See Also:
newJep(String, JepFunction[])
-
formulaToRestriction
public static Restriction formulaToRestriction(String formula, JepFunction... functions) throws BaseException
Convert JEP expression to a Query APIRestriction
. This method is equivalent to:jepToRestriction(newJep(formula, functions))
.- Throws:
BaseException
- See Also:
jepToRestriction(JEP)
-
nodeToExpression
public static Expression nodeToExpression(Node node) throws BaseException
Convert a node with it's children to an expression.- Parameters:
node
- Node to convert to an expression- Returns:
- A
Expression
object - Throws:
BaseException
- If the JEP function in node not is supported.
-
nodeToRestriction
public static Restriction nodeToRestriction(Node node) throws BaseException
Convert a node with it's children to a restriction.- Parameters:
node
- JEP node to convert.- Returns:
- A
Restriction
object - Throws:
BaseException
- If the JEP function in node is not supported by the converter.- Since:
- 2.4
-
nodeToString
public static String nodeToString(Node node) throws BaseException
Convert a node to a string value. Supported node types are constants or variables. Constants will be converted to strings with the toString() method. For variables the name is returned as the string. This allows for unquoted strings in expressions. The NULL string is converted to a NULL value.- Parameters:
node
- JEP node to convert.- Returns:
- A String object or null if the node does not have any value.
- Throws:
BaseException
- If the node could not be converted in some way.
-
nodeToInt
public static int nodeToInt(Node node) throws BaseException
Convert a node to an integer value. Supported node types are constants.- Parameters:
node
- JEP node to convert.- Returns:
- an int object
- Throws:
BaseException
- If the node could not be converted.
-
clearErrorList
public static void clearErrorList(JEP jep)
This is a workaround for a bug in the JEP error handling. Before callingJEP.getValueAsObject()
the error list should be cleared.
-
-