]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/ReadComponentTypeInterfaceRequest.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / scl / ReadComponentTypeInterfaceRequest.java
diff --git a/bundles/org.simantics.structural2/src/org/simantics/structural2/scl/ReadComponentTypeInterfaceRequest.java b/bundles/org.simantics.structural2/src/org/simantics/structural2/scl/ReadComponentTypeInterfaceRequest.java
new file mode 100644 (file)
index 0000000..2c2f0f7
--- /dev/null
@@ -0,0 +1,61 @@
+package org.simantics.structural2.scl;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+\r
+import java.util.Map;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ResourceRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.scl.compiler.environment.Environment;\r
+import org.simantics.scl.compiler.environment.Environments;\r
+import org.simantics.scl.compiler.top.SCLExpressionCompilationException;\r
+import org.simantics.scl.compiler.types.Type;\r
+\r
+public class ReadComponentTypeInterfaceRequest extends ResourceRead<Map<String, ComponentTypeProperty>> {\r
+       \r
+    Environment environment;\r
+    \r
+       public ReadComponentTypeInterfaceRequest(Resource resource, Environment environment) {\r
+               super(resource);\r
+               this.environment = environment;\r
+       }\r
+\r
+       private void collect(ReadGraph graph, Resource t, THashMap<String, ComponentTypeProperty> result) throws DatabaseException {\r
+               Layer0 L0 = Layer0.getInstance(graph);\r
+               for(Resource relation : graph.getObjects(t, L0.DomainOf)) {\r
+                       if(graph.isSubrelationOf(relation, L0.HasProperty)) {\r
+                               String name = graph.getRelatedValue(relation, L0.HasName, Bindings.STRING);\r
+                               String typeName = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);\r
+                               if(typeName == null) continue;\r
+                               Type type;\r
+                               try {\r
+                                   type = Environments.getType(environment, typeName);\r
+                               } catch(SCLExpressionCompilationException e) {\r
+                                   e.printStackTrace();\r
+                                   continue;\r
+                               }\r
+                               ComponentTypeProperty property = new ComponentTypeProperty(relation, type);\r
+                               result.put(name, property);\r
+                       }\r
+               }\r
+       }\r
+       \r
+       @Override\r
+       public Map<String, ComponentTypeProperty> perform(ReadGraph graph)\r
+                       throws DatabaseException {\r
+               \r
+               THashMap<String, ComponentTypeProperty> result = \r
+                               new THashMap<String, ComponentTypeProperty>();\r
+               \r
+               collect(graph, resource, result);\r
+               for(Resource t : graph.getSupertypes(resource)) collect(graph, t, result);\r
+               \r
+               return result;\r
+               \r
+       }\r
+\r
+}\r