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