Last update: 2011-06-23

org.proteios.util.jep
Class Jep

java.lang.Object
  extended by org.proteios.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: 2009-04-09 08:48:11 +0200 (Thu, 09 Apr 2009) $

Constructor Summary
Jep()
           
 
Method Summary
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 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 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
 

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

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 functions/operators are:
+      Expressions.add(Expression, Expression)
-      Expressions.subtract(Expression, Expression)
      Expressions.multiply(Expression, Expression)
/      Expressions.divide(Expression, Expression)
-      Expressions.negate(Expression)
log10  Expressions.log10(Expression)
log2   Expressions.log2(Expression)
ln     Expressions.ln(Expression)
sqrt   Expressions.sqrt(Expression)
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.

Parameters:
jep - The JEP object
Returns:
An Expression object
Throws:
BaseException - If the JEP expression can't be converted
See Also:
newJep(String, JepFunction[])

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.

Throws:
BaseException

nodeToString

public static String nodeToString(Node node)
                           throws BaseException
Convert a node to a string value. Supported node types are constants with a string value or variables in which case the variable name is returned as the string. This allows for unquoted strings in expressions.

Throws:
BaseException

nodeToInt

public static int nodeToInt(Node node)
                     throws BaseException
Convert a node to an integer value. Supported node types are constants.

Throws:
BaseException

Last update: 2011-06-23