]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Linux (and headless) support
authorjplaine <jplaine@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 21 Sep 2010 13:10:09 +0000 (13:10 +0000)
committerjplaine <jplaine@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 21 Sep 2010 13:10:09 +0000 (13:10 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18027 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java
org.simantics.sysdyn/META-INF/MANIFEST.MF
org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java

index 478b81d5abc5ac8d0b8ff0c477a3cd6e920d31c1..779fcd675cbee1869d8435e45a0bdf76743f0580 100644 (file)
@@ -20,14 +20,58 @@ import java.io.PrintStream;
 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
@@ -119,19 +163,21 @@ public class ModelicaManager {
             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
index ca7f931637ceb927bf4df1680e30b8f13dcbe69d..802f3295b171ee0b16a38967ce311cc9d83d51b8 100644 (file)
@@ -28,3 +28,4 @@ Export-Package: org.simantics.sysdyn,
  org.simantics.sysdyn.tableParser
 Bundle-Activator: org.simantics.sysdyn.Activator
 Bundle-ActivationPolicy: lazy
+Import-Package: org.eclipse.ui
index f13e32af15bc9136c8cbd6822962faa1b0f4a781..045a8cf50b0a4ac42c290c72d20c09d338a09734 100644 (file)
@@ -23,6 +23,7 @@ import org.eclipse.ui.console.ConsolePlugin;
 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
@@ -31,13 +32,18 @@ public class SimulationJob extends Job {
 \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
@@ -65,6 +71,36 @@ public class SimulationJob extends Job {
         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