2.17.2: 2011-06-17

net.sf.basedb.util.jep
Class Jep

java.lang.Object
  extended by 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 the newJep(String, JepFunction[]) method. If the expression contains variables set values for those with the JEP.setVarValue(String, Object) method. Then, evaluate the expression using the JEP.getValue() method.

You can also convert the expression to a Query API Expression object with the jepToExpression(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) and jepToRestriction(JEP) for functions/operators supported by the conversion methods.

Version:
2.0
Author:
Nicklas
See Also:
Java Mathematical Expression Parser package
Last modified
$Date: 2010-08-13 10:50:27 +0200 (Fri, 13 Aug 2010) $

Field Summary
private static Enumeration<String,String> functions
           
private static Field jepErrorList
           
private static Enumeration<String,String> operators
           
 
Constructor Summary
Jep()
           
 
Method Summary
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 API Expression.
static Restriction formulaToRestriction(String formula, JepFunction... functions)
          Convert JEP expression to a Query API Restriction.
static Enumeration<String,String> getFunctions()
          Get a list of built-in functions supported by the jepToExpression(JEP) converter.
static Enumeration<String,String> getOperators()
          Get a list of built-in operators supported by the jepToExpression(JEP) and jepToRestriction(JEP) converter.
static Expression jepToExpression(JEP jep)
          Convert a JEP expression to a Query API Expression.
static Restriction jepToRestriction(JEP jep)
          Convert a JEP expression to a Query API Restriction.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

functions

private static volatile Enumeration<String,String> functions

operators

private static volatile Enumeration<String,String> operators

jepErrorList

private static Field jepErrorList
Constructor Detail

Jep

public Jep()
Method Detail

newJep

public static JEP newJep(String formula,
                         JepFunction... functions)
                  throws BaseException
Create a new JEP expression. Evaluate the expression using the JEP.getValue() method. Set values for variables with the JEP.setVarValue(String, Object).

Parameters:
formula - The expression to parse
functions - 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 the jepToExpression(JEP) converter. More functions can be registered when creating the JEP expression (see newJep(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 the jepToExpression(JEP) and jepToRestriction(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 API Expression. The Query API only supports a subset of the functions/operators that JEP supports. Supported operators are:
+      Expressions.add(Expression, Expression)
-      Expressions.subtract(Expression, Expression)
      Expressions.multiply(Expression, Expression)
/      Expressions.divide(Expression, Expression)
-      Expressions.negate(Expression)
log    Expressions.log10(Expression)
log2   Expressions.log2(Expression)
ln     Expressions.ln(Expression)
sqrt   Expressions.sqrt(Expression)
abs    Expressions.abs(Expression)
exp    Expressions.exp(Expression)
raw    Dynamic.rawData(String) *
ch     Dynamic.column(VirtualColumn) *
rawCh  Dynamic.column(VirtualColumn) *
number Expressions.integer(int) or
       Expressions.aFloat(float)
string Expressions.parameter(String, Object)
variable Expressions.parameter(String) **
= This is a optional function that must be registered when creating the JEP object.
= 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 API Expression. 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 API Restriction. The Query API only supports a subset of the functions/operators that JEP supports. Supported functions/operators are:
==      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)
= This is a optional function that must be registered when creating the JEP object.

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 API Restriction. 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 calling JEP.getValueAsObject() the error list should be cleared.

Parameters:
jep -

2.17.2: 2011-06-17