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.utils.Logger;
19 import org.simantics.db.common.validation.L0Validations;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.exception.ValidationException;
22 import org.simantics.db.layer0.exception.MissingVariableValueException;
23 import org.simantics.db.layer0.exception.PendingVariableException;
24 import org.simantics.db.layer0.exception.VariableException;
25 import org.simantics.db.layer0.function.All;
26 import org.simantics.db.layer0.request.PropertyInfo;
27 import org.simantics.db.layer0.request.PropertyInfoRequest;
28 import org.simantics.db.layer0.util.Layer0Utils;
29 import org.simantics.layer0.Layer0;
30 import org.simantics.utils.Development;
31 import org.simantics.utils.datastructures.Pair;
33 public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
35 protected static final PropertyInfo NO_PROPERTY = new PropertyInfo(null, null,
36 false, Collections.<String> emptySet(), null, null, null, null, null, null,
37 Collections.<String, Pair<Resource, ChildReference>> emptyMap(),
40 final public Variable parent;
41 final public Resource parentResource;
42 final public PropertyInfo property;
43 final public Resource represents;
45 transient private int hash = 0;
47 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource property) throws DatabaseException {
48 this(graph, parent, node, (Resource)parent.getPossibleRepresents(graph), getPropertyInfo(graph, property));
51 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, Resource property) throws DatabaseException {
52 this(graph, parent, node, parentResource, getPropertyInfo(graph, property));
55 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, Resource property) throws DatabaseException {
56 this(graph, parent, null, (Resource)parent.getPossibleRepresents(graph), getPropertyInfo(graph, property));
59 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, Resource parentResource, PropertyInfo property) throws DatabaseException {
60 this(parent, null, parentResource, property, getPossibleRepresents(graph, parentResource, property.predicate));
63 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, PropertyInfo property) throws DatabaseException {
64 this(parent, node, parentResource, property, getPossibleRepresents(graph, parentResource, property.predicate));
67 private static PropertyInfo getPropertyInfo(ReadGraph graph, Resource property) throws DatabaseException {
68 return property != null ? graph.syncRequest(new PropertyInfoRequest(property), TransientCacheAsyncListener.<PropertyInfo>instance()) : NO_PROPERTY;
71 private static Resource getPossibleRepresents(ReadGraph graph, Resource parentResource, Resource predicate) throws DatabaseException {
72 if(parentResource == null || predicate == null) return null;
73 return graph.getPossibleObject(parentResource, predicate);
76 public boolean isAsserted() {
80 public StandardGraphPropertyVariable(Variable parent, VariableNode node, Resource parentResource, PropertyInfo property, Resource represents) throws DatabaseException {
82 assert parent != null;
84 this.property = property;
85 this.parentResource = parentResource;
86 this.represents = represents;
90 public String getName(ReadGraph graph) throws DatabaseException {
91 if(node != null) return node.support.manager.getName(node.node);
96 public String getPossibleLabel(ReadGraph graph) throws DatabaseException {
97 return graph.getPossibleRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
101 public String getLabel(ReadGraph graph) throws DatabaseException {
102 return graph.getRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
106 public Variable getParent(ReadGraph graph) throws DatabaseException {
111 public PropertyInfo getPropertyInfo(ReadGraph graph) throws DatabaseException {
115 @SuppressWarnings("unchecked")
117 public <T> T getValue(ReadGraph graph) throws DatabaseException {
119 if(Development.DEVELOPMENT) {
120 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
122 Logger.defaultLogError(new ValidationException(error));
123 //throw new ValidationException(error);
127 return (T)getValueAccessor(graph).getValue(graph, this);
131 @SuppressWarnings("unchecked")
133 public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
134 // Fall back to the bindingless method, if the expected output is a Java object
135 if (binding instanceof ObjectVariantBinding)
136 return getValue(graph);
138 if(Development.DEVELOPMENT) {
139 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
141 Logger.defaultLogError(new ValidationException(error));
142 throw new ValidationException(error);
148 return (T)getValueAccessor(graph).getValue(graph, this, binding);
149 } catch (PendingVariableException e) {
151 } catch (Throwable t) {
152 throw new MissingVariableValueException(t);
158 public Resource getRepresents(ReadGraph graph) throws DatabaseException {
159 if(represents == null)
160 throw new VariableException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");
162 // return graph.getSingleObject(parentResource, property.predicate);
166 public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
167 // if(parentResource == null) return null;
168 // return graph.getPossibleObject(parentResource, property.predicate);
173 public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {
175 if(Development.DEVELOPMENT) {
176 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
178 Logger.defaultLogError(new ValidationException(error));
179 //throw new ValidationException(error);
183 getValueAccessor(graph).setValue(graph, this, value, binding);
188 public void setValue(WriteGraph graph, Object value) throws DatabaseException {
190 if(Development.DEVELOPMENT) {
191 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
193 Logger.defaultLogError(new ValidationException(error));
194 throw new ValidationException(error);
198 getValueAccessor(graph).setValue(graph, this, value);
203 public Binding getDefaultBinding(ReadGraph graph) throws DatabaseException {
204 return Layer0Utils.getDefaultBinding(graph, this);
208 public Binding getPossibleDefaultBinding(ReadGraph graph) throws DatabaseException {
209 return Layer0Utils.getPossibleDefaultBinding(graph, this);
213 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
216 // Binding binding = getPossibleDefaultBinding(graph);
217 // if(binding != null) return binding.type();
219 // Variant vt = getVariantValue(graph);
220 // binding = vt.getBinding();
221 // if(binding == null) throw new DatabaseException("No binding in Variant with value " + vt.getValue());
222 // return binding.type();
226 type = getValueAccessor(graph).getDatatype(graph, this);
227 } catch (Throwable t) {
228 throw new MissingVariableValueException(t);
232 String uri = this.getPossibleURI(graph);
234 throw new DatabaseException("No data type for " + uri);
236 throw new DatabaseException("No data type for " + this.getIdentifier());
245 public Datatype getPossibleDatatype(ReadGraph graph) throws DatabaseException {
248 return getDatatype(graph);
249 } catch (DatabaseException e) {
256 public String getUnit(ReadGraph graph) throws DatabaseException {
258 return Layer0Utils.getUnit(graph, this);
259 } catch (DatabaseException e) {
265 public Resource getPropertyResource(ReadGraph graph) {
266 return property.predicate;
270 public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
271 return parentResource;
275 public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
277 Map<String, Variable> result = new HashMap<String, Variable>();
278 VariableMap map = getPossibleChildVariableMap(graph);
279 if(map != null) map.getVariables(graph, this, result);
280 return result.values();
285 public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
287 VariableMap map = getPossibleChildVariableMap(graph);
288 if(map == null) return null;
290 return map.getVariable(graph, this, name);
291 } catch (DatabaseException e) {
299 protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
301 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
302 if(valueMap == null) return null;
304 return valueMap.getVariable(graph, this, name);
305 } catch (DatabaseException e) {
307 } catch (Throwable t) {
308 System.err.println("err: " + getURI(graph) + " # " + name);
315 public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {
317 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
318 if(valueMap == null) return properties;
319 return valueMap.getVariables(graph, this, properties);
324 public Variable getPredicate(ReadGraph graph) throws DatabaseException {
325 return Variables.getVariable(graph, graph.getURI(property.predicate));
329 public Resource getPredicateResource(ReadGraph graph) throws DatabaseException {
330 return property.predicate;
334 public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {
335 return property.predicate;
339 public int hashCode() {
341 final int prime = 31;
343 result = prime * result + (parent != null ? parent.hashCode() : 0);
344 result = prime * result + (node != null ? node.hashCode() : 0);
345 result = prime * result + (property.predicate != null ? property.predicate.hashCode() : 0);
346 result = prime * result + (parentResource != null ? parentResource.hashCode() : 0);
347 result = prime * result + (represents != null ? represents.hashCode() : 0);
354 public boolean equals(Object obj) {
359 if (getClass() != obj.getClass())
361 StandardGraphPropertyVariable other = (StandardGraphPropertyVariable) obj;
364 if(!node.equals(other.node)) return false;
365 } else if (other.node != null) return false;
367 if(property.predicate != null) {
368 if(!property.predicate.equals(other.property.predicate)) return false;
369 } else if (other.property.predicate != null) return false;
371 if(parentResource != null) {
372 if(!parentResource.equals(other.parentResource)) return false;
373 } else if (other.parentResource != null) return false;
376 if(!parent.equals(other.parent)) return false;
377 } else if (other.parent != null) return false;
379 if(represents != null) {
380 if(!represents.equals(other.represents)) return false;
381 } else if (other.represents != null) return false;
388 protected Variable getNameVariable(ReadGraph graph) throws DatabaseException {
389 throw new UnsupportedOperationException();
392 protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException {
393 if((property == null || property == NO_PROPERTY) && parentResource == null) return All.standardValueAccessor;
394 ValueAccessor accessor = property.valueAccessor;
395 if(accessor != null) return accessor;
397 System.err.println("No value accessor for " + getURI(graph));
398 return All.standardValueAccessor;
402 protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException {
403 if(represents == null) return All.standardPropertyDomainChildren;
404 Resource domainChildren = Layer0.getInstance(graph).domainChildren;
405 return graph.getPossibleRelatedValue2(represents, domainChildren,
406 new StandardGraphPropertyVariable(graph, this, domainChildren));
409 protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {
410 if(property == null) return All.standardPropertyDomainProperties;
411 if(property.predicate == null) return null;
412 return graph.syncRequest(new PropertyVariableMapRequest(property.predicate), TransientCacheAsyncListener.<VariableMap>instance());
415 public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
416 return property.classifications;
420 public Map<String, Variable> collectDomainProperties(ReadGraph graph, String classification, Map<String, Variable> properties) throws DatabaseException {
422 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
423 if(valueMap == null) return properties;
424 return valueMap.getVariables(graph, this, classification, properties);
428 public Collection<Variable> getProperties(ReadGraph graph, String classification) throws DatabaseException {
430 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
431 if(valueMap == null) return Collections.emptyList();
432 Map<String,Variable> propertyMap = valueMap.getVariables(graph, this, classification, null);
433 if(propertyMap == null || propertyMap.isEmpty()) return Collections.emptyList();
434 else return propertyMap.values();