KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > xjep > XJep


1 /* @author rich
2  * Created on 16-Nov-2003
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.xjep;
9
10 import org.nfunk.jep.*;
11 import org.lsmp.djep.xjep.function.*;
12 import java.util.*;
13 import java.io.PrintStream;
14 import java.io.Reader;
15 /**
16  * An extended version of JEP adds various routines for working with trees.
17  * Has a NodeFactory, and OperatorSet, TreeUtils
18  * and Visitors DoNothingVisitor,
19  * @author Rich Morris
20  * Created on 16-Nov-2003
21  */

22 public class XJep extends JEP {
23     /** Creates new nodes */
24     protected NodeFactory nf = null;
25     /** A few utility functions. */
26     protected TreeUtils tu = null;
27     protected DeepCopyVisitor copier = null;
28     protected SubstitutionVisitor subv = null;
29     protected SimplificationVisitor simpv = null;
30     protected CommandVisitor commandv = null;
31     protected PrintVisitor pv = null;
32     private VariableFactory vf = new XVariableFactory();
33
34     /**
35      * Create a new XJep will all the function of JEP plus printing and other features.
36      */

37     public XJep()
38     {
39         this.symTab = new XSymbolTable(vf);
40
41         /* Creates new nodes */
42         nf = new NodeFactory();
43         /* Collects operators **/
44         opSet = new XOperatorSet();
45         /* A few utility functions. */
46         tu = new TreeUtils();
47         
48         copier = new DeepCopyVisitor();
49         subv = new SubstitutionVisitor();
50         ev = new XEvaluatorVisitor();
51         simpv = new SimplificationVisitor();
52         commandv = new CommandVisitor();
53         pv = new PrintVisitor();
54     }
55
56     /** Copy constructions, reuses all the componants of argument. */
57     protected XJep(XJep j)
58     {
59         super((JEP) j);
60         this.commandv=j.commandv;
61         this.copier=j.copier;
62         this.ev=j.ev;
63         this.nf=j.nf;
64         this.opSet=j.opSet;
65         this.pv=j.pv;
66         this.simpv=j.simpv;
67         this.subv=j.subv;
68         this.tu=j.tu;
69     }
70
71     private JEP ingrediant = null;
72     /** Conversion constructor.
73      * Turns a JEP object into an XJep object.
74      * @param j
75      */

76     public XJep(JEP j)
77     {
78         ingrediant=j;
79         /* Creates new nodes */
80         nf = new NodeFactory();
81         this.symTab = new XSymbolTable(vf);
82         this.funTab = j.getFunctionTable();
83         /* Collects operators **/
84         opSet = new XOperatorSet(j.getOperatorSet());
85         /* A few utility functions. */
86         tu = new TreeUtils();
87         copier = new DeepCopyVisitor();
88         subv = new SubstitutionVisitor();
89         ev = new XEvaluatorVisitor();
90         simpv = new SimplificationVisitor();
91         commandv = new CommandVisitor();
92         pv = new PrintVisitor();
93     }
94     /**
95      * Creates a new instance of XJep with the same componants as this one.
96      * Sub classes should overwrite this method to create objects of the correct type.
97      */

98     public XJep newInstance()
99     {
100         XJep newJep = new XJep(this);
101         return newJep;
102     }
103     /**
104      * Creates a new instance of XJep with the same componants as this one and the specified symbol table.
105      * Sub classes should overwrite this method to create objects of the correct type.
106      */

107     public XJep newInstance(SymbolTable st)
108     {
109         XJep newJep = new XJep(this);
110         newJep.symTab = st;
111         return newJep;
112     }
113
114     public void addStandardFunctions()
115     {
116         if(ingrediant!=null)
117         {
118             ingrediant.addStandardFunctions();
119         }
120         else super.addStandardFunctions();
121         addFunction("eval",new Eval());
122         addFunction("Sum",new Sum());
123         addFunction("Product",new Product());
124         addFunction("Min",new Min());
125         addFunction("Max",new Max());
126         addFunction("MinArg",new MinArg());
127         addFunction("MaxArg",new MaxArg());
128     }
129
130     public void addStandardConstants()
131     {
132         if(ingrediant!=null)
133         {
134             ingrediant.addStandardConstants();
135             for(Enumeration enum=ingrediant.getSymbolTable().elements();enum.hasMoreElements();)
136             {
137                 Variable var = (Variable) enum.nextElement();
138                 if(var.isConstant())
139                     this.symTab.addConstant(var.getName(),var.getValue());
140                 //else
141
// this.symTab.addVariable(var.getName(),var.getValue());
142
}
143         }
144         else super.addStandardConstants();
145     }
146
147     /** Returns a deep copy of an expression tree. */
148     public Node deepCopy(Node node) throws ParseException
149     {
150         return copier.deepCopy(node,this);
151     }
152     /** Evaluates a node. */
153     public Object evaluate(Node node) throws Exception
154     {
155         return ev.getValue(node,new Vector(),this.symTab);
156     }
157     /** Returns a simplification of an expression tree. */
158     public Node simplify(Node node) throws ParseException
159     {
160         return simpv.simplify(node,this);
161     }
162     /** Preprocesses an equation to allow the diff and eval operators to be used. */
163     public Node preprocess(Node node) throws ParseException
164     {
165         return commandv.process(node,this);
166     }
167     /** Substitute all occurences of a named variable with an expression tree. */
168     public Node substitute(Node orig,String name,Node replacement) throws ParseException
169     {
170         return subv.substitute(orig,name,replacement,this);
171     }
172     /** Substitute all occurences of a set of named variable with a set of expression tree. */
173     public Node substitute(Node orig,String names[],Node replacements[]) throws ParseException
174     {
175         return subv.substitute(orig,names,replacements,this);
176     }
177     /** Prints the expresion tree on standard output. */
178     public void print(Node node) { pv.print(node); }
179     /** Prints the expresion tree on given stream. */
180     public void print(Node node,PrintStream out) { pv.print(node,out); }
181     /** Prints the expresion tree on standard output with newline at end. */
182     public void println(Node node) { pv.println(node); }
183     /** Prints the expresion tree on given stream with newline at end. */
184     public void println(Node node,PrintStream out) { pv.println(node,out); }
185     /** Returns a string representation of a expresion tree. */
186     public String toString(Node node) { return pv.toString(node); }
187     /** Returns the node factory, used for constructing trees of Nodes. */
188     public NodeFactory getNodeFactory() {return nf;}
189     /** Returns the TreeUtilitities, used for examining properties of nodes. */
190     public TreeUtils getTreeUtils() { return tu; }
191 // public SimplificationVisitor getSimpV() { return simpv; }
192
/** Returns the PrintVisitor, used for printing equations. */
193     public PrintVisitor getPrintVisitor() { return pv; }
194
195     /**
196      * Calculates the value for the variables equation and returns that value.
197      * If the variable does not have an equation just return its value.
198      */

199     public Object calcVarValue(String name) throws Exception
200     {
201         XVariable xvar = (XVariable) getVar(name);
202         return xvar.calcValue(this);
203     }
204
205     /**
206      * Continue parsing without re-initilising the stream.
207      * Allows renetrancy of parser so that strings like
208      * "x=1; y=2; z=3;" can be parsed.
209      * When a semi colon is encountered parsing finishes leaving the rest of the string unparsed.
210      * Parsing can be resumed from the current position by using this method.
211      * For example
212      * <pre>
213      * XJep j = new XJep();
214      * j.restartParser("x=1;y=2; z=3;");
215      * Node node;
216      * try {
217      * while((node = j.continueParsing())!=null) {
218      * j.println(node);
219      * } }catch(ParseException e) {}
220      * </pre>
221      * @return top node of equation parsed to date or null if empty equation
222      * @throws ParseException
223      * @see #restartParser
224      */

225     public Node continueParsing() throws ParseException {
226         return parser.continueParse();
227     }
228
229     /**
230      * Restarts the parser with the given string.
231      * @param str String containing a sequence of equations separated by semi-colons.
232      * @see #continueParsing
233      */

234     public void restartParser(String str) {
235         parser.restart(new java.io.StringReader(str), this);
236     }
237
238     /**
239      * Restarts the parser with the given Reader.
240      * @param reader Reader from which equations separated by semi-colons will be read.
241      * @see #continueParsing
242      */

243     public void restartParser(Reader reader) {
244         parser.restart(reader, this);
245     }
246
247 }
248
Popular Tags