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