1 package org.simantics.graph.compiler.internal.procedures;
3 import gnu.trove.map.hash.TIntObjectHashMap;
5 import java.util.Arrays;
6 import java.util.Collection;
8 import org.simantics.databoard.Bindings;
9 import org.simantics.graph.compiler.ExternalFileLoader;
10 import org.simantics.graph.compiler.internal.store.LocationStore;
11 import org.simantics.graph.compiler.internal.templates.BuiltinTemplates;
12 import org.simantics.graph.compiler.internal.templates.GraphTemplate;
13 import org.simantics.graph.compiler.internal.templates.ITemplate;
14 import org.simantics.graph.compiler.internal.templates.TemplateInstanceStore;
15 import org.simantics.graph.query.IGraph;
16 import org.simantics.graph.query.Path;
17 import org.simantics.graph.query.Paths;
18 import org.simantics.graph.query.Res;
19 import org.simantics.graph.representation.TransferableGraph1;
20 import org.simantics.graph.store.GraphStore;
21 import org.simantics.ltk.Location;
22 import org.simantics.ltk.Problem;
24 public class ApplyTemplates implements Runnable {
27 Collection<Problem> problems;
28 ExternalFileLoader fileLoader;
29 BuiltinTemplates builtinTemplates;
31 public ApplyTemplates(IGraph graph, GraphStore store,
32 Collection<Problem> problems, ExternalFileLoader fileLoader) {
35 this.problems = problems;
36 this.fileLoader = fileLoader;
37 this.builtinTemplates = new BuiltinTemplates(graph.getPaths());
40 TIntObjectHashMap<ITemplate> cache = new TIntObjectHashMap<ITemplate>();
42 ITemplate getTemplate(int id) {
43 ITemplate template = cache.get(id);
44 if(template == null) {
45 template = createTemplate(id);
46 cache.put(id, template);
51 ITemplate createTemplate(int id) {
52 Res res = store.idToRes(id);
53 if(res instanceof Path) {
54 ITemplate template = builtinTemplates.TEMPLATES.get((Path)res);
59 Paths paths = graph.getPaths();
61 Res template = graph.singleRawObject(res, paths.HasTemplate);
62 TransferableGraph1 tg = (TransferableGraph1)
63 graph.getValue(template, TransferableGraph1.BINDING);
65 Res templateParameters = graph.singleRawObject(res, paths.HasTemplateParameters);
66 String[] parameters = (String[])graph.getValue(templateParameters).getValue(Bindings.STRING_ARRAY);
68 return new GraphTemplate(store, parameters, tg);
69 } catch(Exception e) {
70 //e.printStackTrace();
71 Location location = store.getStore(LocationStore.class)
73 problems.add(new Problem(
74 location, e.getMessage()));
81 TemplateInstanceStore templateStore = store.getStore(TemplateInstanceStore.class);
82 if(templateStore == null)
85 for(int[] inst : templateStore.getTemplateInstances()) {
86 ITemplate template = getTemplate(inst[0]);
88 template.apply(graph, store, Arrays.copyOfRange(inst, 1, inst.length), fileLoader, problems);