1 package org.simantics.db.layer0.variable;
\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
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
27 public class StandardGraphChildVariable extends AbstractChildVariable {
\r
33 public Variable getPossibleSpecialChild(ReadGraph graph, String name) throws DatabaseException {
\r
37 public Map<String, Variable> collectSpecialChildren(ReadGraph graph, Map<String, Variable> children) throws DatabaseException {
\r
42 * Standard implementation
\r
46 final protected Variable parent;
\r
47 final public Resource resource;
\r
49 transient private int hash;
\r
51 public StandardGraphChildVariable(Variable parent, VariableNode node, Resource resource) {
\r
53 this.parent = parent;
\r
54 this.resource = resource;
\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
63 protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
\r
64 VariableMap map = getPossiblePropertyVariableMap(graph);
\r
65 if(map == null) return null;
\r
67 return map.getVariable(graph, this, name);
\r
68 } catch (DatabaseException e) {
\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
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
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
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
103 @SuppressWarnings("unchecked")
\r
105 public String getName(ReadGraph graph) throws DatabaseException {
\r
107 return node.support.manager.getName(node.node);
\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
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
120 return super.getNameVariable(graph);
\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
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
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
146 public Variable getParent(ReadGraph graph) throws DatabaseException {
\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
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
162 public Resource getPossibleRepresents(ReadGraph graph)
\r
163 throws DatabaseException {
\r
168 final public String getURI(ReadGraph graph) throws DatabaseException {
\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
180 * All variables must have proper URIs, this is only for debugging purposes.
\r
182 * @see org.simantics.db.layer0.variable.AbstractVariable#getPossibleURI(org.simantics.db.ReadGraph)
\r
185 public String getPossibleURI(ReadGraph graph) throws DatabaseException {
\r
187 return getURI(graph);
\r
188 } catch (DatabaseException e) {
\r
194 public int hashCode() {
\r
196 final int prime = 31;
\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
207 public boolean equals(Object obj) {
\r
212 if (getClass() != obj.getClass())
\r
215 StandardGraphChildVariable other = (StandardGraphChildVariable) obj;
\r
217 if(node != null) {
\r
218 if(!node.equals(other.node)) return false;
\r
219 } else if (other.node != null) return false;
\r
221 if(resource != null) {
\r
222 if(!resource.equals(other.resource)) return false;
\r
223 } else if (other.resource != null) return false;
\r
225 if(parent != null) {
\r
226 if(!parent.equals(other.parent)) return false;
\r
227 } else if (other.parent != null) return false;
\r
234 public <T> T adapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {
\r
235 return graph.adapt(resource, clazz);
\r
239 public <T> T adaptPossible(ReadGraph graph, Class<T> clazz) throws DatabaseException {
\r
240 return graph.getPossibleAdapter(resource, clazz);
\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
249 return new RVI.GuidRVIPart(Role.CHILD, resource, id.mostSignificant, id.leastSignificant);
\r
251 return new ResourceRVIPart(Role.CHILD, resource);
\r
254 public String getIdentifier() {
\r
255 return getClass().getSimpleName() + "[" + resource + "]";
\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
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
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
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
285 public Variable resolvePossible(ReadGraph graph, RVIPart part) throws DatabaseException {
\r
287 return resolve(graph, part);
\r
288 } catch (DatabaseException e) {
\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
301 if (result.isEmpty() && node != null) {
\r
303 result = node.support.manager.getClassifications(node.node);
\r
304 } catch(NodeManagerException e) {
\r
305 throw new DatabaseException(e);
\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
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
331 public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {
\r