]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/templates/GraphTemplate.java
0b3b921795029aa2e174969407c1d69d91bc9c46
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / templates / GraphTemplate.java
1 package org.simantics.graph.compiler.internal.templates;
2
3 import gnu.trove.map.hash.TIntIntHashMap;
4 import gnu.trove.map.hash.TObjectIntHashMap;
5
6 import java.util.Arrays;
7 import java.util.Collection;
8
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;
23
24 public class GraphTemplate implements ITemplate {
25         int[] prototypeResources;
26         int[] parameterMapping;
27         int[] statements;
28         Value[] values;
29         
30         public GraphTemplate(GraphStore store, String[] parameters, TransferableGraph1 tg) {
31                 prototypeResources = new int[tg.resourceCount];
32                 Arrays.fill(prototypeResources, -1);
33                 
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);
40                 
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;
46                                 else
47                                         prototypeResources[id.resource] = store.identities.getRoot(def.name);
48                         }
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], 
54                                                         def.name);
55                         }
56                         else
57                                 throw new RuntimeException("Invalid template");
58                 }
59         }
60         
61         @Override
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."));
67                         return;
68                 }
69                 int[] resources = Arrays.copyOf(prototypeResources, prototypeResources.length);
70                 for(int i=0;i<parameterMapping.length;++i)
71                         resources[parameterMapping[i]] = parameters[i];
72                 
73                 IdentityStore identities = store.identities;
74                 for(int i=0;i<resources.length;++i)
75                         if(resources[i] < 0)
76                                 resources[i] = identities.newResource();
77                 
78                 StatementStore statementStore = store.statements;
79                 for(int i=0;i<statements.length;i+=4) {
80                         statementStore.add(
81                                         resources[statements[i]],
82                                         resources[statements[i+1]],
83                                         resources[statements[i+3]]
84                         );
85                 }
86                 
87                 ValueStore valueStore = store.values;
88                 for(Value value : values) {
89                         valueStore.setValue(resources[value.resource], value.value);
90                 }
91         }
92
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);
98                 }
99         }       
100 }