Math2mat
|
00001 /****************************************************************************** 00002 * IfThenElse 00003 ****************************************************************************** 00004 * Auteur : Trolliet Gregory 00005 * Date : 7 avr. 2009 00006 * Description : 00007 ******************************************************************************/ 00008 package m2m.backend.structure; 00009 00010 import java.util.Vector; 00011 00012 import m2m.backend.buildingblocks.BuildingBlock; 00013 00014 import org.w3c.dom.Document; 00015 import org.w3c.dom.Node; 00016 00024 public class Switch extends Function { 00025 00029 private Vector<Case> cases; 00033 private Variable switchVar; 00034 00038 public Switch() { 00039 this.setName("switch"); 00040 this.cases = new Vector<Case>(); 00041 } 00042 00043 00044 public void modifyNumType(BuildingBlock.NumType type) { 00045 super.modifyNumType(type); 00046 for(Element e: cases) { 00047 e.modifyNumType(type); 00048 } 00049 } 00050 00051 public String getXmlTagName() { 00052 return "Switch"; 00053 } 00054 00055 00056 public boolean insideFromXml(org.w3c.dom.Element el,Function newFunc) { 00057 super.insideFromXml(el,newFunc); 00058 00059 Node node = el.getFirstChild(); 00060 while(node!=null) { 00061 if (node.getNodeName().equalsIgnoreCase("Cases")) { 00062 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node; 00063 Node nodeIn = inputEl.getFirstChild().getNextSibling(); 00064 while(nodeIn!=null) { 00065 if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) { 00066 Element element=getElement((org.w3c.dom.Element)nodeIn,newFunc); 00067 if (element!=null) 00068 cases.add((Case)element); 00069 } 00070 nodeIn=nodeIn.getNextSibling(); 00071 } 00072 } 00073 00074 if (node.getNodeName().equalsIgnoreCase("SwitchVar")) { 00075 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node; 00076 Node nodeIn = inputEl.getFirstChild().getNextSibling(); 00077 while(nodeIn!=null) { 00078 if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) { 00079 Element element=getElement((org.w3c.dom.Element)nodeIn,newFunc); 00080 if (element!=null) 00081 switchVar=(Variable)element; 00082 } 00083 nodeIn=nodeIn.getNextSibling(); 00084 } 00085 } 00086 00087 node=node.getNextSibling(); 00088 } 00089 return false; 00090 } 00091 00092 public void insideToXml(org.w3c.dom.Element el,Document dom,boolean shortRef) { 00093 // This function replaces the one of Function, so we call Element instead 00094 // of function 00095 // superInsideToXml(el,dom,shortRef); 00096 super.insideToXml(el, dom, shortRef); 00097 00098 org.w3c.dom.Element casesEl=dom.createElement("Cases"); 00099 for(Element e: inputVars) { 00100 casesEl.appendChild(e.toXml(dom,false)); 00101 } 00102 el.appendChild(casesEl); 00103 00104 org.w3c.dom.Element switchVarEl=dom.createElement("SwitchVar"); 00105 switchVarEl.appendChild(switchVar.toXml(dom,false)); 00106 el.appendChild(switchVarEl); 00107 } 00108 00113 public void setSwitchVar(Variable var) { 00114 this.switchVar = var; 00115 } 00116 00121 public Variable getSwitchVar() { 00122 return this.switchVar; 00123 } 00124 00130 public void addCase(Vector<Variable> cond, Vector<Element> body) { 00131 Case c = new Case(); 00132 c.setBody(body); 00133 c.setCond(cond); 00134 this.cases.add(c); 00135 } 00136 00141 public void addCase(Case cas) { 00142 this.cases.add(cas); 00143 } 00144 00149 public void setCases(Vector<Case> cases) { 00150 this.cases = cases; 00151 } 00152 00157 public Vector<Case> getCases() { 00158 return this.cases; 00159 } 00160 00166 public Case getCaseAt(int ind) { 00167 if (ind < 0 || ind >= this.cases.size()) { 00168 return null; 00169 } 00170 return this.cases.elementAt(ind); 00171 } 00172 00177 public int getSize() { 00178 return this.cases.size(); 00179 } 00180 00181 @Override 00182 public String toString(int level) { 00183 String s = new String(); 00184 s += tab(level) + "[Switch]\tName: " + this.name; 00185 if (this.switchVar != null) { 00186 s += "\n" + tab(level + 1) + "Switch Var:"; 00187 s += "\n" + this.switchVar.toString(level + 2); 00188 } 00189 s += "\n" + tab(level + 1) + ">>===CASES===<<"; 00190 for (Case cas : this.cases) { 00191 s += "\n" + cas.toString(level + 2); 00192 } 00193 return s; 00194 } 00195 00196 @Override 00197 public String toSchematic(int level, String prefix, 00198 String boxFormat, String color) { 00199 String s = new String(); 00200 int cpt = 0; 00201 s += tab(level) + prefix + "[shape = " + boxFormat + ", label = \"" 00202 + this.getName() + "\" color=\"" + color + "\" style=\"filled\"]"; 00203 if (this.switchVar != null) { 00204 s += "\n" + tab(level) + prefix + "_switchVar [shape = " 00205 + GraphParams.inputShape + ", label = \"" 00206 + this.switchVar.getName() + "\" color=\"" 00207 + GraphParams.inputColor + "\" style=\"filled\"]"; 00208 s += "\n" + tab(level) + prefix + "->" + prefix 00209 + "_switchVar [label = \"Sel\"]"; 00210 } 00211 for (Case c : this.cases) { 00212 s += "\n" + c.toSchematic(level, prefix + "_case_" + cpt, boxFormat, color); 00213 s += "\n" + tab(level) + prefix + " -> " + prefix + "_case_" + cpt; 00214 cpt++; 00215 } 00216 return s; 00217 } 00218 }