Math2mat

/home/ythoma/docs/math2mat/svn/wp1/framework/m2mGUI/src/m2m/backend/processing/OptimizeOperators.java

Go to the documentation of this file.
00001 
00020 package m2m.backend.processing;
00021 
00022 import java.util.Vector;
00023 
00024 import m2m.backend.structure.Addition;
00025 import m2m.backend.structure.Assignment;
00026 import m2m.backend.structure.Element;
00027 import m2m.backend.structure.Operation;
00028 import m2m.backend.structure.StructTreatment;
00029 import m2m.backend.structure.Subtraction;
00030 
00031 
00032 public class OptimizeOperators {
00033 
00034         private StructTreatment struct;
00035         
00036         public OptimizeOperators (StructTreatment baseStruct) {
00037                 struct = baseStruct;
00038         }
00039         
00040         public void optimise() {
00041                 //optimiseOperators(struct.getTop().getBody());
00042                 optimiseAddSub(struct.getTop().getBody());
00043         }
00044         
00045         /*
00046         private void optimiseOperators (Vector<Element> el) {
00047                 if (optimiseAddSub(el) != 0) {
00048                         optimiseOperators(el);
00049                 }
00050                 else {
00051                         for (int i = 0; i < el.size(); i++) {
00052                                 if (el.elementAt(i) instanceof Operation)
00053                                         optimiseOperators(((Operation)el.elementAt(i)).getInput());
00054                         }
00055                 }
00056         }
00057         */
00058         
00059         private int optimiseAddSub (Vector<Element> inputs) {
00060                 //Function tree = struct.getTop();
00061                 
00062                 //Vector<Element> inputs = tree.getBody();
00063                 
00064                 for (int i = 0; i < inputs.size(); i++) {
00065                         //try to find a first addition or subtraction
00066                         if ((inputs.elementAt(i) instanceof Addition) || (inputs.elementAt(i) instanceof Subtraction)) {
00067                                 Vector<Element> inputs2 = ((Operation)inputs.elementAt(i)).getInput();
00068                                 for (int j = 0; j < inputs2.size(); j++) {
00069                                         //try to find a second addition or subtraction
00070                                         if ((inputs2.elementAt(j) instanceof Addition) || (inputs2.elementAt(j) instanceof Subtraction)) {
00071                                                 Vector<Element> inputs3 = ((Operation)inputs2.elementAt(j)).getInput();
00072                                                 for (int k = 0; k < inputs3.size(); k++) {
00073                                                         //try to find a third addition or subtraction
00074                                                         if ((inputs3.elementAt(k) instanceof Addition) || (inputs3.elementAt(k) instanceof Subtraction)) {
00075                                                                 //Element firstOpInput;
00076                                                                 Element input;
00077                                                                 Element output;
00078                                                                 //Element secondOpOutput;
00079                                                                 
00080                                                                 output = ((Operation)inputs.elementAt(i)).getOutput().firstElement();
00081                                                                 //secondOpOutput = ((Operation)inputs2.elementAt(j)).getOutput().firstElement();
00082 
00083                                                                 if (k > 0) {
00084                                                                         input = inputs3.elementAt(0);
00085                                                                         ((Operation)inputs2.elementAt(j)).setInputAt(0, inputs.elementAt(i));
00086                                                                 } else {
00087                                                                         input = inputs3.elementAt(1);
00088                                                                         ((Operation)inputs2.elementAt(j)).setInputAt(1, inputs.elementAt(i));
00089                                                                         //((Operation)inputs2.elementAt(j)).setOutputAt(0, output);
00090                                                                 }
00091                                                                 try {
00092                                                                         ((Operation)inputs2.elementAt(j)).addOutput(output);
00093                                                                 } catch (Exception e) {
00094                                                                         e.printStackTrace();
00095                                                                         System.err.println(e.getMessage());
00096                                                                 }
00097                                                                 
00098                                                                 inputs.setElementAt(inputs2.elementAt(j), i);
00099 
00100                                                                 if (k > 0) {
00101                                                                         ((Operation)((Operation)inputs2.elementAt(j)).getInputAt(0)).getOutput().removeElementAt(0);
00102                                                                         ((Operation)((Operation)inputs2.elementAt(j)).getInputAt(0)).setInputAt(j, input);
00103                                                                 } else {
00104                                                                         ((Operation)((Operation)inputs2.elementAt(j)).getInputAt(0)).getOutput().removeElementAt(0);
00105                                                                         //((Operation)((Operation)inputs2.elementAt(j)).getInputAt(1)).setOutputAt(0, null);
00106                                                                         ((Operation)((Operation)inputs2.elementAt(j)).getInputAt(1)).setInputAt(j, input);
00107                                                                 }
00108                                                                 
00109                                                                 return 1;
00110                                                         }
00111                                                 }
00112                                         }
00113                                 }
00114                         }
00115                 }
00116                 return 0;
00117         }
00118         
00119         public int opTreeDepth () {
00120                 int depth = 0;
00121                 
00122                 Vector<Element> elements = struct.getTop().getBody();
00123                 for (int i = 0; i < elements.size(); i ++) {
00124                         if (elements.elementAt(i) instanceof Operation && !(elements.elementAt(i) instanceof Assignment)) {
00125                                 depth = treeDepth(elements.elementAt(i));
00126                         }
00127                 }
00128                 System.out.println("profondeur de l'arbre : "+depth);
00129                 return depth;
00130         }
00131         
00132         public int treeDepth (Element el) {
00133                 int depth = 0;
00134                 int subDepth = 0;
00135                 
00136                 if (el instanceof Operation) {
00137                         Operation op = (Operation) el;
00138                         depth = 1;
00139 /*                      if (op.getNbInput() == 1) {
00140                                 if (op.getInputAt(0) instanceof Operation) {
00141                                         return depth + treeDepth(op.getInputAt(0));
00142                                 } else {
00143                                         return depth + 1;
00144                                 }
00145                         } else if (op.getNbInput() == 2) {
00146                                 if ((op.getInputAt(0) instanceof Operation) && (op.getInputAt(1) instanceof Operation)) {
00147                                         return depth + java.lang.Math.max(treeDepth(op.getInputAt(0)), treeDepth(op.getInputAt(1)));
00148                                 } else {
00149                                         if (op.getInputAt(0) instanceof Operation) {
00150                                                 return depth + treeDepth(op.getInputAt(0));
00151                                         } else if (op.getInputAt(1) instanceof Operation) {
00152                                                 return depth + treeDepth(op.getInputAt(1));
00153                                         } else {
00154                                                 return depth;
00155                                         }
00156                                 }
00157                         } else if (op.getNbInput() > 2) {
00158                                 int subTreeDepth = 0;
00159 */                              for (int i = 0; i < op.getNbInput(); i++) {
00160 //                                      if (op.getInputAt(i) instanceof Operation) {
00161                                         subDepth = java.lang.Math.max(subDepth, treeDepth(op.getInputAt(i)));
00162 //                                      }
00163                                 }
00164                                 depth += subDepth;
00165 /*                              return depth;
00166                         } else {
00167                                 return depth;
00168                         }
00169                 } else {
00170                         return depth;
00171 */              }
00172                 //System.out.println("profondeur interm�diaire de l'arbre : "+depth);
00173                 
00174                 return depth;
00175         }
00176         
00177 }
 All Classes Namespaces Files Functions Variables Enumerations