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