org.lsmp.djep.xjep
Class XJep

java.lang.Object
  extended byorg.nfunk.jep.JEP
      extended byorg.lsmp.djep.xjep.XJep
Direct Known Subclasses:
DJep

public class XJep
extends JEP

An extended version of JEP adds various routines for working with trees. Has a NodeFactory, and OperatorSet, TreeUtils and Visitors DoNothingVisitor,

Author:
Rich Morris Created on 16-Nov-2003

Field Summary
protected  CommandVisitor commandv
           
protected  DeepCopyVisitor copier
           
protected  NodeFactory nf
          Creates new nodes
protected  PrintVisitor pv
           
protected  SimplificationVisitor simpv
           
protected  SubstitutionVisitor subv
           
protected  TreeUtils tu
          A few utility functions.
 
Fields inherited from class org.nfunk.jep.JEP
allowAssignment, allowUndeclared, errorList, ev, funTab, implicitMul, numberFactory, opSet, parser, symTab
 
Constructor Summary
  XJep()
          Create a new XJep will all the function of JEP plus printing and other features.
  XJep(JEP j)
          Conversion constructor.
protected XJep(XJep j)
          Copy constructions, reuses all the componants of argument.
 
Method Summary
 void addStandardConstants()
          Adds the constants pi and e to the parser.
 void addStandardFunctions()
          Adds the standard functions to the parser.
 java.lang.Object calcVarValue(java.lang.String name)
          Calculates the value for the variables equation and returns that value.
 Node continueParsing()
          Continue parsing without re-initilising the stream.
 Node deepCopy(Node node)
          Returns a deep copy of an expression tree.
 java.lang.Object evaluate(Node node)
          Evaluates a node.
 NodeFactory getNodeFactory()
          Returns the node factory, used for constructing trees of Nodes.
 PrintVisitor getPrintVisitor()
          Returns the PrintVisitor, used for printing equations.
 TreeUtils getTreeUtils()
          Returns the TreeUtilitities, used for examining properties of nodes.
 XJep newInstance()
          Creates a new instance of XJep with the same componants as this one.
 XJep newInstance(SymbolTable st)
          Creates a new instance of XJep with the same componants as this one and the specified symbol table.
 Node preprocess(Node node)
          Preprocesses an equation to allow the diff and eval operators to be used.
 void print(Node node)
          Prints the expresion tree on standard output.
 void print(Node node, java.io.PrintStream out)
          Prints the expresion tree on given stream.
 void println(Node node)
          Prints the expresion tree on standard output with newline at end.
 void println(Node node, java.io.PrintStream out)
          Prints the expresion tree on given stream with newline at end.
 void restartParser(java.io.Reader reader)
          Restarts the parser with the given Reader.
 void restartParser(java.lang.String str)
          Restarts the parser with the given string.
 Node simplify(Node node)
          Returns a simplification of an expression tree.
 Node substitute(Node orig, java.lang.String[] names, Node[] replacements)
          Substitute all occurences of a set of named variable with a set of expression tree.
 Node substitute(Node orig, java.lang.String name, Node replacement)
          Substitute all occurences of a named variable with an expression tree.
 java.lang.String toString(Node node)
          Returns a string representation of a expresion tree.
 
Methods inherited from class org.nfunk.jep.JEP
addComplex, addConstant, addFunction, addVariable, addVariable, addVariable, getAllowAssignment, getAllowUndeclared, getComplexValue, getErrorInfo, getFunctionTable, getImplicitMul, getNumberFactory, getOperatorSet, getParser, getSymbolTable, getTopNode, getTraverse, getValue, getValueAsObject, getVar, getVarValue, hasError, initFunTab, initSymTab, parse, parseExpression, removeFunction, removeVariable, setAllowAssignment, setAllowUndeclared, setImplicitMul, setTraverse, setVarValue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nf

protected NodeFactory nf
Creates new nodes


tu

protected TreeUtils tu
A few utility functions.


copier

protected DeepCopyVisitor copier

subv

protected SubstitutionVisitor subv

simpv

protected SimplificationVisitor simpv

commandv

protected CommandVisitor commandv

pv

protected PrintVisitor pv
Constructor Detail

XJep

public XJep()
Create a new XJep will all the function of JEP plus printing and other features.


XJep

protected XJep(XJep j)
Copy constructions, reuses all the componants of argument.


XJep

public XJep(JEP j)
Conversion constructor. Turns a JEP object into an XJep object.

Parameters:
j -
Method Detail

newInstance

public XJep newInstance()
Creates a new instance of XJep with the same componants as this one. Sub classes should overwrite this method to create objects of the correct type.


newInstance

public XJep newInstance(SymbolTable st)
Creates a new instance of XJep with the same componants as this one and the specified symbol table. Sub classes should overwrite this method to create objects of the correct type.


addStandardFunctions

public void addStandardFunctions()
Description copied from class: JEP
Adds the standard functions to the parser. If this function is not called before parsing an expression, functions such as sin() or cos() would produce an "Unrecognized function..." error. In most cases, this method should be called immediately after the JEP object is created.

Overrides:
addStandardFunctions in class JEP

addStandardConstants

public void addStandardConstants()
Description copied from class: JEP
Adds the constants pi and e to the parser. As addStandardFunctions(), this method should be called immediatly after the JEP object is created.

Overrides:
addStandardConstants in class JEP

deepCopy

public Node deepCopy(Node node)
              throws ParseException
Returns a deep copy of an expression tree.

Throws:
ParseException

evaluate

public java.lang.Object evaluate(Node node)
                          throws java.lang.Exception
Evaluates a node.

Overrides:
evaluate in class JEP
Parameters:
node - the top node of the tree representing the expression.
Returns:
The value of the expression
Throws:
java.lang.Exception - if for some reason the expression could not be evaluated

simplify

public Node simplify(Node node)
              throws ParseException
Returns a simplification of an expression tree.

Throws:
ParseException

preprocess

public Node preprocess(Node node)
                throws ParseException
Preprocesses an equation to allow the diff and eval operators to be used.

Throws:
ParseException

substitute

public Node substitute(Node orig,
                       java.lang.String name,
                       Node replacement)
                throws ParseException
Substitute all occurences of a named variable with an expression tree.

Throws:
ParseException

substitute

public Node substitute(Node orig,
                       java.lang.String[] names,
                       Node[] replacements)
                throws ParseException
Substitute all occurences of a set of named variable with a set of expression tree.

Throws:
ParseException

print

public void print(Node node)
Prints the expresion tree on standard output.


print

public void print(Node node,
                  java.io.PrintStream out)
Prints the expresion tree on given stream.


println

public void println(Node node)
Prints the expresion tree on standard output with newline at end.


println

public void println(Node node,
                    java.io.PrintStream out)
Prints the expresion tree on given stream with newline at end.


toString

public java.lang.String toString(Node node)
Returns a string representation of a expresion tree.


getNodeFactory

public NodeFactory getNodeFactory()
Returns the node factory, used for constructing trees of Nodes.


getTreeUtils

public TreeUtils getTreeUtils()
Returns the TreeUtilitities, used for examining properties of nodes.


getPrintVisitor

public PrintVisitor getPrintVisitor()
Returns the PrintVisitor, used for printing equations.


calcVarValue

public java.lang.Object calcVarValue(java.lang.String name)
                              throws java.lang.Exception
Calculates the value for the variables equation and returns that value. If the variable does not have an equation just return its value.

Throws:
java.lang.Exception

continueParsing

public Node continueParsing()
                     throws ParseException
Continue parsing without re-initilising the stream. Allows renetrancy of parser so that strings like "x=1; y=2; z=3;" can be parsed. When a semi colon is encountered parsing finishes leaving the rest of the string unparsed. Parsing can be resumed from the current position by using this method. For example
 XJep j = new XJep();
 j.restartParser("x=1;y=2; z=3;");
 Node node;
 try {
 while((node = j.continueParsing())!=null) {
    j.println(node);
 } }catch(ParseException e) {}
 

Returns:
top node of equation parsed to date or null if empty equation
Throws:
ParseException
See Also:
restartParser(java.lang.String)

restartParser

public void restartParser(java.lang.String str)
Restarts the parser with the given string.

Parameters:
str - String containing a sequence of equations separated by semi-colons.
See Also:
continueParsing()

restartParser

public void restartParser(java.io.Reader reader)
Restarts the parser with the given Reader.

Parameters:
reader - Reader from which equations separated by semi-colons will be read.
See Also:
continueParsing()


http://www.singularsys.com/jep Copyright © 2004 Singular Systems