Math2mat
|
00001 /* 00002 * To change this template, choose Tools | Templates 00003 * and open the template in the editor. 00004 */ 00005 package m2m.backend.structure; 00006 00007 import m2m.backend.utils.XMLUtils; 00008 00009 import org.w3c.dom.Document; 00010 import org.w3c.dom.Node; 00011 00016 public class Multiplexer extends Operation { 00017 00018 private int size; 00019 private Variable sel; 00020 00021 public Multiplexer() { 00022 super("mux"); 00023 } 00024 00025 public String getXmlTagName() { 00026 return "Multiplexer"; 00027 } 00028 00029 00030 public boolean insideFromXml(org.w3c.dom.Element el,Function newFunc) { 00031 super.insideFromXml(el,newFunc); 00032 size=XMLUtils.getIntValue(el,"Size",0); 00033 00034 Node node = el.getFirstChild(); 00035 while(node!=null) { 00036 if (node.getNodeName().equalsIgnoreCase("Sel")) { 00037 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node; 00038 Node nodeIn = inputEl.getFirstChild().getNextSibling(); 00039 while(nodeIn!=null) { 00040 if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) { 00041 Element element=getElement((org.w3c.dom.Element)nodeIn,newFunc); 00042 if (element!=null) 00043 sel=(Variable)element; 00044 } 00045 nodeIn=nodeIn.getNextSibling(); 00046 } 00047 } 00048 node=node.getNextSibling(); 00049 } 00050 return false; 00051 } 00052 00053 public void insideToXml(org.w3c.dom.Element el,Document dom,boolean shortRef) { 00054 // This function replaces the one of Function, so we call Element instead 00055 // of function 00056 // superInsideToXml(el,dom,shortRef); 00057 super.insideToXml(el, dom, shortRef); 00058 el.appendChild(XMLUtils.createIntElement(dom,"Size",size)); 00059 00060 if (sel!=null) { 00061 org.w3c.dom.Element selEl=dom.createElement("Sel"); 00062 selEl.appendChild(sel.toXml(dom,true)); 00063 el.appendChild(selEl); 00064 } 00065 } 00066 00067 @Override 00068 public void copyTo(Element e,Function newFunc) { 00069 Multiplexer mult=(Multiplexer)e; 00070 mult.size=this.size; 00071 mult.sel=(Variable)this.sel.copy(newFunc); 00072 super.copyTo(mult,newFunc); 00073 } 00074 00075 @Override 00076 public Element copy(Function newFunc) { 00077 Multiplexer mult=new Multiplexer(); 00078 this.copyTo(mult,newFunc); 00079 return mult; 00080 } 00081 00082 public void setSize(int s) { 00083 this.size = s; 00084 } 00085 00086 public int getSize() { 00087 return this.size; 00088 } 00089 00090 public void setSel(Variable var) { 00091 this.sel = var; 00092 } 00093 00094 public Variable getSel() { 00095 return this.sel; 00096 } 00097 00098 00099 @Override 00100 public String toString(int level) { 00101 String s = new String(); 00102 s += tab(level) + "[" + this.opName + "]\tName: " + this.name; 00103 s += "\n" + tab(level + 1) + "Size: " + this.size; 00104 s += "\n" + tab(level + 1) + "Select: "; 00105 if (this.sel != null) { 00106 s += "\n" + this.sel.toString(level + 2); 00107 } 00108 s += "\n" + this.toStringParam(level + 1); 00109 return s; 00110 } 00111 00112 @Override 00113 public String toSchematic(int level, String prefix, String boxFormat, String color) { 00114 String s = new String(); 00115 if (prefix.isEmpty()) { 00116 prefix = "top"; 00117 } 00118 int cpt = 0; 00119 s += tab(level) + prefix + "[shape = \"" + boxFormat + "\", label = \"" 00120 + ((this.generic) ? "fun_" : "") + this.opName + " : " 00121 + this.getName() + "\" color=\"" + color + "\" style=\"filled\"]"; 00122 if (this.sel != null) { 00123 s += this.sel.toSchematic(level, prefix + "_sel_" + cpt, 00124 GraphParams.selShape, GraphParams.selColor); 00125 s += "\n" + tab(level) + prefix + " -> " + prefix + "_sel_" + cpt + " [label = \"Sel\"]"; 00126 cpt++; 00127 } 00128 if (!this.input.isEmpty()) { 00129 cpt = 0; 00130 boolean first = true; 00131 for (Element var : this.input) { 00132 if (var instanceof Variable) { 00133 s += var.toSchematic(level, prefix + "_in_" + cpt, 00134 GraphParams.inputShape, 00135 GraphParams.inputColor); 00136 } else { 00137 s += var.toSchematic(level, prefix + "_in_" + cpt, 00138 boxFormat, GraphParams.defaultColor); 00139 } 00140 if (first) { 00141 s += "\n" + tab(level) + prefix + " -> " + prefix + "_in_" + cpt; 00142 first = false; 00143 } else { 00144 s += "\n" + tab(level) + prefix + "_in_" + (cpt - 1) + " -> " + prefix + "_in_" + cpt; 00145 first = false; 00146 } 00147 cpt++; 00148 } 00149 } 00150 if (!this.output.isEmpty()) { 00151 cpt = 0; 00152 boolean first = true; 00153 for (Element var : this.output) { 00154 if (var instanceof Variable) { 00155 s += var.toSchematic(level, prefix + "_out_" + cpt, 00156 GraphParams.outputShape, 00157 GraphParams.outputColor); 00158 } else { 00159 s += var.toSchematic(level, prefix + "_out_" + cpt, 00160 boxFormat, GraphParams.defaultColor); 00161 } 00162 if (first) { 00163 s += "\n" + tab(level) + prefix + " -> " + prefix + "_out_" + cpt; 00164 first = false; 00165 } else { 00166 s += "\n" + tab(level) + prefix + "_out_" + (cpt - 1) + " -> " + prefix + "_out_" + cpt; 00167 first = false; 00168 } 00169 cpt++; 00170 } 00171 } 00172 return s; 00173 } 00174 00175 @Override 00176 public String toOctave(int level) { 00177 String sOctave = new String(); 00178 sOctave += tab(level) + "switch " + this.sel.getName() + "\n"; 00179 for (int i=0; i<this.size-1; i++) { 00180 sOctave += tab(level+1) + "case " + i + "\n"; 00181 sOctave += tab(level+2) + this.output.firstElement().getName() + "=" + this.input.elementAt(i).getName() + "\n"; 00182 } 00183 sOctave += tab(level+1) + "otherwise"; 00184 sOctave += "\n" + tab(level+2) + this.output.firstElement().getName() + "=" + this.input.elementAt(size-1).getName() + "\n"; 00185 sOctave += tab(level) + "end"; 00186 return sOctave; 00187 } 00188 }