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.scl.SCLDatabaseException;
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;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
36 private static final Logger LOGGER = LoggerFactory.getLogger(StandardGraphPropertyVariable.class);
38 protected static final PropertyInfo NO_PROPERTY = new PropertyInfo(null, null,
39 false, Collections.<String> emptySet(), null, null, null, null, null, null,
40 Collections.<String, Pair<Resource, ChildReference>> emptyMap(),
43 final public Variable parent;
44 final public Resource parentResource;
45 final public PropertyInfo property;
46 final public Resource represents;
48 transient private int hash = 0;
50 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource property) throws DatabaseException {
51 this(graph, parent, node, (Resource)parent.getPossibleRepresents(graph), getPropertyInfo(graph, property));
54 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, Resource property) throws DatabaseException {
55 this(graph, parent, node, parentResource, getPropertyInfo(graph, property));
58 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, Resource property) throws DatabaseException {
59 this(graph, parent, null, (Resource)parent.getPossibleRepresents(graph), getPropertyInfo(graph, property));
62 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, Resource parentResource, PropertyInfo property) throws DatabaseException {
63 this(parent, null, parentResource, property, getPossibleRepresents(graph, parentResource, property.predicate));
66 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, PropertyInfo property) throws DatabaseException {
67 this(parent, node, parentResource, property, getPossibleRepresents(graph, parentResource, property.predicate));
70 private static PropertyInfo getPropertyInfo(ReadGraph graph, Resource property) throws DatabaseException {
71 return property != null ? graph.syncRequest(new PropertyInfoRequest(property), TransientCacheAsyncListener.<PropertyInfo>instance()) : NO_PROPERTY;
74 private static Resource getPossibleRepresents(ReadGraph graph, Resource parentResource, Resource predicate) throws DatabaseException {
75 if(parentResource == null || predicate == null) return null;
76 return graph.getPossibleObject(parentResource, predicate);
79 public boolean isAsserted() {
83 public StandardGraphPropertyVariable(Variable parent, VariableNode node, Resource parentResource, PropertyInfo property, Resource represents) throws DatabaseException {
85 assert parent != null;
87 this.property = property;
88 this.parentResource = parentResource;
89 this.represents = represents;
93 public String getName(ReadGraph graph) throws DatabaseException {
94 if(node != null) return node.support.manager.getName(node.node);
99 public String getPossibleLabel(ReadGraph graph) throws DatabaseException {
100 return graph.getPossibleRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
104 public String getLabel(ReadGraph graph) throws DatabaseException {
105 return graph.getRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
109 public Variable getParent(ReadGraph graph) throws DatabaseException {
114 public PropertyInfo getPropertyInfo(ReadGraph graph) throws DatabaseException {
118 @SuppressWarnings("unchecked")
120 public <T> T getValue(ReadGraph graph) throws DatabaseException {
122 if(Development.DEVELOPMENT) {
123 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
126 //throw new ValidationException(error);
130 return (T)getValueAccessor(graph).getValue(graph, this);
134 @SuppressWarnings("unchecked")
136 public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
137 // Fall back to the bindingless method, if the expected output is a Java object
138 if (binding instanceof ObjectVariantBinding)
139 return getValue(graph);
141 if(Development.DEVELOPMENT) {
142 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
145 throw new ValidationException(error);
151 return (T) getValueAccessor(graph).getValue(graph, this, binding);
152 } catch (SCLDatabaseException e) { // these can be thrown when compiling e.g. derived properties
154 } catch (MissingVariableValueException | PendingVariableException e) {
156 } catch (Throwable t) {
157 throw new MissingVariableValueException(t);
163 public Resource getRepresents(ReadGraph graph) throws DatabaseException {
164 if(represents == null)
165 throw new VariableException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");
167 // return graph.getSingleObject(parentResource, property.predicate);
171 public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
172 // if(parentResource == null) return null;
173 // return graph.getPossibleObject(parentResource, property.predicate);
178 public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {
180 if(Development.DEVELOPMENT) {
181 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
184 //throw new ValidationException(error);
188 getValueAccessor(graph).setValue(graph, this, value, binding);
193 public void setValue(WriteGraph graph, Object value) throws DatabaseException {
195 if(Development.DEVELOPMENT) {
196 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
199 throw new ValidationException(error);
203 getValueAccessor(graph).setValue(graph, this, value);
208 public Binding getDefaultBinding(ReadGraph graph) throws DatabaseException {
209 return Layer0Utils.getDefaultBinding(graph, this);
213 public Binding getPossibleDefaultBinding(ReadGraph graph) throws DatabaseException {
214 return Layer0Utils.getPossibleDefaultBinding(graph, this);
218 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
221 // Binding binding = getPossibleDefaultBinding(graph);
222 // if(binding != null) return binding.type();
224 // Variant vt = getVariantValue(graph);
225 // binding = vt.getBinding();
226 // if(binding == null) throw new DatabaseException("No binding in Variant with value " + vt.getValue());
227 // return binding.type();
231 type = getValueAccessor(graph).getDatatype(graph, this);
232 } catch (Throwable t) {
233 throw new MissingVariableValueException(t);
237 String uri = this.getPossibleURI(graph);
239 throw new DatabaseException("No data type for " + uri);
241 throw new DatabaseException("No data type for " + this.getIdentifier());
250 public Datatype getPossibleDatatype(ReadGraph graph) throws DatabaseException {
253 return getDatatype(graph);
254 } catch (DatabaseException e) {
261 public String getUnit(ReadGraph graph) throws DatabaseException {
263 return Layer0Utils.getUnit(graph, this);
264 } catch (DatabaseException e) {
270 public Resource getPropertyResource(ReadGraph graph) {
271 return property.predicate;
275 public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
276 return parentResource;
280 public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
282 Map<String, Variable> result = new HashMap<String, Variable>();
283 VariableMap map = getPossibleChildVariableMap(graph);
284 if(map != null) map.getVariables(graph, this, result);
285 return result.values();
290 public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
292 VariableMap map = getPossibleChildVariableMap(graph);
293 if(map == null) return null;
295 return map.getVariable(graph, this, name);
296 } catch (DatabaseException e) {
304 protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
306 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
307 if(valueMap == null) return null;
309 return valueMap.getVariable(graph, this, name);
310 } catch (DatabaseException e) {
312 } catch (Exception t) {
313 LOGGER.error("getPossibleDomainProperty is implemented incorrectly, but returns null on Exception for backward compatibility. URI="+getURI(graph)+", name="+name+".", t);
320 public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {
322 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
323 if(valueMap == null) return properties;
324 return valueMap.getVariables(graph, this, properties);
329 public Variable getPredicate(ReadGraph graph) throws DatabaseException {
330 return Variables.getVariable(graph, graph.getURI(property.predicate));
334 public Resource getPredicateResource(ReadGraph graph) throws DatabaseException {
335 return property.predicate;
339 public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {
340 return property.predicate;
344 public int hashCode() {
346 final int prime = 31;
348 result = prime * result + (parent != null ? parent.hashCode() : 0);
349 result = prime * result + (node != null ? node.hashCode() : 0);
350 result = prime * result + (property.predicate != null ? property.predicate.hashCode() : 0);
351 result = prime * result + (parentResource != null ? parentResource.hashCode() : 0);
352 result = prime * result + (represents != null ? represents.hashCode() : 0);
359 public boolean equals(Object obj) {
364 if (getClass() != obj.getClass())
366 StandardGraphPropertyVariable other = (StandardGraphPropertyVariable) obj;
369 if(!node.equals(other.node)) return false;
370 } else if (other.node != null) return false;
372 if(property.predicate != null) {
373 if(!property.predicate.equals(other.property.predicate)) return false;
374 } else if (other.property.predicate != null) return false;
376 if(parentResource != null) {
377 if(!parentResource.equals(other.parentResource)) return false;
378 } else if (other.parentResource != null) return false;
381 if(!parent.equals(other.parent)) return false;
382 } else if (other.parent != null) return false;
384 if(represents != null) {
385 if(!represents.equals(other.represents)) return false;
386 } else if (other.represents != null) return false;
393 protected Variable getNameVariable(ReadGraph graph) throws DatabaseException {
394 throw new UnsupportedOperationException();
397 protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException {
398 if((property == null || property == NO_PROPERTY) && parentResource == null) return All.standardValueAccessor;
399 ValueAccessor accessor = property.valueAccessor;
400 if(accessor != null) return accessor;
402 System.err.println("No value accessor for " + getURI(graph));
403 return All.standardValueAccessor;
407 protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException {
408 if(represents == null) return All.standardPropertyDomainChildren;
409 Resource domainChildren = Layer0.getInstance(graph).domainChildren;
410 return graph.getPossibleRelatedValue2(represents, domainChildren,
411 new StandardGraphPropertyVariable(graph, this, domainChildren));
414 protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {
415 if(property == null) return All.standardPropertyDomainProperties;
416 if(property.predicate == null) return null;
417 return graph.syncRequest(new PropertyVariableMapRequest(property.predicate), TransientCacheAsyncListener.<VariableMap>instance());
420 public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
421 return property.classifications;
425 public Map<String, Variable> collectDomainProperties(ReadGraph graph, String classification, Map<String, Variable> properties) throws DatabaseException {
427 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
428 if(valueMap == null) return properties;
429 return valueMap.getVariables(graph, this, classification, properties);
433 public Collection<Variable> getProperties(ReadGraph graph, String classification) throws DatabaseException {
435 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
436 if(valueMap == null) return Collections.emptyList();
437 Map<String,Variable> propertyMap = valueMap.getVariables(graph, this, classification, null);
438 if(propertyMap == null || propertyMap.isEmpty()) return Collections.emptyList();
439 else return propertyMap.values();