From 9c363f16ff628dff9c4e60ae36fd56b708d7bb80 Mon Sep 17 00:00:00 2001 From: lempinen Date: Wed, 24 Mar 2010 13:21:00 +0000 Subject: [PATCH] Removed contradicting transactions by changing constructor of SysdynModel to take a ReadGraph instead of Session git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@15202 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../simantics/sysdyn/ui/trend/TrendView.java | 6 +- .../simantics/sysdyn/manager/SysdynModel.java | 63 +++++++++---------- .../sysdyn/manager/SysdynModelManager.java | 35 ++++++++++- 3 files changed, 65 insertions(+), 39 deletions(-) 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 4981a0b2..255a6df5 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 @@ -199,12 +199,12 @@ public class TrendView extends ViewPart { }; HashMap models = new HashMap(); - protected SysdynModel getModel(Session session, Resource configuration) { + protected SysdynModel getModel(ReadGraph g, Resource configuration) { synchronized(models) { SysdynModel model = models.get(configuration); if(model == null) { model = - SysdynModelManager.getInstance(session).getModel(configuration); + SysdynModelManager.getInstance(g.getSession()).getModel(g, configuration); SimulationScheduler.start(model); model.addResultListener(updater); models.put(configuration, model); @@ -219,7 +219,7 @@ public class TrendView extends ViewPart { 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); + SysdynModel model = getModel(g, configuration); IElement element = model.getElement(resource); if(element instanceof Variable) { Variable var = (Variable)element; 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 7a14b9a2..28c98110 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -40,46 +40,41 @@ public class SysdynModel implements IMappingListener { Session session; Resource configurationResource; - + IMapping mapping; Configuration configuration; SimulationResult result; - + CopyOnWriteArrayList modificationListeners = new CopyOnWriteArrayList(); 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); - } - }); - } - - public SysdynModel(Session session, Resource configurationResource) { - this.session = session; + + private void createMapping(ReadGraph g) throws DatabaseException { + SysdynSchema schema = new SysdynSchema(g); + mapping = Mappings.createWithListening(schema); + mapping.addMappingListener(SysdynModel.this); + configuration = (Configuration)mapping.map(g, configurationResource); + } + + public SysdynModel(ReadGraph g, Resource configurationResource) { + this.session = g.getSession(); this.configurationResource = configurationResource; - + try { - createMapping(); + createMapping(g); } catch(DatabaseException e) { e.printStackTrace(); } } - + public synchronized void simulate(IModelicaMonitor monitor) throws IOException { try { ModelicaWriter writer = new ModelicaWriter(); writer.write(configuration); - + result = ModelicaManager.runModelica( configuration.name, writer.toString(), @@ -95,7 +90,7 @@ public class SysdynModel implements IMappingListener { listener.run(); } } - + public synchronized boolean update() throws DatabaseException { if(mapping.isDomainModified()) { session.syncRequest(new ReadRequest() { @@ -109,29 +104,29 @@ public class SysdynModel implements IMappingListener { else return false; } - + public SimulationResult getResult() { return result; } - + public void addResultListener(Runnable listener) { synchronized(resultListeners) { resultListeners.add(listener); } } - + public void removeResultListener(Runnable listener) { synchronized(resultListeners) { resultListeners.add(listener); } } - + public void addModificationListener(Runnable listener) { synchronized(modificationListeners) { modificationListeners.add(listener); } } - + public void removeModificationListener(Runnable listener) { synchronized(modificationListeners) { modificationListeners.remove(listener); @@ -149,29 +144,29 @@ public class SysdynModel implements IMappingListener { @Override public void rangeModified() { } - + public Configuration getConfiguration() { return configuration; } - + public IMapping getMapping() { return mapping; } - + 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); } } - + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java index 18a4c202..efb80e38 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java @@ -13,8 +13,11 @@ package org.simantics.sysdyn.manager; import java.util.WeakHashMap; +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; /** * Manages system dynamic models. @@ -29,10 +32,38 @@ public class SysdynModelManager { this.session = session; } - public SysdynModel getModel(Resource resource) { + /** + * Use only if not inside a transaction + */ + public SysdynModel getModel(final Resource resource) { SysdynModel model = models.get(resource); if(model == null) { - model = new SysdynModel(session, resource); + try { + session.syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + models.put(resource, new SysdynModel(graph, resource)); + + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + + model = models.get(resource); + } + return model; + } + + /** + * Should be used if called inside a transaction + */ + public SysdynModel getModel(ReadGraph g, Resource resource) { + SysdynModel model = models.get(resource); + if(model == null) { + model = new SysdynModel(g, resource); models.put(resource, model); } return model; -- 2.47.1