1 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 ; 16 17 24 public class MatrixEvaluator implements ParserVisitor 25 { 26 private Stack stack = new Stack (); 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 visit(SimpleNode node, Object data) { return null; } 36 public Object visit(ASTStart node, Object data) { return null; } 37 38 39 public Object visit(ASTConstant node, Object data) 40 { 41 return ((ASTMConstant) node).getMValue(); 42 } 43 44 public Object visit(ASTVarNode node, Object 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 return res; 56 } 57 } 58 59 60 public Object visit(ASTFunNode node, Object 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 ("Encountered an instance of SpecialEvaluationI"); 92 } 96 else if(pfmc instanceof Comparative) { 97 Object lhsval = (MatrixValueI) node.jjtGetChild(0).jjtAccept(this,data); 98 Object 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 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 |