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