]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.graph.compiler.internal.templates;\r
2 \r
3 import gnu.trove.map.hash.TIntIntHashMap;\r
4 \r
5 import java.util.Collection;\r
6 \r
7 import org.simantics.graph.compiler.internal.procedures.Compactify;\r
8 import org.simantics.graph.compiler.internal.procedures.ConvertPreValues;\r
9 import org.simantics.graph.compiler.internal.procedures.DefaultValueTyping;\r
10 import org.simantics.graph.compiler.internal.procedures.MergeEqualResources;\r
11 import org.simantics.graph.compiler.internal.store.LocationStore;\r
12 import org.simantics.graph.query.CompositeGraph;\r
13 import org.simantics.graph.query.Path;\r
14 import org.simantics.graph.query.TransferableGraphConversion;\r
15 import org.simantics.graph.representation.TransferableGraph1;\r
16 import org.simantics.graph.store.GraphStore;\r
17 import org.simantics.ltk.Problem;\r
18 \r
19 public class TemplateDefinition {\r
20         String[] parameters;\r
21         int[] correspondence;\r
22         GraphStore template;\r
23         \r
24         public TemplateDefinition(String[] parameters, int[] correspondence,\r
25                         GraphStore template) {\r
26                 this.parameters = parameters;\r
27                 this.correspondence = correspondence;\r
28                 this.template = template;\r
29         }\r
30 \r
31         public String[] getParameters() {\r
32                 return parameters;\r
33         }\r
34         \r
35         public GraphStore getTemplate() {\r
36                 return template;\r
37         }\r
38 \r
39         public void map(TIntIntHashMap map) {\r
40                 for(int i=0;i<correspondence.length;i+=2)\r
41                         if(map.containsKey(correspondence[i]))\r
42                                 correspondence[i] = map.get(correspondence[i]);\r
43         }\r
44         \r
45         public TransferableGraph1 convert(CompositeGraph graph, GraphStore parent, Collection<Problem> problems) {\r
46                 LocationStore locations = template.getStore(LocationStore.class);\r
47                 for(int i=0;i<correspondence.length;i+=2) {\r
48                         int parentId = correspondence[i];\r
49                         int childId = correspondence[i+1];\r
50                         Path path = parent.identities.idToPath(parentId);\r
51                         if(path == null) {\r
52                                 problems.add(\r
53                                                 new Problem(locations.getLocation(childId), \r
54                                                 "Template can refer only to resources that have URIs."));\r
55                         }\r
56                         else\r
57                                 template.identities.definePath(path, childId);\r
58                 }\r
59                 new MergeEqualResources(graph.getPaths(), template).run();\r
60                 new Compactify(template).run();\r
61                 new DefaultValueTyping(graph.getPaths(), template).run();       \r
62                 \r
63                 graph.addFragment(template);\r
64                 new ConvertPreValues(graph, template, problems).run();\r
65                 TransferableGraph1 transferableGraph = TransferableGraphConversion.convert(template);\r
66                 graph.undoAddFragment();\r
67                 \r
68                 //transferableGraph.print();\r
69                 \r
70                 return transferableGraph;\r
71         }\r
72 }\r