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