1 package org.simantics.db.layer0.variable;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Collection;
\r
5 import java.util.HashMap;
\r
6 import java.util.HashSet;
\r
7 import java.util.Map;
\r
8 import java.util.Set;
\r
10 import org.simantics.databoard.Bindings;
\r
11 import org.simantics.databoard.binding.Binding;
\r
12 import org.simantics.databoard.binding.impl.ObjectArrayBinding;
\r
13 import org.simantics.databoard.type.Datatype;
\r
14 import org.simantics.db.ReadGraph;
\r
15 import org.simantics.db.Resource;
\r
16 import org.simantics.db.WriteGraph;
\r
17 import org.simantics.db.exception.DatabaseException;
\r
18 import org.simantics.db.layer0.function.All;
\r
19 import org.simantics.db.layer0.util.Layer0Utils;
\r
20 import org.simantics.layer0.Layer0;
\r
22 public class StandardAssertedGraphPropertyVariable extends StandardGraphPropertyVariable {
\r
24 final public Resource object;
\r
26 transient private int hash = 0;
\r
28 public StandardAssertedGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, Resource property, Resource object) throws DatabaseException {
\r
29 super(graph, parent, node, parentResource, property);
\r
30 assert parent != null;
\r
31 assert property != null;
\r
32 this.object = object;
\r
36 public boolean isAsserted() {
\r
41 public void validate(ReadGraph graph) throws DatabaseException {
\r
45 public String getName(ReadGraph graph) throws DatabaseException {
\r
46 return graph.getRelatedValue(property.predicate, graph.getService(Layer0.class).HasName, Bindings.STRING);
\r
50 public String getLabel(ReadGraph graph) throws DatabaseException {
\r
51 return graph.getRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent);
\r
55 // public Object getSerialized(ReadGraph graph) throws DatabaseException {
\r
60 public Variable getParent(ReadGraph graph) throws DatabaseException {
\r
64 @SuppressWarnings("unchecked")
\r
66 public <T> T getValue(ReadGraph graph) throws DatabaseException {
\r
70 return (T)getValueAccessor(graph).getValue(graph, this);
\r
73 @SuppressWarnings("unchecked")
\r
75 public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
\r
76 if (binding instanceof ObjectArrayBinding)
\r
77 return getValue(graph);
\r
81 return (T)getValueAccessor(graph).getValue(graph, this, binding);
\r
86 public Resource getRepresents(ReadGraph graph) throws DatabaseException {
\r
95 public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
\r
104 public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {
\r
106 assertAlive(graph);
\r
108 getValueAccessor(graph).setValue(graph, this, value, binding);
\r
113 public void setValue(WriteGraph graph, Object value) throws DatabaseException {
\r
115 assertAlive(graph);
\r
117 getValueAccessor(graph).setValue(graph, this, value);
\r
122 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
\r
124 return Layer0Utils.getDatatype(graph, this);
\r
125 } catch (DatabaseException e) {
\r
131 public String getUnit(ReadGraph graph) throws DatabaseException {
\r
133 return Layer0Utils.getUnit(graph, this);
\r
134 } catch (DatabaseException e) {
\r
140 public Resource getPropertyResource(ReadGraph graph) {
\r
141 return property.predicate;
\r
145 public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
\r
146 return parent.getRepresents(graph);
\r
150 public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
\r
152 Map<String, Variable> result = new HashMap<String, Variable>();
\r
153 VariableMap map = getPossibleChildVariableMap(graph);
\r
154 if(map != null) map.getVariables(graph, this, result);
\r
155 return result.values();
\r
160 public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
\r
162 VariableMap map = getPossibleChildVariableMap(graph);
\r
163 if(map == null) return null;
\r
165 return map.getVariable(graph, this, name);
\r
166 } catch (DatabaseException e) {
\r
174 protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
\r
176 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
\r
177 if(valueMap == null) return null;
\r
179 return valueMap.getVariable(graph, this, name);
\r
180 } catch (DatabaseException e) {
\r
189 public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {
\r
191 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
\r
192 if(valueMap == null) return properties;
\r
193 return valueMap.getVariables(graph, this, properties);
\r
198 public Variable getPredicate(ReadGraph graph) throws DatabaseException {
\r
199 return Variables.getVariable(graph, graph.getURI(property.predicate));
\r
204 public int hashCode() {
\r
206 final int prime = 31;
\r
208 result = prime * result + parent.hashCode();
\r
209 result = prime * result + property.hashCode();
\r
210 result = prime * result + object.hashCode();
\r
217 public boolean equals(Object obj) {
\r
222 if (getClass() != obj.getClass())
\r
224 StandardAssertedGraphPropertyVariable other = (StandardAssertedGraphPropertyVariable) obj;
\r
225 if (!property.equals(other.property))
\r
227 if (!object.equals(other.object))
\r
229 if (!parent.equals(other.parent))
\r
231 return parent.equals(other.parent);
\r
235 protected Variable getNameVariable(ReadGraph graph) throws DatabaseException {
\r
236 Layer0 L0 = Layer0.getInstance(graph);
\r
237 return new StandardGraphPropertyVariable(graph, this, L0.HasName);
\r
240 private void assertAlive(ReadGraph graph) throws DatabaseException {
\r
243 protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException {
\r
244 // First from literal
\r
245 if(parentResource == null) return All.standardValueAccessor;
\r
246 ValueAccessor accessor = property.valueAccessor;
\r
247 if(accessor != null) return accessor;
\r
249 System.err.println("No value accessor for " + getURI(graph));
\r
250 return All.standardValueAccessor;
\r
254 protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException {
\r
255 Resource value = getRepresents(graph);
\r
256 if(value == null) return null;
\r
257 return graph.getPossibleRelatedValue2(value, Layer0.getInstance(graph).domainChildren, this);
\r
260 protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {
\r
261 Resource value = getRepresents(graph);
\r
262 if(value == null) return null;
\r
263 return graph.getPossibleRelatedValue2(value, Layer0.getInstance(graph).domainProperties, this);
\r
266 public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
\r
267 ArrayList<String> value = getPropertyValue(graph, "classifications");
\r
268 return new HashSet<String>(value);
\r