Math2mat
|
00001 /****************************************************************************** 00002 * LoopFor 00003 ****************************************************************************** 00004 * Auteur : Trolliet Gregory 00005 * Date : 4 mars 2009 00006 * Description : 00007 ******************************************************************************/ 00008 package m2m.backend.structure; 00009 00010 import java.util.Vector; 00011 00012 import org.w3c.dom.Document; 00013 import org.w3c.dom.Node; 00014 00022 public class LoopFor extends Function { 00023 00027 private SimpleVariable start; 00031 private SimpleVariable incr; 00035 private SimpleVariable end; 00039 private Vector<SimpleVariable> loopVariables; 00043 private Operation iterOperation; 00044 00045 00046 public String getXmlTagName() { 00047 return "LoopFor"; 00048 } 00049 00050 00051 public boolean insideFromXml(org.w3c.dom.Element el,Function newFunc) { 00052 super.insideFromXml(el,newFunc); 00053 00054 Node node = el.getFirstChild(); 00055 while(node!=null) { 00056 if (node.getNodeName().equalsIgnoreCase("Start")) { 00057 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node; 00058 Node nodeIn = inputEl.getFirstChild().getNextSibling(); 00059 while(nodeIn!=null) { 00060 if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) { 00061 Element element=getElement((org.w3c.dom.Element)nodeIn,this); 00062 if (element!=null) 00063 start=(SimpleVariable)element; 00064 } 00065 nodeIn=nodeIn.getNextSibling(); 00066 } 00067 } 00068 00069 if (node.getNodeName().equalsIgnoreCase("Incr")) { 00070 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node; 00071 Node nodeIn = inputEl.getFirstChild().getNextSibling(); 00072 while(nodeIn!=null) { 00073 if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) { 00074 Element element=getElement((org.w3c.dom.Element)nodeIn,this); 00075 if (element!=null) 00076 incr=(SimpleVariable)element; 00077 } 00078 nodeIn=nodeIn.getNextSibling(); 00079 } 00080 } 00081 00082 if (node.getNodeName().equalsIgnoreCase("End")) { 00083 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node; 00084 Node nodeIn = inputEl.getFirstChild().getNextSibling(); 00085 while(nodeIn!=null) { 00086 if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) { 00087 Element element=getElement((org.w3c.dom.Element)nodeIn,this); 00088 if (element!=null) 00089 end=(SimpleVariable)element; 00090 } 00091 nodeIn=nodeIn.getNextSibling(); 00092 } 00093 } 00094 00095 if (node.getNodeName().equalsIgnoreCase("IterOp")) { 00096 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node; 00097 Node nodeIn = inputEl.getFirstChild().getNextSibling(); 00098 while(nodeIn!=null) { 00099 if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) { 00100 Element element=getElement((org.w3c.dom.Element)nodeIn,this); 00101 if (element!=null) 00102 iterOperation=(Operation)element; 00103 } 00104 nodeIn=nodeIn.getNextSibling(); 00105 } 00106 } 00107 00108 node=node.getNextSibling(); 00109 } 00110 return false; 00111 } 00112 00113 public void insideToXml(org.w3c.dom.Element el,Document dom,boolean shortRef) { 00114 // This function replaces the one of Function, so we call Element instead 00115 // of function 00116 // superInsideToXml(el,dom,shortRef); 00117 super.insideToXml(el, dom, shortRef); 00118 00119 org.w3c.dom.Element startEl=dom.createElement("Start"); 00120 startEl.appendChild(start.toXml(dom,false)); 00121 el.appendChild(startEl); 00122 org.w3c.dom.Element incrEl=dom.createElement("Incr"); 00123 incrEl.appendChild(incr.toXml(dom,false)); 00124 el.appendChild(incrEl); 00125 org.w3c.dom.Element endEl=dom.createElement("End"); 00126 endEl.appendChild(end.toXml(dom,false)); 00127 el.appendChild(endEl); 00128 org.w3c.dom.Element iterEl=dom.createElement("IterOp"); 00129 iterEl.appendChild(iterOperation.toXml(dom,false)); 00130 el.appendChild(iterEl); 00131 } 00132 00136 public LoopFor() { 00137 this.setName("loopfor"); 00138 this.start = new SimpleVariable("", 1.0); 00139 this.incr = new SimpleVariable("", 1.0); 00140 this.end = new SimpleVariable("", 1.0); 00141 } 00142 00143 00148 @Override 00149 public Element copy(Function newFunc) { 00150 LoopFor newLoop=new LoopFor(); 00151 super.copyTo(newLoop,newFunc); 00152 newLoop.start = (SimpleVariable)this.start.copy(newFunc); 00153 newLoop.incr = (SimpleVariable)this.incr.copy(newFunc); 00154 newLoop.end = (SimpleVariable)this.end.copy(newFunc); 00155 return newLoop; 00156 } 00157 00162 public void setStart(SimpleVariable start) { 00163 this.start = start; 00164 } 00165 00170 public SimpleVariable getStart() { 00171 return this.start; 00172 } 00173 00178 public void setIncr(SimpleVariable incr) { 00179 this.incr = incr; 00180 } 00181 00186 public SimpleVariable getIncr() { 00187 return this.incr; 00188 } 00189 00194 public void setEnd(SimpleVariable end) { 00195 this.end = end; 00196 } 00197 00202 public SimpleVariable getEnd() { 00203 return this.end; 00204 } 00205 00210 public void setLoopVariables(Vector<SimpleVariable> vector) { 00211 loopVariables = vector; 00212 } 00213 00218 public Vector<SimpleVariable> getLoopVariables() { 00219 return loopVariables; 00220 } 00221 00226 public void setIterOperation(Operation op) { 00227 iterOperation = op; 00228 } 00229 00234 public Operation getIterOperation() { 00235 return iterOperation; 00236 } 00237 00238 @Override 00239 public String toString(int level) { 00240 String s = new String(); 00241 s += tab(level) + "[LoopFor]\tName: " + this.name; 00242 s += "\tInterval: "; 00243 if (this.start.getType().equals("const")) { 00244 s += this.start.getVal(); 00245 } else { 00246 s += this.start.getName(); 00247 } 00248 s += " -> "; 00249 if (this.incr.getType().equals("const")) { 00250 s += this.incr.getVal(); 00251 } else { 00252 s += this.incr.getName(); 00253 } 00254 s += " -> "; 00255 if (this.end.getType().equals("const")) { 00256 s += this.end.getVal(); 00257 } else { 00258 s += this.end.getName(); 00259 } 00260 s += "\n" + this.toStringParam(level + 1); 00261 s += "\n" + tab(level) + ">>==========<<"; 00262 for (Element el : this.body) { 00263 s += "\n" + el.toString(level + 1); 00264 } 00265 return s; 00266 } 00267 00268 00269 @Override 00270 public String toSchematic(int level, String prefix, String boxFormat, String color) { 00271 String s = new String(); 00272 s += super.toSchematic(level, prefix, "box", color); 00273 s += this.start.toSchematic(level, prefix + "_start", "box", GraphParams.loopIndColor); 00274 s += "\n" + tab(level) + prefix + " -> " + prefix + "_start [label = \"start\"]"; 00275 s += this.incr.toSchematic(level, prefix + "_incr", "box", GraphParams.loopIndColor); 00276 s += "\n" + tab(level) + prefix + " -> " + prefix + "_incr [label = \"incr\"]"; 00277 s += this.end.toSchematic(level, prefix + "_end", "box", GraphParams.loopIndColor); 00278 s += "\n" + tab(level) + prefix + " -> " + prefix + "_end [label = \"end\"]"; 00279 return s; 00280 } 00281 00282 @Override 00283 public String toOctave(int level) { 00284 String sOctave = new String(); 00285 sOctave += tab(level) + "for "; 00286 sOctave += this.internalVars.firstElement().getName() + "="; 00287 sOctave += ((this.start.getType().equals("const")) ? this.start.getVal() : this.start.getName()); 00288 sOctave += ":" + ((this.incr.getType().equals("const")) ? this.incr.getVal() : this.incr.getName()); 00289 sOctave += ":" + ((this.end.getType().equals("const")) ? this.end.getVal() : this.end.getName()) + "\n"; 00290 /*if (this.output.size() == 1) { 00291 if (this.output.firstElement() instanceof SimpleVariable) { 00292 sOctave += this.output.firstElement().getName(); 00293 } 00294 } else { 00295 sOctave += "["; 00296 boolean first = true; 00297 for (Element e : this.output) { 00298 if (e instanceof SimpleVariable) { 00299 sOctave += ((first) ? "" : ", ") + e.getName(); 00300 first = false; 00301 } 00302 } 00303 sOctave += "]"; 00304 } 00305 sOctave += "=" + this.getName() + "("; 00306 boolean first = true; 00307 for (Element e : this.input) { 00308 if (e instanceof SimpleVariable) { 00309 sOctave += ((first) ? "" : ", ") + e.getName(); 00310 first = false; 00311 } 00312 } 00313 sOctave += ")\n";*/ 00314 for (Element e : this.body) { 00315 sOctave += e.toOctave(level + 1) + "\n"; 00316 } 00317 sOctave += tab(level) + "endfor;"; 00318 return sOctave; 00319 } 00320 00321 00327 public SimpleVariable getLoopVariable(SimpleVariable var) 00328 { 00329 for(Element e : body) { 00330 if(e instanceof IfThenElse && e.getName().contains("loopfor")) 00331 if(((IfThenElse)e).getOutput().firstElement().equals(var)) 00332 return ((SimpleVariable)((Assignment)((IfThenElse)e).getBodyFalse().firstElement()).getInputAt(0)); 00333 } 00334 00335 return null; 00336 } 00337 00343 public SimpleVariable getInitVariable(SimpleVariable var) 00344 { 00345 for(Element e : body) { 00346 if(e instanceof IfThenElse && e.getName().contains("loopfor")) 00347 if(((IfThenElse)e).getOutput().firstElement().equals(var)) 00348 return ((SimpleVariable)((Assignment)((IfThenElse)e).getBodyTrue().firstElement()).getInputAt(0)); 00349 } 00350 00351 return null; 00352 } 00353 }