package org.simantics.db.layer0.variable; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.simantics.databoard.Bindings; import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.impl.ObjectArrayBinding; import org.simantics.databoard.type.Datatype; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.function.All; import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.layer0.Layer0; public class StandardAssertedGraphPropertyVariable extends StandardGraphPropertyVariable { final public Resource object; transient private int hash = 0; public StandardAssertedGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, Resource property, Resource object) throws DatabaseException { super(graph, parent, node, parentResource, property); assert parent != null; assert property != null; this.object = object; } @Override public boolean isAsserted() { return true; } @Override public void validate(ReadGraph graph) throws DatabaseException { } @Override public String getName(ReadGraph graph) throws DatabaseException { return graph.getRelatedValue(property.predicate, graph.getService(Layer0.class).HasName, Bindings.STRING); } @Override public String getLabel(ReadGraph graph) throws DatabaseException { return graph.getRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent); } // @Override // public Object getSerialized(ReadGraph graph) throws DatabaseException { // return property; // } @Override public Variable getParent(ReadGraph graph) throws DatabaseException { return parent; } @SuppressWarnings("unchecked") @Override public T getValue(ReadGraph graph) throws DatabaseException { assertAlive(graph); return (T)getValueAccessor(graph).getValue(graph, this); } @SuppressWarnings("unchecked") @Override public T getValue(ReadGraph graph, Binding binding) throws DatabaseException { if (binding instanceof ObjectArrayBinding) return getValue(graph); assertAlive(graph); return (T)getValueAccessor(graph).getValue(graph, this, binding); } @Override public Resource getRepresents(ReadGraph graph) throws DatabaseException { assertAlive(graph); return object; } @Override public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException { assertAlive(graph); return object; } @Override public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException { assertAlive(graph); getValueAccessor(graph).setValue(graph, this, value, binding); } @Override public void setValue(WriteGraph graph, Object value) throws DatabaseException { assertAlive(graph); getValueAccessor(graph).setValue(graph, this, value); } @Override public Datatype getDatatype(ReadGraph graph) throws DatabaseException { try { return Layer0Utils.getDatatype(graph, this); } catch (DatabaseException e) { return null; } } @Override public String getUnit(ReadGraph graph) throws DatabaseException { try { return Layer0Utils.getUnit(graph, this); } catch (DatabaseException e) { return null; } } @Override public Resource getPropertyResource(ReadGraph graph) { return property.predicate; } @Override public Resource getContainerResource(ReadGraph graph) throws DatabaseException { return parent.getRepresents(graph); } @Override public Collection getChildren(ReadGraph graph) throws DatabaseException { Map result = new HashMap(); VariableMap map = getPossibleChildVariableMap(graph); if(map != null) map.getVariables(graph, this, result); return result.values(); } @Override public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException { VariableMap map = getPossibleChildVariableMap(graph); if(map == null) return null; try { return map.getVariable(graph, this, name); } catch (DatabaseException e) { return null; } } @Override protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException { VariableMap valueMap = getPossiblePropertyVariableMap(graph); if(valueMap == null) return null; try { return valueMap.getVariable(graph, this, name); } catch (DatabaseException e) { return null; } } @Override public Map collectDomainProperties(ReadGraph graph, Map properties) throws DatabaseException { VariableMap valueMap = getPossiblePropertyVariableMap(graph); if(valueMap == null) return properties; return valueMap.getVariables(graph, this, properties); } @Override public Variable getPredicate(ReadGraph graph) throws DatabaseException { return Variables.getVariable(graph, graph.getURI(property.predicate)); } @Override public int hashCode() { if(hash == 0) { final int prime = 31; int result = 1; result = prime * result + parent.hashCode(); result = prime * result + property.hashCode(); result = prime * result + object.hashCode(); hash =result; } return hash; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; StandardAssertedGraphPropertyVariable other = (StandardAssertedGraphPropertyVariable) obj; if (!property.equals(other.property)) return false; if (!object.equals(other.object)) return false; if (!parent.equals(other.parent)) return false; return parent.equals(other.parent); } @Override protected Variable getNameVariable(ReadGraph graph) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); return new StandardGraphPropertyVariable(graph, this, L0.HasName); } private void assertAlive(ReadGraph graph) throws DatabaseException { } protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException { // First from literal if(parentResource == null) return All.standardValueAccessor; ValueAccessor accessor = property.valueAccessor; if(accessor != null) return accessor; else { System.err.println("No value accessor for " + getURI(graph)); return All.standardValueAccessor; } } protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException { Resource value = getRepresents(graph); if(value == null) return null; return graph.getPossibleRelatedValue2(value, Layer0.getInstance(graph).domainChildren, this); } protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException { Resource value = getRepresents(graph); if(value == null) return null; return graph.getPossibleRelatedValue2(value, Layer0.getInstance(graph).domainProperties, this); } public Set getClassifications(ReadGraph graph) throws DatabaseException { ArrayList value = getPropertyValue(graph, "classifications"); return new HashSet(value); } }