]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/ComponentTypeSubstructure.java
Sync git svn branch with SVN repository r33366.
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / ComponentTypeSubstructure.java
1 package org.simantics.modeling;\r
2 \r
3 import gnu.trove.map.hash.THashMap;\r
4 \r
5 import java.util.Collections;\r
6 import java.util.Map;\r
7 \r
8 import org.simantics.db.ReadGraph;\r
9 import org.simantics.db.RequestProcessor;\r
10 import org.simantics.db.Resource;\r
11 import org.simantics.db.common.request.ResourceRead;\r
12 import org.simantics.db.common.uri.UnescapedChildMapOfResource;\r
13 import org.simantics.db.exception.DatabaseException;\r
14 import org.simantics.db.layer0.request.PropertyInfo;\r
15 import org.simantics.db.layer0.request.PropertyInfoRequest;\r
16 import org.simantics.layer0.Layer0;\r
17 import org.simantics.scl.compiler.types.Type;\r
18 import org.simantics.structural.stubs.StructuralResource2;\r
19 import org.simantics.utils.datastructures.Pair;\r
20 \r
21 /**\r
22  * Contains necessary information about a component type to represent its\r
23  * variable space.\r
24  * \r
25  * @author Hannu Niemistö\r
26  */\r
27 public class ComponentTypeSubstructure {\r
28     \r
29     public final THashMap<String, Type> properties;\r
30     public final Map<String, ComponentTypeSubstructure> children;\r
31     \r
32     private ComponentTypeSubstructure(THashMap<String, Type> properties,\r
33             Map<String, ComponentTypeSubstructure> children) {\r
34         this.properties = properties;\r
35         this.children = children;\r
36     }\r
37     \r
38     public Pair<String,Type> possibleTypedRVI(String name) {\r
39         StringBuilder b = new StringBuilder();\r
40         int namePos = 0;\r
41         ComponentTypeSubstructure substructure = this;\r
42         \r
43         // Find the children\r
44         int p;\r
45         while( (p = name.indexOf('.', namePos)) >= 0 ) {\r
46             String childName = name.substring(namePos, p);\r
47             ComponentTypeSubstructure childSubstructure = substructure.children.get(childName);\r
48             if(childSubstructure == null)\r
49                 return null;\r
50             b.append('/').append(childName);\r
51             namePos = p+1;\r
52             substructure = childSubstructure;\r
53         }\r
54         \r
55         // Find the property\r
56         String propertyName = name.substring(namePos);\r
57         Type type = substructure.properties.get(propertyName);\r
58         if(type == null)\r
59             return null;\r
60         b.append('#').append(propertyName);\r
61         return Pair.make(b.toString(), type);\r
62     }\r
63 \r
64     public static ComponentTypeSubstructure forType(RequestProcessor processor, Resource componentType) throws DatabaseException {\r
65         return processor.syncRequest(new ResourceRead<ComponentTypeSubstructure>(componentType) {\r
66             @Override\r
67             public ComponentTypeSubstructure perform(ReadGraph graph)\r
68                     throws DatabaseException {\r
69                 // Properties\r
70                 THashMap<String, Type> properties = new THashMap<String, Type>();\r
71                 collect(graph, resource, properties);\r
72                 for(Resource t : graph.getSupertypes(resource)) collect(graph, t, properties);\r
73                 \r
74                 // Children\r
75                 Map<String, ComponentTypeSubstructure> children;\r
76                 \r
77                 StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
78                 Resource composite = graph.getPossibleObject(resource, STR.IsDefinedBy);\r
79                 if(composite == null)\r
80                     children = Collections.<String, ComponentTypeSubstructure>emptyMap();\r
81                 else {\r
82                     Map<String, Resource> childMap = graph.syncRequest(new UnescapedChildMapOfResource(composite));\r
83                     children = new THashMap<String, ComponentTypeSubstructure>(childMap.size());\r
84                     for(Map.Entry<String,Resource> entry : childMap.entrySet()) {\r
85                         Resource component = entry.getValue();\r
86                         Resource type = graph.getPossibleType(component, STR.Component);\r
87                         if(type != null)\r
88                             children.put(entry.getKey(), forType(graph, type));\r
89                     }\r
90                 }\r
91                     \r
92                 // Return the result\r
93                 return new ComponentTypeSubstructure(properties, children);\r
94             }\r
95         });\r
96     }\r
97     \r
98     private static void collect(ReadGraph graph, Resource t, THashMap<String, Type> result) throws DatabaseException {\r
99         Layer0 L0 = Layer0.getInstance(graph);\r
100         for(Resource relation : graph.getObjects(t, L0.DomainOf)) {\r
101             if(graph.isSubrelationOf(relation, L0.HasProperty)) {\r
102                 PropertyInfo propertyInfo = graph.syncRequest(new PropertyInfoRequest(relation));\r
103                 result.put(propertyInfo.name, SCLTypeUtils.getType(propertyInfo));\r
104             }\r
105         }\r
106     }\r
107 }\r