Math2mat

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

Go to the documentation of this file.
00001 
00022 package m2m.backend.processing;
00023 
00024 import java.io.File;
00025 import java.io.FileReader;
00026 import java.io.FileWriter;
00027 import java.io.IOException;
00028 import java.util.Vector;
00029 
00030 import m2m.backend.project.M2MProject;
00031 import m2m.backend.project.SimulationProperties;
00032 import m2m.backend.structure.Element;
00033 import m2m.backend.structure.Function;
00034 
00035 
00036 public class OctaveScriptCreator {
00037         
00038         private Function top;
00039         private SimulationProperties simProp;
00040         private boolean codeRegen;
00041         
00042         public OctaveScriptCreator(Function top, SimulationProperties simProp, boolean regen) {
00043                 this.top = top;
00044                 this.simProp = simProp;
00045                 this.codeRegen = regen;
00046         }
00047         
00048         public boolean createOctaveScript(M2MProject project, File sourceFile) {
00049                 
00050                 String parameterPath = project.getOctavePath()+"/m2m_parameters.m";
00051                 String samplesPath  = project.getOctavePath()+"/m2m_computesample.m";
00052                 final String projectPath = project.getProperties().getProjectPath();
00053                 
00054                 writeParameters(parameterPath, projectPath+project.subfolder(), sourceFile);
00055                 writeSamples(samplesPath, sourceFile);
00056                 if (codeRegen)
00057                         completeMainScript(projectPath);
00058                 
00059                 return true;
00060         }
00061 
00065         private void completeMainScript(String projectPath) {
00066                 
00067                 File mainScript = new File(projectPath+M2MProject.getOctaveDirName()+"/m2m_doall.m");
00068                 String sOutputRegen = new String();
00069                 
00070                 sOutputRegen += "\n\nprintf(\"\\nCreation of output_regen verification files:\\n\");\n";
00071                 sOutputRegen += "printf(\"--------------------------------------------\\n\");\n";
00072                 sOutputRegen += "\nfor i=1:length(outputs_regen)\n";
00073                 sOutputRegen += "\tfname=sprintf(\"%sfile_%s.dat\",filedir,outputs_regen(i).name);\n";
00074                 sOutputRegen += "\tm2m_writevartofile(fname,outputs_regen(i).values);\n";
00075                 sOutputRegen += "\tprintf(\"file_%s.dat was written\\n\", outputs_regen(i).name);\n";
00076                 sOutputRegen += "endfor\n";
00077                 
00078                 if(top.getMonitoringElement().size() != 0)
00079                 {
00080                         sOutputRegen += "\nprintf(\"\\nCreation of intern verification files:\\n\");\n";
00081                         sOutputRegen += "printf(\"--------------------------------------\\n\");\n";
00082                         sOutputRegen += "\nfor i=1:length(interns)\n";
00083                         sOutputRegen += "\tfname=sprintf(\"%sfile_%s.dat\",filedir,interns(i).name);\n";
00084                         sOutputRegen += "\tm2m_writevartofile(fname,interns(i).values);\n";
00085                         sOutputRegen += "\tprintf(\"file_%s.dat was written\\n\", interns(i).name);\n";
00086                         sOutputRegen += "endfor\n\n";
00087                 }
00088                         
00089                 try {
00090                         FileReader fr = new FileReader(mainScript);
00091                         int readChar;
00092                         String readText = new String();
00093                         //read all the content of the main Octave script (m2m_doall.m)
00094                         while ((readChar = fr.read()) != -1) {
00095                                 readText += (char)readChar;
00096                         }
00097                         fr.close();
00098                         
00099                         //add the few lines to generates regenerated output files
00100                         //sOutputRegen += readText + sOutputRegen;
00101                         
00102                         //write all in the main script file
00103                         FileWriter fw = new FileWriter(mainScript);
00104                         fw.write(readText + sOutputRegen);
00105                         fw.close();
00106                 } catch (IOException e) {
00107                         e.printStackTrace();
00108                 } 
00109         }
00110 
00111         private void writeParameters(String samplesPath, String path,File sourceFile) {
00112                 File file = new File(samplesPath);
00113                 try {
00114                         String samples = createParameters(path,sourceFile);
00115                         FileWriter fw = new FileWriter(file);
00116                         fw.write(samples);
00117                         fw.close();
00118                 } catch (IOException e) {
00119                         e.printStackTrace();
00120                         System.err.println("Cannot open file : " + samplesPath);
00121                 }
00122         }
00123 
00124         private String createParameters(String path, File sourceFile) {
00125                 String sSamples = new String();
00126                 sSamples += "%inputs\n";
00127                 for (int i = 0; i < this.top.getInput().size(); i++) {
00128                         sSamples += "inputs(" + (i + 1) + ").name=\"input" + (i + 1) +"\";\n";
00129                         sSamples += "inputs(" + (i + 1) + ").size=1;\n";
00130                 }
00131                 sSamples += "%outputs\n";
00132                 for (int i = 0; i < this.top.getOutput().size(); i++) {
00133                         sSamples += "outputs(" + (i + 1) + ").name=\"output" + (i + 1) + "\";\n";
00134                         sSamples += "outputs(" + (i + 1) + ").size=1;\n";
00135                 }
00136                 if (this.codeRegen) {
00137                         for (int i = 0; i < this.top.getOutput().size(); i++) {
00138                                 sSamples += "outputs_regen(" + (i + 1) + ").name=\"output_regen" + (i + 1) + "\";\n";
00139                                 sSamples += "outputs_regen(" + (i + 1) + ").size=1;\n";
00140                         }
00141                 }
00142                 Vector<Element> internals = this.top.getMonitoringElement();
00143                 sSamples += "%interns\n";
00144                 for (int i = 0; i < internals.size(); i++) {
00145                         sSamples += "interns(" + (i + 1) + ").name=\"internal" + (i + 1) + "\";\n";
00146                         sSamples += "interns(" + (i + 1) + ").sname=\"" + internals.get(i).getName() + "\";\n";
00147                         sSamples += "interns(" + (i + 1) + ").size=1;\n";
00148                 }
00149                 sSamples += "% number of samples\n";
00150                 sSamples += "nbsamples=" + simProp.getNumberOfSamples() + ";\n\n";
00151                 sSamples += "filedir=\"" + path + M2MProject.getIOFilesDirName()+"/\";\n";
00152                 sSamples += "generate_input=true;\n";
00153                 sSamples += "addpath(\""+sourceFile.getParent().replace("\\", "/")+"\");";
00154                 
00155                 return sSamples;
00156         }
00157 
00158         private void writeSamples(String parameterPath, File sourceFile) {
00159                 File file = new File(parameterPath);
00160 
00161                 String sParameters = new String();
00162                 sParameters += "[";
00163                 for (int i = 0; i < this.top.getOutput().size(); i++) {
00164                         if ( i == 0)
00165                                 sParameters += "outputs(" + (i + 1) + ").values(i,:)";
00166                         else
00167                                 sParameters += ", outputs(" + (i + 1) + ").values(i,:)";
00168                 }
00169                 sParameters += "]=" + sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf(".")) + "(";
00170                 for (int i = 0; i < this.top.getInput().size(); i++) {
00171                         if (i == 0)
00172                                 sParameters += "inputs(" + (i + 1) + ").values(i,:)";
00173                         else
00174                                 sParameters += ", inputs(" + (i + 1) + ").values(i,:)";
00175                 }
00176                 sParameters += ");\n";
00177                 
00178                 if (codeRegen) {
00179                         sParameters += "[";
00180                         for (int i = 0; i < this.top.getOutput().size(); i++) {
00181                                 if ( i == 0)
00182                                         sParameters += "outputs_regen(" + (i + 1) + ").values(i,:)";
00183                                 else
00184                                         sParameters += ", outputs_regen(" + (i + 1) + ").values(i,:)";
00185                         }
00186                         sParameters += "]=" + sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf(".")) + "_regen(";
00187                         for (int i = 0; i < this.top.getInput().size(); i++) {
00188                                 if (i == 0)
00189                                         sParameters += "inputs(" + (i + 1) + ").values(i,:)";
00190                                 else
00191                                         sParameters += ", inputs(" + (i + 1) + ").values(i,:)";
00192                         }
00193                         
00194                         if(top.getMonitoringElement().size() != 0)
00195                                 sParameters += ", i";
00196                         
00197                         sParameters += ");\n";
00198                 }
00199                 
00200                 try {
00201                         FileWriter fw = new FileWriter(file);
00202                         fw.write(sParameters);
00203                         fw.close();
00204                 } catch (IOException e) {
00205                         e.printStackTrace();
00206                         System.err.println("Cannot open file : " + parameterPath);
00207                 }
00208         }
00209 }
 All Classes Namespaces Files Functions Variables Enumerations