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