]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
SimulationJob (sysdyn)
authorniemisto <niemisto@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 29 Jan 2010 07:24:43 +0000 (07:24 +0000)
committerniemisto <niemisto@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 29 Jan 2010 07:24:43 +0000 (07:24 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13755 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/simulation/SimulationJob.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/simulation/SimulationScheduler.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java

diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/simulation/SimulationJob.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/simulation/SimulationJob.java
new file mode 100644 (file)
index 0000000..bcf8dfa
--- /dev/null
@@ -0,0 +1,33 @@
+package org.simantics.sysdyn.ui.simulation;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.core.runtime.jobs.Job;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+\r
+public class SimulationJob extends Job {\r
+\r
+    SysdynModel model;\r
+    \r
+    public SimulationJob(SysdynModel model) {\r
+        super("Simulate " + model.getConfiguration().name);\r
+        this.model = model;\r
+    }\r
+\r
+    @Override\r
+    protected IStatus run(IProgressMonitor monitor) {\r
+        try {\r
+            model.update();\r
+            model.simulate();\r
+        } catch (Exception e) {\r
+            e.printStackTrace();\r
+            return new Status(\r
+                    Status.ERROR, \r
+                    "org.simantics.sysdyn.ui", \r
+                    "Simulation failed: " + e.getMessage());\r
+        } \r
+        return Status.OK_STATUS;\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/simulation/SimulationScheduler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/simulation/SimulationScheduler.java
new file mode 100644 (file)
index 0000000..4874448
--- /dev/null
@@ -0,0 +1,37 @@
+package org.simantics.sysdyn.ui.simulation;\r
+\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+\r
+public class SimulationScheduler {\r
+    SysdynModel model;\r
+\r
+    SimulationJob job;\r
+    \r
+    private SimulationScheduler(SysdynModel model) {    \r
+        this.model = model;\r
+        this.job = new SimulationJob(model);\r
+    }\r
+    \r
+    private void start() {\r
+        job.schedule();\r
+        model.addModificationListener(new Runnable() {\r
+\r
+            @Override\r
+            public void run() {\r
+                job.schedule();\r
+            }\r
+            \r
+        });\r
+    }\r
+\r
+    public static SimulationScheduler start(SysdynModel model) {\r
+        SimulationScheduler scheduler = model.getService(SimulationScheduler.class);\r
+        if(scheduler == null) {\r
+            scheduler = new SimulationScheduler(model);\r
+            model.addService(SimulationScheduler.class, scheduler);\r
+            scheduler.start();\r
+        }\r
+        return scheduler;\r
+    }\r
+    \r
+}\r
index 7c23673dcd4be0634a17d625b5e498ce5c1be6fc..768de97a194d253d5870ad5f2bd0e8254fc5a982 100644 (file)
@@ -1,7 +1,7 @@
 package org.simantics.sysdyn.ui.trend;\r
 \r
 import java.awt.Frame;\r
-import java.io.IOException;\r
+import java.util.HashMap;\r
 \r
 import javax.swing.SwingUtilities;\r
 \r
@@ -18,11 +18,11 @@ import org.jfree.chart.JFreeChart;
 import org.jfree.chart.axis.NumberAxis;\r
 import org.jfree.chart.plot.XYPlot;\r
 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;\r
-import org.jfree.chart.renderer.xy.XYSplineRenderer;\r
 import org.jfree.data.xy.AbstractXYDataset;\r
 import org.simantics.db.Builtins;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
 import org.simantics.db.common.request.ReadRequest;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.modelica.data.DataSet;\r
@@ -32,6 +32,8 @@ import org.simantics.sysdyn.manager.SysdynModel;
 import org.simantics.sysdyn.manager.SysdynModelManager;\r
 import org.simantics.sysdyn.representation.IElement;\r
 import org.simantics.sysdyn.representation.Variable;\r
+import org.simantics.sysdyn.ui.simulation.SimulationJob;\r
+import org.simantics.sysdyn.ui.simulation.SimulationScheduler;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.utils.ui.jface.ActiveSelectionProvider;\r
 \r
@@ -119,8 +121,7 @@ public class TrendView extends ViewPart {
                         else\r
                             return;\r
                     }\r
-                    System.out.println("Selection changed");\r
-                    load(resources);\r
+                    setSelection(resources);\r
                 }                \r
             }\r
         });\r
@@ -135,46 +136,20 @@ public class TrendView extends ViewPart {
                 });*/            \r
     }\r
     \r
-    protected DataSet load(ReadGraph g, Resource resource) throws DatabaseException {\r
-        Builtins b = g.getBuiltins();\r
-        SysdynResource sr = SysdynResource.getInstance(g);\r
-        if(g.isInstanceOf(resource, sr.Variable)) {\r
-            System.out.println("we have a variable here");\r
-            Resource configuration = g.getPossibleObject(resource, b.PartOf);\r
-            if(g.isInstanceOf(configuration, sr.Configuration)) {\r
-                SysdynModel model = \r
-                    SysdynModelManager.getInstance(g.getSession()).getModel(configuration);\r
-                synchronized(model) {\r
-                    boolean updated = model.update();\r
-                    IElement element = model.getElement(resource);\r
-                    if(element instanceof Variable) {\r
-                        Variable var = (Variable)element;\r
-                        \r
-                        try {\r
-                            if(updated || model.getResult() == null)\r
-                                model.simulate();\r
-                        } catch (IOException e) {\r
-                            // TODO Auto-generated catch block\r
-                            e.printStackTrace();\r
-                        }\r
-                        SimulationResult result = model.getResult();\r
-                        if(result == null)\r
-                            return null;\r
-                        return result.getDataSet(var.getName());\r
-                    }                       \r
-                }\r
-            }\r
-        }\r
-        return null;\r
+    Resource[] resources;\r
+    \r
+    protected void setSelection(Resource[] resources) {\r
+        this.resources = resources;\r
+        updateView();\r
     }\r
     \r
-    protected void load(final Resource[] resources) {\r
+    public void updateView() {\r
         SimanticsUI.getSession().asyncRequest(new ReadRequest() {            \r
             @Override\r
             public void run(ReadGraph g) throws DatabaseException {\r
                 SysdynResource sr = SysdynResource.getInstance(g);\r
                 \r
-                DataSet[] sets = new DataSet[resources.length];\r
+                final DataSet[] sets = new DataSet[resources.length];\r
                 for(int i=0;i<resources.length;++i) {\r
                     try {\r
                         sets[i] = load(g, resources[i]);\r
@@ -186,10 +161,61 @@ public class TrendView extends ViewPart {
                         return;\r
                 }\r
                 \r
-                sysdynDataset.setDatasets(sets);\r
+                SwingUtilities.invokeLater(new Runnable() {\r
+\r
+                    @Override\r
+                    public void run() {              \r
+                        sysdynDataset.setDatasets(sets);\r
+                    }\r
+                    \r
+                });\r
+               \r
             }\r
         });\r
     }\r
+    \r
+    Runnable updater = new Runnable() {\r
+        @Override\r
+        public void run() {\r
+            System.out.println("updater called");\r
+            updateView();\r
+            System.out.println("updater done");\r
+        }        \r
+    };\r
+    \r
+    HashMap<Resource, SysdynModel> models = new HashMap<Resource, SysdynModel>(); \r
+    protected SysdynModel getModel(Session session, Resource configuration) {\r
+        SysdynModel model = models.get(configuration);\r
+        if(model == null) {\r
+            model = \r
+                SysdynModelManager.getInstance(session).getModel(configuration);\r
+            SimulationScheduler.start(model);\r
+            model.addResultListener(updater);\r
+            models.put(configuration, model);\r
+        }\r
+        return model;\r
+    }\r
+    \r
+    protected DataSet load(ReadGraph g, Resource resource) throws DatabaseException {\r
+        Builtins b = g.getBuiltins();\r
+        SysdynResource sr = SysdynResource.getInstance(g);\r
+        if(g.isInstanceOf(resource, sr.Variable)) {\r
+            Resource configuration = g.getPossibleObject(resource, b.PartOf);\r
+            if(g.isInstanceOf(configuration, sr.Configuration)) {\r
+                SysdynModel model = getModel(g.getSession(), configuration);\r
+                IElement element = model.getElement(resource);\r
+                if(element instanceof Variable) {\r
+                    Variable var = (Variable)element;                   \r
+                    SimulationResult result = model.getResult();\r
+                    if(result == null)\r
+                        return null;\r
+                    return result.getDataSet(var.getName());\r
+                }                       \r
+\r
+            }\r
+        }\r
+        return null;\r
+    }\r
 \r
     @Override\r
     public void setFocus() {\r
index 7b97c9a13d527447f3bc593f777b0ee5c3d4e344..c591b12cb8c4575d385570d9d5783abd7d42eced 100644 (file)
@@ -1,6 +1,8 @@
 package org.simantics.sysdyn.manager;\r
 \r
 import java.io.IOException;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
 import java.util.concurrent.CopyOnWriteArrayList;\r
 \r
 import org.simantics.db.ReadGraph;\r
@@ -25,7 +27,7 @@ import org.simantics.sysdyn.representation.SysdynSchema;
 public class SysdynModel implements IMappingListener {\r
 \r
     Session session;\r
-    Resource configurationResource;\r
+    Resource configurationResource;    \r
     \r
     IMapping mapping;\r
     Configuration configuration;\r
@@ -36,18 +38,17 @@ public class SysdynModel implements IMappingListener {
     CopyOnWriteArrayList<Runnable> resultListeners =\r
         new CopyOnWriteArrayList<Runnable>();\r
     \r
+    Map<Class, Object> services = new HashMap<Class, Object>();\r
+    \r
     private void createMapping() throws DatabaseException {\r
         session.syncRequest(new ReadRequest() {\r
-\r
             @Override\r
             public void run(ReadGraph graph) throws DatabaseException {\r
                 SysdynSchema schema = new SysdynSchema(graph);\r
                 mapping = Mappings.createWithListening(schema);\r
                 mapping.addMappingListener(SysdynModel.this);\r
-                configuration = (Configuration)mapping.map(graph, configurationResource);\r
-                \r
+                configuration = (Configuration)mapping.map(graph, configurationResource);           \r
             }\r
-            \r
         });\r
     }\r
     \r
@@ -62,7 +63,7 @@ public class SysdynModel implements IMappingListener {
         }\r
     }\r
     \r
-    public void simulate() throws IOException {\r
+    public synchronized void simulate() throws IOException {\r
         try {\r
             ModelicaWriter writer = new ModelicaWriter();\r
             writer.write(configuration);\r
@@ -74,11 +75,13 @@ public class SysdynModel implements IMappingListener {
             e.printStackTrace();\r
             result = null;\r
         }\r
-        for(Runnable listener : resultListeners)\r
-            listener.run();\r
+        synchronized(resultListeners) {\r
+            for(Runnable listener : resultListeners)\r
+                listener.run();\r
+        }\r
     }\r
     \r
-    public boolean update() throws DatabaseException {\r
+    public synchronized boolean update() throws DatabaseException {\r
         if(mapping.isDomainModified()) {\r
             session.syncRequest(new ReadRequest() {\r
                 @Override\r
@@ -97,17 +100,35 @@ public class SysdynModel implements IMappingListener {
     }\r
     \r
     public void addResultListener(Runnable listener) {\r
-        resultListeners.add(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
-        modificationListeners.add(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
-        for(Runnable listener : modificationListeners)\r
-            listener.run();   \r
+        synchronized(modificationListeners) {\r
+            for(Runnable listener : modificationListeners)\r
+                listener.run();\r
+        }\r
     }\r
 \r
     @Override\r
@@ -118,8 +139,20 @@ public class SysdynModel implements IMappingListener {
         return configuration;\r
     }\r
     \r
-    public IElement getElement(Resource resource) {\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
 }\r