Math2mat

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

Go to the documentation of this file.
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 import m2m.backend.utils.XMLUtils;
00014 
00015 import org.w3c.dom.Document;
00016 import org.w3c.dom.Node;
00017 
00025 public class IfThenElse extends Function {
00026 
00030         private Vector<Element> cond;
00034         private Vector<Element> bodyTrue;
00038         private Vector<Element> bodyFalse;
00039 
00043         public IfThenElse() {
00044                 this.setName("ifthen");
00045                 this.cond = new Vector<Element>();
00046                 this.bodyTrue = new Vector<Element>();
00047                 this.bodyFalse = new Vector<Element>();
00048         }
00049 
00050         public void modifyNumType(BuildingBlock.NumType type) {
00051                 super.modifyNumType(type);
00052                 for(Element e: bodyTrue) {
00053                         e.modifyNumType(type);
00054                 }
00055                 for(Element e: bodyFalse) {
00056                         e.modifyNumType(type);
00057                 }
00058         }
00059 
00060         public String getXmlTagName() {
00061                 return "IfThenElse";
00062         }
00063 
00064         public boolean insideFromXml(org.w3c.dom.Element el,Function newFunc) {
00065                 super.insideFromXml(el,newFunc);
00066                 
00067                 name=XMLUtils.getTextValue(el,"Name","");
00068                 monitor=XMLUtils.getBoolValue(el,"Monitor",false);
00069 
00070                 Node node = el.getFirstChild();
00071                 while(node!=null) {
00072                         if (node.getNodeName().equalsIgnoreCase("Cond")) {
00073                                 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node;
00074                                 Node nodeIn = inputEl.getFirstChild().getNextSibling();
00075                                 while(nodeIn!=null) {
00076                                         if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) {
00077                                                 Element element=getElement((org.w3c.dom.Element)nodeIn,newFunc);
00078                                                 if (element!=null)
00079                                                         cond.add(element);
00080                                         }
00081                                         nodeIn=nodeIn.getNextSibling();
00082                                 }
00083                         }
00084 
00085                         if (node.getNodeName().equalsIgnoreCase("BodyTrue")) {
00086                                 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node;
00087                                 Node nodeIn = inputEl.getFirstChild().getNextSibling();
00088                                 while(nodeIn!=null) {
00089                                         if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) {
00090                                                 Element element=getElement((org.w3c.dom.Element)nodeIn,newFunc);
00091                                                 if (element!=null)
00092                                                         bodyTrue.add(element);
00093                                         }
00094                                         nodeIn=nodeIn.getNextSibling();
00095                                 }
00096                         }
00097 
00098                         if (node.getNodeName().equalsIgnoreCase("BodyFalse")) {
00099                                 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node;
00100                                 if (inputEl!=null)
00101                                         if (inputEl.getFirstChild()!=null) {
00102                                                 Node nodeIn = inputEl.getFirstChild().getNextSibling();
00103                                                 while(nodeIn!=null) {
00104                                                         if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) {
00105                                                                 Element element=getElement((org.w3c.dom.Element)nodeIn,newFunc);
00106                                                                 if (element!=null)
00107                                                                         bodyFalse.add(element);
00108                                                         }
00109                                                         nodeIn=nodeIn.getNextSibling();
00110                                                 }
00111                                         }
00112                                 
00113                         }
00114 
00115                         node=node.getNextSibling();
00116                 }
00117                 return false;
00118         }
00119         
00120         public void insideToXml(org.w3c.dom.Element el,Document dom,boolean shortRef) {
00121                 super.insideToXml(el,dom,shortRef);
00122                 org.w3c.dom.Element condEl=dom.createElement("Cond");
00123                 for(Element e: cond) {
00124                         condEl.appendChild(e.toXml(dom,false));
00125                 }
00126                 el.appendChild(condEl);
00127                 org.w3c.dom.Element bodyTEl=dom.createElement("BodyTrue");
00128                 for(Element e: bodyTrue) {
00129                         bodyTEl.appendChild(e.toXml(dom,false));
00130                 }
00131                 el.appendChild(bodyTEl);
00132                 org.w3c.dom.Element bodyFEl=dom.createElement("BodyFalse");
00133                 for(Element e: bodyFalse) {
00134                         bodyFEl.appendChild(e.toXml(dom,false));
00135                 }
00136                 el.appendChild(bodyFEl);
00137         }
00138 
00139 
00140 
00145         @Override
00146         public Element copy(Function newFunc) {
00147                 IfThenElse newIf=new IfThenElse();
00148                 super.copyTo(newIf,newFunc);
00149                 
00150                 for(Element e : cond)
00151                         newIf.cond.add((Element)e.copy(newFunc));
00152                 for(Element e : bodyTrue)
00153                         newIf.bodyTrue.add((Element)e.copy(newFunc));
00154                 for(Element e : bodyFalse)
00155                         newIf.bodyFalse.add((Element)e.copy(newFunc));
00156                 return newIf;
00157         }
00158 
00163         public void setBodyTrue(Vector<Element> body) {
00164                 this.bodyTrue = body;
00165         }
00166 
00171         public Vector<Element> getBodyTrue() {
00172                 return this.bodyTrue;
00173         }
00174 
00179         public void setBodyFalse(Vector<Element> body) {
00180                 this.bodyFalse = body;
00181         }
00182 
00187         public Vector<Element> getBodyFalse() {
00188                 return this.bodyFalse;
00189         }
00190 
00195         public void setCond(Vector<Element> cond) {
00196                 this.cond = cond;
00197         }
00198 
00203         public Vector<Element> getCond() {
00204                 return this.cond;
00205         }
00206 
00207         @Override
00208         public String toString(int level) {
00209                 String s = new String();
00210                 s += tab(level) + "[IfThenElse]\tName: " + this.name;
00211                 s += "\n" + this.toStringParam(level+1);
00212                 s += "\n" + tab(level) + ">>===COND===<<";
00213                 for (Element el : this.cond) {
00214                         s += "\n" + el.toString(level + 1);
00215                 }
00216                 s += "\n" + tab(level) + ">>===TRUE===<<";
00217                 for (Element el : this.bodyTrue) {
00218                         s += "\n" + el.toString(level + 1);
00219                 }
00220                 if (!this.bodyFalse.isEmpty()) {
00221                         s += "\n" + tab(level) + ">>===FALSE==<<";
00222                         for (Element el : this.bodyFalse) {
00223                                 s += "\n" + el.toString(level + 1);
00224                         }
00225                 }
00226                 return s;
00227         }
00228 
00229 
00230         @Override
00231         public String toSchematic(int level, String prefix,
00232                           String boxFormat, String color) {
00233                 String s = new String();
00234                 int cpt = 0;
00235                 s += super.toSchematic(level, prefix, "box", color);
00236                 if (!this.cond.isEmpty()) {
00237                         cpt = 0;
00238                         for (Element el : this.cond) {
00239                                 s += "\n" + el.toSchematic(level, prefix + "_cond_"
00240                                                   + cpt, "box", GraphParams.defaultColor);
00241                                 s += "\n" + tab(level) + prefix + " -> " + prefix + "_cond_" + cpt;
00242                                 cpt++;
00243                         }
00244                 }
00245                 if (!this.bodyTrue.isEmpty()) {
00246                         cpt = 0;
00247                         for (Element el : this.bodyTrue) {
00248                                 s += "\n" + el.toSchematic(level, prefix + "_bodyTrue_"
00249                                                   + cpt, "box", GraphParams.defaultColor);
00250                                 s += "\n" + tab(level) + prefix + " -> " + prefix
00251                                                   + "_bodyTrue_" + cpt + "[label = \"bodyTrue\"]";
00252                                 cpt++;
00253                         }
00254                 }
00255                 if (!this.bodyFalse.isEmpty()) {
00256                         cpt = 0;
00257                         for (Element el : this.bodyFalse) {
00258                                 s += "\n" + el.toSchematic(level, prefix + "_bodyFalse_"
00259                                                   + cpt, "box", GraphParams.defaultColor);
00260                                         s += "\n" + tab(level) + prefix + " -> " + prefix
00261                                                           + "_bodyFalse_" + cpt + "[label = \"bodyFalse\"]";
00262                                 cpt++;
00263                         }
00264                 }
00265                 return s;
00266         }
00267 
00268         @Override
00269         public String toOctave(int level) {
00270                 String sOctave = new String();
00271                 for (Element e : this.cond) {
00272                         sOctave += e.toOctave(level) + "\n";
00273                 }
00274                 sOctave += tab(level) + "if (";
00275                 if (this.cond.lastElement() instanceof Operation) {
00276                         sOctave += ((Operation) this.cond.lastElement()).getOutput().firstElement().getName();
00277                 } else if (this.cond.lastElement() instanceof Variable) {
00278                         sOctave += this.cond.lastElement().getName();
00279                 }
00280                 sOctave += ")\n";
00281                 for (Element e : this.bodyTrue) {
00282                         sOctave += e.toOctave(level + 1) + "\n";
00283                 }
00284                 if (!this.bodyFalse.isEmpty()) {
00285                         sOctave += tab(level) + "else\n";
00286                         for (Element e : this.bodyFalse) {
00287                                 sOctave += e.toOctave(level + 1) + "\n";
00288                         }
00289                 }
00290                 sOctave += tab(level) + "end";
00291                 return sOctave;
00292         }
00293 }
 All Classes Namespaces Files Functions Variables Enumerations