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
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
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
else\r
return;\r
}\r
- System.out.println("Selection changed");\r
- load(resources);\r
+ setSelection(resources);\r
} \r
}\r
});\r
});*/ \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
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
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
public class SysdynModel implements IMappingListener {\r
\r
Session session;\r
- Resource configurationResource;\r
+ Resource configurationResource; \r
\r
IMapping mapping;\r
Configuration configuration;\r
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
}\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
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
}\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
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