1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.viewUtils;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.HashMap;
\r
17 import java.util.HashSet;
\r
18 import java.util.List;
\r
19 import java.util.Set;
\r
21 import org.eclipse.jface.viewers.ISelection;
\r
22 import org.eclipse.jface.viewers.IStructuredSelection;
\r
23 import org.eclipse.ui.ISelectionListener;
\r
24 import org.eclipse.ui.IWorkbenchPart;
\r
25 import org.simantics.databoard.Bindings;
\r
26 import org.simantics.db.ReadGraph;
\r
27 import org.simantics.db.Resource;
\r
28 import org.simantics.db.Session;
\r
29 import org.simantics.db.common.request.ObjectsWithType;
\r
30 import org.simantics.db.common.request.ReadRequest;
\r
31 import org.simantics.db.common.utils.OrderedSetUtils;
\r
32 import org.simantics.db.exception.DatabaseException;
\r
33 import org.simantics.db.layer0.SelectionHints;
\r
34 import org.simantics.db.layer0.exception.MissingVariableException;
\r
35 import org.simantics.db.layer0.variable.Variable;
\r
36 import org.simantics.db.layer0.variable.Variables;
\r
37 import org.simantics.diagram.stubs.DiagramResource;
\r
38 import org.simantics.layer0.Layer0;
\r
39 import org.simantics.modeling.ModelingUtils;
\r
40 import org.simantics.simulation.ontology.SimulationResource;
\r
41 import org.simantics.sysdyn.SysdynResource;
\r
42 import org.simantics.sysdyn.manager.SysdynDataSet;
\r
43 import org.simantics.sysdyn.manager.SysdynModel;
\r
44 import org.simantics.sysdyn.manager.SysdynModelManager;
\r
45 import org.simantics.sysdyn.manager.SysdynResult;
\r
46 import org.simantics.sysdyn.ui.trend.PinTrend;
\r
47 import org.simantics.ui.SimanticsUI;
\r
48 import org.simantics.utils.ui.ISelectionUtils;
\r
50 public abstract class SysdynDatasetSelectionListener implements ISelectionListener {
\r
52 protected abstract void selectionChanged(Collection<SysdynDataSet> activeDatasets);
\r
54 HashMap<SysdynModel, Runnable> resultListeners = new HashMap<SysdynModel, Runnable>();
\r
57 public void selectionChanged(IWorkbenchPart part, final ISelection selection) {
\r
58 if(selection.isEmpty() || Boolean.TRUE.equals(PinTrend.getState()))
\r
60 if(selection instanceof IStructuredSelection) {
\r
61 if(!resultListeners.isEmpty()) {
\r
62 for(SysdynModel model : resultListeners.keySet())
\r
63 model.removeResultListener(resultListeners.get(model));
\r
64 resultListeners.clear();
\r
67 Session session = SimanticsUI.peekSession();
\r
68 if (session == null)
\r
71 session.asyncRequest(new ReadRequest() {
\r
73 public void run(ReadGraph graph) throws DatabaseException {
\r
75 Collection<Variable> vars = ISelectionUtils.filterSetSelection(selection, Variable.class);
\r
77 if(vars.isEmpty()) {
\r
78 Set<Resource> ress = ISelectionUtils.filterSetSelection(selection, Resource.class);
\r
79 List<Resource> runtimes = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_VARIABLE_RESOURCE, Resource.class);
\r
80 if(runtimes.isEmpty())
\r
82 Resource runtime = runtimes.get(0);
\r
84 for(Resource resource : ress) {
\r
85 Variable variable = getVariable(graph, resource, runtime);
\r
86 if(variable != null)
\r
91 updateDatasets(graph, vars);
\r
92 addResultListeners(graph, vars);
\r
99 private void addResultListeners(ReadGraph graph, final Collection<Variable> variables) throws DatabaseException {
\r
100 if(variables.size() < 1) return;
\r
102 HashSet<SysdynModel> models = new HashSet<SysdynModel>();
\r
103 for(Variable variable : variables) {
\r
104 Resource model = Variables.getModel(graph, variable);
\r
105 if(model != null) {
\r
106 models.add(getSysdynModel(graph, model));
\r
110 Runnable listener = new Runnable() {
\r
112 public void run() {
\r
113 Session session = SimanticsUI.peekSession();
\r
114 if (session == null)
\r
117 session.asyncRequest(new ReadRequest() {
\r
120 public void run(ReadGraph graph) throws DatabaseException {
\r
121 updateDatasets(graph, variables);
\r
127 for(SysdynModel model : models) {
\r
128 model.addResultListener(listener);
\r
129 resultListeners.put(model, listener);
\r
133 private void updateDatasets(ReadGraph graph, Collection<Variable> variables) throws DatabaseException {
\r
135 ArrayList<SysdynDataSet> datasets = new ArrayList<SysdynDataSet>();
\r
136 for(Variable variable : variables) {
\r
137 Collection<SysdynDataSet> activeDataSets = loadAllActive(graph, variable);
\r
138 if(activeDataSets != null && !activeDataSets.isEmpty())
\r
139 datasets.addAll(activeDataSets);
\r
141 selectionChanged(datasets);
\r
146 protected Collection<SysdynDataSet> loadAllActive(ReadGraph g, Variable variable) throws DatabaseException {
\r
147 ArrayList<SysdynDataSet> dataSets = new ArrayList<SysdynDataSet>();
\r
148 HashMap<String, String> rvis = new HashMap<String, String>();
\r
150 String rvi = Variables.getRVI(g, variable).replace("/", ".");
\r
151 if(rvi.length() > 1)
\r
152 rvi = rvi.substring(1);
\r
154 Resource r = variable.getPropertyValue(g, Variables.REPRESENTS);
\r
156 SysdynResource sr = SysdynResource.getInstance(g);
\r
157 Resource arrayIndexes = g.getPossibleObject(r, sr.HasArrayIndexes);
\r
158 if(arrayIndexes == null) {
\r
159 rvis.put(rvi, rvi);
\r
161 List<Resource> arrayIndexList= OrderedSetUtils.toList(g, arrayIndexes);
\r
162 resolveActiveArrayIndexes(g, variable, arrayIndexList);
\r
164 if(arrayIndexList.size() > 0)
\r
165 traverseIndexes(g, rvi, rvis, arrayIndexList);
\r
167 rvis.put(rvi, rvi);
\r
170 Resource modelResource = Variables.getModel(g, variable);
\r
171 SysdynModel model = getSysdynModel(g, modelResource);
\r
176 Collection<SysdynResult> activeResults = model.getActiveResults(g);
\r
177 for(SysdynResult sysdynResult : activeResults) {
\r
178 for(String currvi : rvis.keySet()) {
\r
179 SysdynDataSet sds = sysdynResult.getDataSet(currvi);
\r
181 sds.name = rvis.get(currvi);
\r
190 private static List<Resource> resolveActiveArrayIndexes(ReadGraph graph, Variable variable, List<Resource> enumerations) {
\r
192 Layer0 l0 = Layer0.getInstance(graph);
\r
193 SysdynResource sr = SysdynResource.getInstance(graph);
\r
194 String uri = variable.getURI(graph);
\r
195 uri = uri.substring(0, uri.lastIndexOf("/"));
\r
196 Variable v = Variables.getPossibleVariable(graph, uri);
\r
198 Variable rp = (Variable)v.getProperty(graph, Variables.REPRESENTS);
\r
199 Resource module = rp.getValue(graph);
\r
200 if(module != null && graph.isInheritedFrom(graph.getSingleObject(module, l0.InstanceOf), sr.Module)) {
\r
202 boolean somethingIsReplaced = false;
\r
203 for(Resource redeclaration : graph.syncRequest(new ObjectsWithType(module, sr.HasRedeclaration, sr.Redeclaration))) {
\r
204 Resource replaced = graph.getSingleObject(redeclaration, sr.ReplacedEnumeration);
\r
205 if(enumerations.contains(replaced)) {
\r
206 enumerations.add(enumerations.indexOf(replaced), graph.getSingleObject(redeclaration, sr.ReplacingEnumeration));
\r
207 enumerations.remove(replaced);
\r
208 somethingIsReplaced = true;
\r
212 if(somethingIsReplaced) {
\r
213 resolveActiveArrayIndexes(graph, v, enumerations);
\r
217 } catch (DatabaseException e) {
\r
218 e.printStackTrace();
\r
221 return enumerations;
\r
224 private void traverseIndexes(ReadGraph g, String rvi, HashMap<String, String> rvis, List<Resource> arrayIndexes) throws DatabaseException {
\r
225 traverseIndexes(g, rvi, rvis, arrayIndexes, arrayIndexes.get(0), "", "");
\r
228 private void traverseIndexes(ReadGraph g, String rvi, HashMap<String, String> rvis, List<Resource> arrayIndexes, Resource currentEnumeration, String indexesSoFar, String indexNamesSoFar) throws DatabaseException {
\r
229 SysdynResource sr = SysdynResource.getInstance(g);
\r
230 Resource enumerationIndexes = g.getPossibleObject(currentEnumeration, sr.HasEnumerationIndexes);
\r
231 if(enumerationIndexes == null)
\r
233 List<Resource> indexes = OrderedSetUtils.toList(g, enumerationIndexes);
\r
234 for(int i = 0; i < indexes.size(); i++) {
\r
235 Boolean b = g.getPossibleRelatedValue(indexes.get(i), sr.ShowEnumerationIndexInCharts, Bindings.BOOLEAN);
\r
236 if(Boolean.TRUE.equals(b)) {
\r
237 int arrayIndex = arrayIndexes.indexOf(currentEnumeration);
\r
238 String name = g.getRelatedValue(indexes.get(i), Layer0.getInstance(g).HasName);
\r
239 if(arrayIndex < arrayIndexes.size() - 1)
\r
240 traverseIndexes(g, rvi, rvis, arrayIndexes, arrayIndexes.get(arrayIndex + 1),
\r
241 indexesSoFar + (i + 1) +",", indexNamesSoFar + (name) +",");
\r
244 rvi + "[" + indexesSoFar + (i + 1) + "]",
\r
245 rvi + "[" + indexNamesSoFar + (name) + "]");
\r
251 private Variable getVariable(ReadGraph g, Resource element, Resource runtime) throws DatabaseException {
\r
252 SysdynResource sr = SysdynResource.getInstance(g);
\r
253 DiagramResource dr = DiagramResource.getInstance(g);
\r
254 if(runtime == null) return null;
\r
255 Resource resource = ModelingUtils.getPossibleElementCorrespondendence(g, element);
\r
256 if(resource == null || !g.isInstanceOf(resource, sr.Variable)) return null;
\r
257 String variableURI = g.getPossibleRelatedValue(runtime, dr.HasVariable);
\r
259 Variable compositeVariable = Variables.getVariable(g, variableURI);
\r
260 return compositeVariable.browsePossible(g, resource);
\r
261 } catch (MissingVariableException e) {
\r
266 private SysdynModel getSysdynModel(ReadGraph g, Resource model) throws DatabaseException {
\r
267 Resource configuration = g.getPossibleObject(model, SimulationResource.getInstance(g).HasConfiguration);
\r
268 return SysdynModelManager.getInstance(g.getSession()).getModel(g, configuration);
\r