KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* @author rich
2  * Created on 18-Jun-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
9 package org.lsmp.djep.xjep;
10 //import org.lsmp.djep.matrixParser.*;
11
import org.nfunk.jep.*;
12 import org.nfunk.jep.function.PostfixMathCommandI;
13
14 /**
15  * Executes commands like diff and eval embeded in expression trees.
16  * For example you could do
17  * <pre>eval(diff(x^3,x),x,2)</pre>
18  * to differentiate x^3 and then substitute x=2 to get the value 12.
19  * To use do
20  * <pre>
21  * JEP j = ...; Node in = ...;
22  * TreeUtils tu = new TreeUtils(j);
23  * CommandVisitor cv = new CommandVisitor(tu);
24  * Node out = cv.process(in);
25  * </pre>
26  * Commands to be executed must implement
27  * {@link org.lsmp.djep.xjep.CommandVisitorI CommandVisitorI} and {@link org.nfunk.jep.function.PostfixMathCommandI PostfixMathCommandI}.
28  * See {@link org.lsmp.djep.xjep.Eval Eval} for an example of this.
29  * See {@link org.nfunk.jep.ParserVisitor ParserVisitor} for details on the VisitorPattern.
30  * @author R Morris
31  * Created on 19-Jun-2003
32  */

33 public class CommandVisitor extends DoNothingVisitor
34 {
35   private XJep xjep;
36   /** private default constructor to prevent init without a tree utils
37    */

38     public CommandVisitor()
39   {
40   }
41   
42   /**
43    * Decends the tree processing all diff, eval and simplify options
44    */

45
46   public Node process(Node node,XJep xjep) throws ParseException
47   {
48     this.xjep=xjep;
49     Node res = (Node) node.jjtAccept(this,null);
50     return res;
51   }
52
53   public Object JavaDoc visit(ASTFunNode node, Object JavaDoc data) throws ParseException
54   {
55     Node children[] = acceptChildrenAsArray(node,data);
56
57     PostfixMathCommandI pfmc = node.getPFMC();
58     if(pfmc instanceof CommandVisitorI )
59     {
60         CommandVisitorI com = (CommandVisitorI) pfmc;
61         return com.process(node,children,xjep);
62     }
63     TreeUtils.copyChildrenIfNeeded(node,children);
64     return node;
65   }
66 }
67
Popular Tags