--- /dev/null
+package org.simantics.graph.compiler.internal.procedures;\r
+\r
+import gnu.trove.map.hash.TIntObjectHashMap;\r
+\r
+import java.util.Arrays;\r
+import java.util.Collection;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.graph.compiler.ExternalFileLoader;\r
+import org.simantics.graph.compiler.internal.store.LocationStore;\r
+import org.simantics.graph.compiler.internal.templates.BuiltinTemplates;\r
+import org.simantics.graph.compiler.internal.templates.GraphTemplate;\r
+import org.simantics.graph.compiler.internal.templates.ITemplate;\r
+import org.simantics.graph.compiler.internal.templates.TemplateInstanceStore;\r
+import org.simantics.graph.query.IGraph;\r
+import org.simantics.graph.query.Path;\r
+import org.simantics.graph.query.Paths;\r
+import org.simantics.graph.query.Res;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.graph.store.GraphStore;\r
+import org.simantics.ltk.Location;\r
+import org.simantics.ltk.Problem;\r
+\r
+public class ApplyTemplates implements Runnable {\r
+ IGraph graph;\r
+ GraphStore store;\r
+ Collection<Problem> problems;\r
+ ExternalFileLoader fileLoader; \r
+ BuiltinTemplates builtinTemplates;\r
+ \r
+ public ApplyTemplates(IGraph graph, GraphStore store,\r
+ Collection<Problem> problems, ExternalFileLoader fileLoader) {\r
+ this.graph = graph;\r
+ this.store = store;\r
+ this.problems = problems; \r
+ this.fileLoader = fileLoader;\r
+ this.builtinTemplates = new BuiltinTemplates(graph.getPaths());\r
+ }\r
+\r
+ TIntObjectHashMap<ITemplate> cache = new TIntObjectHashMap<ITemplate>(); \r
+ \r
+ ITemplate getTemplate(int id) {\r
+ ITemplate template = cache.get(id);\r
+ if(template == null) {\r
+ template = createTemplate(id);\r
+ cache.put(id, template);\r
+ }\r
+ return template;\r
+ }\r
+ \r
+ ITemplate createTemplate(int id) {\r
+ Res res = store.idToRes(id);\r
+ if(res instanceof Path) {\r
+ ITemplate template = builtinTemplates.TEMPLATES.get((Path)res);\r
+ if(template != null)\r
+ return template;\r
+ }\r
+\r
+ Paths paths = graph.getPaths(); \r
+ try { \r
+ Res template = graph.singleRawObject(res, paths.HasTemplate);\r
+ TransferableGraph1 tg = (TransferableGraph1)\r
+ graph.getValue(template, TransferableGraph1.BINDING);\r
+ \r
+ Res templateParameters = graph.singleRawObject(res, paths.HasTemplateParameters);\r
+ String[] parameters = (String[])graph.getValue(templateParameters).getValue(Bindings.STRING_ARRAY);\r
+ \r
+ return new GraphTemplate(store, parameters, tg);\r
+ } catch(Exception e) {\r
+ //e.printStackTrace();\r
+ Location location = store.getStore(LocationStore.class)\r
+ .getLocation(id);\r
+ problems.add(new Problem(\r
+ location, e.getMessage()));\r
+ return null;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void run() {\r
+ TemplateInstanceStore templateStore = store.getStore(TemplateInstanceStore.class);\r
+ if(templateStore == null)\r
+ return;\r
+ \r
+ for(int[] inst : templateStore.getTemplateInstances()) {\r
+ ITemplate template = getTemplate(inst[0]);\r
+ if(template != null)\r
+ template.apply(graph, store, Arrays.copyOfRange(inst, 1, inst.length), fileLoader, problems);\r
+ }\r
+ } \r
+}\r