Math2mat
|
00001 00021 package m2m.backend.project; 00022 00023 import java.io.BufferedWriter; 00024 import java.io.File; 00025 import java.io.FileWriter; 00026 import java.io.IOException; 00027 import java.io.StringWriter; 00028 00029 import javax.xml.parsers.DocumentBuilder; 00030 import javax.xml.parsers.DocumentBuilderFactory; 00031 import javax.xml.parsers.ParserConfigurationException; 00032 import javax.xml.transform.OutputKeys; 00033 import javax.xml.transform.Transformer; 00034 import javax.xml.transform.TransformerConfigurationException; 00035 import javax.xml.transform.TransformerException; 00036 import javax.xml.transform.TransformerFactory; 00037 import javax.xml.transform.dom.DOMSource; 00038 import javax.xml.transform.stream.StreamResult; 00039 00040 import org.w3c.dom.Document; 00041 import org.w3c.dom.Element; 00042 import org.w3c.dom.Node; 00043 import org.xml.sax.SAXException; 00044 00045 import m2m.backend.octaveparser.ParsingException; 00046 import m2m.backend.processing.OptimizeOperators; 00047 import m2m.backend.structure.StructTreatment; 00048 import m2m.backend.utils.FileUtils; 00049 import m2m.backend.utils.XMLUtils; 00050 import m2m.backend.vhdl.CompileScriptCreator; 00051 import m2m.backend.processing.ProgressMonitoring; 00052 00053 00054 public class M2MProject { 00055 00056 public final static String CURRENTVERSION="0.5.0"; 00057 00058 public String subfolder() { 00059 return "/m2m_"+this.getProperties().getFunctionName()+"/"; 00060 } 00061 00062 public ProgressMonitoring monitoring; 00063 00064 /* 00065 public static String getApplicationPath() { 00066 return ExternalToolsProperties.getReference().getApplicationPath(); 00067 } 00068 */ 00069 00073 private boolean last_monitoring; 00077 private boolean modified; 00078 00079 00080 00084 public boolean isModified() { 00085 return modified; 00086 } 00087 00091 public void setModified(boolean modified) { 00092 this.modified = modified; 00093 } 00094 00095 private SimulationResult simResult; 00096 00100 public SimulationResult getSimulationResult() { 00101 return simResult; 00102 } 00103 00107 public void setSimulationResult(SimulationResult simResult) { 00108 this.simResult = simResult; 00109 } 00110 00111 public void setLastMonitoring(boolean monitor) { 00112 last_monitoring = monitor; 00113 } 00114 00115 // private static String libPath=""; 00116 00117 public static String getLibPath() { 00118 return ExternalToolsProperties.getReference().getLibPath(); 00119 // if (libPath.isEmpty()) 00120 // return getApplicationPath()+"/lib"; 00121 // return libPath; 00122 } 00123 00124 /* 00125 public static void setLibPath(String path) { 00126 libPath=path; 00127 } 00128 */ 00129 00133 private final static String SIMPATH="sim"; 00134 00138 private final static String IOFILESPATH="sim/iofiles"; 00139 00143 private final static String UBIDULEPATH="ubidule"; 00144 00148 private final static String SRCPATH="src_m2m"; 00149 00153 private final static String VHDLPATH="src_VHDL"; 00154 00158 private final static String OCTPATH="src_OCT"; 00159 00163 private final static String SVPATH="src_SV"; 00164 00168 private final static String REPORTPATH="report"; 00169 00173 private final static String COMPPATH="sim/comp"; 00174 00179 private StructTreatment structTreatment; 00180 00184 private ProjectProperties properties; 00185 00189 private boolean useFifo = false; 00190 00191 00192 public boolean openProject(String fileName) { 00193 00194 File file = new File(fileName); 00195 properties.setProjectFile(file); 00196 00197 if (file.exists()) { 00198 if (!loadFile(file)) 00199 return false; 00200 if (getStructTreatment().isEmpty()) { 00201 00202 /* Parsing of the text of the editor */ 00203 try { 00204 getStructTreatment().parse(new File(this.getProperties().getAbsoluteSourceFilename())); 00205 return true; 00206 } 00207 catch (ParsingException e1) { 00208 return true; 00209 } 00210 } 00211 else 00212 return true; 00213 } 00214 else 00215 return false; 00216 } 00217 00218 00219 public boolean CreateEmptyProject(String fileName) { 00220 00221 File file= new File(fileName); 00222 properties.setProjectFile(file); 00223 try { 00224 String projectPath = file.getAbsolutePath().replace("\\", "/").substring(0, file.getAbsolutePath().replace("\\", "/").lastIndexOf("/")+1); 00225 00226 checkFolders(); 00227 00228 File sourceFile = new File(projectPath + subfolder()+ M2MProject.getSourceDirName()+"/" + file.getName().substring(0, file.getName().lastIndexOf(".")+1)+"m"); 00229 00230 if (!checkFolders()) 00231 return false; 00232 FileWriter sourceFileWriter = new FileWriter(sourceFile.getAbsolutePath()); 00233 String functionName=file.getName().substring(0, file.getName().lastIndexOf(".")); 00234 00235 String source = "% Here is a simple function that adds two numbers\n"; 00236 source += "% Feel free to modify it\n\n"; 00237 source += "function s="+functionName+"(a,b)\n\ts=a+b;\nendfunction\n"; 00238 char buffer[] = new char[source.length()]; 00239 source.getChars(0, source.length(), buffer, 0); 00240 sourceFileWriter.write(buffer); 00241 sourceFileWriter.close(); 00242 getProperties().setSourceFilePath(subfolder()+M2MProject.getSourceDirName() + "/" + sourceFile.getName()); 00243 saveFile(file); 00244 try { 00245 this.structTreatment.parse(sourceFile); 00246 saveFile(file); 00247 } 00248 catch (ParsingException e) { 00249 00250 } 00251 } catch (IOException e) { 00252 e.printStackTrace(); 00253 // MessageBox messageBox = new MessageBox(window.getShell(), SWT.ICON_ERROR | SWT.OK); 00254 // messageBox.setMessage("File I/O Error."); 00255 // messageBox.setText("Error"); 00256 // messageBox.open(); 00257 return false; 00258 } 00259 00260 return true; 00261 } 00262 00263 public boolean CreateNewProjectFromMath(String fileName,String fileNameOctave) { 00264 00265 File file= new File(fileName); 00266 properties.setProjectFile(file); 00267 00268 if (!checkFolders()) 00269 return false; 00270 getProperties().setSourceFilePath(this.subfolder()+this.getSourceDirName() + "/" + new File(fileNameOctave).getName()); 00271 if (!FileUtils.copyFile(fileNameOctave, this.getProperties().getAbsoluteSourceFilename())) 00272 return false; 00273 00274 00275 if (!saveFile(file)) 00276 return false; 00277 try { 00278 this.structTreatment.parse(this.getSourceFile()); 00279 saveFile(file); 00280 } 00281 catch (ParsingException e) { 00282 00283 } 00284 return true; 00285 } 00286 00287 public M2MProject() { 00288 00289 last_monitoring = false; 00290 00291 structTreatment = new StructTreatment(); 00292 properties= new ProjectProperties(); 00293 00294 } 00295 00303 /* 00304 public M2MProject(File file) { 00305 00306 last_monitoring = false; 00307 00308 structTreatment = new StructTreatment(); 00309 properties= new ProjectProperties(); 00310 properties.setProjectFile(file); 00311 00312 // get the file extension 00313 String ext = (file.getName().lastIndexOf(".") == -1 ? "" : 00314 file.getName().substring(file.getName().lastIndexOf(".")+1, file.getName().length())); 00315 00316 // if it is a m2m project file (*.m2m), create the corresponding class to extract information 00317 if (ext.equalsIgnoreCase("m2m")) { 00318 if (file.exists()) { 00319 loadFile(file); 00320 } 00321 00322 // } 00323 00324 // String projectPath = file.getAbsolutePath().replace("\\", "/").substring(0, file.getAbsolutePath().replace("\\", "/").lastIndexOf("/")+1); 00325 // File sourceFile = new File(projectPath + M2MProject.getSourceDirName()+"/" + file.getName().substring(0, file.getName().lastIndexOf(".")+1)+"m"); 00326 00327 //m2mParser = new M2MParser(file); 00328 // properties = m2mParser.getProperties(); 00329 // optimisationProperties = properties.getOptimisationProperties(); 00330 // simulationProperties = properties.getSimulationProperties(); 00331 //sourceFilePath = 00332 // properties.setSourceFilePath(projectPath + M2MProject.getSourceDirName()+"/" + file.getName().substring(0, file.getName().lastIndexOf(".")+1)+"m"); 00333 } 00334 // else create a default project 00335 else { 00336 properties = new ProjectProperties(); 00337 // properties.setSourceFilePath(getRelativePath(properties.getProjectFile().getAbsolutePath().replace("\\", "/"))); 00338 } 00339 } 00340 */ 00341 00342 00347 public boolean getUseFifo() { 00348 return this.useFifo; 00349 } 00350 00351 00357 public void setUseFifo (boolean bool) { 00358 this.useFifo = bool; 00359 } 00360 00361 //Getters 00367 public OptimisationProperties getOptimisationProperties() { 00368 return properties.getOptimisationProperties(); 00369 } 00370 00376 public SimulationProperties getSimulationProperties() { 00377 return properties.getSimulationProperties(); 00378 } 00379 00380 //returns the source code file, which is the input file if this input is not a m2m project file 00386 public File getSourceFile() { 00387 /*if (m2mParser == null) { 00388 return file; 00389 } 00390 else { 00391 String dir = ((file.getPath().lastIndexOf("/") == -1 && (file.getPath().lastIndexOf("\\")) == -1) ? "" : 00392 file.getPath().substring(0, Math.max(file.getPath().lastIndexOf("/"), file.getPath().lastIndexOf("\\"))+1)); 00393 File sourceFile = new File(dir+sourceFilePath); 00394 if (sourceFile.exists()) 00395 return sourceFile; 00396 else 00397 return null; 00398 }*/ 00399 File sourceFile = new File(properties.getAbsoluteSourceFilename()); 00400 if (sourceFile.exists()) 00401 return sourceFile; 00402 else 00403 return null; 00404 } 00405 00411 public String getProjectFilename() { 00412 return properties.getProjectFile().getPath(); 00413 } 00414 00415 00416 static public String getUbiduleDirName() { 00417 return UBIDULEPATH; 00418 } 00419 00424 static public String getVHDLDirName() { 00425 return VHDLPATH; 00426 } 00427 00432 public String getVHDLPath() { 00433 return properties.getProjectPath()+subfolder()+getVHDLDirName(); 00434 } 00435 00436 public String getVHDLSubFolder() { 00437 return subfolder()+getVHDLDirName(); 00438 } 00439 00440 00445 public String getUbidulePath() { 00446 return properties.getProjectPath()+subfolder()+getUbiduleDirName(); 00447 } 00448 00453 static public String getSVDirName() { 00454 return SVPATH; 00455 } 00456 00461 public String getSVPath() { 00462 return properties.getProjectPath()+subfolder()+getSVDirName(); 00463 } 00464 00469 static public String getOctaveDirName() { 00470 return OCTPATH; 00471 } 00472 00477 public String getOctavePath() { 00478 return properties.getProjectPath()+subfolder()+getOctaveDirName(); 00479 } 00480 00485 static public String getReportDirName() { 00486 return REPORTPATH; 00487 } 00488 00493 public String getReportPath() { 00494 return properties.getProjectPath()+subfolder()+getReportDirName(); 00495 } 00496 00497 00502 static public String getSimDirName() { 00503 return SIMPATH; 00504 } 00505 00510 public String getSimPath() { 00511 return properties.getProjectPath()+subfolder()+getSimDirName(); 00512 } 00513 00518 static public String getIOFilesDirName() { 00519 return IOFILESPATH; 00520 } 00521 00526 public String getIOFilesPath() { 00527 return properties.getProjectPath()+subfolder()+getIOFilesDirName(); 00528 } 00529 00534 static public String getCompDirName() { 00535 return COMPPATH; 00536 } 00537 00542 public String getCompPath() { 00543 return properties.getProjectPath()+subfolder()+getCompDirName(); 00544 } 00545 00550 static public String getSourceDirName() { 00551 return SRCPATH; 00552 } 00553 00558 public String getSourcePath(){ 00559 return properties.getProjectPath()+subfolder()+getSourceDirName(); 00560 } 00561 00567 public ProjectProperties getProperties() { 00568 return properties; 00569 } 00570 00576 public String getSourceFilePath() { 00577 return properties.getRelativeSourceFilename(); 00578 } 00579 00580 //Setters 00586 public void setProperties(ProjectProperties properties) { 00587 this.properties = properties; 00588 } 00589 00595 public void setFile(File file) { 00596 properties.setProjectFile(file); 00597 } 00598 00599 00606 public boolean compare (M2MProject project) { 00607 return (project.properties.getProjectFile().getAbsolutePath().equalsIgnoreCase(this.properties.getProjectFile().getAbsolutePath()) && project.properties.getRelativeSourceFilename().equalsIgnoreCase(this.properties.getRelativeSourceFilename())); 00608 } 00609 00616 public boolean checkFolders() { 00617 return checkFolders(properties.getProjectPath()+subfolder()); 00618 } 00619 00626 static public boolean checkFolders(String basePath) { 00627 00628 try { 00629 //if the base folder does not exists, create it 00630 File baseFolder = new File(basePath); 00631 if (!(baseFolder.exists() && baseFolder.isDirectory())) { 00632 baseFolder.mkdir(); 00633 } 00634 00635 //if the VHDL folder does not exists, create it 00636 File vhdlFolder = new File(basePath+"/"+getVHDLDirName()); 00637 if (!(vhdlFolder.exists() && vhdlFolder.isDirectory())) { 00638 vhdlFolder.mkdir(); 00639 } 00640 00641 //if the SV folder does not exists, create it 00642 File svFolder = new File(basePath+"/"+getSVDirName()); 00643 if (!(svFolder.exists() && svFolder.isDirectory())) { 00644 svFolder.mkdir(); 00645 } 00646 00647 //if the Octave folder does not exists, create it 00648 File octFolder = new File(basePath+"/"+getOctaveDirName()); 00649 if (!(octFolder.exists() && octFolder.isDirectory())) { 00650 octFolder.mkdir(); 00651 } 00652 00653 //if the report folder does not exists, create it 00654 File reportFolder = new File(basePath+"/"+getReportDirName()); 00655 if (!(reportFolder.exists() && reportFolder.isDirectory())) { 00656 reportFolder.mkdir(); 00657 } 00658 00659 //if the sim folder does not exists, create it 00660 File simFolder = new File(basePath+"/"+getSimDirName()); 00661 if (!(simFolder.exists() && simFolder.isDirectory())) { 00662 simFolder.mkdir(); 00663 } 00664 00665 //if the sim/iofiles folder does not exists, create it 00666 File ioFilesFolder = new File(basePath+"/"+getIOFilesDirName()); 00667 if (!(ioFilesFolder.exists() && ioFilesFolder.isDirectory())) { 00668 ioFilesFolder.mkdir(); 00669 } 00670 00671 //if the sim/comp folder does not exists, create it 00672 File compFolder = new File(basePath+"/"+getCompDirName()); 00673 if (!(compFolder.exists() && compFolder.isDirectory())) { 00674 compFolder.mkdir(); 00675 } 00676 00677 //if the ubidule folder does not exists, create it 00678 File ubiduleFolder = new File(basePath+"/"+getUbiduleDirName()); 00679 if (!(ubiduleFolder.exists() && ubiduleFolder.isDirectory())) { 00680 ubiduleFolder.mkdir(); 00681 } 00682 00683 //if the src folder does not exists, create it 00684 File srcFolder = new File(basePath+"/"+getSourceDirName()); 00685 if (!(srcFolder.exists() && srcFolder.isDirectory())) { 00686 srcFolder.mkdir(); 00687 } 00688 return true; 00689 } 00690 catch (SecurityException e) { 00691 00692 return false; 00693 } 00694 } 00695 00696 public void clear() { 00697 00698 //delete sim directory content 00699 File simDir = new File(getSimPath()); 00700 if (simDir.exists() && simDir.isDirectory()) 00701 if (!FileUtils.deleteDirContent(simDir, null)) 00702 System.err.println("Cannot delete files"); 00703 00704 //delete comp directory content 00705 File compDir = new File(getCompPath()); 00706 if (compDir.exists() && compDir.isDirectory()) 00707 if (!FileUtils.deleteDirContent(compDir, null)) 00708 System.err.println("Cannot delete files"); 00709 00710 //delete OCT directory content 00711 File octSrcDir = new File(getOctavePath()); 00712 if (octSrcDir.exists() && octSrcDir.isDirectory()) 00713 if (!FileUtils.deleteDirContent(octSrcDir, null)) 00714 System.err.println("Cannot delete files"); 00715 00716 //delete SV directory content 00717 File svSrcDir = new File(getSVPath()); 00718 if (svSrcDir.exists() && svSrcDir.isDirectory()) 00719 if (!FileUtils.deleteDirContent(svSrcDir, null)) 00720 System.err.println("Cannot delete files"); 00721 00722 //delete VHDL directory content 00723 File vhdlSrcDir = new File(getVHDLPath()); 00724 if (vhdlSrcDir.exists() && vhdlSrcDir.isDirectory()) 00725 if (!FileUtils.deleteDirContent(vhdlSrcDir, null)) 00726 System.err.println("Cannot delete files"); 00727 00728 //delete comp directory content 00729 File reportDir = new File(getReportPath()); 00730 if (reportDir.exists() && reportDir.isDirectory()) { 00731 if (!FileUtils.deleteDirContent(reportDir, null)) 00732 System.err.println("Cannot delete files"); 00733 reportDir.delete(); 00734 } 00735 00736 //delete simulation.do and transcript.txt files 00737 File simulationScript = new File(properties.getProjectPath() + "simulation.do"); 00738 File transcript = new File(properties.getProjectPath() + "transcript.txt"); 00739 00740 simulationScript.delete(); 00741 transcript.delete(); 00742 } 00743 00744 public boolean generateVHDL() { 00745 structTreatment.setProject(this); 00746 String outFilePath = getVHDLPath() + "/" + structTreatment.getTop().getName() + ".vhd"; 00747 if (!structTreatment.writeVHDL(getVHDLPath() + "/")) 00748 return false; 00749 //create compile script 00750 CompileScriptCreator scriptCreator = new CompileScriptCreator(structTreatment.getTop()); 00751 scriptCreator.createCompileScript(this,properties.getProjectPath(), outFilePath); 00752 00753 return true; 00754 } 00755 00756 00762 public StructTreatment getStructTreatment() { 00763 return structTreatment; 00764 } 00765 00766 00770 public void clearStructTreatment () { 00771 structTreatment = new StructTreatment(); 00772 } 00773 00774 public boolean compareOctaveResult() { 00775 String origOutputPath = getIOFilesPath() + "/file_output"; 00776 String regenOutputPath = getIOFilesPath() + "/file_output_regen"; 00777 00778 for (int i = 0; i < getStructTreatment().getTop().getOutput().size(); i++) { 00779 00780 File orig = new File(origOutputPath + (i+1) + ".dat"); 00781 File regenFile = new File(regenOutputPath + (i+1) + ".dat"); 00782 00783 if (FileUtils.compareDataFiles(orig, regenFile)) { 00784 System.out.println("Original file(s) and regenerated file(s) give the same results"); 00785 } else { 00786 System.err.println("Original file and regenerated file do not give the same results on output " + 00787 getStructTreatment().getTop().getOutput().elementAt(i).getName()); 00788 } 00789 } 00790 return true; 00791 } 00792 00793 public boolean reGenerateOctave() 00794 { 00795 //get the path of the source file without the extension 00796 int dotPos = getSourceFile().getAbsolutePath().lastIndexOf("."); 00797 String srcFile = getSourceFile().getAbsolutePath().substring(0, dotPos); 00798 String generateFile = srcFile + "_regen.m"; 00799 00800 System.out.println("source file path : " + srcFile); 00801 System.out.println("gen file path : " + generateFile); 00802 00803 String fname=structTreatment.getTop().getName(); 00804 structTreatment.getTop().setName(fname+"_regen"); 00805 00806 if (!structTreatment.writeOctaveFile(generateFile)) 00807 return false; 00808 00809 structTreatment.getTop().setName(fname); 00810 00811 System.out.println("Math2Mat file regenerated"); 00812 return true; 00813 } 00814 00815 00816 public boolean generateFlatDotFile() { 00817 00818 File file = this.getSourceFile(); 00819 String path = (file.getAbsolutePath().lastIndexOf(".") == -1 ? "" : 00820 file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf("."))); 00821 00822 String flatFileName = path + "_flat.gv"; 00823 00824 StructTreatment treatment = getStructTreatment(); 00825 return treatment.writeSchematic(flatFileName); 00826 } 00827 00828 public boolean generateTreeDotFile() { 00829 00830 StructTreatment struct=new StructTreatment(); 00831 try{ 00832 struct.parseNoFlat(new File(this.getProperties().getAbsoluteSourceFilename())); 00833 } 00834 catch (ParsingException e) { 00835 00836 } 00837 00838 00839 File file = this.getSourceFile(); 00840 String path = (file.getAbsolutePath().lastIndexOf(".") == -1 ? "" : 00841 file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf("."))); 00842 00843 String treeFileName = path + "_tree.gv"; 00844 00845 //File dotFile = new File(fileName); 00846 return struct.writeSchematic(treeFileName); 00847 } 00848 00849 public boolean generateCopyTreeDotFile() { 00850 00851 File file = this.getSourceFile(); 00852 String path = (file.getAbsolutePath().lastIndexOf(".") == -1 ? "" : 00853 file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf("."))); 00854 00855 String treeFileName = path + "_copy_tree.gv"; 00856 00857 //File dotFile = new File(fileName); 00858 StructTreatment treatment = getStructTreatment().copy(); 00859 00860 getStructTreatment().getTop().modifAllNames("orig_"); 00861 00862 00863 // treatment.getTop().modifAllNames("new_"); 00864 return treatment.writeSchematic(treeFileName); 00865 } 00866 00867 public boolean launchDotGeneration() { 00868 if (!generateTreeDotFile()) 00869 return false; 00870 return generateFlatDotFile(); 00871 } 00872 00873 public boolean optimize() { 00874 00875 OptimizeOperators optimise = new OptimizeOperators(structTreatment); 00876 optimise.opTreeDepth(); 00877 //optimise.optimise(); 00878 return true; 00879 } 00880 00881 public boolean save() { 00882 // return saveFile(new File(properties.getProjectFullFilename())); 00883 File f=new File(properties.getProjectFullFilename()); 00884 saveFile(f); 00885 this.setModified(false); 00886 00887 // M2MProject newproj=new M2MProject(); 00888 // newproj.openProject(f.getAbsolutePath()); 00889 // newproj.saveFile(new File(f.getAbsoluteFile()+"_copy.xml")); 00890 00891 return true; 00892 00893 } 00894 00895 public boolean saveFile(File f) { 00896 //get an instance of factory 00897 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 00898 try { 00899 //get an instance of builder 00900 DocumentBuilder db = dbf.newDocumentBuilder(); 00901 00902 //create an instance of DOM 00903 Document dom = db.newDocument(); 00904 00905 dom.appendChild(this.toXml(dom)); 00906 00908 //Output the XML 00909 00910 //set up a transformer 00911 TransformerFactory transfac = TransformerFactory.newInstance(); 00912 transfac.setAttribute("indent-number", new Integer(4)); 00913 00914 Transformer trans = transfac.newTransformer(); 00915 trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 00916 trans.setOutputProperty(OutputKeys.INDENT, "yes"); 00917 trans.setOutputProperty(OutputKeys.METHOD, "xml"); 00918 trans.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); 00919 00920 00921 00922 //create string from xml tree 00923 StringWriter sw = new StringWriter(); 00924 StreamResult result = new StreamResult(sw); 00925 DOMSource source = new DOMSource(dom); 00926 trans.transform(source, result); 00927 String xmlString = sw.toString(); 00928 00929 try{ 00930 // Create file 00931 FileWriter fstream = new FileWriter(f); 00932 BufferedWriter out = new BufferedWriter(fstream); 00933 out.write(xmlString); 00934 //Close the output stream 00935 out.close(); 00936 }catch (Exception e){//Catch exception if any 00937 System.err.println("Error: " + e.getMessage()); 00938 } 00939 00940 } 00941 catch(ParserConfigurationException pce) { 00942 //dump it 00943 System.out.println("Error while trying to instantiate DocumentBuilder " + pce); 00944 return false; 00945 } catch (TransformerConfigurationException e) { 00946 // TODO Auto-generated catch block 00947 e.printStackTrace(); 00948 return false; 00949 } catch (TransformerException e) { 00950 // TODO Auto-generated catch block 00951 e.printStackTrace(); 00952 return false; 00953 } 00954 00955 return true; 00956 } 00957 00958 public boolean compatibleVersion(String version1,String version2) { 00959 String splitV1[]=version1.split("\\p{Punct}",3); 00960 String splitV2[]=version2.split("\\p{Punct}",3); 00961 int v1[] = new int[3]; 00962 int v2[] = new int[3]; 00963 for(int i=0;i<3;i++) { 00964 v1[i]=Integer.parseInt(splitV1[i]); 00965 v2[i]=Integer.parseInt(splitV2[i]); 00966 } 00967 if (v1[0]>v2[0]) 00968 return false; 00969 if (v1[0]<v2[0]) 00970 return true; 00971 if (v1[1]>v2[1]) 00972 return false; 00973 if (v1[1]<v2[1]) 00974 return true; 00975 if (v1[2]>v2[2]) 00976 return false; 00977 if (v1[2]<v2[2]) 00978 return true; 00979 return true; 00980 } 00981 public boolean loadFile(File f) { 00982 00983 //get the factory 00984 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 00985 00986 try { 00987 00988 //Using factory get an instance of document builder 00989 DocumentBuilder db = dbf.newDocumentBuilder(); 00990 00991 //parse using builder to get DOM representation of the XML file 00992 Document dom = db.parse(f); 00993 00994 dom.getDocumentElement().normalize(); 00995 // System.out.println("Root element " + dom.getDocumentElement().getNodeName()); 00996 00997 Element docEl = dom.getDocumentElement(); 00998 00999 String version=docEl.getAttribute("version"); 01000 if (!version.isEmpty()) 01001 if (!compatibleVersion(version,CURRENTVERSION)) { 01002 System.out.println("The files corresponds to a newer version of Math2mat. Please update your software."); 01003 return false; 01004 } 01005 this.fromXml(docEl); 01006 01007 01008 }catch(ParserConfigurationException pce) { 01009 pce.printStackTrace(); 01010 return false; 01011 }catch(SAXException se) { 01012 se.printStackTrace(); 01013 return false; 01014 }catch(IOException ioe) { 01015 ioe.printStackTrace(); 01016 return false; 01017 } 01018 01019 return true; 01020 } 01021 01022 public void restoreOctave() { 01023 try { 01024 BufferedWriter out = new BufferedWriter(new FileWriter(this.properties.getAbsoluteSourceFilename())); 01025 out.write(this.structTreatment.getSourceCode()); 01026 out.close(); 01027 } 01028 catch (IOException e) 01029 { 01030 System.out.println("Exception "); 01031 } 01032 } 01033 01034 public boolean sourceFileExist() { 01035 File file=this.getSourceFile(); 01036 if (file==null) 01037 return false; 01038 if (!file.exists()) 01039 return false; 01040 return true; 01041 } 01042 01043 public boolean checkSourceFile() { 01044 if (this.structTreatment==null) 01045 return true; 01046 if (this.structTreatment.getSourceMd5()==null) 01047 return true; 01048 if (this.structTreatment.getSourceMd5().isEmpty()) 01049 return true; 01050 01051 File file=this.getSourceFile(); 01052 if (file==null) 01053 return false; 01054 if (file.exists()) { 01055 01056 String sourceMd5=FileUtils.getMd5(file.getAbsolutePath()); 01057 if (!sourceMd5.equals(structTreatment.getSourceMd5())) { 01058 return false; 01059 } 01060 } 01061 return true; 01062 } 01063 01064 01065 public String getXmlTagName() { 01066 return "M2MProject"; 01067 } 01068 01069 public boolean fromXml(Element el) { 01070 this.properties.setSourceFilePath(XMLUtils.getTextValue(el,"SourceFile","")); 01071 Node node = el.getFirstChild(); 01072 while(node!=null) { 01073 if (node.getNodeName().equalsIgnoreCase(this.properties.getOptimisationProperties().getXmlTagName())) { 01074 this.properties.getOptimisationProperties().fromXml((org.w3c.dom.Element)node); 01075 } 01076 else if (node.getNodeName().equalsIgnoreCase(this.properties.getSimulationProperties().getXmlTagName())) { 01077 this.properties.getSimulationProperties().fromXml((org.w3c.dom.Element)node); 01078 } 01079 if (node.getNodeName().equalsIgnoreCase(this.structTreatment.getXmlTagName())) { 01080 this.structTreatment.fromXml((org.w3c.dom.Element)node); 01081 } 01082 node=node.getNextSibling(); 01083 } 01084 return true; 01085 } 01086 01087 public Element toXml(Document dom) { 01088 Element el; 01089 el=dom.createElement(getXmlTagName()); 01090 el.setAttribute("version",CURRENTVERSION); 01091 el.appendChild(XMLUtils.createTextElement(dom,"SourceFile",properties.getRelativeSourceFilename())); 01092 el.appendChild(this.properties.getOptimisationProperties().toXml(dom)); 01093 el.appendChild(this.properties.getSimulationProperties().toXml(dom)); 01094 if (this.structTreatment!=null) 01095 el.appendChild(this.structTreatment.toXml(dom)); 01096 return el; 01097 } 01098 01099 public boolean isUbiduleFilesUpToDate() { 01100 File file = this.getSourceFile(); 01101 long timeCurrent = file.lastModified(); 01102 // timeCurrent = this.getStructTreatment().getParseTime(); 01103 long timeFileRef = (new File(getUbidulePath()+"/M2M_XSVF_generated.xsvf")).lastModified(); 01104 01105 return (timeCurrent < timeFileRef); 01106 } 01107 01108 public boolean isOctaveUpToDate() { 01109 if ((!last_monitoring) && this.structTreatment.getMonitor()) 01110 return false; 01111 File file = this.getSourceFile(); 01112 long timeCurrent = file.lastModified(); 01113 // timeCurrent = this.getStructTreatment().getParseTime(); 01114 long timeFileDoAll = (new File(getOctavePath()+"/m2m_doall.m")).lastModified(); 01115 long timeFileOutput = (new File(getIOFilesPath()+"/file_output1.dat")).lastModified(); 01116 01117 return ((timeCurrent < timeFileDoAll && timeCurrent < timeFileOutput)); 01118 } 01119 01120 public boolean isVHDLUpToDate() { 01121 File file = this.getSourceFile(); 01122 long timeCurrent = file.lastModified(); 01123 // timeCurrent = this.getStructTreatment().getParseTime(); 01124 long timeFilepkg = (new File(getVHDLPath()+"/pkg_cellule.vhd")).lastModified(); 01125 01126 return (timeCurrent < timeFilepkg); 01127 } 01128 01129 } 01130