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