com.rubecula.jep
Class ExpressionParser

java.lang.Object
  |
  +--com.rubecula.jep.ExpressionParser
All Implemented Interfaces:
ExpressionInterface, Identifiable

public class ExpressionParser
extends java.lang.Object
implements ExpressionInterface

This class is the link from JQuantity to JEP - ExpressionParser depends on JEP, but depends only the com.rubecula.util package from the JQuantity project. There is no dependence on com.rubecula.jquantity, either compile-time or dynamic, in this class -- the only reference is in the main (testing) program (as an example of usage).

Since:
V_0_1
Version:
$Revision: 1.13 $
Author:
Robin Hillyard

Constructor Summary
ExpressionParser()
          Constructor to create a new expression parser using both JEP and JQuantity.
 
Method Summary
 java.lang.Double addVariable(java.lang.String string, double value)
          Method to add a predefined variable to the JEP expression parser.
 Presentable evaluate(java.lang.Object parseTree)
          Method to evaluate the result of parsing an expression.
 java.lang.String getIdentifier()
          Method to get the specific identifier for an object.
static void main(java.lang.String[] args)
          Main program for testing purposes only.
 java.lang.Object parse(java.lang.String expression)
          Method to parse an expression and return a reference to the parse tree.
 void setDebug(boolean debug)
          Method to set the debug flag for this instance to the value of debug.
 void setMillFactory(MillFactoryInterface millFactory)
          Method to setup the mill factory for the evaluation of expressions, in particular monadic, dyadic operations, pushing and popping of values.
 void setVariables(Lookupable variables)
          Method to setup the variables map for allowing variables to be dealt with in expressions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExpressionParser

public ExpressionParser()
Constructor to create a new expression parser using both JEP and JQuantity. The number factory of the expression parser is set to a new instance of JQuantityFactory.
This method constructs a new JEP with the following options: traverse = implicitMultiply = false; allowUndeclared = true if variables is non null; NumberFactory = new PresentableNumberFactory(). Furthermore, the following after-market addons are set on the new JEP object: addStandardFunctions();addStandardConstants();addComplex(); *

Method Detail

main

public static void main(java.lang.String[] args)
Main program for testing purposes only. Sets up an expression parser which uses MillFactory for its computation mill.

Parameters:
args - the command line arguments (each argument is an an expression to be parsed and evaluated).

getIdentifier

public java.lang.String getIdentifier()
Method to get the specific identifier for an object.

Specified by:
getIdentifier in interface Identifiable
Returns:
the identifier.

setMillFactory

public void setMillFactory(MillFactoryInterface millFactory)
Method to setup the mill factory for the evaluation of expressions, in particular monadic, dyadic operations, pushing and popping of values. If this method is not invoked on an expression parser, then the native function evaluation of the expression parser will be used instead.

Specified by:
setMillFactory in interface ExpressionInterface
Parameters:
millFactory - a factory for creating mills with which to evaluate expressions.

setVariables

public void setVariables(Lookupable variables)
Method to setup the variables map for allowing variables to be dealt with in expressions. If this method is not invoked on an expression parser, then variables must be predefined before they can be encountered in expression. Predefinition is fine, as long as the variables don't tend to change their values.

Specified by:
setVariables in interface ExpressionInterface
Parameters:
variables - a map of variables, so that they can be looked up (may be null, in which case undeclared variables are not enabled for this expression parser).

addVariable

public java.lang.Double addVariable(java.lang.String string,
                                    double value)
Method to add a predefined variable to the JEP expression parser.

Specified by:
addVariable in interface ExpressionInterface
Parameters:
string - the name of the variable
value - the desired value of the variable.
Returns:
the result of calling @{JEP@addVariable(String,double)} with parameters string and value.

setDebug

public void setDebug(boolean debug)
Method to set the debug flag for this instance to the value of debug.

Specified by:
setDebug in interface ExpressionInterface
Parameters:
debug - true or false.

parse

public java.lang.Object parse(java.lang.String expression)
Method to parse an expression and return a reference to the parse tree.

Specified by:
parse in interface ExpressionInterface
Parameters:
expression - a mathematical expression to parse.
Returns:
a reference to the parse tree.

evaluate

public Presentable evaluate(java.lang.Object parseTree)
                     throws PresentableException
Method to evaluate the result of parsing an expression.

Specified by:
evaluate in interface ExpressionInterface
Parameters:
parseTree - the result of previously calling parse(String).
Returns:
the value of the expression previously parsed.
Throws:
PresentableException - if the result of parsing was an error.