]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/function/PredefinedVariables.java
Logger fixes after merge commit:fdbe8762
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / function / PredefinedVariables.java
1 package org.simantics.diagram.function;\r
2 \r
3 import java.util.Collection;\r
4 import java.util.HashSet;\r
5 import java.util.Set;\r
6 \r
7 import org.simantics.annotation.ontology.AnnotationResource;\r
8 import org.simantics.db.ReadGraph;\r
9 import org.simantics.db.Resource;\r
10 import org.simantics.db.exception.DatabaseException;\r
11 import org.simantics.db.layer0.variable.Variable;\r
12 import org.simantics.db.layer0.variable.Variables;\r
13 import org.simantics.diagram.content.ConnectionUtil;\r
14 import org.simantics.diagram.flag.FlagUtil;\r
15 import org.simantics.diagram.stubs.DiagramResource;\r
16 import org.simantics.layer0.Layer0;\r
17 import org.simantics.modeling.ModelingResources;\r
18 import org.simantics.modeling.template2d.ontology.Template2dResource;\r
19 import org.simantics.structural.stubs.StructuralResource2;\r
20 import org.simantics.ui.SimanticsUI;\r
21 \r
22 public class PredefinedVariables {\r
23         public static final String root = "root";\r
24         public static final String project = "project";\r
25         public static final String model = "model";\r
26         public static final String template = "template";\r
27         public static final String current = ".";\r
28         public static final String diagramComposite = "diagramComposite";\r
29         public static final String diagram = "diagram";\r
30         public static final String flagAnnotation = "annotation";\r
31         \r
32         /** \r
33          *  flagSourceObject    refers to a structural component which has an element on the flag's diagram  \r
34          */\r
35         public static final String flagSourceObject = "flagSourceObject";\r
36 \r
37         /** \r
38          *  flagTargetObject    refers to a structural component which has an element on the counterpart flag's diagram  \r
39          */\r
40         public static final String flagTargetObject = "flagTargetObject";\r
41 \r
42         /** \r
43          *  flagSourceComposite refers to a structural composite which corresponds the flag's diagram  \r
44          */\r
45         public static final String flagSourceComposite = "flagSourceComposite";\r
46 \r
47         /** \r
48          *  flagTargetComposite refers to a structural composite which corresponds the counterpart flag's diagram  \r
49          */\r
50         public static final String flagTargetComposite = "flagTargetComposite";\r
51 \r
52         /** \r
53          *  flagRouterComponent refers to a structural component which corresponds a graphical connection connected with the flag  \r
54          */\r
55         public static final String flagRouterComponent = "flagRouterComponent";\r
56 \r
57         private static PredefinedVariables factory = new PredefinedVariables();\r
58         \r
59         public static PredefinedVariables getInstance(){\r
60                 return factory;\r
61         }\r
62         \r
63         public static void setFactory(PredefinedVariables factory){\r
64                 PredefinedVariables.factory = factory;\r
65         }\r
66         \r
67     Resource connectedComponent = null;\r
68     \r
69     private Resource getConnectedComponent(ReadGraph graph, Resource flag) throws DatabaseException {\r
70                 StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
71                 ModelingResources MOD = ModelingResources.getInstance(graph);\r
72                 ConnectionUtil cu = new ConnectionUtil(graph);\r
73                 Resource connectedComponent = null;\r
74                 Set<Resource> connectors = new HashSet<Resource>();\r
75                 for (Resource connector : graph.getObjects(flag, STR.IsConnectedTo)) {\r
76                     Resource diagramConnection = ConnectionUtil.tryGetConnection(graph, connector);\r
77                     if (diagramConnection != null) {\r
78                         connectors.clear();\r
79                         cu.getConnectors(diagramConnection, connectors);\r
80                         connectors.remove(connector);\r
81                         if (connectors.isEmpty()) {\r
82                             continue;\r
83                         } else {\r
84                             //connectedComponent = graph.getPossibleObject(diagramConnection, MOD.ElementToComponent);\r
85                             if (connectedComponent == null) {\r
86                                 for (Resource conn : connectors) {\r
87                                     Resource element = cu.getConnectedComponent(diagramConnection, conn);\r
88                                     if (element == null)\r
89                                         continue;\r
90                                     connectedComponent = graph.getPossibleObject(element, MOD.ElementToComponent);\r
91                                     if (connectedComponent != null)\r
92                                         break;\r
93                                 }\r
94                             }\r
95                         }\r
96                     }\r
97                 }\r
98                 return connectedComponent;\r
99     }\r
100 \r
101     public Resource getFlagRouterComponent(ReadGraph graph, Variable variable) throws DatabaseException {\r
102         Resource flag = variable.getPossibleRepresents(graph);\r
103         return flag == null ? null : getFlagRouterComponent(graph, flag);\r
104     }\r
105 \r
106     public Resource getFlagRouterComponent(ReadGraph graph, Resource flag) throws DatabaseException {\r
107 \r
108         Layer0 L0 = Layer0.getInstance(graph);\r
109         ModelingResources MOD = ModelingResources.getInstance(graph);\r
110 \r
111         Resource container = graph.getSingleObject(flag, L0.PartOf);\r
112         Resource composite = graph.getSingleObject(container, MOD.DiagramToComposite);\r
113         Variable var = Variables.getVariable(graph, composite);\r
114 \r
115 //        System.err.println("var: " + var.getURI(graph));\r
116 \r
117         Variable result = FlagUtil.getPossibleFlagSignal(graph, var, flag, L0.Entity);\r
118         if(result != null) return result.getRepresents(graph);\r
119         else return null;\r
120 \r
121     }\r
122 \r
123         private Resource getFlagTargetObject(ReadGraph graph, Variable variable){\r
124                 try {\r
125                         Resource flag = variable.getRepresents(graph);\r
126                         Set<Resource> targetFlags = FlagUtil.getCounterparts(graph, flag);\r
127 \r
128                         // if there are multiple targets\r
129                         if (targetFlags.size() != 1)\r
130                                 return null;\r
131 \r
132                         Resource targetFlag = targetFlags.toArray(new Resource[0])[0];\r
133                         if (targetFlag == null)\r
134                                 return null;\r
135                 Resource connectedComponent = getConnectedComponent(graph, targetFlag);\r
136                         if (connectedComponent == null)\r
137                                 return null;\r
138                         return connectedComponent;\r
139                 } catch (DatabaseException e) {\r
140                 }\r
141                 return null;\r
142         }\r
143 \r
144         private Resource getFlagSourceObject(ReadGraph graph, Variable variable){\r
145                 try {\r
146                         Resource flag = variable.getRepresents(graph);\r
147                         Resource connectedComponent = getConnectedComponent(graph, flag);\r
148                         if (connectedComponent == null)\r
149                                 return null;\r
150                         return connectedComponent;\r
151                 } catch (DatabaseException e) {\r
152                 }\r
153                 return null;\r
154         }\r
155         \r
156         private Resource getFlagSourceComposite(ReadGraph graph, Variable variable){\r
157                 try {\r
158                         Layer0 L0 = Layer0.getInstance(graph);\r
159                         ModelingResources MOD = ModelingResources.getInstance(graph);\r
160                         \r
161                         Resource flag = variable.getRepresents(graph);\r
162                         Resource diagram = graph.getPossibleObject(flag, L0.PartOf);\r
163                         if (diagram == null)\r
164                                 return null;\r
165                         Resource composite = graph.getPossibleObject(diagram, MOD.DiagramToComposite);\r
166                         if (composite == null)\r
167                                 return null;\r
168                         return composite;\r
169                 } catch (DatabaseException e) {\r
170                 }\r
171                 return null;\r
172         }\r
173         \r
174         private Resource getFlagTargetComposite(ReadGraph graph, Variable variable){\r
175                 try {\r
176                         Layer0 L0 = Layer0.getInstance(graph);\r
177                         ModelingResources MOD = ModelingResources.getInstance(graph);\r
178                         \r
179                         Resource flag = variable.getRepresents(graph);\r
180                         Set<Resource> targetFlags = FlagUtil.getCounterparts(graph, flag);\r
181                         \r
182                         // if there are multiple targets\r
183                         if (targetFlags.size() != 1)\r
184                                 return null;\r
185                         \r
186                         Resource targetFlag = targetFlags.toArray(new Resource[0])[0];\r
187                         if (targetFlag == null)\r
188                                 return null;\r
189                         Resource diagram = graph.getPossibleObject(targetFlag, L0.PartOf);\r
190                         if (diagram == null)\r
191                                 return null;\r
192                         Resource composite = graph.getPossibleObject(diagram, MOD.DiagramToComposite);\r
193                         if (composite == null)\r
194                                 return null;\r
195                         return composite;\r
196                 } catch (DatabaseException e) {\r
197                 }\r
198                 return null;\r
199         }\r
200 \r
201         public static Resource getAnnotation(ReadGraph graph, Resource res) throws DatabaseException {\r
202                 Layer0 L0 = Layer0.getInstance(graph);\r
203         Collection<Resource> children = graph.getObjects(res, L0.ConsistsOf);\r
204         Resource anno = null;\r
205         AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
206         for (Resource child:children){\r
207                 if (graph.isInstanceOf(child, ANNO.Annotation)){\r
208                         anno = child;\r
209                         break;\r
210                 }\r
211         }\r
212         return anno;\r
213         } \r
214 \r
215         private Resource getDiagramComposite(ReadGraph graph, Variable variable){\r
216                 try {\r
217                         Resource res = variable.getRepresents(graph);\r
218                         if (res == null)\r
219                                 return null;\r
220                         DiagramResource DIA = DiagramResource.getInstance(graph);\r
221                         if (graph.isInstanceOf(res, DIA.Flag)){\r
222                                 ModelingResources MOD = ModelingResources.getInstance(graph);\r
223                                 Layer0 L0 = Layer0.getInstance(graph);\r
224                                 Resource parent = graph.getPossibleObject(res, L0.PartOf);\r
225                                 if (parent == null)\r
226                                         return null;\r
227                                 if (!graph.isInstanceOf(parent, DIA.Diagram))\r
228                                         return null;\r
229                                 return graph.getPossibleObject(parent, MOD.DiagramToComposite);\r
230                         }\r
231                         return res;\r
232                 } catch (DatabaseException e) {\r
233                 }\r
234                 return null;\r
235         }\r
236 \r
237         public Resource getPredefinedResource(ReadGraph graph, Variable selection, String id) throws DatabaseException {\r
238                 Resource predefined = null;\r
239         if (id.equals(root))\r
240                 predefined = graph.getRootLibrary();\r
241         else if (id.equals(project))\r
242                 predefined = SimanticsUI.getProject().get();\r
243         else if (id.equals(model))\r
244                 predefined = Variables.getPossibleModel(graph, selection);\r
245         else if (id.equals(template)){\r
246                         Resource composite = selection.getRepresents(graph);\r
247                         if (composite == null)\r
248                                 return null;\r
249                         ModelingResources MOD = ModelingResources.getInstance(graph);\r
250             Template2dResource TEPLATE2D = Template2dResource.getInstance(graph);\r
251                         Resource diagram = graph.getPossibleObject(composite, MOD.CompositeToDiagram);\r
252                         if (diagram == null)\r
253                                 return null;\r
254                         predefined = graph.getPossibleObject(diagram, TEPLATE2D.HasDrawingTemplate);\r
255         }\r
256         else if (id.equals(current))\r
257                 predefined =  selection.getPossibleRepresents(graph);\r
258         else if (id.equals(flagAnnotation)){\r
259                 Resource flag = selection.getPossibleRepresents(graph);\r
260                 if (flag != null)\r
261                         predefined = getAnnotation(graph, flag);\r
262         }\r
263         else if (id.equals(diagram)){\r
264                         Resource composite = selection.getPossibleRepresents(graph);\r
265                         if (composite == null)\r
266                                 return null;\r
267                         ModelingResources MOD = ModelingResources.getInstance(graph);\r
268                         predefined = graph.getPossibleObject(composite, MOD.CompositeToDiagram);\r
269         }\r
270         else if (id.equals(diagramComposite))\r
271                 predefined = getDiagramComposite(graph, selection);\r
272         else if (id.equals(flagSourceObject))\r
273                 predefined = getFlagSourceObject(graph, selection);\r
274         else if (id.equals(flagTargetObject))\r
275                 predefined = getFlagTargetObject(graph, selection);\r
276         else if (id.equals(flagSourceComposite))\r
277                 predefined = getFlagSourceComposite(graph, selection);\r
278         else if (id.equals(flagTargetComposite))\r
279                 predefined = getFlagTargetComposite(graph, selection);\r
280         else if (id.equals(flagRouterComponent))\r
281                 predefined = getFlagRouterComponent(graph, selection);\r
282         return predefined;\r
283         }\r
284         \r
285         public Variable getPredefinedVariable(ReadGraph graph, Variable selection, String id) throws DatabaseException {\r
286                 Resource predefined = this.getPredefinedResource(graph, selection, id);\r
287                 if (predefined == null)\r
288                         return null;\r
289                 \r
290         Variable v = selection;\r
291         if (predefined != null)\r
292             v = Variables.getPossibleVariable(graph, predefined);\r
293         return v;\r
294         }\r
295         \r
296         public Variable getVariable(ReadGraph graph, String path, Resource converter, Variable selection) throws DatabaseException {\r
297         if (path == null)\r
298                 return null;\r
299         int colonInx = path.indexOf(':');\r
300         int firstSlash = path.indexOf('/');\r
301         int firstHash = path.indexOf('#');\r
302         int firstFlashInx = firstSlash;\r
303         if(firstFlashInx == -1) firstFlashInx = firstHash;\r
304         else {\r
305                 if(firstHash > -1 && firstHash < firstFlashInx) firstFlashInx = firstHash;\r
306         }\r
307 \r
308         String predefined = null;\r
309         String relativePath = null;\r
310 \r
311         // if scheme is followed by absolute uri\r
312         // for examples:\r
313         // pre:/model/Library/logo.svg#data -- get under model\r
314         // pre:/./Library/logo.svg#data-- get under composite which corresponds the diagram\r
315         // pre:/project/Library/FORTUM_LOGO_CMYK.svg#data -- get under project\r
316         // pre:/root/Library/FORTUM_LOGO_CMYK.svg#data -- get under root\r
317         \r
318         if (colonInx != -1 && firstFlashInx != -1 && colonInx+1 == firstFlashInx && path.length() > firstFlashInx+1){\r
319             String scheme = path.substring(0, colonInx);\r
320             String absPath = path.substring(firstFlashInx+1);\r
321             if (scheme.equals("pre")){\r
322                 int endOfPredefined1 = absPath.indexOf('/');\r
323                 int endOfPredefined2 = absPath.indexOf('#');\r
324                 if (endOfPredefined1 == -1 && endOfPredefined2 == -1)\r
325                     predefined = absPath;\r
326                 if (endOfPredefined1 == -1 && endOfPredefined2 != -1)\r
327                     predefined = absPath.substring(0, endOfPredefined2);\r
328                 if (endOfPredefined1 != -1 && endOfPredefined2 == -1)\r
329                     predefined = absPath.substring(0, endOfPredefined1);\r
330                 if (endOfPredefined1 != -1 && endOfPredefined2 != -1){\r
331                     if (endOfPredefined2 < endOfPredefined1)\r
332                         endOfPredefined1 = endOfPredefined2;\r
333                     predefined = absPath.substring(0, endOfPredefined1);\r
334                 }\r
335                 relativePath = absPath.substring(predefined.length());\r
336             }\r
337         }\r
338 \r
339         //Variable activeVariable = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);\r
340         if (selection == null)\r
341                 return null;\r
342         \r
343         Variable v = selection;\r
344         if (predefined != null)\r
345                 v = getPredefinedVariable(graph, selection, predefined);\r
346 \r
347         if (v == null)\r
348             return null;\r
349 \r
350         Variable property = null;\r
351         if (relativePath != null){\r
352                 if (relativePath.startsWith("/."))\r
353                         relativePath = relativePath.substring(1);\r
354             property = v.browsePossible(graph, relativePath);\r
355         }\r
356         else\r
357             property = v.browsePossible(graph, path);\r
358         return property;\r
359     }\r
360 \r
361 }\r