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=48ff1e111b33df5d60f78b86e11f6073cdec816c;hp=ac5986865f10e46ba400b8a1cf7aa0c9f9dedf20;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 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 index ac5986865..48ff1e111 100644 --- 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 @@ -1,91 +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); - } - } -} +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); + } + } +}