]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/templates/TemplateDefinition.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / templates / TemplateDefinition.java
diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/templates/TemplateDefinition.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/templates/TemplateDefinition.java
new file mode 100644 (file)
index 0000000..3c79608
--- /dev/null
@@ -0,0 +1,72 @@
+package org.simantics.graph.compiler.internal.templates;\r
+\r
+import gnu.trove.map.hash.TIntIntHashMap;\r
+\r
+import java.util.Collection;\r
+\r
+import org.simantics.graph.compiler.internal.procedures.Compactify;\r
+import org.simantics.graph.compiler.internal.procedures.ConvertPreValues;\r
+import org.simantics.graph.compiler.internal.procedures.DefaultValueTyping;\r
+import org.simantics.graph.compiler.internal.procedures.MergeEqualResources;\r
+import org.simantics.graph.compiler.internal.store.LocationStore;\r
+import org.simantics.graph.query.CompositeGraph;\r
+import org.simantics.graph.query.Path;\r
+import org.simantics.graph.query.TransferableGraphConversion;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.graph.store.GraphStore;\r
+import org.simantics.ltk.Problem;\r
+\r
+public class TemplateDefinition {\r
+       String[] parameters;\r
+       int[] correspondence;\r
+       GraphStore template;\r
+       \r
+       public TemplateDefinition(String[] parameters, int[] correspondence,\r
+                       GraphStore template) {\r
+               this.parameters = parameters;\r
+               this.correspondence = correspondence;\r
+               this.template = template;\r
+       }\r
+\r
+       public String[] getParameters() {\r
+               return parameters;\r
+       }\r
+       \r
+       public GraphStore getTemplate() {\r
+               return template;\r
+       }\r
+\r
+       public void map(TIntIntHashMap map) {\r
+               for(int i=0;i<correspondence.length;i+=2)\r
+                       if(map.containsKey(correspondence[i]))\r
+                               correspondence[i] = map.get(correspondence[i]);\r
+       }\r
+       \r
+       public TransferableGraph1 convert(CompositeGraph graph, GraphStore parent, Collection<Problem> problems) {\r
+               LocationStore locations = template.getStore(LocationStore.class);\r
+               for(int i=0;i<correspondence.length;i+=2) {\r
+                       int parentId = correspondence[i];\r
+                       int childId = correspondence[i+1];\r
+                       Path path = parent.identities.idToPath(parentId);\r
+                       if(path == null) {\r
+                               problems.add(\r
+                                               new Problem(locations.getLocation(childId), \r
+                                               "Template can refer only to resources that have URIs."));\r
+                       }\r
+                       else\r
+                               template.identities.definePath(path, childId);\r
+               }\r
+               new MergeEqualResources(graph.getPaths(), template).run();\r
+               new Compactify(template).run();\r
+               new DefaultValueTyping(graph.getPaths(), template).run();       \r
+               \r
+               graph.addFragment(template);\r
+               new ConvertPreValues(graph, template, problems).run();\r
+               TransferableGraph1 transferableGraph = TransferableGraphConversion.convert(template);\r
+               graph.undoAddFragment();\r
+               \r
+               //transferableGraph.print();\r
+               \r
+               return transferableGraph;\r
+       }\r
+}\r