Improved Bindings.getBinding(Class) caching for Datatype.class
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / PropertyInfo.java
1 package org.simantics.db.layer0.request;
2
3 import java.util.Collection;
4 import java.util.Map;
5 import java.util.Set;
6
7 import org.simantics.databoard.Bindings;
8 import org.simantics.databoard.accessor.reference.ChildReference;
9 import org.simantics.databoard.binding.Binding;
10 import org.simantics.databoard.type.Datatype;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.variable.ValueAccessor;
15 import org.simantics.db.layer0.variable.VariableBuilder;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.utils.datastructures.Pair;
18
19 public class PropertyInfo {
20         public final Resource predicate;
21         public final String name;
22         public final boolean isHasProperty;
23         public final Set<String> classifications;
24         public final VariableBuilder builder;
25         public final Resource literalRange;
26         public final Datatype requiredDatatype;
27         public final String requiredValueType;
28         public final String definedUnit;
29         public final Binding defaultBinding;
30         public final Map<String,Pair<Resource, ChildReference>> subliteralPredicates;
31         public final ValueAccessor valueAccessor;
32         public final boolean hasEnumerationRange;
33         public PropertyInfo(Resource predicate, String name, boolean isHasProperty, Set<String> classifications, VariableBuilder builder, Resource literalRange, Datatype requiredDatatype, String definedUnit, String requiredValueType, Binding defaultBinding, Map<String,Pair<Resource, ChildReference>> subliteralPredicates, ValueAccessor valueAccessor, boolean hasEnumerationRange) {
34                 this.predicate = predicate;
35                 this.name = name;
36                 this.isHasProperty = isHasProperty;
37                 this.classifications = classifications;
38                 this.builder = builder;
39                 this.literalRange = literalRange;
40                 this.requiredDatatype = requiredDatatype;
41                 this.definedUnit = definedUnit;
42                 this.requiredValueType = requiredValueType;
43                 this.defaultBinding = defaultBinding;
44                 this.subliteralPredicates = subliteralPredicates;
45                 this.valueAccessor = valueAccessor;
46                 this.hasEnumerationRange = hasEnumerationRange;
47         }
48         public static PropertyInfo make(ReadGraph graph, Resource predicate, String name, boolean isHasProperty, Set<String> classifications, VariableBuilder builder, Resource literalRange, Datatype requiredDatatype, String definedUnit, String requiredValueType, Map<String,Pair<Resource, ChildReference>> subliteralPredicates, ValueAccessor valueAccessor, boolean hasEnumerationRange) throws DatabaseException {
49
50                 Layer0 L0 = Layer0.getInstance(graph);
51                 if(literalRange != null) {
52                         Collection<Resource> dts = graph.getAssertedObjects(literalRange, L0.HasDataType);
53                         if(dts.size() == 1) {
54                                 Datatype dt = graph.getPossibleValue(dts.iterator().next(), Bindings.DATATYPE);
55                                 if(requiredDatatype == null) requiredDatatype = dt;
56                         }
57                 }
58                 
59                 Binding defaultBinding = requiredDatatype != null ? Bindings.getBinding(requiredDatatype) : null;
60                 
61                 return new PropertyInfo(predicate, name, isHasProperty, classifications, builder, literalRange, requiredDatatype, definedUnit, requiredValueType, defaultBinding, subliteralPredicates, valueAccessor, hasEnumerationRange);
62
63         }
64         public boolean hasClassification(String classification) {
65                 return classifications.contains(classification);
66         }
67         @Override
68         public String toString() {
69                 StringBuilder sb = new StringBuilder();
70                 sb.append("PropertyInfo [")
71                 .append(name)
72                 .append(" : ")
73                 .append(requiredDatatype)
74                 .append(" :: ")
75                 .append(requiredValueType)
76                 .append(", predicate=")
77                 .append(predicate)
78                 .append(", isHasProperty=")
79                 .append(isHasProperty)
80                 .append(", hasEnumerationRange=")
81                 .append(hasEnumerationRange)
82                 .append(", definedUnit=")
83                 .append(definedUnit != null ? definedUnit : "<none>")
84                 .append(", defaultBinding=")
85                 .append(defaultBinding)
86                 .append(", valueAccessor=")
87                 .append(valueAccessor)
88                 .append("]");
89                 return sb.toString();
90         }
91 }