import java.util.HashMap;\r
\r
public class ModelicaManager {\r
+ \r
+ public enum OSType {\r
+ APPLE, LINUX, SUN, WINDOWS, UNKNOWN\r
+ }\r
\r
+ public static OSType calculateOS() {\r
+ String osName = System.getProperty("os.name");\r
+ assert osName != null;\r
+ osName = osName.toLowerCase();\r
+ if (osName.startsWith("mac os x"))\r
+ return OSType.APPLE;\r
+ if (osName.startsWith("windows"))\r
+ return OSType.WINDOWS;\r
+ if (osName.startsWith("linux"))\r
+ return OSType.LINUX;\r
+ if (osName.startsWith("sun"))\r
+ return OSType.SUN;\r
+ return OSType.UNKNOWN;\r
+ }\r
\r
-\r
- protected static File getOpenModelicaHome() {\r
+ public static File getModelicaExecutable() {\r
String dir = System.getenv("OPENMODELICAHOME");\r
- if(dir == null)\r
- dir = "c:/OpenModelica1.5.0";\r
- return new File(dir);\r
+\r
+ String osName = System.getProperty("os.name");\r
+ OSType os = calculateOS();\r
+\r
+ if (os == OSType.UNKNOWN)\r
+ throw new UnsatisfiedLinkError("unknown OS '" + osName + "' cannot load native fastlz library");\r
+\r
+ if(dir == null) {\r
+ switch (os) {\r
+ case APPLE:\r
+ case LINUX:\r
+ case SUN:\r
+ return new File("/usr/bin/omc");\r
+ case WINDOWS:\r
+ return new File("c:/OpenModelica1.5.0/bin/omc.exe");\r
+ default:\r
+ throw new UnsatisfiedLinkError("Unsupported operating system: " + os);\r
+ }\r
+ } else {\r
+ switch (os) {\r
+ case APPLE:\r
+ case LINUX:\r
+ case SUN:\r
+ return new File(dir+"/bin/omc");\r
+ case WINDOWS:\r
+ return new File(dir+"/bin/omc.exe");\r
+ default:\r
+ throw new UnsatisfiedLinkError("Unsupported operating system: " + os);\r
+ }\r
+ }\r
}\r
\r
protected static File createTempDirectory() throws IOException {\r
s.close();\r
}\r
\r
+ OSType os = calculateOS();\r
+ String suffix = OSType.WINDOWS.equals(os) ? ".exe" : "";\r
+ \r
return new SimulationLocation(\r
simulationDir,\r
new File(simulationDir, modelName + ".mos"),\r
new File(simulationDir, modelName + "_res.plt"),\r
new File(simulationDir, modelName + "_init.txt"),\r
- new File(simulationDir, modelName + ".exe")\r
+ new File(simulationDir, modelName + suffix)\r
);\r
}\r
\r
public static void buildModel(SimulationLocation simulationLocation, IModelicaMonitor monitor) throws ModelicaException {\r
try {\r
- File modelicaHome = getOpenModelicaHome();\r
- File omc = new File(modelicaHome, "bin/omc.exe");\r
+ File omc = getModelicaExecutable();\r
\r
Process process = new ProcessBuilder(\r
omc.getAbsolutePath(),\r
import org.eclipse.ui.console.IConsoleManager;\r
import org.eclipse.ui.console.MessageConsole;\r
import org.eclipse.ui.console.MessageConsoleStream;\r
+import org.eclipse.ui.PlatformUI;\r
import org.simantics.modelica.IModelicaMonitor;\r
import org.simantics.simulation.experiment.Experiment;\r
import org.simantics.sysdyn.manager.SysdynModel;\r
\r
SysdynModel model;\r
Experiment experiment;\r
- ModelicaMonitor monitor;\r
+ IModelicaMonitor monitor;\r
\r
public SimulationJob(SysdynModel model, Experiment experiment) {\r
super("Simulate " + model.getConfiguration().getName());\r
this.model = model;\r
this.experiment = experiment;\r
- this.monitor = new ModelicaMonitor();\r
+ if(PlatformUI.isWorkbenchRunning()) {\r
+ this.monitor = new ModelicaMonitor();\r
+ } else {\r
+ // Fallback to headless\r
+ this.monitor = new HeadlessModelicaMonitor();\r
+ }\r
}\r
\r
@Override\r
return "SimulationJob".equals(family);\r
}\r
\r
+ private class HeadlessModelicaMonitor implements IModelicaMonitor {\r
+ public HeadlessModelicaMonitor() {\r
+ }\r
+\r
+ @Override\r
+ public void message(String message) {\r
+ message(message, "hh:mm:ss");\r
+ }\r
+ \r
+ /**\r
+ * Print message to a console with a specified time stamp format\r
+ * \r
+ * @param message the message to be printed\r
+ * @param timeStampFormat simpledateformat timestamp format. null if no timestamp wanted\r
+ */\r
+ public void message(String message, String timeStampFormat) {\r
+ Calendar cal = Calendar.getInstance();\r
+ SimpleDateFormat sdf = new SimpleDateFormat(timeStampFormat);\r
+ String time = sdf.format(cal.getTime());\r
+ \r
+ System.out.println("[" + time +"] " + message);\r
+ }\r
+\r
+ public void clearConsole() {\r
+ }\r
+ \r
+ public void showConsole() {\r
+ }\r
+\r
+ }\r
\r
private class ModelicaMonitor implements IModelicaMonitor {\r
\r