--- /dev/null
+package org.simantics.db.layer0.variable;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.impl.ObjectArrayBinding;\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.exception.DatabaseException;\r
+import org.simantics.db.layer0.function.All;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class StandardAssertedGraphPropertyVariable extends StandardGraphPropertyVariable {\r
+ \r
+ final public Resource object;\r
+ \r
+ transient private int hash = 0;\r
+\r
+ public StandardAssertedGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, Resource property, Resource object) throws DatabaseException {\r
+ super(graph, parent, node, parentResource, property);\r
+ assert parent != null;\r
+ assert property != null;\r
+ this.object = object;\r
+ }\r
+ \r
+ @Override\r
+ public boolean isAsserted() {\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public void validate(ReadGraph graph) throws DatabaseException {\r
+ }\r
+\r
+ @Override\r
+ public String getName(ReadGraph graph) throws DatabaseException {\r
+ return graph.getRelatedValue(property.predicate, graph.getService(Layer0.class).HasName, 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);\r
+ }\r
+ \r
+// @Override\r
+// public Object getSerialized(ReadGraph graph) throws DatabaseException {\r
+// return property;\r
+// }\r
+\r
+ @Override\r
+ public Variable getParent(ReadGraph graph) throws DatabaseException {\r
+ return parent;\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <T> T getValue(ReadGraph graph) throws DatabaseException {\r
+ \r
+ assertAlive(graph);\r
+ \r
+ return (T)getValueAccessor(graph).getValue(graph, this);\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {\r
+ if (binding instanceof ObjectArrayBinding)\r
+ return getValue(graph);\r
+ \r
+ assertAlive(graph);\r
+\r
+ return (T)getValueAccessor(graph).getValue(graph, this, binding);\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public Resource getRepresents(ReadGraph graph) throws DatabaseException {\r
+ \r
+ assertAlive(graph);\r
+ \r
+ return object;\r
+ \r
+ }\r
+\r
+ @Override\r
+ public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {\r
+ \r
+ assertAlive(graph);\r
+ \r
+ return object;\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {\r
+ \r
+ assertAlive(graph);\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
+ assertAlive(graph);\r
+ \r
+ getValueAccessor(graph).setValue(graph, this, value);\r
+ \r
+ }\r
+\r
+ @Override\r
+ public Datatype getDatatype(ReadGraph graph) throws DatabaseException {\r
+ try {\r
+ return Layer0Utils.getDatatype(graph, this);\r
+ } catch (DatabaseException e) {\r
+ return null;\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 parent.getRepresents(graph);\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
+ }\r
+ \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
+\r
+ @Override\r
+ public int hashCode() {\r
+ if(hash == 0) {\r
+ final int prime = 31;\r
+ int result = 1;\r
+ result = prime * result + parent.hashCode();\r
+ result = prime * result + property.hashCode();\r
+ result = prime * result + object.hashCode();\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
+ StandardAssertedGraphPropertyVariable other = (StandardAssertedGraphPropertyVariable) obj;\r
+ if (!property.equals(other.property))\r
+ return false;\r
+ if (!object.equals(other.object))\r
+ return false;\r
+ if (!parent.equals(other.parent))\r
+ return false;\r
+ return parent.equals(other.parent);\r
+ }\r
+\r
+ @Override\r
+ protected Variable getNameVariable(ReadGraph graph) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ return new StandardGraphPropertyVariable(graph, this, L0.HasName);\r
+ }\r
+ \r
+ private void assertAlive(ReadGraph graph) throws DatabaseException {\r
+ }\r
+ \r
+ protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException {\r
+ // First from literal\r
+ if(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
+ Resource value = getRepresents(graph);\r
+ if(value == null) return null;\r
+ return graph.getPossibleRelatedValue2(value, Layer0.getInstance(graph).domainChildren, this);\r
+ }\r
+\r
+ protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {\r
+ Resource value = getRepresents(graph);\r
+ if(value == null) return null;\r
+ return graph.getPossibleRelatedValue2(value, Layer0.getInstance(graph).domainProperties, this);\r
+ }\r
+ \r
+ public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {\r
+ ArrayList<String> value = getPropertyValue(graph, "classifications"); \r
+ return new HashSet<String>(value);\r
+ }\r
+ \r
+}\r