KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.nfunk.jep.*;
10 /**
11  * Allows substution of a given variable with an expresion tree.
12  * Substitution is best done using the
13  * {@link XJep#substitute XJep.substitute} method.
14  * For example
15  * <pre>
16  * XJepI xjep = ...;
17  * Node node = xjep.parse("x^2+x");
18  * Node sub = xjep.parse("sin(y)");
19  * Node res = xjep.substitute(node,"x",sub,xjep);
20  * </pre>
21  * Will give the expresion "(sin(y))^2+sin(y)".
22  *
23  * @author Rich Morris
24  * Created on 16-Nov-2003
25  */

26 public class SubstitutionVisitor extends DoNothingVisitor {
27
28     private String JavaDoc names[];
29     private Node replacements[];
30     private XJep xjep;
31     public SubstitutionVisitor() {}
32
33     /**
34      * Substitutes all occurences of variable var with replacement.
35      * Does not do a DeepCopy.
36      * @param orig the expresion we wish to perform the substitution on
37      * @param name the name of the variable
38      * @param replacement the expression var is substituted for
39      * @return the tree with variable replace (does not do a DeepCopy)
40      * @throws ParseException
41      */

42     public Node substitute(Node orig,String JavaDoc name,Node replacement,XJep xjep) throws ParseException
43     {
44         this.names = new String JavaDoc[]{name};
45         this.replacements = new Node[]{replacement};
46         this.xjep=xjep;
47         Node res = (Node) orig.jjtAccept(this,null);
48         return res;
49     }
50
51     /**
52      * Substitutes all occurences of a set of variable var with a set of replacements.
53      * Does not do a DeepCopy.
54      * @param orig the expresion we wish to perform the substitution on
55      * @param names the names of the variable
56      * @param replacements the expression var is substituted for
57      * @return the tree with variable replace (does not do a DeepCopy)
58      * @throws ParseException
59      */

60     public Node substitute(Node orig,String JavaDoc names[],Node replacements[],XJep xjep) throws ParseException
61     {
62         this.names = names;
63         this.replacements = replacements;
64         this.xjep=xjep;
65         Node res = (Node) orig.jjtAccept(this,null);
66         return res;
67     }
68
69     public Object JavaDoc visit(ASTVarNode node, Object JavaDoc data) throws ParseException
70     {
71         for(int i=0;i<names.length;++i)
72         {
73             if(names[i].equals(node.getName()))
74                 return xjep.deepCopy(replacements[i]);
75         }
76         if(node.getVar().isConstant())
77             return xjep.getNodeFactory().buildVariableNode(xjep.getSymbolTable().getVar(node.getName()));
78             
79         throw new ParseException("No substitution specified for variable "+node.getName());
80     }
81 }
82
Popular Tags