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