Math2mat
|
00001 /****************************************************************************** 00002 * Function 00003 ****************************************************************************** 00004 * Auteur : Trolliet Gregory 00005 * Date : 4 mars 2009 00006 * Description : Function 00007 ******************************************************************************/ 00008 package m2m.backend.structure; 00009 00010 import java.util.Iterator; 00011 import java.util.LinkedList; 00012 import java.util.Vector; 00013 00014 import m2m.backend.buildingblocks.BuildingBlock; 00015 import m2m.backend.utils.XMLUtils; 00016 00017 import org.w3c.dom.*; 00018 00027 public class Function extends Element { 00028 00032 protected Vector<Element> inputVars; 00036 protected Vector<Element> outputVars; 00040 protected Vector<Element> internalVars; 00044 protected Vector<Element> body; 00048 protected int maxCost; 00052 protected int maxRelativeCost; 00053 00057 public Function() { 00058 this.inputVars = new Vector<Element>(); 00059 this.outputVars = new Vector<Element>(); 00060 this.internalVars = new Vector<Element>(); 00061 this.body = new Vector<Element>(); 00062 } 00063 00064 public void modifyNumType(BuildingBlock.NumType type) { 00065 for(Element e: body) { 00066 e.modifyNumType(type); 00067 } 00068 } 00069 00070 public String getXmlTagName() { 00071 return "Function"; 00072 } 00073 00074 public boolean fromXml(org.w3c.dom.Element el) { 00075 insideFromXml(el,this); 00076 return true; 00077 /* 00078 NodeList nl = el.getElementsByTagName("Function"); 00079 if(nl != null && nl.getLength() > 0) { 00080 org.w3c.dom.Element e = (org.w3c.dom.Element)nl.item(0); 00081 if (e!=null) { 00082 return insideFromXml(el); 00083 } 00084 } 00085 return false; 00086 */ 00087 } 00088 00089 00090 public boolean insideFromXml(org.w3c.dom.Element el,Function newFunc) { 00091 super.insideFromXml(el,newFunc); 00092 00093 name=XMLUtils.getTextValue(el,"Name",""); 00094 monitor=XMLUtils.getBoolValue(el,"Monitor",false); 00095 00096 if (name.isEmpty()) 00097 return true; 00098 00099 Node node = el.getFirstChild(); 00100 while(node!=null) { 00101 if (node.getNodeName().equalsIgnoreCase("Input")) { 00102 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node; 00103 if (inputEl.getFirstChild()!=null) { 00104 Node nodeIn = inputEl.getFirstChild().getNextSibling(); 00105 while(nodeIn!=null) { 00106 if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) { 00107 Element element=getElement((org.w3c.dom.Element)nodeIn,newFunc); 00108 if (element!=null) 00109 inputVars.add(element); 00110 } 00111 nodeIn=nodeIn.getNextSibling(); 00112 } 00113 } 00114 } 00115 00116 if (node.getNodeName().equalsIgnoreCase("Output")) { 00117 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node; 00118 if (inputEl.getFirstChild()!=null) { 00119 Node nodeIn = inputEl.getFirstChild().getNextSibling(); 00120 while(nodeIn!=null) { 00121 if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) { 00122 Element element=getElement((org.w3c.dom.Element)nodeIn,newFunc); 00123 if (element!=null) 00124 outputVars.add(element); 00125 } 00126 nodeIn=nodeIn.getNextSibling(); 00127 } 00128 } 00129 } 00130 00131 if (node.getNodeName().equalsIgnoreCase("Internals")) { 00132 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node; 00133 if (inputEl.getFirstChild()!=null) { 00134 Node nodeIn = inputEl.getFirstChild().getNextSibling(); 00135 while(nodeIn!=null) { 00136 if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) { 00137 Element element=getElement((org.w3c.dom.Element)nodeIn,newFunc); 00138 if (element!=null) 00139 internalVars.add(element); 00140 } 00141 nodeIn=nodeIn.getNextSibling(); 00142 } 00143 } 00144 } 00145 00146 if (node.getNodeName().equalsIgnoreCase("Body")) { 00147 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node; 00148 if (inputEl.getFirstChild()!=null) { 00149 Node nodeIn = inputEl.getFirstChild().getNextSibling(); 00150 while(nodeIn!=null) { 00151 if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) { 00152 Element element=getElement((org.w3c.dom.Element)nodeIn,this); 00153 if (element!=null) 00154 body.add(element); 00155 } 00156 nodeIn=nodeIn.getNextSibling(); 00157 } 00158 } 00159 } 00160 node=node.getNextSibling(); 00161 } 00162 return false; 00163 } 00164 00165 00166 public void superInsideToXml(org.w3c.dom.Element el,Document dom,boolean shortRef) { 00167 super.insideToXml(el,dom,shortRef); 00168 } 00169 00170 public void insideToXml(org.w3c.dom.Element el,Document dom,boolean shortRef) { 00171 super.insideToXml(el,dom,shortRef); 00172 org.w3c.dom.Element inputEl=dom.createElement("Input"); 00173 for(Element e: inputVars) { 00174 inputEl.appendChild(e.toXml(dom,false)); 00175 } 00176 el.appendChild(inputEl); 00177 org.w3c.dom.Element outputEl=dom.createElement("Output"); 00178 for(Element e: outputVars) { 00179 outputEl.appendChild(e.toXml(dom,false)); 00180 } 00181 el.appendChild(outputEl); 00182 org.w3c.dom.Element statEl=dom.createElement("Internals"); 00183 for(Element e: internalVars) { 00184 statEl.appendChild(e.toXml(dom,false)); 00185 } 00186 el.appendChild(statEl); 00187 org.w3c.dom.Element bodyEl=dom.createElement("Body"); 00188 for(Element e: body) { 00189 bodyEl.appendChild(e.toXml(dom,true)); 00190 } 00191 el.appendChild(bodyEl); 00192 } 00193 00198 @Override 00199 public Element copy(Function newFunc) { 00200 Function fcopy=new Function(); 00201 copyTo(fcopy,fcopy); 00202 return fcopy; 00203 } 00204 00205 public void copyTo(Element to, Function newFunc) { 00206 Function fcopy=(Function) to; 00207 super.copyTo(fcopy,newFunc); 00208 00209 for(Element e : inputVars) 00210 fcopy.inputVars.add((Element)e.copy(newFunc)); 00211 for(Element e : outputVars) 00212 fcopy.outputVars.add((Element)e.copy(newFunc)); 00213 for(Element e : internalVars) 00214 fcopy.internalVars.add((Element)e.copy(newFunc)); 00215 for(Element e : body) 00216 fcopy.body.add((Element)e.copy(newFunc)); 00217 } 00218 00219 public Element findElement(String name) { 00220 for(Element e : inputVars) 00221 if (e.getName().equals(name)) 00222 return e; 00223 for(Element e : outputVars) 00224 if (e.getName().equals(name)) 00225 return e; 00226 for(Element e : internalVars) 00227 if (e.getName().equals(name)) 00228 return e; 00229 for(Element e : body) 00230 if (e.findElement(name)!=null) 00231 return e.findElement(name); 00232 return null; 00233 } 00234 00235 public void modifAllNames(String prefix) { 00236 00237 this.setName(prefix+this.getName()); 00238 for(Element e : inputVars) 00239 e.setName(prefix+e.getName()); 00240 for(Element e : outputVars) 00241 e.setName(prefix+e.getName()); 00242 for(Element e : internalVars) 00243 e.setName(prefix+e.getName()); 00244 for(Element e : body) 00245 e.setName(prefix+e.getName()); 00246 } 00247 00248 00253 public void addInput(Element var) { 00254 this.inputVars.add(var); 00255 } 00256 00261 public void setInput(Vector<Element> in) { 00262 this.inputVars = in; 00263 } 00264 00269 public Vector<Element> getInput() { 00270 return this.inputVars; 00271 } 00272 00277 public void addOutput(Element var) { 00278 this.outputVars.add(var); 00279 } 00280 00285 public void setOutput(Vector<Element> out) { 00286 this.outputVars = out; 00287 } 00288 00293 public Vector<Element> getOutput() { 00294 return this.outputVars; 00295 } 00296 00301 public void addInternalVar(Element var) { 00302 this.internalVars.add(var); 00303 } 00304 00309 public void setInternalVars(Vector<Element> list) { 00310 this.internalVars = list; 00311 } 00312 00317 public Vector<Element> getInternalVars() { 00318 return this.internalVars; 00319 } 00320 00325 public Vector<Element> getMonitoringElement() 00326 { 00327 /* Use to get all nodes */ 00328 Element element; 00329 LinkedList<Element> tempElementList = new LinkedList<Element>(); 00330 Iterator<Element> iterElement; 00331 Vector<Element> elementList = new Vector<Element>(); 00332 00333 /* Initialize the nodes list and the node iterator */ 00334 tempElementList.addAll(getBody()); 00335 00336 for(Element e : getInternalVars()) 00337 if(e.getMonitor() && !elementList.contains(e)) 00338 elementList.add(e); 00339 00340 /* Get all the node and connection inside the schema */ 00341 iterElement = tempElementList.iterator(); 00342 while (iterElement.hasNext()) 00343 { 00344 element = iterElement.next(); 00345 00346 /* Check if the node has children */ 00347 if(element instanceof Function) 00348 { 00349 tempElementList.addAll(((Function)element).getBody()); 00350 for(Element e : ((Function)element).getInternalVars()) 00351 if(e.getMonitor() && !elementList.contains(e)) 00352 elementList.add(e); 00353 } 00354 00355 tempElementList.remove(tempElementList.indexOf(element)); 00356 iterElement = tempElementList.iterator(); 00357 } 00358 00359 return elementList; 00360 } 00361 00362 00367 public void addBody(Element el) { 00368 this.body.add(el); 00369 } 00370 00375 public void addBody(Vector<Element> vec) { 00376 this.body.addAll(vec); 00377 } 00378 00383 public void addBodyToTop(Element el) { 00384 this.body.add(0, el); 00385 } 00386 00391 public void setBody(Vector<Element> body) { 00392 this.body = body; 00393 } 00394 00399 public Vector<Element> getBody() { 00400 return this.body; 00401 } 00402 00403 @Override 00404 public String toString(int level) { 00405 String s = new String(); 00406 s += tab(level) + "[Function]\tName: " + this.name; 00407 s += "\n" + this.toStringParam(level + 1); 00408 s += "\n" + tab(level) + ">>==========<<"; 00409 for (Element el : this.body) { 00410 s += "\n" + el.toString(level + 1); 00411 } 00412 return s; 00413 } 00414 00420 protected String toStringParam(int level) { 00421 String s = new String(); 00422 boolean first = true; 00423 if (!this.inputVars.isEmpty()) { 00424 first = false; 00425 s += tab(level) + "Input:"; 00426 for (Element el : this.inputVars) { 00427 s += "\n" + el.toString(level + 1); 00428 } 00429 } 00430 if (!this.outputVars.isEmpty()) { 00431 if (!first) { 00432 s += "\n"; 00433 } 00434 first = false; 00435 s += tab(level) + "Output:"; 00436 for (Element el : this.outputVars) { 00437 s += "\n" + el.toString(level + 1); 00438 } 00439 } 00440 if (!this.internalVars.isEmpty()) { 00441 if (!first) { 00442 s += "\n"; 00443 } 00444 first = false; 00445 s += tab(level) + "Internal:"; 00446 for (Element el : this.internalVars) { 00447 s += "\n" + el.toString(level + 1); 00448 } 00449 } 00450 return s; 00451 } 00452 00453 @Override 00454 public String toSchematic(int level, String prefix, String boxFormat, 00455 String color) { 00456 String s = new String(); 00457 int cpt = 0; 00458 if (prefix.isEmpty()) { 00459 prefix = "top"; 00460 } 00461 if (color.isEmpty()) { 00462 color = GraphParams.defaultColor; 00463 } 00464 s += tab(level) + prefix + "[shape = " + boxFormat + ", label = \"" 00465 + this.getName() + "\" color=\"" + color + "\" style=\"filled\"]"; 00466 if (!this.inputVars.isEmpty()) { 00467 cpt = 0; 00468 for (Element var : this.inputVars) { 00469 s += var.toSchematic(level, prefix + "_i_" + cpt, 00470 GraphParams.inputShape, GraphParams.inputColor); 00471 s += "\n" + tab(level) + prefix + " -> " + prefix + "_i_" + cpt; 00472 cpt++; 00473 } 00474 } 00475 if (!this.outputVars.isEmpty()) { 00476 cpt = 0; 00477 for (Element var : this.outputVars) { 00478 s += var.toSchematic(level, prefix + "_o_" + cpt, 00479 GraphParams.outputShape, 00480 GraphParams.outputColor); 00481 s += "\n" + tab(level) + prefix + " -> " + prefix + "_o_" + cpt; 00482 cpt++; 00483 } 00484 } 00485 if (!this.internalVars.isEmpty()) { 00486 cpt = 0; 00487 for (Element var : this.internalVars) { 00488 s += var.toSchematic(level, prefix + "_s_" + cpt, 00489 GraphParams.internalShape, 00490 GraphParams.internalColor); 00491 s += "\n" + tab(level) + prefix + " -> " + prefix + "_s_" + cpt; 00492 cpt++; 00493 } 00494 } 00495 if (!this.body.isEmpty()) { 00496 cpt = 0; 00497 for (Element el : this.body) { 00498 s += "\n" + el.toSchematic(level, prefix + "_" + cpt, "box", 00499 GraphParams.defaultColor); 00500 s += "\n" + tab(level) + prefix + " -> " + prefix + "_" + cpt; 00501 cpt++; 00502 } 00503 } 00504 return s; 00505 } 00506 00507 @Override 00508 public String toOctave(int level) { 00509 String sOctave = new String(); 00510 sOctave += tab(level) + "function "; 00511 if (this.outputVars.size() == 1) { 00512 if (this.outputVars.firstElement() instanceof SimpleVariable) { 00513 sOctave += this.outputVars.firstElement().getName(); 00514 } 00515 } else { 00516 sOctave += "["; 00517 boolean first = true; 00518 for (Element e : this.outputVars) { 00519 if (e instanceof SimpleVariable) { 00520 sOctave += ((first) ? "" : ", ") + e.getName(); 00521 first = false; 00522 } 00523 } 00524 sOctave += "]"; 00525 } 00526 sOctave += "=" + this.getName() + "("; 00527 boolean first = true; 00528 for (Element e : this.inputVars) { 00529 if (e instanceof SimpleVariable) { 00530 sOctave += ((first) ? "" : ", ") + e.getName(); 00531 first = false; 00532 } 00533 } 00534 00535 if(getMonitoringElement().size() != 0) 00536 { 00537 sOctave += ", i_m2m_regen)\n"; 00538 sOctave += "global interns;\n"; 00539 } 00540 else 00541 sOctave += ")\n"; 00542 00543 for (Element e : this.body) { 00544 sOctave += e.toOctave(level + 1) + "\n"; 00545 } 00546 sOctave += tab(level) + "endfunction;"; 00547 return sOctave; 00548 } 00549 00550 00556 public boolean isInput(Element element) 00557 { 00558 if(element instanceof SimpleVariable){ 00559 String trueName = StructTreatment.getTrueName(element.getName()); 00560 for(Element e : getInput()) { 00561 if(e.getName().equals(trueName)) 00562 return true; 00563 } 00564 } 00565 00566 return false; 00567 } 00568 00569 00575 public boolean containsOuputName(String name) 00576 { 00577 for(Element e : outputVars) 00578 if(e.getName().equals(name)) 00579 return true; 00580 00581 return false; 00582 } 00583 00584 00590 public boolean containsInputName(String name) 00591 { 00592 for(Element e : outputVars) 00593 if(e.getName().equals(name)) 00594 return true; 00595 00596 return false; 00597 } 00598 00599 00604 public void setMaxCost(int cost) { 00605 maxCost = cost; 00606 } 00607 00608 00613 public int getMaxCost(){ 00614 return maxCost; 00615 } 00616 00617 00622 public void setRelativeMaxCost() { 00623 int tmpMaxCost = 0; 00624 for(Element e : inputVars) { 00625 tmpMaxCost = tmpMaxCost < ((SimpleVariable)e).getCost() ? ((SimpleVariable)e).getCost() : tmpMaxCost; 00626 } 00627 maxRelativeCost = maxCost-tmpMaxCost+1; 00628 } 00629 00630 00635 public int getRelativeMaxCost(){ 00636 return maxRelativeCost; 00637 } 00638 }