]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PropertyInfoRequest.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / PropertyInfoRequest.java
1 package org.simantics.db.layer0.request;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.Map;
6 import java.util.Set;
7
8 import org.simantics.databoard.Bindings;
9 import org.simantics.databoard.accessor.reference.ChildReference;
10 import org.simantics.databoard.accessor.reference.IndexReference;
11 import org.simantics.databoard.accessor.reference.NameReference;
12 import org.simantics.databoard.type.Datatype;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.common.request.ResourceRead;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.layer0.util.Layer0Utils;
18 import org.simantics.db.layer0.variable.ValueAccessor;
19 import org.simantics.db.layer0.variable.VariableBuilder;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.operation.Layer0X;
22 import org.simantics.utils.datastructures.Pair;
23
24 import gnu.trove.map.hash.THashMap;
25
26 final public class PropertyInfoRequest extends ResourceRead<PropertyInfo> {
27
28         public PropertyInfoRequest(Resource resource) {
29                 super(resource);
30         }
31
32         @Override
33         public PropertyInfo perform(ReadGraph graph) throws DatabaseException {
34                 
35                 Layer0 L0 = Layer0.getInstance(graph);
36                 Layer0X L0X = Layer0X.getInstance(graph);
37                 
38                 boolean isHasProperty = graph.isSubrelationOf(resource, L0.HasProperty);
39                 
40                 String name = graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING);
41                 if(name != null) name = name.intern();
42                 
43                 Set<String> classifications = graph.sync(new ClassificationsRequest(graph.getPrincipalTypes(resource)));
44                 VariableBuilder variableBuilder = graph.getPossibleAdapter(resource, VariableBuilder.class);
45                 
46                 Datatype requiredDataType = graph.getPossibleRelatedValue(resource, L0X.RequiresDataType, Layer0Utils.datatype_binging);
47                 
48                 String definedUnit = graph.getPossibleRelatedValue(resource, L0X.HasUnit, Bindings.STRING);
49                 
50                 Resource literalRange = graph.getPossibleObject(resource, L0.HasRange);
51                 String requiredValueType = graph.getPossibleRelatedValue(resource, L0.RequiresValueType, Bindings.STRING);
52                 //System.out.println(name + " -> " + requiredValueType);
53                 
54                 ValueAccessor accessor = graph.getPossibleRelatedValue2(resource, Layer0.getInstance(graph).valueAccessor, resource);
55                 
56                 boolean hasEnumerationRange = graph.syncRequest(new HasEnumerationRange(resource));
57
58                 Collection<Resource> ranges = graph.getObjects(resource, L0.HasRange);
59                 if (!ranges.isEmpty()) {
60                         for (Resource range : ranges) {
61                                 if (requiredValueType == null) {
62                                         // Check if the single range defines value type
63                                         Collection<Resource> valueTypes = graph.getAssertedObjects(range, L0.HasValueType);
64                                         if (valueTypes.size() > 0) {
65                                                 for (Resource valueType : valueTypes) {
66                                                         String vt = graph.getPossibleValue(valueType, Bindings.STRING);
67                                                         if (vt != null && !vt.isEmpty()) {
68                                                                 requiredValueType = vt;
69                                                                 continue;
70                                                         }
71                                                 }
72                                         }
73                                 }
74
75                                 Collection<Resource> subliterals = graph.getObjects(range, L0.HasSubliteralPredicate);
76                                 if(!subliterals.isEmpty()) {
77                                         Map<String,Pair<Resource, ChildReference>> map = new THashMap<String,Pair<Resource, ChildReference>>(); 
78                                         for(Resource p : subliterals) {
79                                                 String pN = graph.getPossibleRelatedValue(p, L0.HasName, Bindings.STRING);
80                                                 if(pN == null) continue;
81                                                 if(pN.startsWith("n-")) {
82                                                         ChildReference r = new NameReference(pN.substring(2));
83                                                         map.put(pN, Pair.make(p, r));
84                                                 } else if (pN.startsWith("i-")) {
85                                                         ChildReference r = new IndexReference(Integer.parseInt(pN.substring(2)));
86                                                         map.put(pN, Pair.make(p, r));
87                                                 }
88                                         }
89                                         
90                                         return PropertyInfo.make(graph, resource, name, isHasProperty, classifications, variableBuilder, literalRange, requiredDataType, definedUnit, requiredValueType, map, accessor, hasEnumerationRange);
91                                         
92                                 }
93                         }
94                 }
95                 
96                 return PropertyInfo.make(graph, resource, name, isHasProperty, classifications, variableBuilder, literalRange, requiredDataType, definedUnit, requiredValueType, Collections.<String,Pair<Resource,ChildReference>>emptyMap(), accessor, hasEnumerationRange);
97                 
98         }
99         
100         @Override
101         public boolean equals(Object object) {
102         if (this == object)
103             return true;
104         else if (object == null)
105             return false;
106         else if (!(object instanceof PropertyInfoRequest))
107             return false;
108         PropertyInfoRequest r = (PropertyInfoRequest)object;
109         return r.resource.equals(resource);
110         }
111         
112         @Override
113         public int hashCode() {
114                 return resource.hashCode() + classHash;
115         }
116         
117         private static int classHash = 31*PropertyInfo.class.hashCode();
118
119 }