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