Math2mat

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

Go to the documentation of this file.
00001 /******************************************************************************
00002  *                                                                                      SimpleVariable
00003  ******************************************************************************
00004  * Auteur               : Trolliet Gregory
00005  * Date                 : 4 mars 2009
00006  * Description : SimpleVariable
00007  ******************************************************************************/
00008 package m2m.backend.structure;
00009 
00010 import java.util.ArrayList;
00011 import java.util.HashMap;
00012 import java.util.Vector;
00013 
00014 import m2m.backend.utils.XMLUtils;
00015 
00016 import org.w3c.dom.Document;
00017 import org.w3c.dom.Node;
00018 
00027 public class SimpleVariable extends Variable {
00028 
00029         public class MapVHDLSimpleVariable
00030         {
00034                 private Vector<SimpleVariable> multSimpleVar;
00035                 private int multSimpleVarIndex;
00036                 
00040                 public MapVHDLSimpleVariable() {                        
00041                         multSimpleVar = new Vector<SimpleVariable>();
00042                         multSimpleVarIndex = 0;
00043                 }               
00044 
00050                 public SimpleVariable getMapVHDLSimpleVariable(int cost) {                      
00051                         for(int i = multSimpleVarIndex ; i < multSimpleVar.size(); i++){
00052                                 if(multSimpleVar.elementAt(i).getCost() == cost) {
00053                                         multSimpleVar.add(0, multSimpleVar.elementAt(i));
00054                                         multSimpleVar.removeElementAt(i+1);
00055                                         multSimpleVarIndex++;
00056                                         return multSimpleVar.elementAt(0);
00057                                 }
00058                         }                       
00059                         return null;
00060                 }               
00061                 
00066                 public Vector<SimpleVariable> getMultSimpleVar() {
00067                         return multSimpleVar;           
00068                 }
00069         }
00070         
00071         
00075         private ArrayList<Double> values;
00079         private String type;
00084         private int size;
00088         private int cost;
00092         private HashMap<Function, MapVHDLSimpleVariable> variableMap = new HashMap<Function, SimpleVariable.MapVHDLSimpleVariable>();
00096         private Vector<Integer> multiCost = new Vector<Integer>();
00100         private Vector<Element> variablesDependencies = new Vector<Element>();
00104         private boolean simpleFifo = false;
00108         private int simpleFifoCost = 0;
00112         private SimpleVariable inputLoopVariable;
00116         private SimpleVariable outputLoopVariable;
00117 
00118         public boolean insideFromXml(org.w3c.dom.Element el,Function newFunc) {
00119                 super.insideFromXml(el,newFunc);
00120                 size=XMLUtils.getIntValue(el,"Size",0);
00121         //      simpleFifo=XMLUtils.getBoolValue(el,"SimpleFifo",false);
00122                 type=XMLUtils.getTextValue(el, "Type", "");
00123                 if (name.isEmpty())
00124                         return false;
00125                 Node node = el.getFirstChild();
00126                 while(node!=null) {
00127                         if (node.getNodeName().equalsIgnoreCase("Value")) {
00128                                 org.w3c.dom.Element inputEl=(org.w3c.dom.Element)node;
00129 
00130                                 String textVal = inputEl.getFirstChild().getNodeValue();
00131                                 values.add(Double.parseDouble(textVal));
00132                         }
00133                         node=node.getNextSibling();
00134                 }
00135 
00136                 return true;
00137         }
00138         
00139         public String getXmlTagName() {
00140                 return "SimpleVariable";
00141         }
00142         public void insideToXml(org.w3c.dom.Element el,Document dom,boolean shortRef) {
00143                 if (shortRef)
00144                         el.appendChild(XMLUtils.createTextElement(dom,"Name",name));
00145                 else {
00146                         super.insideToXml(el,dom,shortRef);
00147                         el.appendChild(XMLUtils.createIntElement(dom, "Size",size));
00148                 //      el.appendChild(XMLUtils.createBoolElement(dom, "SimpleFifo", simpleFifo));
00149                         el.appendChild(XMLUtils.createTextElement(dom, "Type", type));
00150                         for(double v : values) {
00151                                 el.appendChild(XMLUtils.createDoubleElement(dom, "Value", v));
00152                         }
00153                 }
00154         }
00155         
00156         @SuppressWarnings("unchecked")
00157         public Element copy(Function newFunc) {
00158                 Element e=newFunc.findElement(this.name);
00159                 if (e!=null)
00160                         return e;
00161                 SimpleVariable newVar= new SimpleVariable();
00162                 newVar.name=this.name;
00163                 newVar.values=(ArrayList<Double>)this.values.clone();
00164                 newVar.type=this.type;
00165                 newVar.size=this.size;
00166                 newVar.cost=this.cost;
00167                 newVar.variablesDependencies= this.variablesDependencies;
00168                 return newVar;
00169         }
00170         
00175         public SimpleVariable() {
00176                 this.name = new String();
00177                 this.values = new ArrayList<Double>();
00178                 this.type = "";
00179                 this.size = 0;
00180         }
00181 
00186         public SimpleVariable(String n) {
00187                 this.name = n;
00188                 this.values = new ArrayList<Double>();
00189                 this.type = "";
00190                 //this.size = 1;
00191         }
00192 
00197         public SimpleVariable(double v) {
00198                 this.values = new ArrayList<Double>();
00199                 this.values.add(v);
00200                 this.name = "";
00201                 this.type = "";
00202                 this.size = 1;
00203         }
00204 
00205         public SimpleVariable(ArrayList<Double> v) {
00206                 this.values = v;
00207                 this.name = "";
00208                 this.type = "";
00209                 this.size = v.size();
00210         }
00211 
00217         public SimpleVariable(String n, double v) {
00218                 this.values = new ArrayList<Double>();
00219                 this.values.add(v);
00220                 this.name = n;
00221                 this.type = "";
00222         }
00223 
00228         public SimpleVariable(SimpleVariable var) {
00229                 this.values = new ArrayList<Double>(var.values);
00230                 this.type = var.getType();
00231                 this.name = var.getName();
00232                 this.size = var.size;
00233         }
00234 
00241         public SimpleVariable(String n, double v, String t) {
00242                 this.values = new ArrayList<Double>();
00243                 this.values.add(v);
00244                 this.name = n;
00245                 if (t == null) {
00246                         this.type = "";
00247                 } else {
00248                         this.type = t;
00249                 }
00250                 this.size = 1;
00251         }
00252 
00253         public SimpleVariable(String n, ArrayList<Double> v, String t) {
00254                 this.values = v;
00255                 this.name = n;
00256                 this.type = t;
00257                 this.size = v.size();
00258         }
00259 
00264         public void setVal(double v) {
00265                 this.values.clear();
00266                 this.values.add(v);
00267                 this.size = 1;
00268         }
00269 
00270         public void setVal(ArrayList<Double> v) {
00271                 this.values = v;
00272                 this.size = v.size();
00273         }
00274 
00279         @Override
00280         public double getVal() {
00281                 if (this.values == null || !this.values.isEmpty()) {
00282                         return this.values.get(0);
00283                 }
00284                 return Double.NaN;
00285         }
00286 
00287         public double getVal(int pos) {
00288                 if (pos < this.values.size()) {
00289                         this.values.get(pos);
00290                 }
00291                 return Double.NaN;
00292         }
00293 
00294         public ArrayList<Double> getValues() {
00295                 return this.values;
00296         }
00297 
00298         public void addVal(double v) {
00299                 this.values.add(v);
00300                 this.size++;
00301         }
00302 
00307         @Override
00308         public void setType(String t) {
00309                 this.type = t;
00310         }
00311 
00316         public String getType() {
00317                 return this.type;
00318         }
00319 
00320         public void setSize(int n) {
00321                 this.size = n;
00322         }
00323 
00324         public int getSize() {
00325                 return this.size;
00326         }
00327 
00328         @Override
00329         public String toString(int level) {
00330                 String s = new String();
00331                 s += this.tab(level) + "[SimpleVariable]";
00332                 if (!this.type.equals("const")) {
00333                         s += "\tName: " + this.name + "\tType: " + this.type;
00334                 } else {
00335                         s += "\tName: " + this.name + "\tVal: " + this.values;
00336                 }
00337                 s += "\tSize: " + this.size;
00338                 return s;
00339         }
00340 
00341         @Override
00342         public String toSchematic(int level, String prefix, String boxFormat, String color) {
00343                 String s = new String();
00344                 if (prefix.isEmpty()) {
00345                         prefix = "var";
00346                 }
00347                 if (this.type.equals("const")) {
00348                         s += "\n" + tab(level) + prefix + "[shape=\"" + boxFormat + "\" label = \"" + this.getName() + ":\\n" + this.getVal() + "\" color=\"" + color + "\" style=\"filled\"]";
00349                 } else {
00350                         s += "\n" + tab(level) + prefix + "[shape=\"" + boxFormat + "\" label = \"" + this.getName() + "\" color=\"" + color + "\" style=\"filled\"]";
00351                 }
00352                 return s;
00353         }
00354         
00355         
00360         public void setCost(int cost) {
00361                 this.cost = cost;
00362         }
00363         
00364         
00369         public int getCost() {
00370                 return this.cost;
00371         }
00372         
00373         
00378         public Vector<Integer> getMultiCost() {
00379                 return multiCost;
00380         }
00381 
00382         
00387         public HashMap<Function, MapVHDLSimpleVariable> getVariableMap() {
00388                 return variableMap;
00389         }
00390         
00391         
00396         public Vector<Element> getVariableDependencies() {
00397                 return variablesDependencies;
00398         }
00399         
00400         
00405         public void setSimpleFifo(boolean value) {
00406                 simpleFifo = value;
00407         }
00408         
00409         
00414         public boolean getSimpleFifo() {
00415                 return simpleFifo;
00416         }
00417         
00418         
00423         public int getSimpleFifoCost() {
00424                 return simpleFifo ? simpleFifoCost+1 : simpleFifoCost;
00425         }
00426         
00427         
00432         public void setSimpleFifoCost(int value) {
00433                 simpleFifoCost = value;
00434         }
00435         
00436         
00441         public void setOutputLoopVariable(SimpleVariable var){
00442                 outputLoopVariable = var;
00443         }
00444         
00449         public SimpleVariable getOutputLoopVariable(){
00450                 return outputLoopVariable;
00451         }
00452         
00457         public void setInputLoopVariable(SimpleVariable var){
00458                 inputLoopVariable = var;
00459         }
00460         
00465         public SimpleVariable getInputLoopVariable(){
00466                 return inputLoopVariable;
00467         }
00468 }
 All Classes Namespaces Files Functions Variables Enumerations