]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardExpressionGraphPropertyVariable.java
Initial support for concurrency in databoard, bindings and serializers
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / StandardExpressionGraphPropertyVariable.java
1 package org.simantics.db.layer0.variable;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.HashSet;
7 import java.util.Map;
8 import java.util.Set;
9
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;
21
22 abstract public class StandardExpressionGraphPropertyVariable extends StandardGraphPropertyVariable {
23         
24     final public String expressionText;
25         
26         transient private int hash = 0;
27
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;
34         }
35
36         @Override
37         public void validate(ReadGraph graph) throws DatabaseException {
38         }
39
40         @Override
41         public String getName(ReadGraph graph) throws DatabaseException {
42                 return graph.getRelatedValue(property.predicate, graph.getService(Layer0.class).HasName, Bindings.STRING);
43         }
44
45         @Override
46         public String getLabel(ReadGraph graph) throws DatabaseException {
47                 return graph.getRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent);
48         }
49
50         @Override
51         public Variable getParent(ReadGraph graph) throws DatabaseException {
52                 return parent;
53         }
54
55         @Override
56         public <T> T getValue(ReadGraph graph) throws DatabaseException {
57                 
58                 assertAlive(graph);
59                 
60                 return (T)getValueAccessor(graph).getValue(graph, this);
61         }
62
63         @Override
64         public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
65                 
66                 assertAlive(graph);
67
68                 return (T)getValueAccessor(graph).getValue(graph, this, binding);
69         }
70         
71         @Override
72         public Resource getRepresents(ReadGraph graph) throws DatabaseException {
73                 
74                 assertAlive(graph);
75                 
76                 return null;
77                 
78         }
79
80         @Override
81         public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
82                 
83                 assertAlive(graph);
84                 
85                 return null;
86                 
87         }
88
89         @Override
90         public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {
91                 assertAlive(graph);
92                 throw new UnsupportedOperationException("Procedural expression variable cannot be set.");
93         }
94         
95         @Override
96         public void setValue(WriteGraph graph, Object value) throws DatabaseException {
97                 assertAlive(graph);
98                 throw new UnsupportedOperationException("Procedural expression variable cannot be set.");
99         }
100
101         @Override
102         public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
103                 try {
104                         return Layer0Utils.getDatatype(graph, this);
105                 } catch (DatabaseException e) {
106                         return null;
107                 }
108         }
109         
110         @Override
111         public String getUnit(ReadGraph graph) throws DatabaseException {
112                 try {
113                         return Layer0Utils.getUnit(graph, this);
114                 } catch (DatabaseException e) {
115                         return null;
116                 }
117         }
118         
119         @Override
120         public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
121                 return null;
122         }
123
124     @Override
125     public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
126
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();
131         
132     }
133     
134     @Override
135     public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
136
137         VariableMap map = getPossibleChildVariableMap(graph);
138         if(map == null) return null;
139         try {
140             return map.getVariable(graph, this, name);
141         } catch (DatabaseException e) {
142             return null;
143         }
144         
145     }
146         
147         @Override
148         protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
149
150                 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
151                 if(valueMap == null) return null;
152                 try {
153                         return valueMap.getVariable(graph, this, name);
154                 } catch (DatabaseException e) {
155                         return null;
156                 }
157                 
158         }
159         
160         @Override
161         public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {
162
163                 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
164                 if(valueMap == null) return properties;
165                 return valueMap.getVariables(graph, this, properties);
166                 
167         }
168         
169         @Override
170         public Variable getPredicate(ReadGraph graph) throws DatabaseException {
171                 return Variables.getVariable(graph, graph.getURI(property.predicate));
172         }
173         
174         @Override
175         public Resource getPredicateResource(ReadGraph graph) throws DatabaseException {
176                 return property.predicate;
177         }
178         
179         @Override
180         public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {
181                 return property.predicate;
182         }
183
184         @Override
185         public int hashCode() {
186                 if(hash == 0) {
187                         final int prime = 31;
188                         int result = 1;
189                         result = prime * result + parent.hashCode();
190                         result = prime * result + property.hashCode();
191                         result = prime * result + expressionText.hashCode();
192                         hash =result;
193                 }
194                 return hash;
195         }
196
197         @Override
198         public boolean equals(Object obj) {
199                 if (this == obj)
200                         return true;
201                 if (obj == null)
202                         return false;
203                 if (getClass() != obj.getClass())
204                         return false;
205                 StandardExpressionGraphPropertyVariable other = (StandardExpressionGraphPropertyVariable) obj;
206                 if (!expressionText.equals(other.expressionText))
207                         return false;
208                 return super.equals(other);
209         }
210
211         @Override
212         protected Variable getNameVariable(ReadGraph graph) throws DatabaseException {
213                 Layer0 L0 = Layer0.getInstance(graph);
214                 return new StandardGraphPropertyVariable(graph, this, L0.HasName);
215         }
216         
217         private void assertAlive(ReadGraph graph) throws DatabaseException {
218         }
219         
220         protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException {
221             return new ValueAccessor() {
222             
223             @Override
224             public void setValue(WriteGraph graph, Variable context, Object value, Binding binding) throws DatabaseException {
225                 throw new UnsupportedOperationException();
226             }
227             
228             @Override
229             public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
230                 throw new UnsupportedOperationException();
231             }
232             
233             @Override
234             public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException {
235                 return compute(graph, context, binding);
236             }
237             
238             @Override
239             public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
240                 return compute(graph, context);
241             }
242
243                         @Override
244                         public Datatype getDatatype(ReadGraph graph, Variable context) throws DatabaseException {
245                                 try {
246                                         Object value = compute(graph, context);
247                                         return Datatypes.getDatatype(value.getClass());
248                                 } catch (DatatypeConstructionException e) {
249                                         throw new DatabaseException(e);
250                                 }
251                         }
252         };
253         }
254         
255         abstract protected Object compute(ReadGraph graph, Variable context) throws DatabaseException;
256         abstract protected Object compute(ReadGraph graph, Variable context, Binding binding) throws DatabaseException;
257         
258         protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException {
259         return graph.getPossibleRelatedValue2(property.predicate, Layer0.getInstance(graph).domainChildren, this);
260         }
261
262         protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {
263                 return graph.getPossibleRelatedValue2(property.predicate, Layer0.getInstance(graph).domainProperties, this);
264         }
265         
266         public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
267                 ArrayList<String> value = getPropertyValue(graph, "classifications"); 
268                 return new HashSet<String>(value);
269         }
270         
271 }