1 package org.simantics.graph.compiler.internal.templates;
3 import gnu.trove.map.hash.TIntIntHashMap;
4 import gnu.trove.map.hash.TObjectIntHashMap;
6 import java.util.Arrays;
7 import java.util.Collection;
9 import org.simantics.graph.compiler.ExternalFileLoader;
10 import org.simantics.graph.compiler.internal.store.LocationStore;
11 import org.simantics.graph.query.IGraph;
12 import org.simantics.graph.representation.External;
13 import org.simantics.graph.representation.Identity;
14 import org.simantics.graph.representation.Root;
15 import org.simantics.graph.representation.TransferableGraph1;
16 import org.simantics.graph.representation.Value;
17 import org.simantics.graph.store.GraphStore;
18 import org.simantics.graph.store.IdentityStore;
19 import org.simantics.graph.store.StatementStore;
20 import org.simantics.graph.store.ValueStore;
21 import org.simantics.ltk.Location;
22 import org.simantics.ltk.Problem;
24 public class GraphTemplate implements ITemplate {
25 int[] prototypeResources;
26 int[] parameterMapping;
30 public GraphTemplate(GraphStore store, String[] parameters, TransferableGraph1 tg) {
31 prototypeResources = new int[tg.resourceCount];
32 Arrays.fill(prototypeResources, -1);
34 this.values = tg.values;
35 this.statements = tg.statements;
36 this.parameterMapping = new int[parameters.length];
37 TObjectIntHashMap<String> pmap = new TObjectIntHashMap<String>();
38 for(int i=0;i<parameters.length;++i)
39 pmap.put(parameters[i], i);
41 for(Identity id : tg.identities) {
42 if(id.definition instanceof Root) {
43 Root def = (Root)id.definition;
44 if(pmap.containsKey(def.name))
45 parameterMapping[pmap.get(def.name)] = id.resource;
47 prototypeResources[id.resource] = store.identities.getRoot(def.name);
49 else if(id.definition instanceof External) {
50 External def = (External)id.definition;
51 prototypeResources[id.resource] =
52 store.identities.getChild(
53 prototypeResources[def.parent],
57 throw new RuntimeException("Invalid template");
62 public void apply(IGraph graph, GraphStore store, int[] parameters, ExternalFileLoader fileLoader, Collection<Problem> problems) {
63 if(parameters.length != parameterMapping.length) {
64 Location location = store.getStore(LocationStore.class)
65 .getLocation(parameters[0]);
66 problems.add(new Problem(location, "A template applied with wrong number of parameters."));
69 int[] resources = Arrays.copyOf(prototypeResources, prototypeResources.length);
70 for(int i=0;i<parameterMapping.length;++i)
71 resources[parameterMapping[i]] = parameters[i];
73 IdentityStore identities = store.identities;
74 for(int i=0;i<resources.length;++i)
76 resources[i] = identities.newResource();
78 StatementStore statementStore = store.statements;
79 for(int i=0;i<statements.length;i+=4) {
81 resources[statements[i]],
82 resources[statements[i+1]],
83 resources[statements[i+3]]
87 ValueStore valueStore = store.values;
88 for(Value value : values) {
89 valueStore.setValue(resources[value.resource], value.value);
93 public void map(TIntIntHashMap map) {
94 for(int i=0;i<prototypeResources.length;++i) {
95 int temp = prototypeResources[i];
96 if(temp >= 0 && map.contains(temp))
97 prototypeResources[i] = map.get(temp);