1 package org.simantics.db.layer0.variable;
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashMap;
9 import org.simantics.databoard.Bindings;
10 import org.simantics.databoard.accessor.reference.ChildReference;
11 import org.simantics.databoard.binding.Binding;
12 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
13 import org.simantics.databoard.type.Datatype;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
18 import org.simantics.db.common.validation.L0Validations;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.exception.ValidationException;
21 import org.simantics.db.layer0.exception.MissingVariableValueException;
22 import org.simantics.db.layer0.exception.PendingVariableException;
23 import org.simantics.db.layer0.exception.VariableException;
24 import org.simantics.db.layer0.function.All;
25 import org.simantics.db.layer0.request.PropertyInfo;
26 import org.simantics.db.layer0.request.PropertyInfoRequest;
27 import org.simantics.db.layer0.util.Layer0Utils;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.utils.Development;
30 import org.simantics.utils.datastructures.Pair;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
35 private static final Logger LOGGER = LoggerFactory.getLogger(StandardGraphPropertyVariable.class);
37 protected static final PropertyInfo NO_PROPERTY = new PropertyInfo(null, null,
38 false, Collections.<String> emptySet(), null, null, null, null, null, null,
39 Collections.<String, Pair<Resource, ChildReference>> emptyMap(),
42 final public Variable parent;
43 final public Resource parentResource;
44 final public PropertyInfo property;
45 final public Resource represents;
47 transient private int hash = 0;
49 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource property) throws DatabaseException {
50 this(graph, parent, node, (Resource)parent.getPossibleRepresents(graph), getPropertyInfo(graph, property));
53 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, Resource property) throws DatabaseException {
54 this(graph, parent, node, parentResource, getPropertyInfo(graph, property));
57 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, Resource property) throws DatabaseException {
58 this(graph, parent, null, (Resource)parent.getPossibleRepresents(graph), getPropertyInfo(graph, property));
61 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, Resource parentResource, PropertyInfo property) throws DatabaseException {
62 this(parent, null, parentResource, property, getPossibleRepresents(graph, parentResource, property.predicate));
65 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, PropertyInfo property) throws DatabaseException {
66 this(parent, node, parentResource, property, getPossibleRepresents(graph, parentResource, property.predicate));
69 private static PropertyInfo getPropertyInfo(ReadGraph graph, Resource property) throws DatabaseException {
70 return property != null ? graph.syncRequest(new PropertyInfoRequest(property), TransientCacheAsyncListener.<PropertyInfo>instance()) : NO_PROPERTY;
73 private static Resource getPossibleRepresents(ReadGraph graph, Resource parentResource, Resource predicate) throws DatabaseException {
74 if(parentResource == null || predicate == null) return null;
75 return graph.getPossibleObject(parentResource, predicate);
78 public boolean isAsserted() {
82 public StandardGraphPropertyVariable(Variable parent, VariableNode node, Resource parentResource, PropertyInfo property, Resource represents) throws DatabaseException {
84 assert parent != null;
86 this.property = property;
87 this.parentResource = parentResource;
88 this.represents = represents;
92 public String getName(ReadGraph graph) throws DatabaseException {
93 if(node != null) return node.support.manager.getName(node.node);
98 public String getPossibleLabel(ReadGraph graph) throws DatabaseException {
99 return graph.getPossibleRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
103 public String getLabel(ReadGraph graph) throws DatabaseException {
104 return graph.getRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
108 public Variable getParent(ReadGraph graph) throws DatabaseException {
113 public PropertyInfo getPropertyInfo(ReadGraph graph) throws DatabaseException {
117 @SuppressWarnings("unchecked")
119 public <T> T getValue(ReadGraph graph) throws DatabaseException {
121 if(Development.DEVELOPMENT) {
122 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
125 //throw new ValidationException(error);
129 return (T)getValueAccessor(graph).getValue(graph, this);
133 @SuppressWarnings("unchecked")
135 public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
136 // Fall back to the bindingless method, if the expected output is a Java object
137 if (binding instanceof ObjectVariantBinding)
138 return getValue(graph);
140 if(Development.DEVELOPMENT) {
141 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
144 throw new ValidationException(error);
150 return (T)getValueAccessor(graph).getValue(graph, this, binding);
151 } catch (PendingVariableException e) {
153 } catch (Throwable t) {
154 throw new MissingVariableValueException(t);
160 public Resource getRepresents(ReadGraph graph) throws DatabaseException {
161 if(represents == null)
162 throw new VariableException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");
164 // return graph.getSingleObject(parentResource, property.predicate);
168 public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
169 // if(parentResource == null) return null;
170 // return graph.getPossibleObject(parentResource, property.predicate);
175 public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {
177 if(Development.DEVELOPMENT) {
178 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
181 //throw new ValidationException(error);
185 getValueAccessor(graph).setValue(graph, this, value, binding);
190 public void setValue(WriteGraph graph, Object value) throws DatabaseException {
192 if(Development.DEVELOPMENT) {
193 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
196 throw new ValidationException(error);
200 getValueAccessor(graph).setValue(graph, this, value);
205 public Binding getDefaultBinding(ReadGraph graph) throws DatabaseException {
206 return Layer0Utils.getDefaultBinding(graph, this);
210 public Binding getPossibleDefaultBinding(ReadGraph graph) throws DatabaseException {
211 return Layer0Utils.getPossibleDefaultBinding(graph, this);
215 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
218 // Binding binding = getPossibleDefaultBinding(graph);
219 // if(binding != null) return binding.type();
221 // Variant vt = getVariantValue(graph);
222 // binding = vt.getBinding();
223 // if(binding == null) throw new DatabaseException("No binding in Variant with value " + vt.getValue());
224 // return binding.type();
228 type = getValueAccessor(graph).getDatatype(graph, this);
229 } catch (Throwable t) {
230 throw new MissingVariableValueException(t);
234 String uri = this.getPossibleURI(graph);
236 throw new DatabaseException("No data type for " + uri);
238 throw new DatabaseException("No data type for " + this.getIdentifier());
247 public Datatype getPossibleDatatype(ReadGraph graph) throws DatabaseException {
250 return getDatatype(graph);
251 } catch (DatabaseException e) {
258 public String getUnit(ReadGraph graph) throws DatabaseException {
260 return Layer0Utils.getUnit(graph, this);
261 } catch (DatabaseException e) {
267 public Resource getPropertyResource(ReadGraph graph) {
268 return property.predicate;
272 public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
273 return parentResource;
277 public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
279 Map<String, Variable> result = new HashMap<String, Variable>();
280 VariableMap map = getPossibleChildVariableMap(graph);
281 if(map != null) map.getVariables(graph, this, result);
282 return result.values();
287 public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
289 VariableMap map = getPossibleChildVariableMap(graph);
290 if(map == null) return null;
292 return map.getVariable(graph, this, name);
293 } catch (DatabaseException e) {
301 protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
303 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
304 if(valueMap == null) return null;
306 return valueMap.getVariable(graph, this, name);
307 } catch (DatabaseException e) {
309 } catch (Exception t) {
310 LOGGER.error("getPossibleDomainProperty is implemented incorrectly, but returns null on Exception for backward compatibility. URI="+getURI(graph)+", name="+name+".", t);
317 public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {
319 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
320 if(valueMap == null) return properties;
321 return valueMap.getVariables(graph, this, properties);
326 public Variable getPredicate(ReadGraph graph) throws DatabaseException {
327 return Variables.getVariable(graph, graph.getURI(property.predicate));
331 public Resource getPredicateResource(ReadGraph graph) throws DatabaseException {
332 return property.predicate;
336 public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {
337 return property.predicate;
341 public int hashCode() {
343 final int prime = 31;
345 result = prime * result + (parent != null ? parent.hashCode() : 0);
346 result = prime * result + (node != null ? node.hashCode() : 0);
347 result = prime * result + (property.predicate != null ? property.predicate.hashCode() : 0);
348 result = prime * result + (parentResource != null ? parentResource.hashCode() : 0);
349 result = prime * result + (represents != null ? represents.hashCode() : 0);
356 public boolean equals(Object obj) {
361 if (getClass() != obj.getClass())
363 StandardGraphPropertyVariable other = (StandardGraphPropertyVariable) obj;
366 if(!node.equals(other.node)) return false;
367 } else if (other.node != null) return false;
369 if(property.predicate != null) {
370 if(!property.predicate.equals(other.property.predicate)) return false;
371 } else if (other.property.predicate != null) return false;
373 if(parentResource != null) {
374 if(!parentResource.equals(other.parentResource)) return false;
375 } else if (other.parentResource != null) return false;
378 if(!parent.equals(other.parent)) return false;
379 } else if (other.parent != null) return false;
381 if(represents != null) {
382 if(!represents.equals(other.represents)) return false;
383 } else if (other.represents != null) return false;
390 protected Variable getNameVariable(ReadGraph graph) throws DatabaseException {
391 throw new UnsupportedOperationException();
394 protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException {
395 if((property == null || property == NO_PROPERTY) && parentResource == null) return All.standardValueAccessor;
396 ValueAccessor accessor = property.valueAccessor;
397 if(accessor != null) return accessor;
399 System.err.println("No value accessor for " + getURI(graph));
400 return All.standardValueAccessor;
404 protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException {
405 if(represents == null) return All.standardPropertyDomainChildren;
406 Resource domainChildren = Layer0.getInstance(graph).domainChildren;
407 return graph.getPossibleRelatedValue2(represents, domainChildren,
408 new StandardGraphPropertyVariable(graph, this, domainChildren));
411 protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {
412 if(property == null) return All.standardPropertyDomainProperties;
413 if(property.predicate == null) return null;
414 return graph.syncRequest(new PropertyVariableMapRequest(property.predicate), TransientCacheAsyncListener.<VariableMap>instance());
417 public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
418 return property.classifications;
422 public Map<String, Variable> collectDomainProperties(ReadGraph graph, String classification, Map<String, Variable> properties) throws DatabaseException {
424 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
425 if(valueMap == null) return properties;
426 return valueMap.getVariables(graph, this, classification, properties);
430 public Collection<Variable> getProperties(ReadGraph graph, String classification) throws DatabaseException {
432 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
433 if(valueMap == null) return Collections.emptyList();
434 Map<String,Variable> propertyMap = valueMap.getVariables(graph, this, classification, null);
435 if(propertyMap == null || propertyMap.isEmpty()) return Collections.emptyList();
436 else return propertyMap.values();