]> gerrit.simantics Code Review - simantics/platform.git/blob
7927614cd37f9779292ee4ace98e5fd4ba7f00f0
[simantics/platform.git] /
1 package org.simantics.db.layer0.variable;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.Set;
8
9 import org.simantics.databoard.Bindings;
10 import org.simantics.databoard.accessor.reference.ChildReference;
11 import org.simantics.databoard.binding.Binding;
12 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
13 import org.simantics.databoard.type.Datatype;
14 import org.simantics.db.DevelopmentKeys;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
19 import org.simantics.db.common.validation.L0Validations;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.exception.DatatypeNotFoundException;
22 import org.simantics.db.exception.ValidationException;
23 import org.simantics.db.layer0.exception.InvalidVariableException;
24 import org.simantics.db.layer0.exception.MissingVariableValueException;
25 import org.simantics.db.layer0.exception.PendingVariableException;
26 import org.simantics.db.layer0.function.All;
27 import org.simantics.db.layer0.request.PropertyInfo;
28 import org.simantics.db.layer0.request.PropertyInfoRequest;
29 import org.simantics.db.layer0.scl.SCLDatabaseException;
30 import org.simantics.db.layer0.util.Layer0Utils;
31 import org.simantics.layer0.Layer0;
32 import org.simantics.utils.Development;
33 import org.simantics.utils.datastructures.Pair;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
38     private static final Logger LOGGER = LoggerFactory.getLogger(StandardGraphPropertyVariable.class);
39     
40         protected static final PropertyInfo NO_PROPERTY = new PropertyInfo(null, null,
41                         false, false, Collections.<String> emptySet(), null, null, null, null, null, null,
42                         Collections.<String, Pair<Resource, ChildReference>> emptyMap(),
43                         null, false);
44
45         final public Variable parent;
46         final public Resource parentResource;
47         final public PropertyInfo property;
48         final public Resource represents;
49         
50         transient private int hash = 0;
51
52         public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource property) throws DatabaseException {
53                 this(graph, parent, node, (Resource)parent.getPossibleRepresents(graph), getPropertyInfo(graph, property));
54         }
55         
56         public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, Resource property) throws DatabaseException {
57                 this(graph, parent, node, parentResource, getPropertyInfo(graph, property));
58         }
59
60         public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, Resource property) throws DatabaseException {
61         this(graph, parent, null, (Resource)parent.getPossibleRepresents(graph), getPropertyInfo(graph, property));
62     }
63
64     public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, Resource parentResource, PropertyInfo property) throws DatabaseException {
65         this(parent, null, parentResource, property, getPossibleRepresents(graph, parentResource, property.predicate));
66     }
67     
68     public StandardGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, PropertyInfo property) throws DatabaseException {
69         this(parent, node, parentResource, property, getPossibleRepresents(graph, parentResource, property.predicate));
70     }
71
72     private static PropertyInfo getPropertyInfo(ReadGraph graph, Resource property) throws DatabaseException {
73             return property != null ? graph.syncRequest(new PropertyInfoRequest(property), TransientCacheAsyncListener.<PropertyInfo>instance()) : NO_PROPERTY;
74     }
75     
76     private static Resource getPossibleRepresents(ReadGraph graph, Resource parentResource, Resource predicate) throws DatabaseException {
77         if(parentResource == null || predicate == null) return null;
78         return graph.getPossibleObject(parentResource, predicate);
79     }
80     
81     public boolean isAsserted() {
82         return false;
83     }
84
85     public StandardGraphPropertyVariable(Variable parent, VariableNode node, Resource parentResource, PropertyInfo property, Resource represents) throws DatabaseException {
86         super(node);
87                 assert parent != null;
88                 this.parent = parent;
89                 this.property = property;
90                 this.parentResource = parentResource;
91                 this.represents = represents;
92         }
93
94         @Override
95         public String getName(ReadGraph graph) throws DatabaseException {
96             if(node != null) return node.support.manager.getName(node.node);
97                 return property.name;
98         }
99
100         @Override
101         public String getPossibleLabel(ReadGraph graph) throws DatabaseException {
102                 return graph.getPossibleRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
103         }
104
105         @Override
106         public String getLabel(ReadGraph graph) throws DatabaseException {
107                 return graph.getRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
108         }
109
110         @Override
111         public Variable getParent(ReadGraph graph) throws DatabaseException {
112                 return parent;
113         }
114         
115         @Override
116         public PropertyInfo getPropertyInfo(ReadGraph graph) throws DatabaseException {
117                 return property;
118         }
119
120         @SuppressWarnings("unchecked")
121         @Override
122         public <T> T getValue(ReadGraph graph) throws DatabaseException {
123                 
124                 if(Development.DEVELOPMENT) {
125                         if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
126                                 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
127                                 if(error != null) {
128                                     LOGGER.error(error);
129                                         throw new ValidationException(error);
130                                 }
131                         }
132                 }
133                 
134                 return (T)getValueAccessor(graph).getValue(graph, this);
135                 
136         }
137
138         @SuppressWarnings("unchecked")
139         @Override
140         public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
141                 // Fall back to the bindingless method, if the expected output is a Java object 
142                 if (binding instanceof ObjectVariantBinding)
143                         return getValue(graph);         
144
145                 if(Development.DEVELOPMENT) {
146                         if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
147                                 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
148                                 if(error != null) {
149                                         LOGGER.error(error);
150                                         throw new ValidationException(error);
151                                 }
152                         }
153                 }
154         
155                 try {
156                         
157                         return (T) getValueAccessor(graph).getValue(graph, this, binding);
158                 } catch (SCLDatabaseException e) { // these can be thrown when compiling e.g. derived properties
159                         throw e;
160                 } catch (MissingVariableValueException | PendingVariableException e) {
161                         throw e;
162                 } catch (Throwable t) {
163                         throw new MissingVariableValueException(t);
164                 }
165                 
166         }
167         
168         @Override
169         public Resource getRepresents(ReadGraph graph) throws DatabaseException {
170                 if(represents == null)
171                         throw new InvalidVariableException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");
172                 return represents;
173 //              return graph.getSingleObject(parentResource, property.predicate);
174         }
175
176         @Override
177         public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
178 //        if(parentResource == null) return null;
179 //              return graph.getPossibleObject(parentResource, property.predicate);
180                 return represents;
181         }
182
183         @Override
184         public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {
185                 
186                 if(Development.DEVELOPMENT) {
187                         if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
188                                 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
189                                 if(error != null) {
190                                         LOGGER.error(error);
191                                         throw new ValidationException(error);
192                                 }
193                         }
194                 }
195                 
196                 getValueAccessor(graph).setValue(graph, this, value, binding);
197                 
198         }
199         
200         @Override
201         public void setValue(WriteGraph graph, Object value) throws DatabaseException {
202                 
203                 if(Development.DEVELOPMENT) {
204                         if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
205                                 String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
206                                 if(error != null) {
207                                         LOGGER.error(error);
208                                         throw new ValidationException(error);
209                                 }
210                         }
211                 }
212                 
213                 getValueAccessor(graph).setValue(graph, this, value);
214                 
215         }
216
217         @Override
218         public Binding getDefaultBinding(ReadGraph graph) throws DatabaseException {
219                 return Layer0Utils.getDefaultBinding(graph, this);
220         }
221         
222         @Override
223         public Binding getPossibleDefaultBinding(ReadGraph graph) throws DatabaseException {
224                 return Layer0Utils.getPossibleDefaultBinding(graph, this);
225         }
226
227         @Override
228         public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
229 //<<<<<<< .mine
230 //              
231 //      Binding binding = getPossibleDefaultBinding(graph);
232 //        if(binding != null) return binding.type();
233 //
234 //              Variant vt = getVariantValue(graph);
235 //              binding = vt.getBinding();
236 //              if(binding == null) throw new DatabaseException("No binding in Variant with value " + vt.getValue());
237 //              return binding.type();
238 //=======
239                 Datatype type;
240                 try {                   
241                         type = getValueAccessor(graph).getDatatype(graph, this);
242                 } catch (Throwable t) {
243                         throw new MissingVariableValueException(t);
244                 }
245                 
246                 if (type == null) {
247                         String uri = this.getPossibleURI(graph);
248                         if (uri != null)
249                                 throw new DatatypeNotFoundException("No data type for " + uri);
250                         else
251                                 throw new DatatypeNotFoundException("No data type for " + this.getIdentifier());
252                 }
253                 
254                 return type;
255 //>>>>>>> .r30794
256                 
257         }
258         
259         @Override
260         public Datatype getPossibleDatatype(ReadGraph graph) throws DatabaseException {
261
262                 try {
263                         return getDatatype(graph);
264                 } catch (DatabaseException e) {
265                         return null;
266                 }
267                 
268         }
269         
270         @Override
271         public String getUnit(ReadGraph graph) throws DatabaseException {
272                 try {
273                         return Layer0Utils.getUnit(graph, this);
274                 } catch (DatabaseException e) {
275                         return null;
276                 }
277         }
278
279         @Override
280         public Resource getPropertyResource(ReadGraph graph) {
281                 return property.predicate;
282         }
283         
284         @Override
285         public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
286                 return parentResource;
287         }
288
289         @Override
290         public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
291
292                 Map<String, Variable> result = new HashMap<String, Variable>();
293                 VariableMap map = getPossibleChildVariableMap(graph);
294                 if(map != null) map.getVariables(graph, this, result);
295                 return result.values();
296                 
297         }
298         
299         @Override
300         public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
301
302                 VariableMap map = getPossibleChildVariableMap(graph);
303                 if(map == null) return null;
304                 try {
305                         return map.getVariable(graph, this, name);
306                 } catch (DatabaseException e) {
307                         return null;
308                 }
309                 
310         }
311         
312         
313         @Override
314         protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
315
316                 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
317                 if(valueMap == null) return null;
318                 try {
319                         return valueMap.getVariable(graph, this, name);
320                 } catch (DatabaseException e) {
321                         return null;
322                 } catch (Exception t) {
323                         LOGGER.error("getPossibleDomainProperty is implemented incorrectly, but returns null on Exception for backward compatibility. URI="+getURI(graph)+", name="+name+".", t);
324                         return null;
325                 }
326                 
327         }
328         
329         @Override
330         public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {
331
332                 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
333                 if(valueMap == null) return properties;
334                 return valueMap.getVariables(graph, this, properties);
335                 
336         }
337         
338         @Override
339         public Variable getPredicate(ReadGraph graph) throws DatabaseException {
340                 return Variables.getVariable(graph, graph.getURI(property.predicate));
341         }
342         
343         @Override
344         public Resource getPredicateResource(ReadGraph graph) throws DatabaseException {
345                 return property.predicate;
346         }
347         
348         @Override
349         public Resource getPossiblePredicateResource(ReadGraph graph) throws DatabaseException {
350                 return property.predicate;
351         }
352
353         @Override
354         public int hashCode() {
355                 if(hash == 0) {
356                         final int prime = 31;
357                         int result = 1;
358             result = prime * result + (parent != null ? parent.hashCode() : 0);
359             result = prime * result + (node != null ? node.hashCode() : 0);
360             result = prime * result + (property.predicate != null ? property.predicate.hashCode() : 0);
361             result = prime * result + (parentResource != null ? parentResource.hashCode() : 0);
362             result = prime * result + (represents != null ? represents.hashCode() : 0);
363                         hash =result;
364                 }
365                 return hash;
366         }
367
368         @Override
369         public boolean equals(Object obj) {
370                 if (this == obj)
371                         return true;
372                 if (obj == null)
373                         return false;
374                 if (getClass() != obj.getClass())
375                         return false;
376                 StandardGraphPropertyVariable other = (StandardGraphPropertyVariable) obj;
377                 
378         if(node != null) {
379                 if(!node.equals(other.node)) return false;
380         } else if (other.node != null) return false;
381
382         if(property.predicate != null) {
383                 if(!property.predicate.equals(other.property.predicate)) return false;
384         } else if (other.property.predicate != null) return false;
385         
386         if(parentResource != null) {
387                 if(!parentResource.equals(other.parentResource)) return false;
388         } else if (other.parentResource != null) return false;
389
390         if(parent != null) {
391                 if(!parent.equals(other.parent)) return false;
392         } else if (other.parent != null) return false;
393
394         if(represents != null) {
395                 if(!represents.equals(other.represents)) return false;
396         } else if (other.represents != null) return false;
397
398         return true;
399                 
400         }
401
402         @Override
403         protected Variable getNameVariable(ReadGraph graph) throws DatabaseException {
404             throw new UnsupportedOperationException();
405         }
406         
407         protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException {
408                 if((property == null || property == NO_PROPERTY) && parentResource == null) return All.standardValueAccessor;
409             ValueAccessor accessor = property.valueAccessor;
410             if(accessor != null) return accessor;
411             else {
412                 System.err.println("No value accessor for " + getURI(graph));
413                 return All.standardValueAccessor;
414             }
415         }
416
417         protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException {
418                 if(represents == null) return All.standardPropertyDomainChildren;
419             Resource domainChildren = Layer0.getInstance(graph).domainChildren;
420                 return graph.getPossibleRelatedValue2(represents, domainChildren, 
421                                 new StandardGraphPropertyVariable(graph, this, domainChildren));
422         }
423
424         protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {
425         if(property == null) return All.standardPropertyDomainProperties;
426         if(property.predicate == null) return null;
427                 return graph.syncRequest(new PropertyVariableMapRequest(property.predicate), TransientCacheAsyncListener.<VariableMap>instance());
428         }
429         
430         public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
431                 return property.classifications;
432         }
433         
434         @Override
435     public Map<String, Variable> collectDomainProperties(ReadGraph graph, String classification, Map<String, Variable> properties) throws DatabaseException {
436         
437         VariableMap valueMap = getPossiblePropertyVariableMap(graph);
438                 if(valueMap == null) return properties;
439                 return valueMap.getVariables(graph, this, classification, properties);
440                 
441     }
442         
443     public Collection<Variable> getProperties(ReadGraph graph, String classification) throws DatabaseException {
444         
445         VariableMap valueMap = getPossiblePropertyVariableMap(graph);
446                 if(valueMap == null) return Collections.emptyList();
447                 Map<String,Variable> propertyMap = valueMap.getVariables(graph, this, classification, null);
448         if(propertyMap == null || propertyMap.isEmpty()) return Collections.emptyList();
449         else return propertyMap.values();
450         
451     }
452         
453 }