1 package org.simantics.graph.compiler.internal.templates;
3 import gnu.trove.map.hash.TIntIntHashMap;
5 import java.util.Collection;
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;
19 public class TemplateDefinition {
24 public TemplateDefinition(String[] parameters, int[] correspondence,
25 GraphStore template) {
26 this.parameters = parameters;
27 this.correspondence = correspondence;
28 this.template = template;
31 public String[] getParameters() {
35 public GraphStore getTemplate() {
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]);
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);
53 new Problem(locations.getLocation(childId),
54 "Template can refer only to resources that have URIs."));
57 template.identities.definePath(path, childId);
59 new MergeEqualResources(graph.getPaths(), template).run();
60 new Compactify(template).run();
61 new DefaultValueTyping(graph.getPaths(), template).run();
63 graph.addFragment(template);
64 new ConvertPreValues(graph, template, problems).run();
65 TransferableGraph1 transferableGraph = TransferableGraphConversion.convert(template);
66 graph.undoAddFragment();
68 //transferableGraph.print();
70 return transferableGraph;