]> gerrit.simantics Code Review - simantics/platform.git/blob
48ff1e111b33df5d60f78b86e11f6073cdec816c
[simantics/platform.git] /
1 package org.simantics.graph.compiler.internal.procedures;
2
3 import gnu.trove.map.hash.TIntObjectHashMap;
4
5 import java.util.Arrays;
6 import java.util.Collection;
7
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;
23
24 public class ApplyTemplates implements Runnable {
25         IGraph graph;
26         GraphStore store;
27         Collection<Problem> problems;
28         ExternalFileLoader fileLoader;  
29         BuiltinTemplates builtinTemplates;
30         
31         public ApplyTemplates(IGraph graph, GraphStore store,
32                         Collection<Problem> problems, ExternalFileLoader fileLoader) {
33                 this.graph = graph;
34                 this.store = store;
35                 this.problems = problems;       
36                 this.fileLoader = fileLoader;
37                 this.builtinTemplates = new BuiltinTemplates(graph.getPaths());
38         }
39
40         TIntObjectHashMap<ITemplate> cache = new TIntObjectHashMap<ITemplate>();        
41         
42         ITemplate getTemplate(int id) {
43                 ITemplate template = cache.get(id);
44                 if(template == null) {
45                         template = createTemplate(id);
46                         cache.put(id, template);
47                 }
48                 return template;
49         }
50         
51         ITemplate createTemplate(int id) {
52                 Res res = store.idToRes(id);
53                 if(res instanceof Path) {
54                         ITemplate template = builtinTemplates.TEMPLATES.get((Path)res);
55                         if(template != null)
56                                 return template;
57                 }
58
59                 Paths paths = graph.getPaths();                 
60                 try {                   
61                         Res template = graph.singleRawObject(res, paths.HasTemplate);
62                         TransferableGraph1 tg = (TransferableGraph1)
63                                 graph.getValue(template, TransferableGraph1.BINDING);
64                         
65                         Res templateParameters = graph.singleRawObject(res, paths.HasTemplateParameters);
66                         String[] parameters = (String[])graph.getValue(templateParameters).getValue(Bindings.STRING_ARRAY);
67                         
68                         return new GraphTemplate(store, parameters, tg);
69                 } catch(Exception e) {
70                         //e.printStackTrace();
71                         Location location = store.getStore(LocationStore.class)
72                                 .getLocation(id);
73                         problems.add(new Problem(
74                                         location, e.getMessage()));
75                         return null;
76                 }
77         }
78
79         @Override
80         public void run() {
81                 TemplateInstanceStore templateStore = store.getStore(TemplateInstanceStore.class);
82                 if(templateStore == null)
83                         return;
84                 
85                 for(int[] inst : templateStore.getTemplateInstances()) {
86                         ITemplate template = getTemplate(inst[0]);
87                         if(template != null)
88                                 template.apply(graph, store, Arrays.copyOfRange(inst, 1, inst.length), fileLoader, problems);
89                 }
90         }       
91 }