+package org.simantics.db.layer0.variable;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.impl.ObjectVariantBinding;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.common.validation.L0Validations;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ValidationException;\r
+import org.simantics.db.layer0.exception.MissingVariableValueException;\r
+import org.simantics.db.layer0.exception.PendingVariableException;\r
+import org.simantics.db.layer0.exception.VariableException;\r
+import org.simantics.db.layer0.function.All;\r
+import org.simantics.db.layer0.request.PropertyInfo;\r
+import org.simantics.db.layer0.request.PropertyInfoRequest;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.Development;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class StandardGraphPropertyVariable extends AbstractPropertyVariable {\r
+\r
+ protected static final PropertyInfo NO_PROPERTY = new PropertyInfo(null, null,\r
+ false, Collections.<String> emptySet(), null, null, null, null, null, null,\r
+ Collections.<String, Pair<Resource, ChildReference>> emptyMap(),\r
+ null, false);\r
+\r
+ final public Variable parent;\r
+ final public Resource parentResource;\r
+ final public PropertyInfo property;\r
+ final public Resource represents;\r
+ \r
+ transient private int hash = 0;\r
+\r
+ public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource property) throws DatabaseException {\r
+ this(graph, parent, node, (Resource)parent.getPossibleRepresents(graph), getPropertyInfo(graph, property));\r
+ }\r
+ \r
+ public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, Resource property) throws DatabaseException {\r
+ this(graph, parent, node, parentResource, getPropertyInfo(graph, property));\r
+ }\r
+\r
+ public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, Resource property) throws DatabaseException {\r
+ this(graph, parent, null, (Resource)parent.getPossibleRepresents(graph), getPropertyInfo(graph, property));\r
+ }\r
+\r
+ public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, Resource parentResource, PropertyInfo property) throws DatabaseException {\r
+ this(parent, null, parentResource, property, getPossibleRepresents(graph, parentResource, property.predicate));\r
+ }\r
+ \r
+ public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, PropertyInfo property) throws DatabaseException {\r
+ this(parent, node, parentResource, property, getPossibleRepresents(graph, parentResource, property.predicate));\r
+ }\r
+\r
+ private static PropertyInfo getPropertyInfo(ReadGraph graph, Resource property) throws DatabaseException {\r
+ return property != null ? graph.syncRequest(new PropertyInfoRequest(property), TransientCacheAsyncListener.<PropertyInfo>instance()) : NO_PROPERTY;\r
+ }\r
+ \r
+ private static Resource getPossibleRepresents(ReadGraph graph, Resource parentResource, Resource predicate) throws DatabaseException {\r
+ if(parentResource == null || predicate == null) return null;\r
+ return graph.getPossibleObject(parentResource, predicate);\r
+ }\r
+ \r
+ public boolean isAsserted() {\r
+ return false;\r
+ }\r
+\r
+ public StandardGraphPropertyVariable(Variable parent, VariableNode node, Resource parentResource, PropertyInfo property, Resource represents) throws DatabaseException {\r
+ super(node);\r
+ assert parent != null;\r
+ this.parent = parent;\r
+ this.property = property;\r
+ this.parentResource = parentResource;\r
+ this.represents = represents;\r
+ }\r
+\r
+ @Override\r
+ public String getName(ReadGraph graph) throws DatabaseException {\r
+ if(node != null) return node.support.manager.getName(node.node);\r
+ return property.name;\r
+ }\r
+\r
+ @Override\r
+ public String getPossibleLabel(ReadGraph graph) throws DatabaseException {\r
+ return graph.getPossibleRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);\r
+ }\r
+\r
+ @Override\r
+ public String getLabel(ReadGraph graph) throws DatabaseException {\r
+ return graph.getRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);\r
+ }\r
+\r
+ @Override\r
+ public Variable getParent(ReadGraph graph) throws DatabaseException {\r
+ return parent;\r
+ }\r
+ \r
+ @Override\r
+ public PropertyInfo getPropertyInfo(ReadGraph graph) throws DatabaseException {\r
+ return property;\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <T> T getValue(ReadGraph graph) throws DatabaseException {\r
+ \r
+ if(Development.DEVELOPMENT) {\r
+ String error = L0Validations.checkValueType(graph, parentResource, property.predicate);\r
+ if(error != null) {\r
+ Logger.defaultLogError(new ValidationException(error));\r
+ //throw new ValidationException(error);\r
+ }\r
+ }\r
+ \r
+ return (T)getValueAccessor(graph).getValue(graph, this);\r
+ \r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {\r
+ // Fall back to the bindingless method, if the expected output is a Java object \r
+ if (binding instanceof ObjectVariantBinding)\r
+ return getValue(graph); \r
+\r
+ if(Development.DEVELOPMENT) {\r
+ String error = L0Validations.checkValueType(graph, parentResource, property.predicate);\r
+ if(error != null) {\r
+ Logger.defaultLogError(new ValidationException(error));\r
+ throw new ValidationException(error);\r
+ }\r
+ }\r
+ \r
+ try {\r
+ \r
+ return (T)getValueAccessor(graph).getValue(graph, this, binding);\r
+ } catch (PendingVariableException e) {\r
+ throw e;\r
+ } catch (Throwable t) {\r
+ throw new MissingVariableValueException(t);\r
+ }\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public Resource getRepresents(ReadGraph graph) throws DatabaseException {\r
+ if(represents == null)\r
+ throw new VariableException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");\r
+ return represents;\r
+// return graph.getSingleObject(parentResource, property.predicate);\r
+ }\r
+\r
+ @Override\r
+ public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {\r
+// if(parentResource == null) return null;\r
+// return graph.getPossibleObject(parentResource, property.predicate);\r
+ return represents;\r
+ }\r
+\r
+ @Override\r
+ public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {\r
+ \r
+ if(Development.DEVELOPMENT) {\r
+ String error = L0Validations.checkValueType(graph, parentResource, property.predicate);\r
+ if(error != null) {\r
+ Logger.defaultLogError(new ValidationException(error));\r
+ //throw new ValidationException(error);\r
+ }\r
+ }\r
+ \r
+ getValueAccessor(graph).setValue(graph, this, value, binding);\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void setValue(WriteGraph graph, Object value) throws DatabaseException {\r
+ \r
+ if(Development.DEVELOPMENT) {\r
+ String error = L0Validations.checkValueType(graph, parentResource, property.predicate);\r
+ if(error != null) {\r
+ Logger.defaultLogError(new ValidationException(error));\r
+ throw new ValidationException(error);\r
+ }\r
+ }\r
+ \r
+ getValueAccessor(graph).setValue(graph, this, value);\r
+ \r
+ }\r
+\r
+ @Override\r
+ public Binding getDefaultBinding(ReadGraph graph) throws DatabaseException {\r
+ return Layer0Utils.getDefaultBinding(graph, this);\r
+ }\r
+ \r
+ @Override\r
+ public Binding getPossibleDefaultBinding(ReadGraph graph) throws DatabaseException {\r
+ return Layer0Utils.getPossibleDefaultBinding(graph, this);\r
+ }\r
+\r
+ @Override\r
+ public Datatype getDatatype(ReadGraph graph) throws DatabaseException {\r
+//<<<<<<< .mine\r
+// \r
+// Binding binding = getPossibleDefaultBinding(graph);\r
+// if(binding != null) return binding.type();\r
+//\r
+// Variant vt = getVariantValue(graph);\r
+// binding = vt.getBinding();\r
+// if(binding == null) throw new DatabaseException("No binding in Variant with value " + vt.getValue());\r
+// return binding.type();\r
+//=======\r
+ Datatype type;\r
+ try { \r
+ type = getValueAccessor(graph).getDatatype(graph, this);\r
+ } catch (Throwable t) {\r
+ throw new MissingVariableValueException(t);\r
+ }\r
+ \r
+ if (type == null) {\r
+ String uri = this.getPossibleURI(graph);\r
+ if (uri != null)\r
+ throw new DatabaseException("No data type for " + uri);\r
+ else\r
+ throw new DatabaseException("No data type for " + this.getIdentifier());\r
+ }\r
+ \r
+ return type;\r
+//>>>>>>> .r30794\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public Datatype getPossibleDatatype(ReadGraph graph) throws DatabaseException {\r
+\r
+ try {\r
+ return getDatatype(graph);\r
+ } catch (DatabaseException e) {\r
+ return null;\r
+ }\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public String getUnit(ReadGraph graph) throws DatabaseException {\r
+ try {\r
+ return Layer0Utils.getUnit(graph, this);\r
+ } catch (DatabaseException e) {\r
+ return null;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Resource getPropertyResource(ReadGraph graph) {\r
+ return property.predicate;\r
+ }\r
+ \r
+ @Override\r
+ public Resource getContainerResource(ReadGraph graph) throws DatabaseException {\r
+ return parentResource;\r
+ }\r
+\r
+ @Override\r
+ public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {\r
+\r
+ Map<String, Variable> result = new HashMap<String, Variable>();\r
+ VariableMap map = getPossibleChildVariableMap(graph);\r
+ if(map != null) map.getVariables(graph, this, result);\r
+ return result.values();\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {\r
+\r
+ VariableMap map = getPossibleChildVariableMap(graph);\r
+ if(map == null) return null;\r
+ try {\r
+ return map.getVariable(graph, this, name);\r
+ } catch (DatabaseException e) {\r
+ return null;\r
+ }\r
+ \r
+ }\r
+ \r
+ \r
+ @Override\r
+ protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {\r
+\r
+ VariableMap valueMap = getPossiblePropertyVariableMap(graph);\r
+ if(valueMap == null) return null;\r
+ try {\r
+ return valueMap.getVariable(graph, this, name);\r
+ } catch (DatabaseException e) {\r
+ return null;\r
+ } catch (Throwable t) {\r
+ System.err.println("err: " + getURI(graph) + " # " + name);\r
+ return null;\r
+ }\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {\r
+\r
+ VariableMap valueMap = getPossiblePropertyVariableMap(graph);\r
+ if(valueMap == null) return properties;\r
+ return valueMap.getVariables(graph, this, properties);\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public Variable getPredicate(ReadGraph graph) throws DatabaseException {\r
+ return Variables.getVariable(graph, graph.getURI(property.predicate));\r
+ }\r
+ \r
+ @Override\r
+ public Resource getPredicateResource(ReadGraph graph) throws DatabaseException {\r
+ return property.predicate;\r
+ }\r
+ \r
+ @Override\r
+ public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {\r
+ return property.predicate;\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ if(hash == 0) {\r
+ final int prime = 31;\r
+ int result = 1;\r
+ result = prime * result + (parent != null ? parent.hashCode() : 0);\r
+ result = prime * result + (node != null ? node.hashCode() : 0);\r
+ result = prime * result + (property.predicate != null ? property.predicate.hashCode() : 0);\r
+ result = prime * result + (parentResource != null ? parentResource.hashCode() : 0);\r
+ result = prime * result + (represents != null ? represents.hashCode() : 0);\r
+ hash =result;\r
+ }\r
+ return hash;\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (this == obj)\r
+ return true;\r
+ if (obj == null)\r
+ return false;\r
+ if (getClass() != obj.getClass())\r
+ return false;\r
+ StandardGraphPropertyVariable other = (StandardGraphPropertyVariable) obj;\r
+ \r
+ if(node != null) {\r
+ if(!node.equals(other.node)) return false;\r
+ } else if (other.node != null) return false;\r
+\r
+ if(property.predicate != null) {\r
+ if(!property.predicate.equals(other.property.predicate)) return false;\r
+ } else if (other.property.predicate != null) return false;\r
+ \r
+ if(parentResource != null) {\r
+ if(!parentResource.equals(other.parentResource)) return false;\r
+ } else if (other.parentResource != null) return false;\r
+\r
+ if(parent != null) {\r
+ if(!parent.equals(other.parent)) return false;\r
+ } else if (other.parent != null) return false;\r
+\r
+ if(represents != null) {\r
+ if(!represents.equals(other.represents)) return false;\r
+ } else if (other.represents != null) return false;\r
+\r
+ return true;\r
+ \r
+ }\r
+\r
+ @Override\r
+ protected Variable getNameVariable(ReadGraph graph) throws DatabaseException {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+ \r
+ protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException {\r
+ if((property == null || property == NO_PROPERTY) && parentResource == null) return All.standardValueAccessor;\r
+ ValueAccessor accessor = property.valueAccessor;\r
+ if(accessor != null) return accessor;\r
+ else {\r
+ System.err.println("No value accessor for " + getURI(graph));\r
+ return All.standardValueAccessor;\r
+ }\r
+ }\r
+\r
+ protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException {\r
+ if(represents == null) return All.standardPropertyDomainChildren;\r
+ return graph.getPossibleRelatedValue2(represents, Layer0.getInstance(graph).domainChildren, this);\r
+ }\r
+\r
+ protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {\r
+ if(property == null) return All.standardPropertyDomainProperties;\r
+ if(property.predicate == null) return null;\r
+ return graph.syncRequest(new PropertyVariableMapRequest(property.predicate), TransientCacheAsyncListener.<VariableMap>instance());\r
+ }\r
+ \r
+ public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {\r
+ return property.classifications;\r
+ }\r
+ \r
+ @Override\r
+ public Map<String, Variable> collectDomainProperties(ReadGraph graph, String classification, Map<String, Variable> properties) throws DatabaseException {\r
+ \r
+ VariableMap valueMap = getPossiblePropertyVariableMap(graph);\r
+ if(valueMap == null) return properties;\r
+ return valueMap.getVariables(graph, this, classification, properties);\r
+ \r
+ }\r
+ \r
+ public Collection<Variable> getProperties(ReadGraph graph, String classification) throws DatabaseException {\r
+ \r
+ VariableMap valueMap = getPossiblePropertyVariableMap(graph);\r
+ if(valueMap == null) return Collections.emptyList();\r
+ Map<String,Variable> propertyMap = valueMap.getVariables(graph, this, classification, null);\r
+ if(propertyMap == null || propertyMap.isEmpty()) return Collections.emptyList();\r
+ else return propertyMap.values();\r
+ \r
+ }\r
+ \r
+}\r