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.Datatypes;
\r
12 import org.simantics.databoard.binding.Binding;
\r
13 import org.simantics.databoard.binding.error.DatatypeConstructionException;
\r
14 import org.simantics.databoard.type.Datatype;
\r
15 import org.simantics.db.ReadGraph;
\r
16 import org.simantics.db.Resource;
\r
17 import org.simantics.db.WriteGraph;
\r
18 import org.simantics.db.exception.DatabaseException;
\r
19 import org.simantics.db.layer0.util.Layer0Utils;
\r
20 import org.simantics.layer0.Layer0;
\r
22 public class StandardConstantGraphPropertyVariable extends StandardGraphPropertyVariable {
\r
24 final public Object value;
\r
26 transient private int hash = 0;
\r
28 public StandardConstantGraphPropertyVariable(ReadGraph graph, Variable parent, Resource property, Object value) throws DatabaseException {
\r
29 super(graph, parent, null, property);
\r
30 assert parent != null;
\r
31 assert property != null;
\r
32 assert value != null;
\r
37 public void validate(ReadGraph graph) throws DatabaseException {
\r
41 public String getName(ReadGraph graph) throws DatabaseException {
\r
42 return graph.getRelatedValue(property.predicate, graph.getService(Layer0.class).HasName, Bindings.STRING);
\r
46 public String getLabel(ReadGraph graph) throws DatabaseException {
\r
47 return graph.getRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent);
\r
51 // public Object getSerialized(ReadGraph graph) throws DatabaseException {
\r
56 public Variable getParent(ReadGraph graph) throws DatabaseException {
\r
61 public <T> T getValue(ReadGraph graph) throws DatabaseException {
\r
65 return (T)getValueAccessor(graph).getValue(graph, this);
\r
69 public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
\r
73 return (T)getValueAccessor(graph).getValue(graph, this, binding);
\r
77 public Resource getRepresents(ReadGraph graph) throws DatabaseException {
\r
86 public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
\r
95 public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {
\r
99 getValueAccessor(graph).setValue(graph, this, value, binding);
\r
104 public void setValue(WriteGraph graph, Object value) throws DatabaseException {
\r
106 assertAlive(graph);
\r
108 getValueAccessor(graph).setValue(graph, this, value);
\r
113 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
\r
115 return Layer0Utils.getDatatype(graph, this);
\r
116 } catch (DatabaseException e) {
\r
122 public String getUnit(ReadGraph graph) throws DatabaseException {
\r
124 return Layer0Utils.getUnit(graph, this);
\r
125 } catch (DatabaseException e) {
\r
131 public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
\r
136 public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
\r
138 Map<String, Variable> result = new HashMap<String, Variable>();
\r
139 VariableMap map = getPossibleChildVariableMap(graph);
\r
140 if(map != null) map.getVariables(graph, this, result);
\r
141 return result.values();
\r
146 public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
\r
148 VariableMap map = getPossibleChildVariableMap(graph);
\r
149 if(map == null) return null;
\r
151 return map.getVariable(graph, this, name);
\r
152 } catch (DatabaseException e) {
\r
159 protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
\r
161 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
\r
162 if(valueMap == null) return null;
\r
164 return valueMap.getVariable(graph, this, name);
\r
165 } catch (DatabaseException e) {
\r
172 public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {
\r
174 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
\r
175 if(valueMap == null) return properties;
\r
176 return valueMap.getVariables(graph, this, properties);
\r
181 public Variable getPredicate(ReadGraph graph) throws DatabaseException {
\r
182 return Variables.getVariable(graph, graph.getURI(property.predicate));
\r
186 public Resource getPredicateResource(ReadGraph graph) throws DatabaseException {
\r
187 return property.predicate;
\r
191 public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {
\r
192 return property.predicate;
\r
196 public int hashCode() {
\r
198 final int prime = 31;
\r
200 result = prime * result + parent.hashCode();
\r
201 result = prime * result + property.hashCode();
\r
202 result = prime * result + value.hashCode();
\r
209 public boolean equals(Object obj) {
\r
214 if (getClass() != obj.getClass())
\r
216 StandardConstantGraphPropertyVariable other = (StandardConstantGraphPropertyVariable) obj;
\r
217 if (!value.equals(other.value))
\r
219 return super.equals(other);
\r
223 protected Variable getNameVariable(ReadGraph graph) throws DatabaseException {
\r
224 Layer0 L0 = Layer0.getInstance(graph);
\r
225 return new StandardGraphPropertyVariable(graph, this, L0.HasName);
\r
228 private void assertAlive(ReadGraph graph) throws DatabaseException {
\r
231 protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException {
\r
232 return new ValueAccessor() {
\r
235 public void setValue(WriteGraph graph, Variable context, Object value, Binding binding) throws DatabaseException {
\r
236 throw new UnsupportedOperationException();
\r
240 public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
\r
241 throw new UnsupportedOperationException();
\r
245 public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException {
\r
250 public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
\r
255 public Datatype getDatatype(ReadGraph graph, Variable context) throws DatabaseException {
\r
257 return Datatypes.getDatatype(value.getClass());
\r
258 } catch (DatatypeConstructionException e) {
\r
259 throw new DatabaseException(e);
\r
265 protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException {
\r
266 return graph.getPossibleRelatedValue2(property.predicate, Layer0.getInstance(graph).domainChildren, this);
\r
269 protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {
\r
270 return graph.getPossibleRelatedValue2(property.predicate, Layer0.getInstance(graph).domainProperties, this);
\r
273 public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
\r
274 ArrayList<String> value = getPropertyValue(graph, "classifications");
\r
275 return new HashSet<String>(value);
\r