]> gerrit.simantics Code Review - simantics/platform.git/blob
577f48e46dbb969cfa8395f357a1e23cf784718e
[simantics/platform.git] /
1 package org.simantics.document.linking.report.evaluator;
2
3 import java.util.Collection;
4
5 import org.simantics.annotation.ontology.AnnotationResource;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.layer0.variable.Variable;
10 import org.simantics.db.layer0.variable.Variables;
11 import org.simantics.layer0.Layer0;
12 import org.simantics.ui.SimanticsUI;
13
14 /**
15  * Copy-paste from org.simantics.diagram.function.PredefinedVariables (removed diagram specific stuff)
16  * 
17  * 
18  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
19  *
20  */
21 public class PredefinedVariables {
22         public static final String root = "root";
23         public static final String project = "project";
24         public static final String model = "model";
25         public static final String template = "template";
26         public static final String current = ".";
27
28
29         private static PredefinedVariables factory = new PredefinedVariables();
30         
31         public static PredefinedVariables getInstance(){
32                 return factory;
33         }
34         
35         public static void setFactory(PredefinedVariables factory){
36                 PredefinedVariables.factory = factory;
37         }
38         
39     Resource connectedComponent = null;
40     
41
42
43
44
45         public static Resource getAnnotation(ReadGraph graph, Resource res) throws DatabaseException {
46                 Layer0 L0 = Layer0.getInstance(graph);
47         Collection<Resource> children = graph.getObjects(res, L0.ConsistsOf);
48         Resource anno = null;
49         AnnotationResource ANNO = AnnotationResource.getInstance(graph);
50         for (Resource child:children){
51                 if (graph.isInstanceOf(child, ANNO.Annotation)){
52                         anno = child;
53                         break;
54                 }
55         }
56         return anno;
57         } 
58
59
60
61         public Resource getPredefinedResource(ReadGraph graph, Variable selection, String id) throws DatabaseException {
62                 Resource predefined = null;
63         if (id.equals(root))
64                 predefined = graph.getRootLibrary();
65         else if (id.equals(project))
66                 predefined = SimanticsUI.getProject().get();
67         else if (id.equals(model))
68                 predefined = Variables.getModel(graph, selection);
69         else if (id.equals(template)){
70 //                      Resource composite = selection.getRepresents(graph);
71 //                      if (composite == null)
72 //                              return null;
73 //                      ModelingResources MOD = ModelingResources.getInstance(graph);
74 //            Template2dResource TEPLATE2D = Template2dResource.getInstance(graph);
75 //                      Resource diagram = graph.getPossibleObject(composite, MOD.CompositeToDiagram);
76 //                      if (diagram == null)
77 //                              return null;
78 //                      predefined = graph.getPossibleObject(diagram, TEPLATE2D.HasDrawingTemplate);
79         }
80         else if (id.equals(current))
81                 predefined =  selection.getRepresents(graph);
82         return predefined;
83         }
84         
85         public Variable getPredefinedVariable(ReadGraph graph, Variable selection, String id) throws DatabaseException {
86                 Resource predefined = this.getPredefinedResource(graph, selection, id);
87                 if (predefined == null)
88                         return null;
89                 
90         Variable v = selection;
91         //Variable test = Variables.getPossibleVariable(graph, "http://Projects/Development%20Project/Model/Configuration/NewAutomationDiagram2/__CONTAINER__/__DIAGRAM__/4");
92         if (predefined != null)
93             v = graph.adapt(predefined, Variable.class);
94         return v;
95         }
96         
97         public Variable getVariable(ReadGraph graph, String path, Resource converter, Variable selection) throws DatabaseException {
98         if (path == null)
99                 return null;
100         int colonInx = path.indexOf(':');
101         int firstSlash = path.indexOf('/');
102         int firstHash = path.indexOf('#');
103         int firstFlashInx = firstSlash;
104         if(firstFlashInx == -1) firstFlashInx = firstHash;
105         else {
106                 if(firstHash > -1 && firstHash < firstFlashInx) firstFlashInx = firstHash;
107         }
108
109         String predefined = null;
110         String relativePath = null;
111
112         // if scheme is followed by absolute uri
113         // for examples:
114         // pre:/model/Library/logo.svg#data -- get under model
115         // pre:/./Library/logo.svg#data-- get under composite which corresponds the diagram
116         // pre:/project/Library/FORTUM_LOGO_CMYK.svg#data -- get under project
117         // pre:/root/Library/FORTUM_LOGO_CMYK.svg#data -- get under root
118         
119         if (colonInx != -1 && firstFlashInx != -1 && colonInx+1 == firstFlashInx && path.length() > firstFlashInx+1){
120             String scheme = path.substring(0, colonInx);
121             String absPath = path.substring(firstFlashInx+1);
122             if (scheme.equals("pre")){
123                 int endOfPredefined1 = absPath.indexOf('/');
124                 int endOfPredefined2 = absPath.indexOf('#');
125                 if (endOfPredefined1 == -1 && endOfPredefined2 == -1)
126                     predefined = absPath;
127                 if (endOfPredefined1 == -1 && endOfPredefined2 != -1)
128                     predefined = absPath.substring(0, endOfPredefined2);
129                 if (endOfPredefined1 != -1 && endOfPredefined2 == -1)
130                     predefined = absPath.substring(0, endOfPredefined1);
131                 if (endOfPredefined1 != -1 && endOfPredefined2 != -1){
132                     if (endOfPredefined2 < endOfPredefined1)
133                         endOfPredefined1 = endOfPredefined2;
134                     predefined = absPath.substring(0, endOfPredefined1);
135                 }
136                 relativePath = absPath.substring(predefined.length());
137             }
138         }
139
140         //Variable activeVariable = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
141         if (selection == null)
142                 return null;
143         
144         Variable v = selection;
145         if (predefined != null)
146                 v = getPredefinedVariable(graph, selection, predefined);
147
148         if (v == null)
149             return null;
150
151         Variable property = null;
152         if (relativePath != null){
153                 if (relativePath.startsWith("/."))
154                         relativePath = relativePath.substring(1);
155             property = v.browsePossible(graph, relativePath);
156         }
157         else
158             property = v.browsePossible(graph, path);
159         return property;
160     }
161
162 }