KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* @author rich
2  * Created on 30-Oct-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.lsmp.djep.matrixJep.nodeTypes.*;
11 import org.lsmp.djep.vectorJep.function.*;
12 import org.lsmp.djep.vectorJep.values.*;
13 import org.lsmp.djep.vectorJep.Dimensions;
14 import org.nfunk.jep.function.*;
15 import java.util.Stack JavaDoc;
16
17 /**
18  * This visitor evaluates a the tree representing the equation.
19  *
20  * @author Rich Morris
21  * Created on 30-Oct-2003
22  * @since 2.3.2 Hack so comparitive operations work with vectors and matricies.
23  */

24 public class MatrixEvaluator implements ParserVisitor
25 {
26 // private DimensionCalculator dimCalc;
27
private Stack JavaDoc stack = new Stack JavaDoc();
28     private MatrixJep mjep;
29     public MatrixValueI evaluate(MatrixNodeI node,MatrixJep mjep) throws ParseException
30     {
31         this.mjep=mjep;
32         return (MatrixValueI) node.jjtAccept(this,null);
33     }
34
35     public Object JavaDoc visit(SimpleNode node, Object JavaDoc data) { return null; }
36     public Object JavaDoc visit(ASTStart node, Object JavaDoc data) { return null; }
37
38     /** constants **/
39     public Object JavaDoc visit(ASTConstant node, Object JavaDoc data)
40     {
41         return ((ASTMConstant) node).getMValue();
42     }
43     /** multidimensions differentiable variables */
44     public Object JavaDoc visit(ASTVarNode node, Object JavaDoc data) throws ParseException
45     {
46         MatrixVariableI var = (MatrixVariableI) node.getVar();
47         if(var.hasValidValue())
48             return var.getMValue();
49         else
50         {
51             MatrixValueI res = (MatrixValueI) var.getEquation().jjtAccept(this,data);
52             var.setMValue(res);
53 // val.setEles(res);
54
// var.setValidValue(true);
55
return res;
56         }
57     }
58     
59     /** other functions **/
60     public Object JavaDoc visit(ASTFunNode node, Object JavaDoc data) throws ParseException
61     {
62         MatrixNodeI mnode = (MatrixNodeI) node;
63         PostfixMathCommandI pfmc = node.getPFMC();
64         if(pfmc instanceof MatrixSpecialEvaluationI)
65         {
66             MatrixSpecialEvaluationI se = (MatrixSpecialEvaluationI) pfmc;
67             return se.evaluate(mnode,this,mjep);
68         }
69         else if(pfmc instanceof BinaryOperatorI)
70         {
71             BinaryOperatorI bin = (BinaryOperatorI) pfmc;
72             MatrixValueI lhsval = (MatrixValueI) node.jjtGetChild(0).jjtAccept(this,data);
73             MatrixValueI rhsval = (MatrixValueI) node.jjtGetChild(1).jjtAccept(this,data);
74             return bin.calcValue(mnode.getMValue(),lhsval,rhsval);
75         }
76         else if(pfmc instanceof UnaryOperatorI)
77         {
78             UnaryOperatorI uni = (UnaryOperatorI) pfmc;
79             MatrixValueI val = (MatrixValueI) node.jjtGetChild(0).jjtAccept(this,data);
80             return uni.calcValue(mnode.getMValue(),val);
81         }
82         else if(pfmc instanceof NaryOperatorI)
83         {
84             NaryOperatorI uni = (NaryOperatorI) pfmc;
85             MatrixValueI results[] = new MatrixValueI[node.jjtGetNumChildren()];
86             for(int i=0;i<results.length;++i)
87                 results[i] = (MatrixValueI) node.jjtGetChild(i).jjtAccept(this,data);
88             return uni.calcValue(mnode.getMValue(),results);
89         }
90         else if (pfmc instanceof SpecialEvaluationI) {
91             throw new IllegalArgumentException JavaDoc("Encountered an instance of SpecialEvaluationI");
92 // ((SpecialEvaluationI) node.getPFMC()).evaluate(
93
// node,data,this,stack,mjep.getSymbolTable());
94
// mnode.getMValue().setEle(0,stack.peek());
95
}
96         else if(pfmc instanceof Comparative) {
97             Object JavaDoc lhsval = (MatrixValueI) node.jjtGetChild(0).jjtAccept(this,data);
98             Object JavaDoc rhsval = (MatrixValueI) node.jjtGetChild(1).jjtAccept(this,data);
99             stack.push(lhsval);
100             stack.push(rhsval);
101             pfmc.setCurNumberOfParameters(2);
102             pfmc.run(stack);
103             mnode.getMValue().setEle(0,stack.pop());
104             return mnode.getMValue();
105         }
106
107         // not a clever op use old style call
108
// assumes
109
int num = node.jjtGetNumChildren();
110         for(int i=0;i<num;++i)
111         {
112             MatrixValueI vec = (MatrixValueI) node.jjtGetChild(i).jjtAccept(this,data);
113             if(!vec.getDim().equals(Dimensions.ONE))
114                 throw new ParseException("Arguments of "+node.getName()+" must be scalers");
115             stack.push(vec.getEle(0));
116         }
117         pfmc.setCurNumberOfParameters(num);
118         pfmc.run(stack);
119         mnode.getMValue().setEle(0,stack.pop());
120         return mnode.getMValue();
121     }
122 }
123
Popular Tags