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 abstract public class StandardExpressionGraphPropertyVariable extends StandardGraphPropertyVariable {
24 final public String expressionText;
26 transient private int hash = 0;
28 public StandardExpressionGraphPropertyVariable(ReadGraph graph, Variable parent, Resource property, String expressionText) throws DatabaseException {
29 super(graph, parent, null, property);
30 assert parent != null;
31 assert property != null;
32 assert expressionText != null;
33 this.expressionText = expressionText;
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 Variable getParent(ReadGraph graph) throws DatabaseException {
56 public <T> T getValue(ReadGraph graph) throws DatabaseException {
60 return (T)getValueAccessor(graph).getValue(graph, this);
64 public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
68 return (T)getValueAccessor(graph).getValue(graph, this, binding);
72 public Resource getRepresents(ReadGraph graph) throws DatabaseException {
81 public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
90 public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {
92 throw new UnsupportedOperationException("Procedural expression variable cannot be set.");
96 public void setValue(WriteGraph graph, Object value) throws DatabaseException {
98 throw new UnsupportedOperationException("Procedural expression variable cannot be set.");
102 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
104 return Layer0Utils.getDatatype(graph, this);
105 } catch (DatabaseException e) {
111 public String getUnit(ReadGraph graph) throws DatabaseException {
113 return Layer0Utils.getUnit(graph, this);
114 } catch (DatabaseException e) {
120 public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
125 public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
127 Map<String, Variable> result = new HashMap<String, Variable>();
128 VariableMap map = getPossibleChildVariableMap(graph);
129 if(map != null) map.getVariables(graph, this, result);
130 return result.values();
135 public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
137 VariableMap map = getPossibleChildVariableMap(graph);
138 if(map == null) return null;
140 return map.getVariable(graph, this, name);
141 } catch (DatabaseException e) {
148 protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
150 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
151 if(valueMap == null) return null;
153 return valueMap.getVariable(graph, this, name);
154 } catch (DatabaseException e) {
161 public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {
163 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
164 if(valueMap == null) return properties;
165 return valueMap.getVariables(graph, this, properties);
170 public Variable getPredicate(ReadGraph graph) throws DatabaseException {
171 return Variables.getVariable(graph, graph.getURI(property.predicate));
175 public Resource getPredicateResource(ReadGraph graph) throws DatabaseException {
176 return property.predicate;
180 public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {
181 return property.predicate;
185 public int hashCode() {
187 final int prime = 31;
189 result = prime * result + parent.hashCode();
190 result = prime * result + property.hashCode();
191 result = prime * result + expressionText.hashCode();
198 public boolean equals(Object obj) {
203 if (getClass() != obj.getClass())
205 StandardExpressionGraphPropertyVariable other = (StandardExpressionGraphPropertyVariable) obj;
206 if (!expressionText.equals(other.expressionText))
208 return super.equals(other);
212 protected Variable getNameVariable(ReadGraph graph) throws DatabaseException {
213 Layer0 L0 = Layer0.getInstance(graph);
214 return new StandardGraphPropertyVariable(graph, this, L0.HasName);
217 private void assertAlive(ReadGraph graph) throws DatabaseException {
220 protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException {
221 return new ValueAccessor() {
224 public void setValue(WriteGraph graph, Variable context, Object value, Binding binding) throws DatabaseException {
225 throw new UnsupportedOperationException();
229 public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
230 throw new UnsupportedOperationException();
234 public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException {
235 return compute(graph, context, binding);
239 public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
240 return compute(graph, context);
244 public Datatype getDatatype(ReadGraph graph, Variable context) throws DatabaseException {
246 Object value = compute(graph, context);
247 return Datatypes.getDatatype(value.getClass());
248 } catch (DatatypeConstructionException e) {
249 throw new DatabaseException(e);
255 abstract protected Object compute(ReadGraph graph, Variable context) throws DatabaseException;
256 abstract protected Object compute(ReadGraph graph, Variable context, Binding binding) throws DatabaseException;
258 protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException {
259 return graph.getPossibleRelatedValue2(property.predicate, Layer0.getInstance(graph).domainChildren, this);
262 protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {
263 return graph.getPossibleRelatedValue2(property.predicate, 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);