]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardGraphChildVariable.java
Merge commit 'ad8333027322fda6b9a8a524c7a7e15a54c52f38'
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / StandardGraphChildVariable.java
1 package org.simantics.db.layer0.variable;\r
2 \r
3 import java.util.Collection;\r
4 import java.util.Collections;\r
5 import java.util.Map;\r
6 import java.util.Set;\r
7 \r
8 import org.simantics.databoard.Bindings;\r
9 import org.simantics.datatypes.literal.GUID;\r
10 import org.simantics.db.ReadGraph;\r
11 import org.simantics.db.Resource;\r
12 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
13 import org.simantics.db.exception.AssumptionException;\r
14 import org.simantics.db.exception.DatabaseException;\r
15 import org.simantics.db.layer0.exception.InvalidVariableException;\r
16 import org.simantics.db.layer0.exception.VariableException;\r
17 import org.simantics.db.layer0.function.All;\r
18 import org.simantics.db.layer0.request.ClassificationsRequest;\r
19 import org.simantics.db.layer0.request.VariableURI;\r
20 import org.simantics.db.layer0.variable.RVI.RVIPart;\r
21 import org.simantics.db.layer0.variable.RVI.ResourceRVIPart;\r
22 import org.simantics.db.layer0.variable.RVI.StringRVIPart;\r
23 import org.simantics.db.layer0.variable.Variables.Role;\r
24 import org.simantics.layer0.Layer0;\r
25 import org.simantics.simulator.variable.exceptions.NodeManagerException;\r
26 \r
27 public class StandardGraphChildVariable extends AbstractChildVariable {\r
28 \r
29         /*\r
30          * Extension points\r
31          * \r
32          */\r
33         public Variable getPossibleSpecialChild(ReadGraph graph, String name) throws DatabaseException {\r
34                 return null;\r
35         }\r
36 \r
37         public Map<String, Variable> collectSpecialChildren(ReadGraph graph, Map<String, Variable> children) throws DatabaseException {\r
38                 return children;\r
39         }\r
40 \r
41         /*\r
42          * Standard implementation\r
43          * \r
44          */\r
45         \r
46         final protected Variable parent;\r
47         final public Resource resource;\r
48         \r
49         transient private int hash;\r
50         \r
51         public StandardGraphChildVariable(Variable parent, VariableNode node, Resource resource) {\r
52                 super(node);\r
53                 this.parent = parent;\r
54                 this.resource = resource;\r
55         }\r
56 \r
57         @Override\r
58         public void validate(ReadGraph graph) throws DatabaseException {\r
59                 if(!graph.hasStatement(resource)) throw new InvalidVariableException("The resource has been removed: " + resource);\r
60         }\r
61         \r
62         @Override\r
63         protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {\r
64                 VariableMap map = getPossiblePropertyVariableMap(graph);\r
65                 if(map == null) return null;\r
66                 try {\r
67                         return map.getVariable(graph, this, name);\r
68                 } catch (DatabaseException e) {\r
69                         return null;\r
70                 }\r
71         }\r
72 \r
73         @Override\r
74         public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {\r
75                 VariableMap map = getPossibleChildVariableMap(graph);\r
76                 if(map == null) return getPossibleSpecialChild(graph, name);\r
77                 try {\r
78                         Variable result = map.getVariable(graph, this, name);\r
79                         if(result != null) return result;\r
80                         else return getPossibleSpecialChild(graph, name);\r
81                 } catch (DatabaseException e) {\r
82                         return null;\r
83                 }\r
84         }\r
85 \r
86         @Override\r
87         public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {\r
88                 VariableMap map = getPossiblePropertyVariableMap(graph);\r
89                 if(map == null) return properties;\r
90                 return map.getVariables(graph, this, properties);\r
91         }\r
92 \r
93         @Override\r
94         public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {\r
95                 Map<String, Variable> result = null;\r
96                 VariableMap map = getPossibleChildVariableMap(graph);\r
97                 if(map != null) result = map.getVariables(graph, this, result);\r
98                 result = collectSpecialChildren(graph, result);\r
99                 if(result == null) return Collections.emptyList();\r
100                 else return result.values();\r
101         }\r
102 \r
103         @SuppressWarnings("unchecked")\r
104         @Override\r
105         public String getName(ReadGraph graph) throws DatabaseException {\r
106             if(node != null) {\r
107                 return node.support.manager.getName(node.node);\r
108             }\r
109                 String unescapedName = graph.getPossibleRelatedValue(resource, graph.getService(Layer0.class).HasName, Bindings.STRING);\r
110                 if(unescapedName == null) return "r" + resource.getResourceId();\r
111                 return unescapedName;\r
112         }\r
113 \r
114         @Override\r
115         public Variable getNameVariable(ReadGraph graph) throws DatabaseException {\r
116                 Resource resource = (Resource) getPossibleRepresents(graph);\r
117                 if (resource != null) {\r
118                         return new StandardGraphPropertyVariable(graph, this, null, resource, Layer0.getInstance(graph).HasName);\r
119                 }\r
120                 return super.getNameVariable(graph);\r
121         }\r
122         \r
123         \r
124         @Override\r
125         public String getLabel(ReadGraph graph) throws DatabaseException {\r
126             if(resource == null) return getName(graph);\r
127                 String label = graph.getPossibleRelatedValue2(resource, graph.getService(Layer0.class).HasLabel);\r
128                 return label != null ? label : getName(graph);\r
129         }\r
130 \r
131         @Override\r
132         final protected Variable getLabelVariable(ReadGraph graph) throws DatabaseException {\r
133                 Layer0 L0 = Layer0.getInstance(graph);\r
134                 if(resource == null) return null;\r
135                 return new StandardGraphPropertyVariable(graph, this, L0.HasLabel);\r
136         }\r
137         \r
138 //      @Override\r
139 //      final public Object getSerialized(ReadGraph graph) throws DatabaseException {\r
140 //              Resource represents = getRepresents(graph);\r
141 //              if(represents != null) return represents;\r
142 //              else return getName(graph);\r
143 //      }\r
144 \r
145         @Override\r
146         public Variable getParent(ReadGraph graph) throws DatabaseException {\r
147                 return parent;\r
148         }\r
149         \r
150         @Override\r
151         final public Resource getRepresents(ReadGraph graph) throws DatabaseException {\r
152             if(resource == null)\r
153                 throw new VariableException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");\r
154             return resource;\r
155 //              Layer0X L0X = Layer0X.getInstance(graph);\r
156 //              Resource represents = graph.getPossibleObject(resource, L0X.Represents);\r
157 //              if(represents != null) return represents;\r
158 //              else return resource;\r
159         }\r
160 \r
161         @Override\r
162         public Resource getPossibleRepresents(ReadGraph graph)\r
163                 throws DatabaseException {\r
164             return resource;\r
165         }\r
166         \r
167         @Override\r
168         final public String getURI(ReadGraph graph) throws DatabaseException {\r
169                 \r
170                 try {\r
171                         if(parent == null) return "http:/";\r
172                         String parentURI = graph.syncRequest(new VariableURI(parent), TransientCacheAsyncListener.<String>instance());\r
173                         return parentURI + Role.CHILD.getIdentifier() + encodeString(getName(graph));\r
174                 } catch (AssumptionException e) {\r
175                         throw new InvalidVariableException(e);\r
176                 }\r
177         }\r
178 \r
179         /**\r
180          * All variables must have proper URIs, this is only for debugging purposes.\r
181          * \r
182          * @see org.simantics.db.layer0.variable.AbstractVariable#getPossibleURI(org.simantics.db.ReadGraph)\r
183          */\r
184         @Override\r
185         public String getPossibleURI(ReadGraph graph) throws DatabaseException {\r
186         try {\r
187             return getURI(graph);\r
188         } catch (DatabaseException e) {\r
189             return "<no uri>";\r
190         }\r
191         }\r
192 \r
193         @Override\r
194         public int hashCode() {\r
195                 if(hash == 0) {\r
196                         final int prime = 31;\r
197                         int result = 1;\r
198                         result = prime * result + (parent != null ? parent.hashCode() : 0);\r
199                         result = prime * result + (node != null ? node.hashCode() : 0);\r
200                         result = prime * result + (resource != null ? resource.hashCode() : 0);\r
201                         hash =result;\r
202                 }\r
203                 return hash;\r
204         }\r
205 \r
206         @Override\r
207         public boolean equals(Object obj) {\r
208                 if (this == obj)\r
209                         return true;\r
210                 if (obj == null)\r
211                         return false;\r
212                 if (getClass() != obj.getClass())\r
213                         return false;\r
214                 \r
215                 StandardGraphChildVariable other = (StandardGraphChildVariable) obj;\r
216                 \r
217                 if(node != null) { \r
218                         if(!node.equals(other.node)) return false;\r
219                 } else if (other.node != null) return false;\r
220 \r
221         if(resource != null) {\r
222                 if(!resource.equals(other.resource)) return false;\r
223         } else if (other.resource != null) return false;\r
224                 \r
225         if(parent != null) {\r
226                 if(!parent.equals(other.parent)) return false;\r
227         } else if (other.parent != null) return false;\r
228         \r
229         return true;\r
230                 \r
231         }\r
232         \r
233         @Override\r
234         public <T> T adapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
235                 return graph.adapt(resource, clazz);\r
236         }\r
237 \r
238     @Override\r
239     public <T> T adaptPossible(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
240         return graph.getPossibleAdapter(resource, clazz);\r
241     }\r
242 \r
243         @Override\r
244         public RVIPart getRVIPart(ReadGraph graph) throws DatabaseException {\r
245             if(resource == null) return new StringRVIPart(Role.CHILD, getName(graph));\r
246         Layer0 L0 = Layer0.getInstance(graph);\r
247         GUID id = graph.getPossibleRelatedValue(resource, L0.identifier, GUID.BINDING);\r
248         if(id != null)\r
249                 return new RVI.GuidRVIPart(Role.CHILD, resource, id.mostSignificant, id.leastSignificant);\r
250         else\r
251                 return new ResourceRVIPart(Role.CHILD, resource);\r
252         }\r
253         \r
254     public String getIdentifier() {\r
255         return getClass().getSimpleName() + "[" + resource + "]";\r
256     }\r
257         \r
258         protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {\r
259         if(resource == null) return All.standardChildDomainProperties;\r
260                 return graph.getPossibleRelatedValue2(resource, Layer0.getInstance(graph).domainProperties, this);\r
261         }\r
262 \r
263         protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException {\r
264             if(resource == null) return All.standardChildDomainChildren;\r
265                 return graph.getPossibleRelatedValue2(resource, Layer0.getInstance(graph).domainChildren, this);\r
266         }\r
267         \r
268         @Override\r
269     public Map<String, Variable> collectDomainProperties(ReadGraph graph, String classification, Map<String, Variable> properties) throws DatabaseException {\r
270         VariableMap valueMap = getPossiblePropertyVariableMap(graph);\r
271                 if(valueMap == null) return properties;\r
272                 return valueMap.getVariables(graph, this, classification, properties);\r
273     }\r
274         \r
275     @Override\r
276     public Variable resolve(ReadGraph graph, RVIPart part) throws DatabaseException {\r
277         Resource represents = getPossibleRepresents(graph);\r
278         if (represents == null)\r
279             return StandardRVIResolver.INSTANCE.getVariable(graph, this, part);\r
280         RVIResolver resolver = graph.adapt(represents, RVIResolver.class);\r
281         return resolver.getVariable(graph, this, part);\r
282     }    \r
283 \r
284     @Override\r
285     public Variable resolvePossible(ReadGraph graph, RVIPart part) throws DatabaseException {\r
286         try {\r
287             return resolve(graph, part);\r
288         } catch (DatabaseException e) {\r
289             return null;\r
290         }\r
291     }\r
292 \r
293     @SuppressWarnings("unchecked")\r
294     public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {\r
295         Resource represents = getPossibleRepresents(graph);\r
296         Set<String> result = (represents != null) ? graph.syncRequest(new ClassificationsRequest(graph.getPrincipalTypes(represents))) : Collections.<String>emptySet();\r
297         if (result.isEmpty()) {\r
298                 Resource type = getPossibleType(graph);\r
299                 if(type != null) result = graph.syncRequest(new ClassificationsRequest(Collections.singleton(type)));\r
300         }\r
301         if (result.isEmpty() && node != null) {\r
302             try {\r
303                 result = node.support.manager.getClassifications(node.node);\r
304             } catch(NodeManagerException e) {\r
305                 throw new DatabaseException(e);\r
306             }\r
307         }\r
308         return result;\r
309     }\r
310 \r
311     @Override\r
312     public RVI getRVI(ReadGraph graph) throws DatabaseException {\r
313         Resource represents = getPossibleRepresents(graph);\r
314         if (represents == null)\r
315             return StandardRVIResolver.INSTANCE.getRVI(graph, this);\r
316         RVIResolver resolver = graph.adapt(represents, RVIResolver.class);\r
317         return resolver.getRVI(graph, this);\r
318     }    \r
319     \r
320     @Override\r
321     public RVI getPossibleRVI(ReadGraph graph) throws DatabaseException {\r
322         Resource represents = getPossibleRepresents(graph);\r
323         if (represents == null)\r
324             return StandardRVIResolver.INSTANCE.getPossibleRVI(graph, this);\r
325         RVIResolver resolver = graph.getPossibleAdapter(represents, RVIResolver.class);\r
326         if(resolver == null) return null;\r
327         return resolver.getPossibleRVI(graph, this);\r
328     }\r
329 \r
330     @Override\r
331     public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {\r
332         return null;\r
333     }\r
334         \r
335 }\r