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.DevelopmentKeys;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
19 import org.simantics.db.common.validation.L0Validations;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.exception.DatatypeNotFoundException;
22 import org.simantics.db.exception.ValidationException;
23 import org.simantics.db.layer0.exception.InvalidVariableException;
24 import org.simantics.db.layer0.exception.MissingVariableValueException;
25 import org.simantics.db.layer0.exception.PendingVariableException;
26 import org.simantics.db.layer0.function.All;
27 import org.simantics.db.layer0.request.PropertyInfo;
28 import org.simantics.db.layer0.request.PropertyInfoRequest;
29 import org.simantics.db.layer0.scl.SCLDatabaseException;
30 import org.simantics.db.layer0.util.Layer0Utils;
31 import org.simantics.layer0.Layer0;
32 import org.simantics.utils.Development;
33 import org.simantics.utils.datastructures.Pair;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
39 private static final Logger LOGGER = LoggerFactory.getLogger(StandardGraphPropertyVariable.class);
41 protected static final PropertyInfo NO_PROPERTY = new PropertyInfo(null, null, true,
42 false, false, Collections.<String> emptySet(), null, null, null, null, null, null,
43 Collections.<String, Pair<Resource, ChildReference>> emptyMap(),
46 final public Variable parent;
47 final public Resource parentResource;
48 final public PropertyInfo property;
49 final public Resource represents;
51 transient private int hash = 0;
53 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource property) throws DatabaseException {
54 this(graph, parent, node, (Resource)parent.getPossibleRepresents(graph), getPropertyInfo(graph, property));
57 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, Resource property) throws DatabaseException {
58 this(graph, parent, node, parentResource, getPropertyInfo(graph, property));
61 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, Resource property) throws DatabaseException {
62 this(graph, parent, null, (Resource)parent.getPossibleRepresents(graph), getPropertyInfo(graph, property));
65 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, Resource parentResource, PropertyInfo property) throws DatabaseException {
66 this(parent, null, parentResource, property, getPossibleRepresents(graph, parentResource, property.predicate));
69 public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, PropertyInfo property) throws DatabaseException {
70 this(parent, node, parentResource, property, getPossibleRepresents(graph, parentResource, property.predicate));
73 private static PropertyInfo getPropertyInfo(ReadGraph graph, Resource property) throws DatabaseException {
74 return property != null ? graph.syncRequest(new PropertyInfoRequest(property), TransientCacheAsyncListener.<PropertyInfo>instance()) : NO_PROPERTY;
77 private static Resource getPossibleRepresents(ReadGraph graph, Resource parentResource, Resource predicate) throws DatabaseException {
78 if(parentResource == null || predicate == null) return null;
79 return graph.getPossibleObject(parentResource, predicate);
82 public boolean isAsserted() {
86 public StandardGraphPropertyVariable(Variable parent, VariableNode node, Resource parentResource, PropertyInfo property, Resource represents) throws DatabaseException {
88 assert parent != null;
90 this.property = property;
91 this.parentResource = parentResource;
92 this.represents = represents;
96 public String getName(ReadGraph graph) throws DatabaseException {
97 if(node != null) return node.support.manager.getName(node.node);
102 public String getPossibleLabel(ReadGraph graph) throws DatabaseException {
103 return graph.getPossibleRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
107 public String getLabel(ReadGraph graph) throws DatabaseException {
108 return graph.getRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
112 public Variable getParent(ReadGraph graph) throws DatabaseException {
117 public PropertyInfo getPropertyInfo(ReadGraph graph) throws DatabaseException {
121 @SuppressWarnings("unchecked")
123 public <T> T getValue(ReadGraph graph) throws DatabaseException {
125 if(Development.DEVELOPMENT) {
126 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
127 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
130 throw new ValidationException(error);
135 return (T)getValueAccessor(graph).getValue(graph, this);
139 @SuppressWarnings("unchecked")
141 public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
142 // Fall back to the bindingless method, if the expected output is a Java object
143 if (binding instanceof ObjectVariantBinding)
144 return getValue(graph);
146 if(Development.DEVELOPMENT) {
147 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
148 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
151 throw new ValidationException(error);
158 return (T) getValueAccessor(graph).getValue(graph, this, binding);
159 } catch (SCLDatabaseException e) { // these can be thrown when compiling e.g. derived properties
161 } catch (MissingVariableValueException | PendingVariableException e) {
163 } catch (Throwable t) {
164 throw new MissingVariableValueException(t);
170 public Resource getRepresents(ReadGraph graph) throws DatabaseException {
171 if(represents == null)
172 throw new InvalidVariableException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");
177 public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
182 public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {
184 if(Development.DEVELOPMENT) {
185 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
186 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
189 throw new ValidationException(error);
194 getValueAccessor(graph).setValue(graph, this, value, binding);
199 public void setValue(WriteGraph graph, Object value) throws DatabaseException {
201 if(Development.DEVELOPMENT) {
202 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
203 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
206 throw new ValidationException(error);
211 getValueAccessor(graph).setValue(graph, this, value);
216 public Binding getDefaultBinding(ReadGraph graph) throws DatabaseException {
217 return Layer0Utils.getDefaultBinding(graph, this);
221 public Binding getPossibleDefaultBinding(ReadGraph graph) throws DatabaseException {
222 return Layer0Utils.getPossibleDefaultBinding(graph, this);
226 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
229 type = getValueAccessor(graph).getDatatype(graph, this);
230 } catch (Throwable t) {
231 throw new MissingVariableValueException(t);
235 String uri = this.getPossibleURI(graph);
237 throw new DatatypeNotFoundException("No data type for " + uri);
239 throw new DatatypeNotFoundException("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();