com.rubecula.jep
Class ExpressionParser

java.lang.Object
  extended bycom.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.18 $
Author:
Robin Hillyard

Field Summary
private  java.util.Vector _AlgebraicVariables
           
private  boolean _Debug
           
private  org.nfunk.jep.JEP _JEP
          JEP instance to parse and evaluate expressions.
private  MillFactoryInterface _MillFactory
          Factory to create Mills for evaluating expressions.
private  long _MillID
          Counter for identifying mills used for expression evaluation.
private  PresentableNumberFactory _NumberFactory
          Factory to create numbers, implementing the NumberFactory interface.
private  Lookupable _Variables
          Store in which to lookup the current values of variables that arise in the terms of expressions.
 
Constructor Summary
ExpressionParser()
          Constructor to create a new expression parser using both JEP and JQuantity.
 
Method Summary
 java.lang.Double addAlgebraicVariable(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.util.Collection getExpressionVariables()
           
 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 resetAlgebraicVariables()
          Method to remove all predefined variables from the expression parser.
 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.
private  java.lang.String transform(java.lang.String expression)
          Method to transform the operation names, etc. which on input conform to the Microsoft Excel names into JEP operation names).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_JEP

private final org.nfunk.jep.JEP _JEP
JEP instance to parse and evaluate expressions.


_MillFactory

private MillFactoryInterface _MillFactory
Factory to create Mills for evaluating expressions. Currently, Each expression has a different mill to work with.


_NumberFactory

private PresentableNumberFactory _NumberFactory
Factory to create numbers, implementing the NumberFactory interface. Further, however, the numbers created must be instances of Presentable.


_Variables

private Lookupable _Variables
Store in which to lookup the current values of variables that arise in the terms of expressions.


_AlgebraicVariables

private final java.util.Vector _AlgebraicVariables

_MillID

private long _MillID
Counter for identifying mills used for expression evaluation.


_Debug

private boolean _Debug
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 PresentableNumberFactory.PresentableNumberFactory().
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 MillFactoryfor 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).

addAlgebraicVariable

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

Specified by:
addAlgebraicVariable 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.

transform

private java.lang.String transform(java.lang.String expression)
Method to transform the operation names, etc. which on input conform to the Microsoft Excel names into JEP operation names).

Parameters:
expression - an Excel-like mathematical expression to transform.
Returns:
an expression suitable for parsing by JEP.

getExpressionVariables

public java.util.Collection getExpressionVariables()
Specified by:
getExpressionVariables in interface ExpressionInterface

resetAlgebraicVariables

public void resetAlgebraicVariables()
Description copied from interface: ExpressionInterface
Method to remove all predefined variables from the expression parser.

Specified by:
resetAlgebraicVariables in interface ExpressionInterface