]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
b650cd89d294e2e8de36752d5e0cbe45106f1651
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.viewUtils;\r
13 \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
20 \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
49 \r
50 public abstract class SysdynDatasetSelectionListener implements ISelectionListener {\r
51         \r
52         protected abstract void selectionChanged(Collection<SysdynDataSet> activeDatasets);\r
53         \r
54         HashMap<SysdynModel, Runnable> resultListeners = new HashMap<SysdynModel, Runnable>();\r
55 \r
56         @Override\r
57         public void selectionChanged(IWorkbenchPart part, final ISelection selection) {\r
58                 if(selection.isEmpty() || Boolean.TRUE.equals(PinTrend.getState()))\r
59                         return;\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
65                         }\r
66                         \r
67                         Session session = SimanticsUI.peekSession();\r
68                         if (session == null)\r
69                                 return;\r
70 \r
71                         session.asyncRequest(new ReadRequest() {\r
72                                 @Override\r
73                                 public void run(ReadGraph graph) throws DatabaseException {\r
74                                         \r
75                                         Collection<Variable> vars = ISelectionUtils.filterSetSelection(selection, Variable.class);\r
76                                         \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
81                                                         return;\r
82                                                 Resource runtime = runtimes.get(0);\r
83                                                 \r
84                                                 for(Resource resource : ress) {\r
85                                                         Variable variable = getVariable(graph, resource, runtime);\r
86                                                         if(variable != null)\r
87                                                                 vars.add(variable);\r
88                                                 }\r
89                                         }\r
90                                         \r
91                                         updateDatasets(graph, vars);\r
92                                         addResultListeners(graph, vars);\r
93                                 }\r
94                         });\r
95                 }\r
96         }\r
97         \r
98         \r
99         private void addResultListeners(ReadGraph graph, final Collection<Variable> variables) throws DatabaseException {\r
100                 if(variables.size() < 1) return;\r
101                 \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
107                         }\r
108                 }\r
109                 \r
110                 Runnable listener = new Runnable() {\r
111                         @Override\r
112                         public void run() {\r
113                                 Session session = SimanticsUI.peekSession();\r
114                                 if (session == null)\r
115                                         return;\r
116                                 \r
117                                 session.asyncRequest(new ReadRequest() {\r
118                                         \r
119                                         @Override\r
120                                         public void run(ReadGraph graph) throws DatabaseException {\r
121                                                 updateDatasets(graph, variables);\r
122                                         }\r
123                                 });\r
124                         }\r
125                 };\r
126                 \r
127                 for(SysdynModel model : models) {\r
128                         model.addResultListener(listener);\r
129                         resultListeners.put(model, listener);\r
130                 }\r
131         }\r
132         \r
133         private void updateDatasets(ReadGraph graph, Collection<Variable> variables) throws DatabaseException {\r
134 \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
140                 }\r
141                 selectionChanged(datasets);\r
142 \r
143         }\r
144 \r
145 \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
149 \r
150                 String rvi = Variables.getRVI(g, variable).replace("/", ".");           \r
151                 if(rvi.length() > 1)\r
152                         rvi = rvi.substring(1);\r
153 \r
154                 Resource r = variable.getPropertyValue(g, Variables.REPRESENTS);\r
155                 \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
160                 } else {\r
161                         List<Resource> arrayIndexList= OrderedSetUtils.toList(g, arrayIndexes);\r
162                         resolveActiveArrayIndexes(g, variable, arrayIndexList);\r
163                         \r
164                         if(arrayIndexList.size() > 0)\r
165                                 traverseIndexes(g, rvi, rvis, arrayIndexList);\r
166                         else\r
167                                 rvis.put(rvi, rvi);\r
168                 }\r
169                 \r
170                 Resource modelResource = Variables.getModel(g, variable);\r
171                 SysdynModel model = getSysdynModel(g, modelResource);\r
172                 \r
173                 if(model == null)\r
174                         return dataSets;\r
175                 \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
180                                 if(sds != null) {\r
181                                         sds.name = rvis.get(currvi);\r
182                                         dataSets.add(sds);\r
183                                 }\r
184                         }\r
185                 }\r
186                 return dataSets;\r
187 \r
188         }\r
189         \r
190         private static List<Resource> resolveActiveArrayIndexes(ReadGraph graph, Variable variable, List<Resource> enumerations) {\r
191                 try {\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
197                         if(v != null) {\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
201                                         \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
209                                                 }\r
210                                         }\r
211                                         \r
212                                         if(somethingIsReplaced) {\r
213                                                 resolveActiveArrayIndexes(graph, v, enumerations);\r
214                                         }\r
215                                 }\r
216                         }\r
217                 } catch (DatabaseException e) {\r
218                         e.printStackTrace();\r
219                         \r
220                 }\r
221                 return enumerations;\r
222         }\r
223         \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
226         }\r
227         \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
232                         return;\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
242                                 else {\r
243                                         rvis.put(\r
244                                                         rvi + "[" + indexesSoFar + (i + 1) + "]", \r
245                                                         rvi + "[" + indexNamesSoFar + (name) + "]");\r
246                                 }\r
247                         }\r
248                 }\r
249         }\r
250         \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
258                 try {\r
259                         Variable compositeVariable = Variables.getVariable(g, variableURI);\r
260                         return compositeVariable.browsePossible(g, resource);\r
261                 } catch (MissingVariableException e) {\r
262                         return null;\r
263                 }\r
264         }\r
265         \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
269         }\r
270 \r
271 }\r