]> gerrit.simantics Code Review - simantics/platform.git/blob
1fedb881810f6fee32a9a82acb10600d658f7f64
[simantics/platform.git] /
1 package org.simantics.graph.compiler.internal.templates;
2
3 import gnu.trove.map.hash.TIntIntHashMap;
4
5 import java.util.Collection;
6
7 import org.simantics.graph.compiler.internal.procedures.Compactify;
8 import org.simantics.graph.compiler.internal.procedures.ConvertPreValues;
9 import org.simantics.graph.compiler.internal.procedures.DefaultValueTyping;
10 import org.simantics.graph.compiler.internal.procedures.MergeEqualResources;
11 import org.simantics.graph.compiler.internal.store.LocationStore;
12 import org.simantics.graph.query.CompositeGraph;
13 import org.simantics.graph.query.Path;
14 import org.simantics.graph.query.TransferableGraphConversion;
15 import org.simantics.graph.representation.TransferableGraph1;
16 import org.simantics.graph.store.GraphStore;
17 import org.simantics.ltk.Problem;
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 }