X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2Finternal%2Fprocedures%2FApplyTemplates.java;fp=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2Finternal%2Fprocedures%2FApplyTemplates.java;h=ac5986865f10e46ba400b8a1cf7aa0c9f9dedf20;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/ApplyTemplates.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/ApplyTemplates.java new file mode 100644 index 000000000..ac5986865 --- /dev/null +++ b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/ApplyTemplates.java @@ -0,0 +1,91 @@ +package org.simantics.graph.compiler.internal.procedures; + +import gnu.trove.map.hash.TIntObjectHashMap; + +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.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 org.simantics.ltk.Location; +import org.simantics.ltk.Problem; + +public class ApplyTemplates implements Runnable { + IGraph graph; + GraphStore store; + Collection problems; + ExternalFileLoader fileLoader; + BuiltinTemplates builtinTemplates; + + public ApplyTemplates(IGraph graph, GraphStore store, + Collection problems, ExternalFileLoader fileLoader) { + this.graph = graph; + this.store = store; + this.problems = problems; + this.fileLoader = fileLoader; + this.builtinTemplates = new BuiltinTemplates(graph.getPaths()); + } + + TIntObjectHashMap cache = new TIntObjectHashMap(); + + 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); + } + } +}