1 package org.simantics.db.layer0.variable;
3 import java.util.Collection;
4 import java.util.Collections;
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.exception.VariableException;
16 import org.simantics.db.layer0.exception.InvalidVariableException;
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;
27 public class StandardGraphChildVariable extends AbstractChildVariable {
33 public Variable getPossibleSpecialChild(ReadGraph graph, String name) throws DatabaseException {
37 public Map<String, Variable> collectSpecialChildren(ReadGraph graph, Map<String, Variable> children) throws DatabaseException {
42 * Standard implementation
46 final protected Variable parent;
47 final public Resource resource;
49 transient private int hash;
51 public StandardGraphChildVariable(Variable parent, VariableNode node, Resource resource) {
54 this.resource = resource;
58 public void validate(ReadGraph graph) throws DatabaseException {
59 if(!graph.hasStatement(resource)) throw new InvalidVariableException("The resource has been removed: " + resource);
63 protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
64 VariableMap map = getPossiblePropertyVariableMap(graph);
65 if(map == null) return null;
67 return map.getVariable(graph, this, name);
68 } catch (DatabaseException e) {
74 public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
75 VariableMap map = getPossibleChildVariableMap(graph);
76 if(map == null) return getPossibleSpecialChild(graph, name);
78 Variable result = map.getVariable(graph, this, name);
79 if(result != null) return result;
80 else return getPossibleSpecialChild(graph, name);
81 } catch (DatabaseException e) {
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);
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();
103 @SuppressWarnings("unchecked")
105 public String getName(ReadGraph graph) throws DatabaseException {
107 return node.support.manager.getName(node.node);
109 String unescapedName = graph.getPossibleRelatedValue(resource, graph.getService(Layer0.class).HasName, Bindings.STRING);
110 if(unescapedName == null) return "r" + resource.getResourceId();
111 return unescapedName;
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);
120 return super.getNameVariable(graph);
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);
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);
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);
146 public Variable getParent(ReadGraph graph) throws DatabaseException {
151 final public Resource getRepresents(ReadGraph graph) throws DatabaseException {
153 throw new InvalidVariableException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");
155 // Layer0X L0X = Layer0X.getInstance(graph);
156 // Resource represents = graph.getPossibleObject(resource, L0X.Represents);
157 // if(represents != null) return represents;
158 // else return resource;
162 public Resource getPossibleRepresents(ReadGraph graph)
163 throws DatabaseException {
168 final public String getURI(ReadGraph graph) throws DatabaseException {
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);
180 * All variables must have proper URIs, this is only for debugging purposes.
182 * @see org.simantics.db.layer0.variable.AbstractVariable#getPossibleURI(org.simantics.db.ReadGraph)
185 public String getPossibleURI(ReadGraph graph) throws DatabaseException {
187 return getURI(graph);
188 } catch (DatabaseException e) {
194 public int hashCode() {
196 final int prime = 31;
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);
207 public boolean equals(Object obj) {
212 if (getClass() != obj.getClass())
215 StandardGraphChildVariable other = (StandardGraphChildVariable) obj;
218 if(!node.equals(other.node)) return false;
219 } else if (other.node != null) return false;
221 if(resource != null) {
222 if(!resource.equals(other.resource)) return false;
223 } else if (other.resource != null) return false;
226 if(!parent.equals(other.parent)) return false;
227 } else if (other.parent != null) return false;
234 public <T> T adapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {
235 return graph.adapt(resource, clazz);
239 public <T> T adaptPossible(ReadGraph graph, Class<T> clazz) throws DatabaseException {
240 return graph.getPossibleAdapter(resource, clazz);
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);
249 return new RVI.GuidRVIPart(Role.CHILD, resource, id.mostSignificant, id.leastSignificant);
251 return new ResourceRVIPart(Role.CHILD, resource);
254 public String getIdentifier() {
255 return getClass().getSimpleName() + "[" + resource + "]";
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));
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));
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);
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);
289 public Variable resolvePossible(ReadGraph graph, RVIPart part) throws DatabaseException {
291 return resolve(graph, part);
292 } catch (DatabaseException e) {
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)));
305 if (result.isEmpty() && node != null) {
307 result = node.support.manager.getClassifications(node.node);
308 } catch(NodeManagerException e) {
309 throw new DatabaseException(e);
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);
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);
335 public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {