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