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