]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Better (error) messages for simulation, simulation buttons react to running simulations
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 1 Sep 2010 09:11:28 +0000 (09:11 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 1 Sep 2010 09:11:28 +0000 (09:11 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17742 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/RunBasicExperiment.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java
org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java
org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationScheduler.java

index 1bddcf4583b63a378623878d7b96c60e1e6ed7be..3723a30e47f30959e86367154ac6837f16d3c4ce 100644 (file)
@@ -73,7 +73,11 @@ public class ModelicaManager {
                             b.append((char)c);\r
                         else {\r
                             System.out.println("OMC output: " + b.toString());\r
-                            monitor.message(b.toString());\r
+                            String message = b.toString();\r
+                            message = message.trim();\r
+                            if(message.startsWith("\""))\r
+                               message = message.substring(1);\r
+                            monitor.message(message);\r
                             b.delete(0, b.length());\r
                         }\r
                     } catch (IOException e) {\r
@@ -141,7 +145,7 @@ public class ModelicaManager {
             printProcessOutput(process, monitor);\r
             \r
             if(!simulationLocation.exeFile.isFile())\r
-               throw new ModelicaException("exe file not created");            \r
+               throw new ModelicaException(".exe file not created");            \r
 \r
         } catch(IOException e) {\r
             e.printStackTrace();\r
index 5780d4aee79fa4fe9bc7e977f13a8bcb4a9fa3be..d5f48ebfa83b8b743339b942fda06dbe4997ae4c 100644 (file)
@@ -4,16 +4,15 @@ package org.simantics.sysdyn.ui.handlers;
 import java.util.Map;\r
 \r
 import org.eclipse.core.commands.AbstractHandler;\r
-import org.eclipse.core.commands.Command;\r
 import org.eclipse.core.commands.ExecutionEvent;\r
 import org.eclipse.core.commands.ExecutionException;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.eclipse.ui.commands.ICommandService;\r
 import org.eclipse.ui.commands.IElementUpdater;\r
 import org.eclipse.ui.menus.UIElement;\r
+import org.simantics.simulation.experiment.ExperimentState;\r
 import org.simantics.simulation.experiment.IDynamicExperiment;\r
 import org.simantics.simulation.experiment.IExperiment;\r
 import org.simantics.simulation.project.IExperimentManager;\r
+import org.simantics.sysdyn.manager.SysdynExperiment;\r
 import org.simantics.ui.SimanticsUI;\r
 \r
 public class RunBasicExperiment extends AbstractHandler implements IElementUpdater {\r
@@ -33,14 +32,16 @@ public class RunBasicExperiment extends AbstractHandler implements IElementUpdat
     @SuppressWarnings("rawtypes")\r
        @Override\r
     public void updateElement(UIElement element, Map parameters) {\r
-        ICommandService commandService =\r
-            (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);\r
-        Command command = commandService.getCommand(ToggleSimulation.COMMAND);\r
-        boolean checked = (Boolean) command.getState(ToggleSimulation.STATE).getValue();\r
-        if(checked)\r
-            this.setBaseEnabled(false);\r
-        else\r
-            this.setBaseEnabled(true);\r
-\r
+        IExperimentManager manager = \r
+            SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
+        IExperiment experiment = manager.getActiveExperiment();\r
+        if(experiment instanceof SysdynExperiment) {\r
+               ExperimentState state = experiment.getState();\r
+               if(state == ExperimentState.RUNNING) {\r
+                       this.setBaseEnabled(false);\r
+               } else {\r
+                        this.setBaseEnabled(true);\r
+               }\r
+        }        \r
     }\r
 }\r
index b810ba47a47d4e53caeeba15a49994a4c28b2b5c..b3b9855fe67636defa5933c179efaa3caa2fcb63 100644 (file)
@@ -1,14 +1,10 @@
 package org.simantics.sysdyn.manager;\r
 \r
 import java.io.File;\r
-import java.util.ArrayList;\r
-import java.util.TreeMap;\r
 import java.util.UUID;\r
 import java.util.concurrent.locks.Lock;\r
 \r
 import org.eclipse.core.runtime.Platform;\r
-import org.simantics.databoard.annotations.Optional;\r
-import org.simantics.databoard.binding.mutable.Variant;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.Session;\r
@@ -29,259 +25,196 @@ import org.simantics.sysdyn.simulation.SimulationScheduler;
 \r
 public class SysdynExperiment extends Experiment implements IDynamicExperiment {\r
 \r
-    Session session;\r
-    Runnable modificationListener;\r
-    Runnable modificationListener2;\r
-    Resource experiment;\r
-    SysdynModel sysdynModel;\r
-\r
-    public SysdynExperiment(Resource experiment, Resource model) {\r
-        super(model);\r
-        this.experiment = experiment;\r
-        // TODO Auto-generated constructor stub\r
-    }\r
-    \r
-    public Resource getResource() {\r
-        return this.experiment;\r
-    }\r
-\r
-    @Override\r
-    public void rewindTo(double time) {\r
-        // TODO Auto-generated method stub\r
-        System.out.println("rewindTo");\r
-    }\r
-\r
-    @Override\r
-    public void saveState() {\r
-        // TODO Auto-generated method stub\r
-        System.out.println("saveState");\r
-        if(sysdynModel.getSysdynResult() == null) return;\r
-\r
-        try {\r
-            session.syncRequest(new WriteRequest() {\r
-\r
-                @Override\r
-                public void perform(WriteGraph graph) throws DatabaseException {\r
-                       Layer0 l0 = Layer0.getInstance(graph);\r
-                    Resource model = graph.getSingleObject(experiment, l0.PartOf);\r
-                    Resource project = graph.getSingleObject(model, l0.PartOf);\r
-                    String projectName = graph.getPossibleRelatedValue(project, l0.HasName);\r
-                    File root = new File(Platform.getLocation().toOSString(), "www.simantics.org");\r
-                    if(!root.isDirectory()) root.mkdir();\r
-                    File projectRoot = new File(root, projectName);\r
-                    if(!projectRoot.isDirectory()) projectRoot.mkdir();\r
-                    File file = new File( projectRoot, UUID.randomUUID().toString() + ".dbb");\r
-                    \r
-                    String name = GraphUtils.findFreshName(graph, "Result", model, l0.ConsistsOf, "%s%d");\r
-                    \r
-                    SysdynResource sr = SysdynResource.getInstance(graph);\r
-                    Resource res = GraphUtils.create2(graph, sr.Result,\r
-                            l0.HasLabel, name,\r
-                            l0.HasName, name,\r
-                            l0.PartOf, model,\r
-                            sr.HasResultFile, file.getAbsolutePath());\r
-                    graph.claim(experiment, sr.HasResult, res);\r
-                    sysdynModel.getSysdynResult().saveToFile(file);\r
-                }\r
-            });\r
-        } catch (DatabaseException e) {\r
-            e.printStackTrace();\r
-        }\r
-    }\r
-\r
-\r
-    @Override\r
-    public void simulate(boolean enabled) {\r
-        System.out.println("simulate");\r
-        if(enabled) {\r
-            changeState(ExperimentState.STOPPED);\r
-            startSimulation();\r
-        }\r
-    }\r
-\r
-\r
-    private void startSimulation() {\r
-        try {\r
-            session.syncRequest(new ReadRequest() {\r
-\r
-                @Override\r
-                public void run(ReadGraph graph) throws DatabaseException {\r
-                    SimulationScheduler.start(sysdynModel);\r
-                }\r
-            });\r
-        } catch (DatabaseException e) {\r
-            e.printStackTrace();\r
-        }\r
-    }\r
-\r
-    public void toggleSimulation(boolean enabled) {\r
-        if(enabled) {\r
-            changeState(ExperimentState.RUNNING);\r
-            if(modificationListener == null) {   \r
-                \r
-                modificationListener = new Runnable() {\r
-\r
-                    @Override\r
-                    public void run() {\r
-                        session.asyncRequest(new ReadRequest() {\r
-\r
-                            @Override\r
-                            public void run(ReadGraph graph) throws DatabaseException {\r
-                                if(getState() == ExperimentState.RUNNING) {\r
-                                    SimulationScheduler.start(sysdynModel);\r
-                                }\r
-\r
-                            }\r
-                        });\r
-\r
-                    };\r
-                };\r
-                sysdynModel.addModificationListener(modificationListener);\r
-            } \r
-        }\r
-        else {\r
-            changeState(ExperimentState.STOPPED);            \r
-        }\r
-\r
-    }\r
-\r
-    @Override\r
-    public void simulateDuration(double duration) {\r
-        // TODO Auto-generated method stub\r
-        System.out.println("simulateDuartion");\r
-    }\r
-\r
-    @Override\r
-    public void refresh(Session session) {\r
-        System.out.println("refresh");\r
-        session.asyncRequest(new ReadRequest() {\r
-\r
-            @Override\r
-            public void run(ReadGraph graph) throws DatabaseException {\r
-                init(graph);\r
-            }\r
-            \r
-        });\r
-    }\r
-\r
-    public void init(ReadGraph g) {\r
-        System.out.println("init");\r
-        changeState(ExperimentState.STOPPED);\r
-        this.session = g.getSession();\r
-        \r
-        session.asyncRequest(new ReadRequest() {\r
-            \r
-            @Override\r
-            public void run(ReadGraph graph) throws DatabaseException {\r
-                final Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration);\r
-                sysdynModel = SysdynModelManager.getInstance(session).getModel(graph, configuration);\r
-                toggleActivation(graph, true);\r
-                \r
-                if (modificationListener2 == null) {\r
-                    modificationListener2 = new Runnable() {\r
-\r
-                        @Override\r
-                        public void run() {\r
-                            refresh(session);\r
-                        };\r
-                    };\r
-                    sysdynModel.addModificationListener(modificationListener2);\r
-                }\r
-            }\r
-        });\r
-        \r
-    }\r
-\r
-    @Override\r
-    protected void localStateChange() {\r
-        ExperimentState state = getState();\r
-        switch(state) {\r
-            case DISPOSED:\r
-                System.out.println("disposed");\r
-                session.asyncRequest(new ReadRequest() {\r
-\r
-                    @Override\r
-                    public void run(ReadGraph graph) throws DatabaseException {\r
-                        sysdynModel.removeModificationListener(modificationListener);\r
-                        sysdynModel.removeModificationListener(modificationListener2);\r
-                        modificationListener = null;\r
-                        toggleActivation(graph, false);\r
-                    }\r
-                });\r
-                break;\r
-        }\r
-    }\r
-    \r
-    private void toggleActivation(ReadGraph graph, final boolean activate) {\r
-        VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);\r
-        final Session session = graph.getSession();\r
-        session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("experiments")) {\r
-            @Override\r
-            public void perform(WriteGraph graph) throws DatabaseException {\r
-                VirtualGraph runtime = graph.getService(VirtualGraph.class);\r
-\r
-                session.asyncRequest(new WriteRequest(runtime) {\r
-\r
-                    @Override\r
-                    public void perform(WriteGraph graph) throws DatabaseException {\r
-                        SimulationResource SIMU = SimulationResource.getInstance(graph);\r
-                        if(activate)\r
-                            graph.claim(experiment, SIMU.IsActive, experiment);\r
-                        else\r
-                            graph.denyStatement(experiment, SIMU.IsActive, experiment);\r
-                    }\r
-\r
-                });\r
-            }\r
-        });\r
-    }\r
-  \r
-    public static class Sample {\r
-        public double value;\r
-        \r
-        @Override\r
-        public String toString() {\r
-            return ""+value;\r
-        }\r
-    }\r
-    \r
-    public static class Segment {\r
-        public int startTime, endTime;\r
-        public ArrayList<Sample> samples;\r
-        \r
-        @Override\r
-        public String toString() {\r
-            return "[startTime="+startTime+", endTime="+endTime+", samples="+samples+"]";\r
-        }\r
-        \r
-    }\r
-\r
-    public static class Recording {\r
-        public String id;\r
-        public TreeMap<String, String> labels;\r
-        public ArrayList<Segment> segments;\r
-        \r
-        @Override\r
-        public String toString() {\r
-            return "id="+id+", labels="+labels+", segments="+segments;\r
-        }\r
-    \r
-    }\r
-\r
-    public static class RecordingSession {\r
-        public @Optional Variant datasource;\r
-        public Variant timeVarialbeId;\r
-        public TreeMap<Variant, Variant> recordings;\r
-        \r
-        @Override\r
-        public String toString() {\r
-            return recordings.toString();\r
-        }\r
-    }\r
-    \r
-    public static class NodeId {\r
-        public String nodeId;\r
-    }\r
+       Session session;\r
+       Runnable modificationListener;\r
+       Resource experiment;\r
+       SysdynModel sysdynModel;\r
+       boolean toggled = false;\r
+\r
+       public SysdynExperiment(Resource experiment, Resource model) {\r
+               super(model);\r
+               this.experiment = experiment;\r
+               // TODO Auto-generated constructor stub\r
+       }\r
+\r
+       public Resource getResource() {\r
+               return this.experiment;\r
+       }\r
+\r
+       @Override\r
+       public void rewindTo(double time) {\r
+               // TODO Auto-generated method stub\r
+               System.out.println("rewindTo");\r
+       }\r
+\r
+       @Override\r
+       public void saveState() {\r
+               if(sysdynModel.getSysdynResult() == null) return;\r
+\r
+               try {\r
+                       session.syncRequest(new WriteRequest() {\r
+\r
+                               @Override\r
+                               public void perform(WriteGraph graph) throws DatabaseException {\r
+                                       Layer0 l0 = Layer0.getInstance(graph);\r
+                                       Resource model = graph.getSingleObject(experiment, l0.PartOf);\r
+                                       Resource project = graph.getSingleObject(model, l0.PartOf);\r
+                                       String projectName = graph.getPossibleRelatedValue(project, l0.HasName);\r
+                                       File root = new File(Platform.getLocation().toOSString(), "www.simantics.org");\r
+                                       if(!root.isDirectory()) root.mkdir();\r
+                                       File projectRoot = new File(root, projectName);\r
+                                       if(!projectRoot.isDirectory()) projectRoot.mkdir();\r
+                                       File file = new File( projectRoot, UUID.randomUUID().toString() + ".dbb");\r
+\r
+                                       String name = GraphUtils.findFreshName(graph, "Result", model, l0.ConsistsOf, "%s%d");\r
+\r
+                                       SysdynResource sr = SysdynResource.getInstance(graph);\r
+                                       Resource res = GraphUtils.create2(graph, sr.Result,\r
+                                                       l0.HasLabel, name,\r
+                                                       l0.HasName, name,\r
+                                                       l0.PartOf, model,\r
+                                                       sr.HasResultFile, file.getAbsolutePath());\r
+                                       graph.claim(experiment, sr.HasResult, res);\r
+                                       sysdynModel.getSysdynResult().saveToFile(file);\r
+                               }\r
+                       });\r
+               } catch (DatabaseException e) {\r
+                       e.printStackTrace();\r
+               }\r
+       }\r
+\r
+\r
+       @Override\r
+       public void simulate(boolean enabled) {\r
+               if(enabled) {\r
+                       changeState(ExperimentState.RUNNING);\r
+                       startSimulation();\r
+               } else if (!toggled){\r
+                       changeState(ExperimentState.STOPPED);\r
+               }\r
+       }\r
+\r
+\r
+       private void startSimulation() {\r
+               session.asyncRequest(new ReadRequest() {\r
+\r
+                       @Override\r
+                       public void run(ReadGraph graph) throws DatabaseException {\r
+                               SimulationScheduler.start(sysdynModel, SysdynExperiment.this);\r
+                       }\r
+               });\r
+       }\r
+\r
+       public void toggleSimulation(boolean enabled) {\r
+               if(enabled) {\r
+                       this.toggled = true;\r
+                       changeState(ExperimentState.RUNNING);\r
+                       if(modificationListener == null) {   \r
+\r
+                               modificationListener = new Runnable() {\r
+\r
+                                       @Override\r
+                                       public void run() {\r
+                                               session.asyncRequest(new ReadRequest() {\r
+\r
+                                                       @Override\r
+                                                       public void run(ReadGraph graph) throws DatabaseException {\r
+                                                               if(getState() == ExperimentState.RUNNING) {\r
+                                                                       SimulationScheduler.start(sysdynModel, SysdynExperiment.this);\r
+                                                               }\r
+\r
+                                                       }\r
+                                               });\r
+\r
+                                       };\r
+                               };\r
+                               sysdynModel.addModificationListener(modificationListener);\r
+                       } \r
+               }\r
+               else {\r
+                       changeState(ExperimentState.STOPPED);\r
+                       this.toggled = false;\r
+               }\r
+\r
+       }\r
+\r
+       @Override\r
+       public void simulateDuration(double duration) {\r
+               // TODO Auto-generated method stub\r
+               System.out.println("simulateDuartion");\r
+       }\r
+\r
+       @Override\r
+       public void refresh(Session session) {\r
+               session.asyncRequest(new ReadRequest() {\r
+\r
+                       @Override\r
+                       public void run(ReadGraph graph) throws DatabaseException {\r
+                               init(graph);\r
+                       }\r
+\r
+               });\r
+       }\r
+\r
+       public void init(ReadGraph g) {\r
+               changeState(ExperimentState.STOPPED);\r
+               this.session = g.getSession();\r
+\r
+               session.asyncRequest(new ReadRequest() {\r
+\r
+                       @Override\r
+                       public void run(ReadGraph graph) throws DatabaseException {\r
+                               final Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration);\r
+                               sysdynModel = SysdynModelManager.getInstance(session).getModel(graph, configuration);\r
+                               toggleActivation(graph, true);\r
+                       }\r
+               });\r
+\r
+       }\r
+\r
+       @Override\r
+       protected void localStateChange() {\r
+               ExperimentState state = getState();\r
+               switch(state) {\r
+               case DISPOSED:\r
+                       System.out.println("disposed");\r
+                       session.asyncRequest(new ReadRequest() {\r
+\r
+                               @Override\r
+                               public void run(ReadGraph graph) throws DatabaseException {\r
+                                       sysdynModel.cancelSimulation();\r
+                                       sysdynModel.removeModificationListener(modificationListener);\r
+                                       modificationListener = null;\r
+                                       toggleActivation(graph, false);\r
+                               }\r
+                       });\r
+                       break;\r
+               }\r
+       }\r
+\r
+       private void toggleActivation(ReadGraph graph, final boolean activate) {\r
+               VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);\r
+               final Session session = graph.getSession();\r
+               session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("experiments")) {\r
+                       @Override\r
+                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                               VirtualGraph runtime = graph.getService(VirtualGraph.class);\r
+\r
+                               session.asyncRequest(new WriteRequest(runtime) {\r
+\r
+                                       @Override\r
+                                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                                               SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+                                               if(activate)\r
+                                                       graph.claim(experiment, SIMU.IsActive, experiment);\r
+                                               else\r
+                                                       graph.denyStatement(experiment, SIMU.IsActive, experiment);\r
+                                       }\r
+\r
+                               });\r
+                       }\r
+               });\r
+       }\r
 \r
        @Override\r
        public Lock getDatasourceLock() {\r
index c7a7e88e5d9fbba5c04826138e6fd3145f63e448..5d81e0dfb6821bed7f6fd7d2163dde1908163a82 100644 (file)
@@ -39,6 +39,8 @@ import org.simantics.modelica.data.SimulationResult;
 import org.simantics.objmap.IMapping;\r
 import org.simantics.objmap.IMappingListener;\r
 import org.simantics.objmap.Mappings;\r
+import org.simantics.simulation.experiment.Experiment;\r
+import org.simantics.simulation.experiment.ExperimentState;\r
 import org.simantics.simulation.experiment.IExperiment;\r
 import org.simantics.simulation.model.IModel;\r
 import org.simantics.simulation.ontology.SimulationResource;\r
@@ -61,361 +63,373 @@ import org.simantics.sysdyn.representation.expressions.ParameterExpression;
  */\r
 public class SysdynModel implements IMappingListener, IModel {\r
 \r
-    Session session;\r
+       Session session;\r
 \r
-    IMapping mapping;\r
+       IMapping mapping;\r
 \r
-    Resource configurationResource;\r
-    Configuration configuration;\r
+       Resource configurationResource;\r
+       Configuration configuration;\r
 \r
-    Set<Configuration> modules = new HashSet<Configuration>();\r
+       Set<Configuration> modules = new HashSet<Configuration>();\r
 \r
-    Process process;\r
-    boolean canceled;\r
-    SimulationResult result;\r
-    SysdynResult sysdynResult;\r
+       Process process;\r
+       boolean canceled;\r
+       SimulationResult result;\r
+       SysdynResult sysdynResult;\r
 \r
-    CopyOnWriteArrayList<Runnable> modificationListeners =\r
-        new CopyOnWriteArrayList<Runnable>();\r
-    CopyOnWriteArrayList<Runnable> resultListeners =\r
-        new CopyOnWriteArrayList<Runnable>();\r
+       CopyOnWriteArrayList<Runnable> modificationListeners =\r
+               new CopyOnWriteArrayList<Runnable>();\r
+       CopyOnWriteArrayList<Runnable> resultListeners =\r
+               new CopyOnWriteArrayList<Runnable>();\r
 \r
-    @SuppressWarnings("rawtypes")\r
+       @SuppressWarnings("rawtypes")\r
        Map<Class, Object> services = new HashMap<Class, Object>();\r
 \r
-    String previousModelStructure;\r
-    HashMap<String, String> previousImportantInits = new HashMap<String, String>();\r
-\r
-    File simulationDir;\r
-\r
-    void readModules(ReadGraph graph, Resource configResource, Set<Resource> result) throws DatabaseException {\r
-\r
-        if(!result.add(configResource)) return;\r
-\r
-        Layer0 l0 = Layer0.getInstance(graph);\r
-        SysdynResource sr = SysdynResource.getInstance(graph);\r
-        StructuralResource2 str = StructuralResource2.getInstance(graph);\r
-\r
-        for(Resource part : graph.getObjects(configResource, l0.ConsistsOf)) {\r
-            if(graph.isInstanceOf(part, sr.Module)) {\r
-                Resource type = graph.getPossibleType(part, sr.Module);\r
-                Resource config = graph.getPossibleObject(type, str.IsDefinedBy);\r
-                readModules(graph, config, result);\r
-            }\r
-        }\r
-\r
-    }\r
-\r
-    Set<Resource> readModules(ReadGraph graph, Resource configResource) throws DatabaseException {\r
-        HashSet<Resource> result = new HashSet<Resource>();\r
-        readModules(graph, configResource, result);\r
-        return result;\r
-    }\r
-\r
-    private void createMapping(ReadGraph g) throws DatabaseException {\r
-        SysdynSchema schema = new SysdynSchema(g);\r
-        mapping = Mappings.createWithListening(schema);\r
-        mapping.addMappingListener(SysdynModel.this);\r
-        configuration = (Configuration)mapping.map(g, configurationResource);\r
-        for(Resource config : readModules(g, configurationResource)) {\r
-            modules.add((Configuration)mapping.map(g, config));\r
-        }\r
-        System.out.println("Loaded model with " + modules.size() + " modules.");\r
-    }\r
-\r
-    public SysdynModel(ReadGraph g, Resource configurationResource) {\r
-        this.session = g.getSession();\r
-        this.configurationResource = configurationResource;\r
-\r
-        try {\r
-            createMapping(g);\r
-        } catch(DatabaseException e) {\r
-            e.printStackTrace();\r
-        }\r
-        sysdynResult = new SysdynResult();\r
-        sysdynResult.setResult(new SimulationResult());\r
-\r
-        previousModelStructure = "";\r
-\r
-        File modelsDir = Activator.getBundleContext().getDataFile("models");\r
-        String configName = configuration.getName();\r
-        List<String> files = Arrays.asList(modelsDir.list());\r
-        if (files.contains(configName)) {\r
-            int i = 2;\r
-            while (files.contains(configName + "_" + i)){\r
-                i++;\r
-            }\r
-            configName += "_" + i;\r
-        }\r
-\r
-        simulationDir  = Activator.getBundleContext().getDataFile("models/" + configName);\r
-        if (!simulationDir.exists()) {\r
-            simulationDir.mkdir();\r
-        }\r
-    }\r
-\r
-\r
-    /*\r
-     * some dummy(?) stuff for experiments\r
-     */\r
-    public SysdynModel(Resource configurationResource) {\r
-        this.configurationResource = configurationResource;\r
-    }\r
-\r
-    public synchronized void simulate(IModelicaMonitor monitor, final IProgressMonitor progressMonitor) throws IOException {\r
-        canceled = false;\r
-        \r
-        ModelicaWriter writer = new ModelicaWriter();\r
-        //writer.write(configuration);\r
-        for(Configuration c : modules) {\r
-            writer.write(c);\r
-        }\r
-\r
-        progressMonitor.worked(20);\r
-\r
-        String modelText = writer.toString();\r
-\r
-        HashMap<String, String> inits = getInits(configuration, "");\r
-\r
-        Model model = configuration.getModel();\r
-\r
-        String startTime = model.getStartTime().toString();\r
-        String stopTime = model.getStopTime().toString();\r
-        inits.put("start value", startTime);\r
-        inits.put("stop value", stopTime);\r
-        if(model.getSolver() != null)\r
-            inits.put("solver", model.getSolver());\r
-        if(model.getTolerance() != null)\r
-            inits.put("tolerance", model.getTolerance().toString());\r
-\r
-        final SimulationLocation simulationLocation = ModelicaManager.createInputFiles(\r
-                simulationDir,\r
-                configuration.getName(),\r
-                writer.toString(),\r
-                inits);\r
-\r
-        progressMonitor.worked(20);\r
-\r
-        if (hasStructureChanged(modelText, inits)) {\r
-            previousModelStructure = modelText;\r
-            System.out.println("== Modelica == ");\r
-            System.out.println(writer.toString());\r
-            System.out.println("== Modelica ends == ");\r
-\r
-            try {\r
+       String previousModelStructure;\r
+       HashMap<String, String> previousImportantInits = new HashMap<String, String>();\r
+\r
+       File simulationDir;\r
+\r
+       void readModules(ReadGraph graph, Resource configResource, Set<Resource> result) throws DatabaseException {\r
+\r
+               if(!result.add(configResource)) return;\r
+\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               StructuralResource2 str = StructuralResource2.getInstance(graph);\r
+\r
+               for(Resource part : graph.getObjects(configResource, l0.ConsistsOf)) {\r
+                       if(graph.isInstanceOf(part, sr.Module)) {\r
+                               Resource type = graph.getPossibleType(part, sr.Module);\r
+                               Resource config = graph.getPossibleObject(type, str.IsDefinedBy);\r
+                               readModules(graph, config, result);\r
+                       }\r
+               }\r
+\r
+       }\r
+\r
+       Set<Resource> readModules(ReadGraph graph, Resource configResource) throws DatabaseException {\r
+               HashSet<Resource> result = new HashSet<Resource>();\r
+               readModules(graph, configResource, result);\r
+               return result;\r
+       }\r
+\r
+       private void createMapping(ReadGraph g) throws DatabaseException {\r
+               SysdynSchema schema = new SysdynSchema(g);\r
+               mapping = Mappings.createWithListening(schema);\r
+               mapping.addMappingListener(SysdynModel.this);\r
+               configuration = (Configuration)mapping.map(g, configurationResource);\r
+               for(Resource config : readModules(g, configurationResource)) {\r
+                       modules.add((Configuration)mapping.map(g, config));\r
+               }\r
+               System.out.println("Loaded model with " + modules.size() + " modules.");\r
+       }\r
+\r
+       public SysdynModel(ReadGraph g, Resource configurationResource) {\r
+               this.session = g.getSession();\r
+               this.configurationResource = configurationResource;\r
+\r
+               try {\r
+                       createMapping(g);\r
+               } catch(DatabaseException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               sysdynResult = new SysdynResult();\r
+               sysdynResult.setResult(new SimulationResult());\r
+\r
+               previousModelStructure = "";\r
+\r
+               File modelsDir = Activator.getBundleContext().getDataFile("models");\r
+               String configName = configuration.getName();\r
+               List<String> files = Arrays.asList(modelsDir.list());\r
+               if (files.contains(configName)) {\r
+                       int i = 2;\r
+                       while (files.contains(configName + "_" + i)){\r
+                               i++;\r
+                       }\r
+                       configName += "_" + i;\r
+               }\r
+\r
+               simulationDir  = Activator.getBundleContext().getDataFile("models/" + configName);\r
+               if (!simulationDir.exists()) {\r
+                       simulationDir.mkdir();\r
+               }\r
+       }\r
+\r
+\r
+       /*\r
+        * some dummy(?) stuff for experiments\r
+        */\r
+       public SysdynModel(Resource configurationResource) {\r
+               this.configurationResource = configurationResource;\r
+       }\r
+\r
+       public synchronized void simulate(IModelicaMonitor monitor, final IProgressMonitor progressMonitor, final Experiment experiment) throws IOException {\r
+               canceled = false;\r
+               progressMonitor.subTask("Write modelica classes");\r
+               ModelicaWriter writer = new ModelicaWriter();\r
+               for(Configuration c : modules) {\r
+                       writer.write(c);\r
+               }\r
+               progressMonitor.worked(1);\r
+\r
+               progressMonitor.subTask("Write initial files");\r
+               String modelText = writer.toString();\r
+               HashMap<String, String> inits = getInits(configuration, "");\r
+               Model model = configuration.getModel();\r
+               String startTime = model.getStartTime().toString();\r
+               String stopTime = model.getStopTime().toString();\r
+               inits.put("start value", startTime);\r
+               inits.put("stop value", stopTime);\r
+               if(model.getSolver() != null)\r
+                       inits.put("solver", model.getSolver());\r
+               if(model.getTolerance() != null)\r
+                       inits.put("tolerance", model.getTolerance().toString());\r
+\r
+               final SimulationLocation simulationLocation = ModelicaManager.createInputFiles(\r
+                               simulationDir,\r
+                               configuration.getName(),\r
+                               writer.toString(),\r
+                               inits);\r
+\r
+               progressMonitor.worked(1);\r
+\r
+\r
+               if (!simulationLocation.exeFile.isFile() || hasStructureChanged(modelText, inits)) {\r
+                       progressMonitor.subTask("Build model");\r
+                       previousModelStructure = modelText;\r
+                       System.out.println("== Modelica == ");\r
+                       System.out.println(writer.toString());\r
+                       System.out.println("== Modelica ends == ");\r
+\r
+                       try {\r
+                               simulationLocation.exeFile.delete();\r
                                ModelicaManager.buildModel(simulationLocation, monitor);\r
-                   previousImportantInits.clear();\r
-                   previousImportantInits.put("start value", startTime);\r
-                   previousImportantInits.put("stop value", stopTime);\r
+                               previousImportantInits.clear();\r
+                               previousImportantInits.put("start value", startTime);\r
+                               previousImportantInits.put("stop value", stopTime);\r
                        } catch (ModelicaException e) {\r
                                monitor.showConsole();\r
                                canceled = true;\r
                                previousModelStructure = "";\r
                        }\r
-        }\r
-\r
-        progressMonitor.worked(20);\r
-\r
-        if(simulationLocation != null && !canceled) {\r
-            process = ModelicaManager.runModelica(\r
-                    simulationLocation,\r
-                    monitor,\r
-                    inits\r
-            );\r
-            ModelicaManager.printProcessOutput(process, monitor);\r
-\r
-            Thread resultThread = new Thread() {\r
-                @Override\r
-                public void run() {\r
-                    try {\r
-                        process.waitFor();\r
-\r
-                        if(!canceled) {\r
-                            result = new SimulationResult();\r
-                            result.read(simulationLocation.outputFile);\r
-                            result.readInits(simulationLocation.initFile);\r
-                            result.filter();\r
-                            progressMonitor.worked(20);\r
-                            sysdynResult.setResult(result);\r
-                            progressMonitor.worked(20);\r
-                            synchronized(resultListeners) {\r
-                                for(Runnable listener : resultListeners)\r
-                                    listener.run();\r
-                            }\r
-                        }\r
-                    } catch (FileNotFoundException e) {\r
-                        e.printStackTrace();\r
-                    } catch (IOException e) {\r
-                        e.printStackTrace();\r
-                    } catch (InterruptedException e) {\r
-                        e.printStackTrace();\r
-                    }\r
-                }\r
-            };\r
-            resultThread.run();\r
-        }\r
-        process = null;\r
-    }\r
-\r
-    private boolean hasStructureChanged(String modelText, Map<String, String> inits) {\r
-       if (!modelText.equals(previousModelStructure))\r
-               return true;\r
-       for(String key : previousImportantInits.keySet()) {\r
-               if(!inits.containsKey(key) || !previousImportantInits.get(key).equals(inits.get(key)))\r
-                       return true;\r
-       }\r
-       return false;\r
-    }\r
-    \r
-    public void cancelSimulation() {\r
-        canceled = true;\r
-        if(process != null) {\r
-            process.destroy();\r
-        }\r
-    }\r
-\r
-    public synchronized boolean update() throws DatabaseException {\r
-        if(mapping.isDomainModified()) {\r
-            session.syncRequest(new ReadRequest() {\r
-                @Override\r
-                public void run(ReadGraph graph) throws DatabaseException {\r
-                    mapping.updateRange(graph);\r
-                }\r
-            });\r
-            return true;\r
-        }\r
-        else\r
-            return false;\r
-    }\r
-\r
-    public SimulationResult getSimulationResult() {\r
-        return result;\r
-    }\r
-\r
-    public SysdynResult getSysdynResult() {\r
-        return sysdynResult;\r
-    }\r
-\r
-    public void addResultListener(Runnable listener) {\r
-        synchronized(resultListeners) {\r
-            resultListeners.add(listener);\r
-        }\r
-    }\r
-\r
-    public void removeResultListener(Runnable listener) {\r
-        synchronized(resultListeners) {\r
-            resultListeners.add(listener);\r
-        }\r
-    }\r
-\r
-    public void addModificationListener(Runnable listener) {\r
-        synchronized(modificationListeners) {\r
-            modificationListeners.add(listener);\r
-        }\r
-    }\r
-\r
-    public void removeModificationListener(Runnable listener) {\r
-        synchronized(modificationListeners) {\r
-            modificationListeners.remove(listener);\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public void domainModified() {\r
-        synchronized(modificationListeners) {\r
-            for(Runnable listener : modificationListeners)\r
-                listener.run();\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public void rangeModified() {\r
-    }\r
-\r
-    public Configuration getConfiguration() {\r
-        return configuration;\r
-    }\r
-\r
-    public IMapping getMapping() {\r
-        return mapping;\r
-    }\r
-\r
-    public synchronized IElement getElement(Resource resource) {\r
-        return (IElement)mapping.get(resource);\r
-    }\r
-\r
-    public <T> T getService(Class<T> clazz) {\r
-        synchronized(services) {\r
-            return clazz.cast(services.get(clazz));\r
-        }\r
-    }\r
-\r
-    public <T> void addService(Class<T> clazz, T service) {\r
-        synchronized(services) {\r
-            services.put(clazz, service);\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public IExperiment loadExperiment(ReadGraph g, Resource experiment, IExperimentActivationListener listener) {\r
-        SysdynExperiment exp = new SysdynExperiment(experiment, configurationResource);\r
-        try {\r
-            exp.init(g);\r
-            listener.onExperimentActivated(exp);\r
-            return exp;\r
-        } catch(Exception e) {\r
-            listener.onFailure(e);\r
-            return null;\r
-        }\r
-    }\r
-\r
-    public Collection<SysdynResult> getActiveResults(ReadGraph graph) {\r
-        ArrayList<SysdynResult> results = new ArrayList<SysdynResult>();\r
-\r
-        try {\r
-            Layer0 l0 = Layer0.getInstance(graph);\r
-            SysdynResource sr = SysdynResource.getInstance(graph);\r
-            SimulationResource SIMU = SimulationResource.getInstance(graph);\r
-            Resource model = graph.getSingleObject(configurationResource, SIMU.IsConfigurationOf);\r
-            Collection<Resource> experiments = graph.getObjects(model, l0.ConsistsOf);\r
-            for(Resource experiment : experiments) {\r
-                Collection<Resource> experimentResults = graph.getObjects(experiment, sr.HasResult);\r
-                for(Resource result : experimentResults) {\r
-                    if(graph.hasStatement(result, SIMU.IsActive)) {\r
-                        SysdynResult sysdynResult = new SysdynResult(\r
-                                (String) graph.getPossibleRelatedValue(result, l0.HasLabel),\r
-                                (String) graph.getPossibleRelatedValue(result, sr.HasResultFile));\r
-                        results.add(sysdynResult);\r
-                    }\r
-                }\r
-            }\r
-        } catch (Exception e) {\r
-            e.printStackTrace();\r
-        }\r
-\r
-        if(getSysdynResult() != null)\r
-            results.add(0, getSysdynResult() );\r
-\r
-\r
-\r
-        return results;\r
-    }\r
-\r
-    private HashMap<String, String> getInits(Configuration configuration, String prefix) {\r
-        HashMap<String, String> inits = new HashMap<String, String>();\r
-        for (IElement element : configuration.getElements()) {\r
-            if (element instanceof Module) {\r
-                Module module = (Module) element;\r
-                Configuration conf = module.getType().getConfiguration();\r
-                inits.putAll(getInits(conf, prefix + module.getName() + "."));\r
-            } else if (element instanceof IndependentVariable) {\r
-                IndependentVariable variable = (IndependentVariable) element;\r
-                if (variable.getExpression() instanceof ParameterExpression) {\r
-                    inits.put(prefix + variable.getName(), "" + ((ParameterExpression)variable.getExpression()).getValue());\r
-                }\r
-            }\r
-        }\r
-        return inits;\r
-    }\r
+               }\r
+\r
+               progressMonitor.worked(1);\r
+\r
+               if(simulationLocation != null && !canceled) {\r
+                       progressMonitor.subTask("Simulate model");\r
+                       process = ModelicaManager.runModelica(\r
+                                       simulationLocation,\r
+                                       monitor,\r
+                                       inits\r
+                       );\r
+                       ModelicaManager.printProcessOutput(process, monitor);\r
+\r
+                       Thread resultThread = new Thread() {\r
+                               @Override\r
+                               public void run() {\r
+                                       try {\r
+                                               process.waitFor();\r
+\r
+                                               if(!canceled) {\r
+                                                       progressMonitor.worked(1);\r
+                                                       progressMonitor.subTask("Read results");\r
+                                                       result = new SimulationResult();\r
+                                                       result.read(simulationLocation.outputFile);\r
+                                                       result.readInits(simulationLocation.initFile);\r
+                                                       result.filter();\r
+                                                       sysdynResult.setResult(result);\r
+                                                       progressMonitor.worked(1);\r
+                                                       synchronized(resultListeners) {\r
+                                                               for(Runnable listener : resultListeners)\r
+                                                                       listener.run();\r
+                                                       }\r
+                                                       setExperimentStopped(experiment);\r
+                                               }\r
+                                       } catch (FileNotFoundException e) {\r
+                                               e.printStackTrace();\r
+                                       } catch (IOException e) {\r
+                                               e.printStackTrace();\r
+                                       } catch (InterruptedException e) {\r
+                                               e.printStackTrace();\r
+                                       }\r
+                               }\r
+                       };\r
+                       resultThread.run();\r
+               }\r
+               if(canceled)\r
+                       setExperimentStopped(experiment);\r
+               process = null;\r
+       }\r
+\r
+       private void setExperimentStopped(Experiment experiment) {\r
+               if(experiment instanceof SysdynExperiment) {\r
+                       SysdynExperiment e = (SysdynExperiment)experiment;\r
+                       if(e.getState() != ExperimentState.DISPOSED)\r
+                               e.simulate(false);\r
+               }\r
+       }\r
+\r
+       private boolean hasStructureChanged(String modelText, Map<String, String> inits) {\r
+               if (!modelText.equals(previousModelStructure))\r
+                       return true;\r
+               for(String key : previousImportantInits.keySet()) {\r
+                       if(!inits.containsKey(key) || !previousImportantInits.get(key).equals(inits.get(key)))\r
+                               return true;\r
+               }\r
+               return false;\r
+       }\r
+\r
+       public void cancelSimulation() {\r
+               canceled = true;\r
+               if(process != null) {\r
+                       process.destroy();\r
+               }\r
+       }\r
+\r
+       public synchronized boolean update() throws DatabaseException {\r
+               if(mapping.isDomainModified()) {\r
+                       session.syncRequest(new ReadRequest() {\r
+                               @Override\r
+                               public void run(ReadGraph graph) throws DatabaseException {\r
+                                       mapping.updateRange(graph);\r
+                               }\r
+                       });\r
+                       return true;\r
+               }\r
+               else\r
+                       return false;\r
+       }\r
+\r
+       public SimulationResult getSimulationResult() {\r
+               return result;\r
+       }\r
+\r
+       public SysdynResult getSysdynResult() {\r
+               return sysdynResult;\r
+       }\r
+\r
+       public void addResultListener(Runnable listener) {\r
+               synchronized(resultListeners) {\r
+                       resultListeners.add(listener);\r
+               }\r
+       }\r
+\r
+       public void removeResultListener(Runnable listener) {\r
+               synchronized(resultListeners) {\r
+                       resultListeners.add(listener);\r
+               }\r
+       }\r
+\r
+       public void addModificationListener(Runnable listener) {\r
+               synchronized(modificationListeners) {\r
+                       modificationListeners.add(listener);\r
+               }\r
+       }\r
+\r
+       public void removeModificationListener(Runnable listener) {\r
+               synchronized(modificationListeners) {\r
+                       modificationListeners.remove(listener);\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public void domainModified() {\r
+               synchronized(modificationListeners) {\r
+                       for(Runnable listener : modificationListeners)\r
+                               listener.run();\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public void rangeModified() {\r
+       }\r
+\r
+       public Configuration getConfiguration() {\r
+               return configuration;\r
+       }\r
+\r
+       public IMapping getMapping() {\r
+               return mapping;\r
+       }\r
+\r
+       public synchronized IElement getElement(Resource resource) {\r
+               return (IElement)mapping.get(resource);\r
+       }\r
+\r
+       public <T> T getService(Class<T> clazz) {\r
+               synchronized(services) {\r
+                       return clazz.cast(services.get(clazz));\r
+               }\r
+       }\r
+\r
+       public <T> void addService(Class<T> clazz, T service) {\r
+               synchronized(services) {\r
+                       services.put(clazz, service);\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public IExperiment loadExperiment(ReadGraph g, Resource experiment, IExperimentActivationListener listener) {\r
+               SysdynExperiment exp = new SysdynExperiment(experiment, configurationResource);\r
+               try {\r
+                       exp.init(g);\r
+                       listener.onExperimentActivated(exp);\r
+                       return exp;\r
+               } catch(Exception e) {\r
+                       listener.onFailure(e);\r
+                       return null;\r
+               }\r
+       }\r
+\r
+       public Collection<SysdynResult> getActiveResults(ReadGraph graph) {\r
+               ArrayList<SysdynResult> results = new ArrayList<SysdynResult>();\r
+\r
+               try {\r
+                       Layer0 l0 = Layer0.getInstance(graph);\r
+                       SysdynResource sr = SysdynResource.getInstance(graph);\r
+                       SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+                       Resource model = graph.getSingleObject(configurationResource, SIMU.IsConfigurationOf);\r
+                       Collection<Resource> experiments = graph.getObjects(model, l0.ConsistsOf);\r
+                       for(Resource experiment : experiments) {\r
+                               Collection<Resource> experimentResults = graph.getObjects(experiment, sr.HasResult);\r
+                               for(Resource result : experimentResults) {\r
+                                       if(graph.hasStatement(result, SIMU.IsActive)) {\r
+                                               SysdynResult sysdynResult = new SysdynResult(\r
+                                                               (String) graph.getPossibleRelatedValue(result, l0.HasLabel),\r
+                                                               (String) graph.getPossibleRelatedValue(result, sr.HasResultFile));\r
+                                               results.add(sysdynResult);\r
+                                       }\r
+                               }\r
+                       }\r
+               } catch (Exception e) {\r
+                       e.printStackTrace();\r
+               }\r
+\r
+               if(getSysdynResult() != null)\r
+                       results.add(0, getSysdynResult() );\r
+\r
+\r
+\r
+               return results;\r
+       }\r
+\r
+       private HashMap<String, String> getInits(Configuration configuration, String prefix) {\r
+               HashMap<String, String> inits = new HashMap<String, String>();\r
+               for (IElement element : configuration.getElements()) {\r
+                       if (element instanceof Module) {\r
+                               Module module = (Module) element;\r
+                               Configuration conf = module.getType().getConfiguration();\r
+                               inits.putAll(getInits(conf, prefix + module.getName() + "."));\r
+                       } else if (element instanceof IndependentVariable) {\r
+                               IndependentVariable variable = (IndependentVariable) element;\r
+                               if (variable.getExpression() instanceof ParameterExpression) {\r
+                                       inits.put(prefix + variable.getName(), "" + ((ParameterExpression)variable.getExpression()).getValue());\r
+                               }\r
+                       }\r
+               }\r
+               return inits;\r
+       }\r
 \r
 }\r
index 02166691763e9cafcac3ee81790bb4e527a151e9..8f8a4fb773b6469ba47e99fced225397f53f884d 100644 (file)
@@ -11,6 +11,9 @@
  *******************************************************************************/\r
 package org.simantics.sysdyn.simulation;\r
 \r
+import java.text.SimpleDateFormat;\r
+import java.util.Calendar;\r
+\r
 import org.eclipse.core.runtime.IProgressMonitor;\r
 import org.eclipse.core.runtime.IStatus;\r
 import org.eclipse.core.runtime.Status;\r
@@ -21,34 +24,38 @@ import org.eclipse.ui.console.IConsoleManager;
 import org.eclipse.ui.console.MessageConsole;\r
 import org.eclipse.ui.console.MessageConsoleStream;\r
 import org.simantics.modelica.IModelicaMonitor;\r
+import org.simantics.simulation.experiment.Experiment;\r
 import org.simantics.sysdyn.manager.SysdynModel;\r
 \r
 public class SimulationJob extends Job {\r
 \r
     SysdynModel model;\r
+    Experiment experiment;\r
     ModelicaMonitor monitor;\r
 \r
-    public SimulationJob(SysdynModel model) {\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
     }\r
 \r
     @Override\r
     protected IStatus run(IProgressMonitor monitor) {\r
-        monitor.beginTask("Simulate " + model.getConfiguration().getName(), 100);\r
-        this.monitor.clearConsole();\r
+        monitor.beginTask("Simulate " + model.getConfiguration().getName(), 5);        \r
+        this.monitor.message("Simulate " + model.getConfiguration().getName());\r
         try {\r
             model.update();\r
-            model.simulate(this.monitor, monitor);\r
-            monitor.worked(50);\r
+            model.simulate(this.monitor, monitor, experiment);\r
         } catch (Exception e) {\r
             e.printStackTrace();\r
+            this.monitor.showConsole();\r
             return new Status(\r
                     Status.ERROR, \r
                     "org.simantics.sysdyn.ui", \r
                     "Simulation failed: \n" + e.getMessage());\r
         } \r
+        this.monitor.message("\n");\r
         monitor.done(); \r
         return Status.OK_STATUS;\r
     }\r
@@ -79,8 +86,25 @@ public class SimulationJob extends Job {
 \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
             MessageConsoleStream out = this.console.newMessageStream();\r
-            out.println(message);\r
+            if(message.length() > 1)\r
+               out.println("[" + time +"] " + message);\r
+            if(message.contains("Error:"))\r
+               showConsole();\r
         }\r
 \r
         public void clearConsole() {\r
index cd048be4a506e3cedae267c5055fbc80f9acb624..534168f360f91080b09fcd031cede55cf506f97f 100644 (file)
  *******************************************************************************/\r
 package org.simantics.sysdyn.simulation;\r
 \r
+import org.simantics.simulation.experiment.Experiment;\r
 import org.simantics.sysdyn.manager.SysdynModel;\r
 \r
 public class SimulationScheduler {\r
     SysdynModel model;\r
-\r
+    Experiment experiment;\r
     SimulationJob job;\r
     \r
-    private SimulationScheduler(SysdynModel model) {    \r
+    private SimulationScheduler(SysdynModel model, Experiment experiment) {    \r
         this.model = model;\r
-        this.job = new SimulationJob(model);\r
+        this.experiment = experiment;\r
+        this.job = new SimulationJob(model, experiment);\r
     }\r
     \r
     private void start() {\r
         job.schedule();\r
-        /*\r
-         * Don't want this behavior. Use this in simulate on change or somewhere\r
-        model.addModificationListener(new Runnable() {\r
-\r
-            @Override\r
-            public void run() {\r
-                job.schedule();\r
-            }\r
-            \r
-        });\r
-        */\r
     }\r
 \r
-    public static synchronized SimulationScheduler start(SysdynModel model) {\r
+    public static synchronized SimulationScheduler start(SysdynModel model, Experiment experiment) {\r
         SimulationScheduler scheduler = model.getService(SimulationScheduler.class);\r
         if(scheduler == null) {\r
-            scheduler = new SimulationScheduler(model);\r
+            scheduler = new SimulationScheduler(model, experiment);\r
             model.addService(SimulationScheduler.class, scheduler);\r
             scheduler.start();\r
         } else {\r