import java.io.InputStream;\r
import java.io.InputStreamReader;\r
import java.io.PrintStream;\r
+import java.io.Reader;\r
import java.net.URL;\r
import java.net.URLDecoder;\r
import java.util.ArrayList;\r
\r
import org.eclipse.core.runtime.FileLocator;\r
import org.eclipse.core.runtime.Platform;\r
-import org.eclipse.core.runtime.preferences.DefaultScope;\r
+import org.eclipse.core.runtime.preferences.ConfigurationScope;\r
import org.eclipse.core.runtime.preferences.IScopeContext;\r
import org.osgi.framework.Bundle;\r
import org.osgi.service.prefs.Preferences;\r
public static File getModelicaHome() {\r
\r
// Try preferences\r
- IScopeContext context = DefaultScope.INSTANCE;\r
+ IScopeContext context = ConfigurationScope.INSTANCE;\r
Preferences node = context.getNode(Activator.PLUGIN_ID);\r
String omHomePath = node.get(OpenModelicaPreferences.OM_HOME, null);\r
if(omHomePath != null) {\r
BufferedReader in = new BufferedReader(r);\r
StringBuilder output = new StringBuilder();\r
String line;\r
- while ((line = in.readLine()) != null)\r
+ boolean first = true;\r
+ while ((line = in.readLine()) != null) {\r
+ if(!first)\r
+ output.append(System.getProperty("line.separator"));\r
+ first = false;\r
+ \r
output.append(line);\r
+ }\r
return output.toString();\r
} catch (Exception e) { // exception thrown\r
System.err.println("getProcessOutput failed!");\r
ArrayList<String> parameters = new ArrayList<String>();\r
parameters.add(simulationLocation.mosFile.getAbsolutePath());\r
runOMC(simulationLocation.simulationDir, simulationLocation.omcHome, monitor, parameters);\r
- createFullMo(simulationLocation, monitor);\r
\r
if(!simulationLocation.executableFile.isFile())\r
// If .exe file was not created, something went wrong\r
String version = experimentParameters.get(OMC_VERSION);\r
if(version == null)\r
version = getOMCVersion(simulationLocation.omcHome);\r
- if(version.startsWith("1.9")) {\r
+ \r
+ Double versionNumber = 1.9;\r
+ try {\r
+ versionNumber = Double.parseDouble(version.substring(0,3));\r
+ } catch (NumberFormatException e) {}\r
+ \r
+ if(versionNumber < 1.9) {\r
+ writeInits(simulationLocation, experimentParameters, null);\r
+ } else {\r
// Handled in experiment\r
if(parameterChanges.size() == 1) {\r
commands.add("-override");\r
commands.add(parameterChanges.keySet().iterator().next() + "=" + parameterChanges.values().iterator().next());\r
- } else\r
+ } else {\r
+ // FIXME: if you change 1 parameter AND some experiment parameters, only the parameter change takes effect\r
updateInitFile(simulationLocation, experimentParameters, parameterChanges);\r
- } else {\r
- writeInits(simulationLocation, experimentParameters, monitor);\r
+ }\r
}\r
\r
}\r
}\r
return null;\r
}\r
+ \r
+ public static String getFlatModelText(SimulationLocation simulationLocation, IModelicaMonitor monitor, List<String> additionalPaths) {\r
+ \r
+ ArrayList<String> commands = new ArrayList<String>();\r
+ File omHome = getModelicaHome();\r
+ commands.add(omHome + "\\bin\\omc.exe");\r
+ String path = simulationLocation.mosFile.getAbsolutePath();\r
+ path = path.substring(0, path.length() - 1);\r
+ commands.add(path);\r
+ \r
+ for(String additionalPath : additionalPaths) {\r
+ commands.add(additionalPath);\r
+ }\r
+ \r
+ ProcessBuilder processBuilder = new ProcessBuilder(commands)\r
+ .redirectErrorStream(true).directory(simulationLocation.simulationDir);\r
+ \r
+ Map<String, String> env = processBuilder.environment();\r
+ env.put("OPENMODELICAHOME", omHome.getAbsolutePath());\r
+ \r
+ // Start the building process\r
+ try {\r
+ Process process = processBuilder.start();\r
+ return getProcessOutput(process);\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ return null;\r
+ }\r
+\r
\r
/**\r
- * Get all parameter variables and their values from the full model description\r
- * @param simulationLocation\r
- * @param monitor\r
- * @return\r
+ * Get all parameter variables and their values from model. Model text is\r
+ * provided in a Reader.\r
+ * @param reader Reader containing model text\r
+ * @return all parameters and their values\r
*/\r
- public static HashMap<String, String> getFullModelParameters(SimulationLocation simulationLocation, IModelicaMonitor monitor) {\r
+ public static HashMap<String, String> getModelParameters(Reader reader) {\r
HashMap<String, String> result = new HashMap<String, String>();\r
\r
try {\r
- ArrayList<String> commands = new ArrayList<String>();\r
- File omHome = getModelicaHome();\r
- commands.add(omHome + "\\bin\\omc.exe");\r
- commands.add(simulationLocation.fullModel.getAbsolutePath());\r
-\r
- // Create the build process\r
- ProcessBuilder processBuilder = new ProcessBuilder(commands)\r
- .redirectErrorStream(true);\r
-\r
- Map<String, String> env = processBuilder.environment();\r
- env.put("OPENMODELICAHOME", omHome.getAbsolutePath());\r
- \r
- // Start the building process\r
- Process process = processBuilder.start();\r
-\r
- InputStream lsOut = process.getInputStream();\r
- InputStreamReader r = new InputStreamReader(lsOut);\r
- BufferedReader in = new BufferedReader(r);\r
+ BufferedReader in = new BufferedReader(reader);\r
String line;\r
String name;\r
String value;\r
}\r
}\r
in.close();\r
- r.close();\r
- lsOut.close(); \r
-\r
- process.waitFor();\r
} catch (IOException e) {\r
-\r
- } catch (InterruptedException e) {\r
e.printStackTrace();\r
}\r
return result;\r
}\r
\r
+\r
/**\r
* Updates inits.xml with the latest values. This is not needed if the model structure\r
* has changed. When model structure is changed, the model is compiled and a new xml is \r
\r
try {\r
// Create the full model code into one file\r
- ArrayList<String> parameters = new ArrayList<String>();\r
- parameters.add(simulationLocation.fullMosFile.getAbsolutePath());\r
- \r
- Process process = runOMC(simulationLocation.simulationDir, simulationLocation.omcHome, null, parameters);\r
- process.waitFor();\r
- \r
- trimExtraFromFullModel(simulationLocation);\r
+ createFullMo(simulationLocation, null);\r
\r
// Create simulation files from the full description\r
- parameters.clear();\r
+ ArrayList<String> parameters = new ArrayList<String>();\r
parameters.add("+s");\r
parameters.add(simulationLocation.fullModel.getAbsolutePath());\r
\r
\r
protected String experimentName;\r
protected static String omcVersion = null;\r
- \r
+ protected static String omcHome = null;\r
+\r
public static SysdynExperiment INSTANCE;\r
\r
public SysdynExperiment(Resource experiment, Resource model) {\r
* @param modelText\r
* @param monitor\r
*/\r
- protected void buildModel(SimulationLocation simulationLocation, String modelText, IModelicaMonitor monitor) {\r
+ protected void buildModel(SimulationLocation simulationLocation, IModelicaMonitor monitor) {\r
\r
try {\r
simulationLocation.executableFile.delete();\r
- previousModelStructure = modelText;\r
ModelicaManager.buildModel(simulationLocation, monitor);\r
- previousParameters = ModelicaManager.getFullModelParameters(simulationLocation, monitor);\r
} catch (ModelicaException e) {\r
if(e.getMessage() != null)\r
monitor.message(e.getMessage());\r
canceled = false;\r
progressMonitor.subTask("Write modelica classes");\r
\r
- // Write Modelica files\r
- String modelText = getModelicaCode(monitor, false, getOpenModelicaVersion());\r
+ File home = ModelicaManager.getModelicaHome();\r
+ if (omcHome == null || !home.getAbsolutePath().equals(omcHome)) {\r
+ omcVersion = ModelicaManager.getOMCVersion(home);\r
+ omcHome = home.getAbsolutePath();\r
+ }\r
+ \r
+ // Get Modelica code\r
+ String modelText = getModelicaCode(monitor, false, omcVersion);\r
if(modelText == null)\r
return;\r
progressMonitor.worked(1);\r
// Write initial files and add init-parameters\r
progressMonitor.subTask("Write simulation files");\r
HashMap<String, String> experimentParameters = getExperimentParameters(monitor);\r
-\r
+ \r
// add loadFile script to load all related functions and function libraries\r
String additionalScript = getAdditionalScripts();\r
-\r
+ \r
// Create simulation files\r
SimulationLocation simulationLocation = createSimulationFiles(sysdynModel, modelText, experimentParameters, additionalScript, false);\r
progressMonitor.worked(1);\r
-\r
+ \r
// Build the model and store previous model structure and inits that affect the building\r
// If there is no exe file OR the model structure has not changed, no need to build\r
- boolean structureChanged = hasStructureChanged(modelText);\r
+ String flatModelText = ModelicaManager.getFlatModelText(simulationLocation, monitor, FunctionUtils.getLibraryPathsForModelica(this));\r
+ \r
+ boolean structureChanged = hasStructureChanged(flatModelText);\r
if (!simulationLocation.executableFile.isFile() || structureChanged) {\r
progressMonitor.subTask("Build model");\r
- buildModel(simulationLocation, modelText, monitor);\r
+ previousModelStructure = flatModelText;\r
+ StringReader reader = new StringReader(previousModelStructure);\r
+ previousParameters = ModelicaManager.getModelParameters(reader);\r
+ reader.close();\r
}\r
\r
// Add changed parameters in case that structure has not changed\r
HashMap<String, String> changes = structureChanged ? null : new HashMap<String, String>();\r
- if (omcVersion == null) {\r
- omcVersion = ModelicaManager.getOMCVersion(simulationLocation.omcHome);\r
- }\r
if(!structureChanged && previousParameters != null && omcVersion.startsWith("1.9")) {\r
- try {\r
- ModelicaManager.createFullMo(simulationLocation, monitor);\r
- HashMap<String, String> newParameters = ModelicaManager.getFullModelParameters(simulationLocation, monitor);\r
+ StringReader reader = new StringReader(flatModelText);\r
+ HashMap<String, String> newParameters = ModelicaManager.getModelParameters(reader);\r
+ reader.close();\r
for(String key : previousParameters.keySet()) {\r
if(!previousParameters.get(key).equals(newParameters.get(key))) {\r
changes.put(key, newParameters.get(key));\r
}\r
}\r
previousParameters = newParameters;\r
- } catch (ModelicaException e) {\r
- e.printStackTrace();\r
- }\r
}\r
progressMonitor.worked(1);\r
-\r
+ \r
if(simulationLocation != null && !canceled) {\r
// Simulate the model\r
runModelica(simulationLocation, modelText, monitor, progressMonitor, experimentParameters, changes);\r
}\r
+ \r
if(canceled)\r
simulate(false);\r
process = null;\r
while (c != null && p != null) {\r
// if the lines are the same, no need for further examination\r
if(!c.equals(p)) {\r
- if(\r
- c.contains("parameter") && c.contains("/* Actual value read from init file */") &&\r
- p.contains("parameter") && p.contains("/* Actual value read from init file */")) {\r
+ if(c.contains("parameter") && p.contains("parameter")) {\r
/*\r
* The line is a parameter definition.\r
* In this case only what is before '=' matters\r