1 package org.simantics.graph.compiler.internal.templates;
\r
3 import gnu.trove.map.hash.TIntIntHashMap;
\r
5 import java.util.Collection;
\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
19 public class TemplateDefinition {
\r
20 String[] parameters;
\r
21 int[] correspondence;
\r
22 GraphStore template;
\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
31 public String[] getParameters() {
\r
35 public GraphStore getTemplate() {
\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
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
53 new Problem(locations.getLocation(childId),
\r
54 "Template can refer only to resources that have URIs."));
\r
57 template.identities.definePath(path, childId);
\r
59 new MergeEqualResources(graph.getPaths(), template).run();
\r
60 new Compactify(template).run();
\r
61 new DefaultValueTyping(graph.getPaths(), template).run();
\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
68 //transferableGraph.print();
\r
70 return transferableGraph;
\r