Math2mat

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

Go to the documentation of this file.
00001 /******************************************************************************
00002  *                                                                                      Case
00003  ******************************************************************************
00004  * Auteur               : Trolliet Gregory
00005  * Date                 : 5 janv. 2010
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 
00022 public class Case extends Element {
00023 
00027         private Vector<Variable> cond;
00031         private Vector<Element> body;
00032 
00036         public Case() {
00037                 this.cond = null;
00038                 this.body = null;
00039         }
00040         
00041 
00042         public void modifyNumType(BuildingBlock.NumType type) {
00043                 super.modifyNumType(type);
00044                 for(Element e: cond) {
00045                         e.modifyNumType(type);
00046                 }
00047                 for(Element e: body) {
00048                         e.modifyNumType(type);
00049                 }
00050         }
00051 
00052         public String getXmlTagName() {
00053                 return "Case";
00054         }
00055 
00060         @Override
00061         public Element copy(Function newFunc) {
00062                 Case ccopy=new Case();
00063                 copyTo(ccopy,newFunc);
00064                 return ccopy;
00065         }
00066         
00067         public void copyTo(Element to, Function newFunc) {
00068                 Case ccopy=(Case) to;
00069                 super.copyTo(ccopy,newFunc);
00070 
00071                 for(Variable e : cond)
00072                         ccopy.cond.add((Variable)e.copy(newFunc));
00073                 for(Element e : body)
00074                         ccopy.body.add((Element)e.copy(newFunc));
00075         }
00076         
00077         public boolean insideFromXml(org.w3c.dom.Element el,Function newFunc) {
00078                 super.insideFromXml(el,newFunc);
00079                 
00080                 name=XMLUtils.getTextValue(el,"Name","");
00081                 monitor=XMLUtils.getBoolValue(el,"Monitor",false);
00082 
00083                 Node node = el.getFirstChild();
00084                 while(node!=null) {
00085                         if (node.getNodeName().equalsIgnoreCase("Cond")) {
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                                                         cond.add((Variable)element);
00093                                         }
00094                                         nodeIn=nodeIn.getNextSibling();
00095                                 }
00096                         }
00097 
00098                         if (node.getNodeName().equalsIgnoreCase("Body")) {
00099                                 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node;
00100                                 Node nodeIn = inputEl.getFirstChild().getNextSibling();
00101                                 while(nodeIn!=null) {
00102                                         if (nodeIn.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE) {
00103                                                 Element element=getElement((org.w3c.dom.Element)nodeIn,newFunc);
00104                                                 if (element!=null)
00105                                                         body.add(element);
00106                                         }
00107                                         nodeIn=nodeIn.getNextSibling();
00108                                 }
00109                         }
00110 
00111                         node=node.getNextSibling();
00112                 }
00113                 return false;
00114         }
00115         
00116         public void insideToXml(org.w3c.dom.Element el,Document dom,boolean shortRef) {
00117                 super.insideToXml(el,dom,shortRef);
00118                 org.w3c.dom.Element condEl=dom.createElement("Cond");
00119                 for(Element e: cond) {
00120                         condEl.appendChild(e.toXml(dom,false));
00121                 }
00122                 el.appendChild(condEl);
00123                 org.w3c.dom.Element bodyEl=dom.createElement("Body");
00124                 for(Element e: body) {
00125                         bodyEl.appendChild(e.toXml(dom,false));
00126                 }
00127                 el.appendChild(bodyEl);
00128         }
00129 
00130 
00135         public void setCond(Vector<Variable> cond) {
00136                 this.cond = cond;
00137         }
00138 
00143         public Vector<Variable> getCond() {
00144                 return this.cond;
00145         }
00146 
00151         public void setBody(Vector<Element> body) {
00152                 this.body = body;
00153         }
00154 
00159         public Vector<Element> getBody() {
00160                 return this.body;
00161         }
00162 
00168         public String toString(int level) {
00169                 String s = new String();
00170                 s += tab(level) + ">>===COND===<<";
00171                 if (this.cond == null) {
00172                         s += "\n" + tab(level + 1) + "Otherwise";
00173                 } else {
00174                         for (Element el : this.cond) {
00175                                 s += "\n" + el.toString(level + 1);
00176                         }
00177                 }
00178                 s += "\n" + tab(level) + ">>===BODY===<<";
00179                 if (this.body != null) {
00180                         for (Element el : this.body) {
00181                                 s += "\n" + el.toString(level + 1);
00182                         }
00183                 }
00184                 return s;
00185         }
00186 
00187 
00196         public String toSchematic(int level, String prefix,
00197                           String boxFormat, String color) {
00198                 String s = new String();
00199                 if (this.cond == null || this.cond.isEmpty()) {
00200                         s += tab(level) + prefix + "[shape = " + boxFormat
00201                                           + ", label = \"Otherwise\" color=\""
00202                                           + GraphParams.otherwColor + "\" style=\"filled\"]";
00203                 } else {
00204                         String list = new String();
00205                         boolean first = true;
00206                         for (Variable var : this.cond) {
00207                                 if (!first) {
00208                                         list += ", ";
00209                                 }
00210                                 if (var.getName().isEmpty()) {
00211                                         list += var.getVal();
00212                                 } else {
00213                                         list += var.getName();
00214                                 }
00215                                 first = false;
00216                         }
00217                         s += tab(level) + prefix + "[shape = " + boxFormat + ", label = \""
00218                                           + list + "\" color=\"" + GraphParams.otherwColor
00219                                           + "\" style=\"filled\"]";
00220                 }
00221                 if (!this.body.isEmpty()) {
00222                         int cpt = 0;
00223                         for (Element el : this.body) {
00224                                 s += "\n" + el.toSchematic(level, prefix + "_" + cpt, "box",
00225                                                   GraphParams.defaultColor);
00226                                 s += "\n" + tab(level) + prefix + " -> " + prefix + "_" + cpt;
00227                                 cpt++;
00228                         }
00229                 }
00230                 return s;
00231         }
00232 
00233         
00239         /*
00240         private String tab(int nb) {
00241                 String sTab = new String();
00242                 for (int i = 0; i < nb; i++) {
00243                         sTab += "\t";
00244                 }
00245                 return sTab;
00246         }
00247         */
00248 }
 All Classes Namespaces Files Functions Variables Enumerations