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