]> gerrit.simantics Code Review - simantics/platform.git/blob
17fc21ebb911bb2130ff3cd10c662f9a083b9162
[simantics/platform.git] /
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                 return graph.getPossibleRelatedValue2(resource, Layer0.getInstance(graph).domainChildren, this);
268         }
269         
270         @Override
271     public Map<String, Variable> collectDomainProperties(ReadGraph graph, String classification, Map<String, Variable> properties) throws DatabaseException {
272         VariableMap valueMap = getPossiblePropertyVariableMap(graph);
273                 if(valueMap == null) return properties;
274                 return valueMap.getVariables(graph, this, classification, properties);
275     }
276         
277     @Override
278     public Variable resolve(ReadGraph graph, RVIPart part) throws DatabaseException {
279         Resource represents = getPossibleRepresents(graph);
280         if (represents == null)
281             return StandardRVIResolver.INSTANCE.getVariable(graph, this, part);
282         RVIResolver resolver = graph.adapt(represents, RVIResolver.class);
283         return resolver.getVariable(graph, this, part);
284     }    
285
286     @Override
287     public Variable resolvePossible(ReadGraph graph, RVIPart part) throws DatabaseException {
288         try {
289             return resolve(graph, part);
290         } catch (DatabaseException e) {
291             return null;
292         }
293     }
294
295     @SuppressWarnings("unchecked")
296     public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
297         Resource represents = getPossibleRepresents(graph);
298         Set<String> result = (represents != null) ? graph.syncRequest(new ClassificationsRequest(graph.getPrincipalTypes(represents))) : Collections.<String>emptySet();
299         if (result.isEmpty()) {
300                 Resource type = getPossibleType(graph);
301                 if(type != null) result = graph.syncRequest(new ClassificationsRequest(Collections.singleton(type)));
302         }
303         if (result.isEmpty() && node != null) {
304             try {
305                 result = node.support.manager.getClassifications(node.node);
306             } catch(NodeManagerException e) {
307                 throw new DatabaseException(e);
308             }
309         }
310         return result;
311     }
312
313     @Override
314     public RVI getRVI(ReadGraph graph) throws DatabaseException {
315         Resource represents = getPossibleRepresents(graph);
316         if (represents == null)
317             return StandardRVIResolver.INSTANCE.getRVI(graph, this);
318         RVIResolver resolver = graph.adapt(represents, RVIResolver.class);
319         return resolver.getRVI(graph, this);
320     }    
321     
322     @Override
323     public RVI getPossibleRVI(ReadGraph graph) throws DatabaseException {
324         Resource represents = getPossibleRepresents(graph);
325         if (represents == null)
326             return StandardRVIResolver.INSTANCE.getPossibleRVI(graph, this);
327         RVIResolver resolver = graph.getPossibleAdapter(represents, RVIResolver.class);
328         if(resolver == null) return null;
329         return resolver.getPossibleRVI(graph, this);
330     }
331
332     @Override
333     public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {
334         return null;
335     }
336         
337 }