\r
import java.util.ArrayList;\r
import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
import java.util.List;\r
import java.util.Set;\r
\r
import org.simantics.db.layer0.variable.Variable;\r
import org.simantics.db.layer0.variable.Variables;\r
import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.layer0.Layer0;\r
import org.simantics.modeling.ModelingUtils;\r
import org.simantics.simulation.ontology.SimulationResource;\r
import org.simantics.sysdyn.SysdynResource;\r
public abstract class SysdynDatasetSelectionListener implements ISelectionListener {\r
\r
protected abstract void selectionChanged(Collection<SysdynDataSet> activeDatasets);\r
+ \r
+ HashMap<SysdynModel, Runnable> resultListeners = new HashMap<SysdynModel, Runnable>();\r
\r
@Override\r
public void selectionChanged(IWorkbenchPart part, ISelection selection) {\r
if(selection.isEmpty() || Boolean.TRUE.equals(PinTrend.getState()))\r
return;\r
if(selection instanceof IStructuredSelection) {\r
+ if(!resultListeners.isEmpty()) {\r
+ for(SysdynModel model : resultListeners.keySet())\r
+ model.removeResultListener(resultListeners.get(model));\r
+ resultListeners.clear();\r
+ }\r
Set<BrowserSelection> bs = ISelectionUtils.filterSetSelection(selection, BrowserSelection.class);\r
if(!bs.isEmpty()) {\r
- updateDatasets(bs.toArray(new BrowserSelection[bs.size()]));\r
+ BrowserSelection[] browserSelections = bs.toArray(new BrowserSelection[bs.size()]);\r
+ updateDatasets(browserSelections);\r
+ addResultListeners(browserSelections);\r
} else {\r
Set<Resource> ress = ISelectionUtils.filterSetSelection(selection, Resource.class);\r
List<Resource> runtimes = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_VARIABLE_RESOURCE, Resource.class);\r
if(runtimes.isEmpty())\r
return;\r
Resource runtime = runtimes.get(0);\r
- updateDatasets(ress.toArray(Resource.NONE), runtime);\r
+ Resource[] resources = ress.toArray(Resource.NONE);\r
+ updateDatasets(resources, runtime);\r
+ addResultListeners(resources, runtime);\r
}\r
}\r
}\r
\r
+ private void addResultListeners(final BrowserSelection[] browserSelections) {\r
+ Session session = SimanticsUI.peekSession();\r
+ if (session == null || browserSelections.length < 1)\r
+ return;\r
\r
+ session.asyncRequest(new ReadRequest() {\r
+ @Override\r
+ public void run(ReadGraph g) throws DatabaseException {\r
+ HashSet<SysdynModel> models = new HashSet<SysdynModel>(); \r
+ for(BrowserSelection bs : browserSelections) {\r
+ if(bs.getModel() != null) {\r
+ models.add(getSysdynModel(g, bs.getModel()));\r
+ }\r
+ }\r
+ \r
+ Runnable listener = new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ updateDatasets(browserSelections);\r
+ }\r
+ };\r
+ \r
+ for(SysdynModel model : models) {\r
+ model.addResultListener(listener);\r
+ resultListeners.put(model, listener);\r
+ }\r
+ }\r
+ });\r
+ }\r
\r
+ private void addResultListeners(final Resource[] resources, final Resource runtime) {\r
+ Session session = SimanticsUI.peekSession();\r
+ if (session == null || resources.length < 1)\r
+ return;\r
+ \r
+ session.asyncRequest(new ReadRequest() {\r
+ @Override\r
+ public void run(ReadGraph g) throws DatabaseException {\r
+ Variable variable = getVariable(g, resources[0], runtime);\r
+ SysdynModel model = getSysdynModel(g, Variables.getModel(g, variable));\r
+ Runnable listener = new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ updateDatasets(resources, runtime);\r
+ }\r
+ };\r
+ model.addResultListener(listener);\r
+ resultListeners.put(model, listener);\r
+ }\r
+ });\r
+\r
+ }\r
+ \r
private void updateDatasets(final Resource[] resources, final Resource runtime) {\r
Session session = SimanticsUI.peekSession();\r
if (session == null)\r
public void run(ReadGraph g) throws DatabaseException {\r
ArrayList<SysdynDataSet> datasets = new ArrayList<SysdynDataSet>();\r
for(Resource resource : resources) {\r
- Collection<SysdynDataSet> activeDataSets = loadAllActive(g, resource, runtime);\r
+ Variable variable = getVariable(g, resource, runtime);\r
+ Collection<SysdynDataSet> activeDataSets = loadAllActive(g, Variables.getRVI(g, variable).substring(1), getSysdynModel(g, Variables.getModel(g, variable)));\r
if(activeDataSets != null && !activeDataSets.isEmpty())\r
datasets.addAll(activeDataSets);\r
}\r
ArrayList<SysdynDataSet> datasets = new ArrayList<SysdynDataSet>();\r
for(BrowserSelection bs : browserSelections) {\r
if(bs.getURI() != null && bs.getModel() != null) {\r
- Collection<SysdynDataSet> activeDataSets = loadAllActive(g, bs.getURI(), bs.getModel());\r
+ Collection<SysdynDataSet> activeDataSets = loadAllActive(g, bs.getURI(), getSysdynModel(g, bs.getModel()));\r
if(activeDataSets != null && !activeDataSets.isEmpty()) \r
datasets.addAll(activeDataSets);\r
}\r
});\r
}\r
\r
+ /*\r
protected Collection<SysdynDataSet> loadAllActive(ReadGraph g, Resource element, Resource runtime) throws DatabaseException {\r
\r
Layer0 l0 = Layer0.getInstance(g);\r
return loadAllActive(g, rvi.substring(1), model);\r
\r
}\r
+ */\r
\r
- protected Collection<SysdynDataSet> loadAllActive(ReadGraph g, String rvi, Resource model) throws DatabaseException {\r
+ protected Collection<SysdynDataSet> loadAllActive(ReadGraph g, String rvi, SysdynModel model) throws DatabaseException {\r
ArrayList<SysdynDataSet> dataSets = new ArrayList<SysdynDataSet>();\r
-\r
- Resource configuration = g.getPossibleObject(model, SimulationResource.getInstance(g).HasConfiguration);\r
- SysdynModel sm = SysdynModelManager.getInstance(g.getSession()).getModel(g, configuration);\r
-\r
- Collection<SysdynResult> activeResults = sm.getActiveResults(g);\r
+ if(model == null || rvi == null)\r
+ return dataSets;\r
+ \r
+ Collection<SysdynResult> activeResults = model.getActiveResults(g);\r
for(SysdynResult sysdynResult : activeResults) {\r
SysdynDataSet sds = sysdynResult.getDataSet(rvi.replace("/", "."));\r
if(sds != null)\r
return dataSets;\r
\r
}\r
+ \r
+ private Variable getVariable(ReadGraph g, Resource element, Resource runtime) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(g);\r
+ DiagramResource dr = DiagramResource.getInstance(g);\r
+ if(runtime == null) return null;\r
+ Resource resource = ModelingUtils.getPossibleElementCorrespondendence(g, element);\r
+ if(resource == null || !g.isInstanceOf(resource, sr.Variable)) return null;\r
+ String variableURI = g.getPossibleRelatedValue(runtime, dr.HasVariable);\r
+ Variable compositeVariable = Variables.getVariable(g, variableURI);\r
+ return compositeVariable.browsePossible(g, resource);\r
+ }\r
+ \r
+ private SysdynModel getSysdynModel(ReadGraph g, Resource model) throws DatabaseException {\r
+ Resource configuration = g.getPossibleObject(model, SimulationResource.getInstance(g).HasConfiguration);\r
+ return SysdynModelManager.getInstance(g.getSession()).getModel(g, configuration);\r
+ }\r
\r
}\r