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