edu.buffalo.fs7.mathlib
Class Parse

java.lang.Object
  |
  +--edu.buffalo.fs7.mathlib.Parse

public class Parse
extends java.lang.Object

class providing methods to parse an arithmetic expression. A string with an arithmetic expression is first preprocessed to allow certain short notations such like |01><01| for an outer product, then then string is divided into MathObject tokens by the Tokenizer class. The parse class finally evaluates the expression and returns the result of the expression or throws an exception in case of an error.

Since this class is the central class for evalutating an expression it also provides the mathlib event handling. Everytime there is for example an assignment in an expression and the expression was completely evaluated, a MathlibEvent is fired to notify listeners about the newly assigned variable. The MathlibEvent mechnism is very similar to the event model implemented in Java itself. Potential listener classes need to implement the MathlibEventListener interface and register themselves here in the Parse object.

For this class to have all features, a Mathlib object must be instantiated. Although this class provides static expression evalutation methods, it is strongly recommended to provide a mathlib object in order to use the full functinality (the Mathlib class provides the variable management).

MathlibEvent listeners are for example the classes Mathlib, QVarTree, GateTable.

See Also:
Tokenizer, Mathlib, MathlibEvent, MathlibEventListener

Field Summary
protected static java.lang.String category
           
static boolean echoAnswer
          still in use ?
protected static java.util.Vector listeners
          holds the MathlibEventListeners
protected  Mathlib mathlib
          holds the Mathlib object
 
Constructor Summary
Parse(Mathlib mathlib)
          creates a Parse object associated with a certain Mathlib object
 
Method Summary
static void addMathlibEventListener(MathlibEventListener m)
          registers a MathlibEventListener
 java.lang.String evaluateExpression(java.lang.String str)
          this method fully evaluates an expression and assignes the result variable.
static void fireMathlibEvent(java.lang.String objectName, MathObject mathObject, int action)
          creates a MathlibEvent and pumps it to processEvent
static void fireMathlibEvent(java.lang.String objectName, java.lang.String newName)
          creates a MathlibEvent and pumps it to processEvent
static void fireMathlibEvent(java.lang.String objectName, java.lang.String newName, java.lang.String category, MathObject mathObject, int action)
          creates a MathlibEvent and pumps it to processEvent
static MathObject parseExpression(java.lang.String str)
          static method which can parse an expression without using variable and without any assignments.
protected static void processEvent(MathlibEvent event)
          is called internally by fireMathlibEvent in order to dispatch the event
static void removeMathlibEventListener(MathlibEventListener m)
          removes a MathlibEventListener
static java.lang.String replaceSubstring(java.lang.String workStr, java.lang.String oldStr, java.lang.String newStr)
          static method replacing a substring by another substring used by the preprocessor
 void setCategory(java.lang.String newCategory)
          set a preset category in order to let all MathlibEvents generated by the parse methods belong to this preset category (not used anymore)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

echoAnswer

public static boolean echoAnswer
still in use ?

listeners

protected static java.util.Vector listeners
holds the MathlibEventListeners

mathlib

protected Mathlib mathlib
holds the Mathlib object

category

protected static java.lang.String category
Constructor Detail

Parse

public Parse(Mathlib mathlib)
creates a Parse object associated with a certain Mathlib object
Method Detail

addMathlibEventListener

public static void addMathlibEventListener(MathlibEventListener m)
registers a MathlibEventListener
Parameters:
m - listener object

removeMathlibEventListener

public static void removeMathlibEventListener(MathlibEventListener m)
removes a MathlibEventListener

processEvent

protected static void processEvent(MathlibEvent event)
is called internally by fireMathlibEvent in order to dispatch the event
Parameters:
e - MathlibEvent to dispatch

fireMathlibEvent

public static void fireMathlibEvent(java.lang.String objectName,
                                    MathObject mathObject,
                                    int action)
creates a MathlibEvent and pumps it to processEvent
Parameters:
objectName - name of the mathobject
mathObject - the actual mathobject
the - action to perform

fireMathlibEvent

public static void fireMathlibEvent(java.lang.String objectName,
                                    java.lang.String newName,
                                    java.lang.String category,
                                    MathObject mathObject,
                                    int action)
creates a MathlibEvent and pumps it to processEvent
Parameters:
objectName - name of the mathobject
newName - new name of the mathobject in case of a CHANGE_NAM event
category - logical category of the mathobject
mathObject - the actual mathobject
the - action to perform

fireMathlibEvent

public static void fireMathlibEvent(java.lang.String objectName,
                                    java.lang.String newName)
creates a MathlibEvent and pumps it to processEvent
Parameters:
objectName - name of the mathobject
newName - new name of the mathobject
the - action to perform

setCategory

public void setCategory(java.lang.String newCategory)
set a preset category in order to let all MathlibEvents generated by the parse methods belong to this preset category (not used anymore)

parseExpression

public static MathObject parseExpression(java.lang.String str)
                                  throws java.lang.IllegalArgumentException
static method which can parse an expression without using variable and without any assignments.
Parameters:
str - string to parse
Returns:
the mathobject which was identified

evaluateExpression

public java.lang.String evaluateExpression(java.lang.String str)
                                    throws java.lang.IllegalArgumentException
this method fully evaluates an expression and assignes the result variable. If you just want to evaluate an algebraic expression without assigning the result variable use parseExpression
Parameters:
str - string to be evaluated
See Also:
.

replaceSubstring

public static java.lang.String replaceSubstring(java.lang.String workStr,
                                                java.lang.String oldStr,
                                                java.lang.String newStr)
static method replacing a substring by another substring used by the preprocessor
Parameters:
workStr - string to work in
oldStr - substring to replace
newStr - string to replace with