KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > matrixJep > MatrixNodeFactory


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.matrixJep;
9 import org.nfunk.jep.*;
10 import org.nfunk.jep.function.*;
11 import org.lsmp.djep.matrixJep.nodeTypes.*;
12 import org.lsmp.djep.vectorJep.*;
13 import org.lsmp.djep.vectorJep.function.*;
14 import org.lsmp.djep.xjep.*;
15
16 /**
17  * This class is used to create nodes of specified types.
18  * It can be subclassed to change the nature of how nodes
19  * are constructed. Generally there are two methods for creating
20  * nodes, methods which take an existing node and methods which
21  * take the components.
22  *
23  * @author Rich Morris
24  * Created on 16-Nov-2003
25  */

26 public class MatrixNodeFactory extends NodeFactory {
27
28     public MatrixNodeFactory()
29     {
30     }
31     
32     /** Creates an ASTConstant node with specified value. **/
33     public ASTConstant buildConstantNode(Object JavaDoc value) throws ParseException
34     {
35         ASTMConstant node = new ASTMConstant(ParserTreeConstants.JJTCONSTANT);
36         node.setValue(value);
37         return node;
38     }
39
40     /** Creates a ASTVariable node with specified value. **/
41     public ASTVarNode buildVariableNode(Variable var) throws ParseException
42     {
43         ASTMVarNode node = new ASTMVarNode(ParserTreeConstants.JJTVARNODE);
44         node.setVar(var);
45         return node;
46     }
47     
48
49     /**
50      * Builds a function with n arguments
51      * @param name of function.
52      * @param pfmc PostfixMathCommand for function.
53      * @param arguments the arguments to the function.
54      * @return top Node of expression
55      */

56
57     public ASTFunNode buildFunctionNode(String JavaDoc name,PostfixMathCommandI pfmc,Node[] arguments) throws ParseException
58     {
59         ASTMFunNode res = new ASTMFunNode(ParserTreeConstants.JJTFUNNODE);
60         res.setFunction(name,pfmc);
61         copyChildren(res,arguments);
62         res.setDim(calcDim(name,pfmc,arguments));
63         return res;
64     }
65
66     /** Calculates the dimension of node using the dimensions
67      * of the children. Does not recurese down the tree.
68      */

69     public Dimensions calcDim(String JavaDoc name,PostfixMathCommandI pfmc,Node arguments[])
70         throws ParseException
71     {
72         MatrixNodeI children[] = new MatrixNodeI[arguments.length];
73         for(int i=0;i<arguments.length;++i)
74             children[i] = (MatrixNodeI) arguments[i];
75
76         if(pfmc instanceof BinaryOperatorI)
77         {
78             if(children.length!=2) throw new ParseException("Operator "+name+" must have two elements, it has "+children.length);
79             BinaryOperatorI bin = (BinaryOperatorI) pfmc;
80             Dimensions dim = bin.calcDim(children[0].getDim(),children[1].getDim());
81             return dim;
82         }
83         else if(pfmc instanceof UnaryOperatorI)
84         {
85             if(children.length!=1) throw new ParseException("Operator "+name+" must have one elements, it has "+children.length);
86             UnaryOperatorI uni = (UnaryOperatorI) pfmc;
87             Dimensions dim = uni.calcDim(children[0].getDim());
88             return dim;
89         }
90         else if(pfmc instanceof NaryOperatorI)
91         {
92             Dimensions dims[] = new Dimensions[children.length];
93             for(int i=0;i<children.length;++i)
94                 dims[i]=children[i].getDim();
95             //if(arguments.length!=1) throw new ParseException("Operator "+op.getName()+" must have one elements, it has "+arguments.length);
96
NaryOperatorI uni = (NaryOperatorI) pfmc;
97             Dimensions dim = uni.calcDim(dims);
98             return dim;
99         }
100         else
101         {
102             return Dimensions.ONE;
103     // System.out.println("Warning: assuming 1 for dimensons of "+node.getName());
104
}
105     }
106
107     /** Calculates the dimension of node using the dimensions
108      * of the children.
109      */

110     public Dimensions calcDim(Operator op,Node arguments[]) throws ParseException
111     {
112         return calcDim(op.getName(),op.getPFMC(),arguments);
113     }
114     
115     /**
116      * Builds a function with n arguments
117      * @param node the properties (name and pfmc) of this node will be copied.
118      * @param arguments the arguments to the function.
119      * @return top Node of expression
120      *
121      * @since 2.3.3 if possible use dimension of existing node. (Needed when deep copying MList functions)
122      */

123     public ASTFunNode buildFunctionNode(ASTFunNode node,Node[] children) throws ParseException
124     {
125         if(node instanceof ASTMFunNode)
126         {
127             if(node.isOperator())
128                 return buildOperatorNode(node.getOperator(),children,
129                 ((ASTMFunNode) node).getDim());
130             ASTMFunNode res = new ASTMFunNode(ParserTreeConstants.JJTFUNNODE);
131             res.setFunction(node.getName(),node.getPFMC());
132             copyChildren(res,children);
133             res.setDim(((ASTMFunNode) node).getDim());
134             return res;
135         }
136         //MatrixNodeI children[] = (MatrixNodeI []) arguments;
137
if(node.isOperator())
138             return buildOperatorNode(node.getOperator(),children);
139         ASTMFunNode res = new ASTMFunNode(ParserTreeConstants.JJTFUNNODE);
140         res.setFunction(node.getName(),node.getPFMC());
141         copyChildren(res,children);
142         res.setDim(calcDim(node.getName(),node.getPFMC(),children));
143         return res;
144     }
145     
146     
147     /**
148      * Builds a operator node with n arguments
149      * @param name of function.
150      * @param pfmc PostfixMathCommand for function.
151      * @param arguments the arguments to the function.
152      * @return top Node of expression
153      */

154
155     public ASTFunNode buildOperatorNode(Operator op,Node[] arguments) throws ParseException
156     {
157             
158         ASTMFunNode res = new ASTMFunNode(ParserTreeConstants.JJTFUNNODE);
159         res.setOperator(op);
160         copyChildren(res,arguments);
161         res.setDim(calcDim(op,arguments));
162         return res;
163     }
164
165     /** create a function node with a known dimension */
166     public ASTFunNode buildOperatorNode(Operator op,Node[] arguments,Dimensions dim)
167     {
168         ASTMFunNode res = new ASTMFunNode(ParserTreeConstants.JJTFUNNODE);
169         res.setOperator(op);
170         res.setDim(dim);
171         copyChildren(res,arguments);
172         return res;
173     }
174
175     /**
176      * Creates an operator node, but don't fill in the children or calculate
177      * its dimension.
178      */

179     public ASTFunNode buildUnfinishedOperatorNode(Operator op)
180     {
181         ASTMFunNode res = new ASTMFunNode(ParserTreeConstants.JJTFUNNODE);
182         res.setOperator(op);
183         return res;
184     }
185 }
186
Popular Tags