Math2mat

/home/ythoma/docs/math2mat/svn/wp1/framework/m2mGUI/src/m2m/backend/structure/Operation.java

Go to the documentation of this file.
00001 /******************************************************************************
00002  *                                                                                      Operation
00003  ******************************************************************************
00004  * Auteur               : Trolliet Gregory
00005  * Date                 : 4 mars 2009
00006  * Description : Operation
00007  ******************************************************************************/
00008 package m2m.backend.structure;
00009 
00010 import java.util.Vector;
00011 import java.util.List;
00012 
00013 import org.w3c.dom.Document;
00014 import org.w3c.dom.Node;
00015 
00016 import m2m.backend.buildingblocks.*;
00017 import m2m.backend.buildingblocks.BuildingBlock.NumType;
00018 import m2m.backend.utils.XMLUtils;
00019 
00028 public abstract class Operation extends Element {
00029 
00033         protected Vector<Element> input;
00034         protected Vector<Element> output;
00038         protected BuildingBlock block;
00042         protected String opName;
00043         protected boolean generic = false;
00044         
00045 
00046         public String getXmlTagName() {
00047                 return "Operation";
00048         }
00049         
00050 
00051         public boolean insideFromXml(org.w3c.dom.Element el,Function newFunc) {
00052                 super.insideFromXml(el,newFunc);
00053                 Node node = el.getFirstChild();
00054                 while(node!=null) {
00055                         if (node.getNodeName().equalsIgnoreCase("Input")) {
00056                                 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node;
00057                                 Node nodeIn = inputEl.getFirstChild().getNextSibling();
00058                                 while(nodeIn!=null) {
00059                                         if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) {
00060                                                 Element element=getElement((org.w3c.dom.Element)nodeIn,newFunc);
00061                                                 if (element!=null)
00062                                                         input.add(element);
00063                                         }
00064                                         nodeIn=nodeIn.getNextSibling();
00065                                 }
00066                         }
00067         
00068                         if (node.getNodeName().equalsIgnoreCase("Output")) {
00069                                 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node;
00070                                 Node nodeIn = inputEl.getFirstChild().getNextSibling();
00071                                 while(nodeIn!=null) {
00072                                         if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) {
00073                                                 Element element=getElement((org.w3c.dom.Element)nodeIn,newFunc);
00074                                                 if (element!=null)
00075                                                         output.add(element);
00076                                         }
00077                                         nodeIn=nodeIn.getNextSibling();
00078                                 }
00079                         }
00080                         node=node.getNextSibling();
00081                 }
00082 
00083                 String bl=XMLUtils.getTextValue(el,"Block","");
00084                 
00085                 BuildingBlocksManager manager = BuildingBlocksManager.getInstance();
00086                 List<BuildingBlock> list = null;
00087                 block=null;
00088                 if (this.opName != null) {
00089                         list = manager.blockNamed(this.opName);
00090                         for (BuildingBlock b : list) {
00091                                 if (b.getClass().getName().equals(bl)) {
00092                                         block=b;
00093                                 }
00094                         }
00095                 }
00096                 return true;
00097         }
00098         
00099         public void insideToXml(org.w3c.dom.Element el,Document dom,boolean shortRef) {
00100                 super.insideToXml(el,dom, shortRef);
00101                 el.appendChild(XMLUtils.createTextElement(dom, "OpName",opName));
00102                 el.appendChild(XMLUtils.createBoolElement(dom, "Generic", generic));
00103                 if (block!=null)
00104                         el.appendChild(XMLUtils.createTextElement(dom, "Block", block.getClass().getName()/*block.toString()*/));
00105                 org.w3c.dom.Element inputEl=dom.createElement("Input");
00106                 for(Element e: input) {
00107                         inputEl.appendChild(e.toXml(dom, shortRef));
00108                 }
00109                 el.appendChild(inputEl);
00110                 org.w3c.dom.Element outputEl=dom.createElement("Output");
00111                 for(Element e: output) {
00112                         outputEl.appendChild(e.toXml(dom, shortRef));
00113                 }
00114                 el.appendChild(outputEl);
00115         }
00116 
00120         public Operation() {
00121                 this.input = new Vector<Element>();
00122                 this.output = new Vector<Element>();
00123                 this.block = null;
00124                 this.opName = "void";
00125         }
00126         
00127         public Element findElement(String name) {
00128                 Element result=super.findElement(name);
00129                 if (result!=null)
00130                         return result;
00131                 for(Element e : input) {
00132                         result=e.findElement(name);
00133                         if (result!=null)
00134                                 return result;
00135                 }
00136                 for(Element e : output) {
00137                         result=e.findElement(name);
00138                         if (result!=null)
00139                                 return result;
00140                 }
00141                 return null;
00142         }
00143         
00144         public void copyTo(Element e,Function newFunc) {
00145                 Operation op=(Operation)e;
00146                 super.copyTo(op, newFunc);
00147                 op.opName=this.opName;
00148                 op.block=this.block;
00149                 for(Element el: input) {
00150                         Element found=newFunc.findElement(el.getName());
00151                         op.input.add(found);
00152                 }
00153 
00154                 for(Element el: output) {
00155                         Element found=newFunc.findElement(el.getName());
00156                         op.output.add(found);
00157                 }
00158         }
00159         
00160 /*      public Element copy(Function newFunc) {
00161                 Operation op=new Operation();
00162                 
00163                 super.copyTo(op, newFunc);
00164                 op.opName=this.opName;
00165                 op.block=this.block;
00166                 for(Element e: input) {
00167                         Element found=newFunc.findElement(e.getName());
00168                         op.input.add(found);
00169                 }
00170 
00171                 for(Element e: output) {
00172                         Element found=newFunc.findElement(e.getName());
00173                         op.output.add(found);
00174                 }
00175                 return op;
00176         }
00177 */
00178         
00179         public Operation(String opName) {
00180                 this();
00181                 this.opName = opName;
00182                 this.block = this.getFirstPipelined(NumType.FLOAT32);
00183         }
00184 
00185         public Operation(String opName, boolean gen) {
00186                 this();
00187                 this.generic = gen;
00188                 this.opName = opName;
00189                 this.block = this.getFirstPipelined(NumType.FLOAT32);
00190         }
00191 
00197         public BuildingBlock getFirstPipelined(NumType type) {
00198                 BuildingBlocksManager manager = BuildingBlocksManager.getInstance();
00199                 List<BuildingBlock> list = null;
00200                 if (this.opName != null) {
00201                         list = manager.blockNamed(this.opName);
00202                         for (BuildingBlock b : list) {
00203                                 if (b.numType()==type)
00204                                 if (b.implType() == BuildingBlock.ImplType.PIPELINE) {
00205                                         return b;
00206                                 }
00207                         }
00208                         return this.getFirst();
00209                 }
00210                 return null;
00211         }
00212         
00213         public void modifyNumType(BuildingBlock.NumType type) {
00214                 setBlock(this.getFirstPipelined(type));
00215         }       
00216 
00221         public BuildingBlock getFirst() {
00222                 BuildingBlocksManager manager = BuildingBlocksManager.getInstance();
00223                 List<BuildingBlock> list = null;
00224                 if (this.opName != null) {
00225                         list = manager.blockNamed(this.opName);
00226                         if (!list.isEmpty()) {
00227                                 return list.get(0);
00228                         }
00229                 }
00230                 return null;
00231         }
00232 
00237         public void addInput(Element op) {
00238                 this.input.add(op);
00239         }
00240 
00245         public void setInput(Vector<Element> op) {
00246                 this.input = op;
00247         }
00248 
00254         public void setInputAt(int i, Element op) {
00255                 this.input.set(i, op);
00256         }
00257 
00262         public Vector<Element> getInput() {
00263                 return this.input;
00264         }
00265 
00270         public int getNbInput() {
00271                 return this.input.size();
00272         }
00273 
00279         public Element getInputAt(int i) {
00280                 Element el = null;
00281                 try {
00282                         el = this.input.elementAt(i);
00283                 } catch (ArrayIndexOutOfBoundsException e) {
00284                         e.printStackTrace();
00285                 }
00286                 return el;
00287         }
00288 
00293         public void addOutput(Element op) {
00294                 this.output.add(op);
00295         }
00296 
00301         public void setOutput(Vector<Element> op) {
00302                 this.output = op;
00303         }
00304 
00310         public void setOutputAt(int i, Element op) {
00311                 this.output.set(i, op);
00312         }
00313 
00318         public Vector<Element> getOutput() {
00319                 return this.output;
00320         }
00321 
00326         public int getNbOutput() {
00327                 return this.output.size();
00328         }
00329 
00335         public Element getOutputAt(int i) {
00336                 Element el = null;
00337                 try {
00338                         el = this.output.elementAt(i);
00339                 } catch (ArrayIndexOutOfBoundsException e) {
00340                         e.printStackTrace();
00341                 }
00342                 return el;
00343         }
00344 
00349         public void setBlock(BuildingBlock block) {
00350                 this.block = block;
00351         }
00352 
00357         public BuildingBlock getBlock() {
00358                 return this.block;
00359         }
00360 
00365         public void setOpName(String name) {
00366                 this.opName = name;
00367         }
00368 
00373         public String getOpName() {
00374                 return this.opName;
00375         }
00376 
00381         public String getOpSymbol() {
00382                 return "op";
00383         }
00384 
00385         @Override
00386         public String toString(int level) {
00387                 String s = new String();
00388                 s += tab(level) + "[" + ((this.generic) ? "fun_" : "")
00389                                   + this.opName + "]\tName: " + this.name;
00390                 if (this.block != null) {
00391                         s += tab(level) + "BlockName : " + this.block.entityName();
00392                 }
00393                 s += "\n" + this.toStringParam(level + 1);
00394                 return s;
00395         }
00396         
00402         protected String toStringParam(int level) {
00403                 String s = new String();
00404                 if (!this.output.isEmpty()) {
00405                         s += tab(level) + "Output:";
00406                         for (Element el : this.output) {
00407                                 if (el != null) {
00408                                         s += "\n" + el.toString(level + 1);
00409                                 }
00410                         }
00411                 }
00412                 if (!this.input.isEmpty()) {
00413                         s += "\n" + tab(level) + "Input:";
00414                         for (Element el : this.input) {
00415                                 if (el != null) {
00416                                         s += "\n" + el.toString(level + 1);
00417                                 }
00418                         }
00419                 }
00420                 return s;
00421         }
00422 
00423         @Override
00424         public String toSchematic(int level, String prefix, String boxFormat, String color) {
00425                 String s = new String();
00426                 if (prefix.isEmpty()) {
00427                         prefix = "top";
00428                 }
00429                 int cpt = 0;
00430                 s += tab(level) + prefix + "[shape = \"" + boxFormat + "\", label = \""
00431                                   + ((this.generic) ? "fun_" : "") + this.opName + " : "
00432                                   + this.getName() + "\" color=\"" + color + "\" style=\"filled\"]";
00433                 if (!this.input.isEmpty()) {
00434                         cpt = 0;
00435                         for (Element var : this.input) {
00436                                 if (var instanceof Variable) {
00437                                         s += var.toSchematic(level, prefix + "_in_" + cpt,
00438                                                           GraphParams.inputShape, GraphParams.inputColor);
00439                                 } else {
00440                                         s += var.toSchematic(level, prefix + "_in_" + cpt,
00441                                                           boxFormat, GraphParams.defaultColor);
00442                                 }
00443                                 s += "\n" + tab(level) + prefix + " -> " + prefix + "_in_" + cpt;
00444                                 cpt++;
00445                         }
00446                 }
00447                 if (!this.output.isEmpty()) {
00448                         cpt = 0;
00449                         for (Element var : this.output) {
00450                                 if (var instanceof Variable) {
00451                                         s += var.toSchematic(level, prefix + "_out_" + cpt,
00452                                                           GraphParams.outputShape,
00453                                                           GraphParams.outputColor);
00454                                 } else {
00455                                         s += var.toSchematic(level, prefix + "_out_" + cpt,
00456                                                           boxFormat, GraphParams.defaultColor);
00457                                 }
00458                                 s += "\n" + tab(level) + prefix + " -> " + prefix + "_out_" + cpt;
00459                                 cpt++;
00460                         }
00461                 }
00462                 return s;
00463         }
00464 
00472         public String toOctave(int level, String op) {
00473                 String sOctave = new String();
00474                 Element out = this.output.firstElement();
00475                 sOctave += tab(level) + out.getName() + "\t= ";
00476                 boolean first = true;
00477                 for (Element e : this.input) {
00478                         if (e instanceof SimpleVariable) {
00479                                 sOctave += ((first) ? "" : " " + op + " ")
00480                                                   + ((((SimpleVariable)e).getType().equals("const")) ? ((SimpleVariable) e).getVal() : e.getName());
00481                         }
00482                         first = false;
00483                 }
00485                 if (!monitor) {
00486                         sOctave += ";";
00487                 }
00488                 if (out instanceof SimpleVariable & out.monitor) 
00489                 {       
00490                         sOctave += "\n" + tab(level) + "for j_m2m_regen=1:length(interns)";
00491                         sOctave += "\n" + tab(level+1) + "if(length(interns(j_m2m_regen).sname) == length(\"" + out.getName() +"\") && interns(j_m2m_regen).sname == \"" + out.getName() + "\")";
00492                         sOctave += "\n" + tab(level+2) + "interns(j_m2m_regen).values(i_m2m_regen,:) = " + out.getName() + ";";
00493                         sOctave += "\n" + tab(level+1) + "endif";
00494                     sOctave += "\n" + tab(level) + "endfor"; 
00495                 }
00496                 return sOctave;
00497         }
00498 }
 All Classes Namespaces Files Functions Variables Enumerations