From fe928e02a96464924337bff215c594cfe95f4212 Mon Sep 17 00:00:00 2001 From: niemisto Date: Fri, 29 Jan 2010 07:24:43 +0000 Subject: [PATCH] SimulationJob (sysdyn) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13755 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/simulation/SimulationJob.java | 33 ++++++ .../ui/simulation/SimulationScheduler.java | 37 +++++++ .../simantics/sysdyn/ui/trend/TrendView.java | 102 +++++++++++------- .../simantics/sysdyn/manager/SysdynModel.java | 61 ++++++++--- 4 files changed, 181 insertions(+), 52 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/simulation/SimulationJob.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/simulation/SimulationScheduler.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 index 00000000..bcf8dfa4 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/simulation/SimulationJob.java @@ -0,0 +1,33 @@ +package org.simantics.sysdyn.ui.simulation; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.simantics.sysdyn.manager.SysdynModel; + +public class SimulationJob extends Job { + + SysdynModel model; + + public SimulationJob(SysdynModel model) { + super("Simulate " + model.getConfiguration().name); + this.model = model; + } + + @Override + protected IStatus run(IProgressMonitor monitor) { + try { + model.update(); + model.simulate(); + } catch (Exception e) { + e.printStackTrace(); + return new Status( + Status.ERROR, + "org.simantics.sysdyn.ui", + "Simulation failed: " + e.getMessage()); + } + return Status.OK_STATUS; + } + +} 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 index 00000000..48744484 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/simulation/SimulationScheduler.java @@ -0,0 +1,37 @@ +package org.simantics.sysdyn.ui.simulation; + +import org.simantics.sysdyn.manager.SysdynModel; + +public class SimulationScheduler { + SysdynModel model; + + SimulationJob job; + + private SimulationScheduler(SysdynModel model) { + this.model = model; + this.job = new SimulationJob(model); + } + + private void start() { + job.schedule(); + model.addModificationListener(new Runnable() { + + @Override + public void run() { + job.schedule(); + } + + }); + } + + public static SimulationScheduler start(SysdynModel model) { + SimulationScheduler scheduler = model.getService(SimulationScheduler.class); + if(scheduler == null) { + scheduler = new SimulationScheduler(model); + model.addService(SimulationScheduler.class, scheduler); + scheduler.start(); + } + return scheduler; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java index 7c23673d..768de97a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java @@ -1,7 +1,7 @@ package org.simantics.sysdyn.ui.trend; import java.awt.Frame; -import java.io.IOException; +import java.util.HashMap; import javax.swing.SwingUtilities; @@ -18,11 +18,11 @@ import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; -import org.jfree.chart.renderer.xy.XYSplineRenderer; import org.jfree.data.xy.AbstractXYDataset; import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.Session; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.modelica.data.DataSet; @@ -32,6 +32,8 @@ import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.manager.SysdynModelManager; import org.simantics.sysdyn.representation.IElement; import org.simantics.sysdyn.representation.Variable; +import org.simantics.sysdyn.ui.simulation.SimulationJob; +import org.simantics.sysdyn.ui.simulation.SimulationScheduler; import org.simantics.ui.SimanticsUI; import org.simantics.utils.ui.jface.ActiveSelectionProvider; @@ -119,8 +121,7 @@ public class TrendView extends ViewPart { else return; } - System.out.println("Selection changed"); - load(resources); + setSelection(resources); } } }); @@ -135,46 +136,20 @@ public class TrendView extends ViewPart { });*/ } - protected DataSet load(ReadGraph g, Resource resource) throws DatabaseException { - Builtins b = g.getBuiltins(); - SysdynResource sr = SysdynResource.getInstance(g); - if(g.isInstanceOf(resource, sr.Variable)) { - System.out.println("we have a variable here"); - Resource configuration = g.getPossibleObject(resource, b.PartOf); - if(g.isInstanceOf(configuration, sr.Configuration)) { - SysdynModel model = - SysdynModelManager.getInstance(g.getSession()).getModel(configuration); - synchronized(model) { - boolean updated = model.update(); - IElement element = model.getElement(resource); - if(element instanceof Variable) { - Variable var = (Variable)element; - - try { - if(updated || model.getResult() == null) - model.simulate(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - SimulationResult result = model.getResult(); - if(result == null) - return null; - return result.getDataSet(var.getName()); - } - } - } - } - return null; + Resource[] resources; + + protected void setSelection(Resource[] resources) { + this.resources = resources; + updateView(); } - protected void load(final Resource[] resources) { + public void updateView() { SimanticsUI.getSession().asyncRequest(new ReadRequest() { @Override public void run(ReadGraph g) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(g); - DataSet[] sets = new DataSet[resources.length]; + final DataSet[] sets = new DataSet[resources.length]; for(int i=0;i models = new HashMap(); + protected SysdynModel getModel(Session session, Resource configuration) { + SysdynModel model = models.get(configuration); + if(model == null) { + model = + SysdynModelManager.getInstance(session).getModel(configuration); + SimulationScheduler.start(model); + model.addResultListener(updater); + models.put(configuration, model); + } + return model; + } + + protected DataSet load(ReadGraph g, Resource resource) throws DatabaseException { + Builtins b = g.getBuiltins(); + SysdynResource sr = SysdynResource.getInstance(g); + if(g.isInstanceOf(resource, sr.Variable)) { + Resource configuration = g.getPossibleObject(resource, b.PartOf); + if(g.isInstanceOf(configuration, sr.Configuration)) { + SysdynModel model = getModel(g.getSession(), configuration); + IElement element = model.getElement(resource); + if(element instanceof Variable) { + Variable var = (Variable)element; + SimulationResult result = model.getResult(); + if(result == null) + return null; + return result.getDataSet(var.getName()); + } + + } + } + return null; + } @Override public void setFocus() { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java index 7b97c9a1..c591b12c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -1,6 +1,8 @@ package org.simantics.sysdyn.manager; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; import org.simantics.db.ReadGraph; @@ -25,7 +27,7 @@ import org.simantics.sysdyn.representation.SysdynSchema; public class SysdynModel implements IMappingListener { Session session; - Resource configurationResource; + Resource configurationResource; IMapping mapping; Configuration configuration; @@ -36,18 +38,17 @@ public class SysdynModel implements IMappingListener { CopyOnWriteArrayList resultListeners = new CopyOnWriteArrayList(); + Map services = new HashMap(); + private void createMapping() throws DatabaseException { session.syncRequest(new ReadRequest() { - @Override public void run(ReadGraph graph) throws DatabaseException { SysdynSchema schema = new SysdynSchema(graph); mapping = Mappings.createWithListening(schema); mapping.addMappingListener(SysdynModel.this); - configuration = (Configuration)mapping.map(graph, configurationResource); - + configuration = (Configuration)mapping.map(graph, configurationResource); } - }); } @@ -62,7 +63,7 @@ public class SysdynModel implements IMappingListener { } } - public void simulate() throws IOException { + public synchronized void simulate() throws IOException { try { ModelicaWriter writer = new ModelicaWriter(); writer.write(configuration); @@ -74,11 +75,13 @@ public class SysdynModel implements IMappingListener { e.printStackTrace(); result = null; } - for(Runnable listener : resultListeners) - listener.run(); + synchronized(resultListeners) { + for(Runnable listener : resultListeners) + listener.run(); + } } - public boolean update() throws DatabaseException { + public synchronized boolean update() throws DatabaseException { if(mapping.isDomainModified()) { session.syncRequest(new ReadRequest() { @Override @@ -97,17 +100,35 @@ public class SysdynModel implements IMappingListener { } public void addResultListener(Runnable listener) { - resultListeners.add(listener); + synchronized(resultListeners) { + resultListeners.add(listener); + } + } + + public void removeResultListener(Runnable listener) { + synchronized(resultListeners) { + resultListeners.add(listener); + } } public void addModificationListener(Runnable listener) { - modificationListeners.add(listener); + synchronized(modificationListeners) { + modificationListeners.add(listener); + } + } + + public void removeModificationListener(Runnable listener) { + synchronized(modificationListeners) { + modificationListeners.remove(listener); + } } @Override public void domainModified() { - for(Runnable listener : modificationListeners) - listener.run(); + synchronized(modificationListeners) { + for(Runnable listener : modificationListeners) + listener.run(); + } } @Override @@ -118,8 +139,20 @@ public class SysdynModel implements IMappingListener { return configuration; } - public IElement getElement(Resource resource) { + public synchronized IElement getElement(Resource resource) { return (IElement)mapping.get(resource); } + + public T getService(Class clazz) { + synchronized(services) { + return clazz.cast(services.get(clazz)); + } + } + + public void addService(Class clazz, T service) { + synchronized(services) { + services.put(clazz, service); + } + } } -- 2.47.1