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