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%2FCreateTemplates.java;fp=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2Finternal%2Fprocedures%2FCreateTemplates.java;h=3b3e217016a9855c7f7abdc4f0ee7d0db89367c4;hp=6a452a9c520d2ec551f7eb7983529c56a23d8b60;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/CreateTemplates.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/CreateTemplates.java index 6a452a9c5..3b3e21701 100644 --- a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/CreateTemplates.java +++ b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/CreateTemplates.java @@ -1,104 +1,104 @@ -package org.simantics.graph.compiler.internal.procedures; - -import gnu.trove.procedure.TIntObjectProcedure; -import gnu.trove.set.hash.THashSet; - -import java.util.Collection; - -import org.simantics.databoard.Bindings; -import org.simantics.databoard.binding.mutable.Variant; -import org.simantics.graph.compiler.internal.store.LocationStore; -import org.simantics.graph.compiler.internal.templates.TemplateDefinition; -import org.simantics.graph.compiler.internal.templates.TemplateDefinitionStore; -import org.simantics.graph.query.CompositeGraph; -import org.simantics.graph.query.Paths; -import org.simantics.graph.representation.TransferableGraph1; -import org.simantics.graph.store.GraphStore; -import org.simantics.ltk.Location; -import org.simantics.ltk.Problem; - -public class CreateTemplates implements Runnable { - - CompositeGraph graph; - GraphStore store; - Collection problems; - - int HasTemplate; - int HasTemplateParameters; - int InstanceOf; - int StringArray; - int Graph; - - public CreateTemplates(CompositeGraph graph, GraphStore store, - Collection problems) { - this.graph = graph; - this.store = store; - this.problems = problems; - } - - @Override - public void run() { - TemplateDefinitionStore templateStore = store.getStore(TemplateDefinitionStore.class); - if(templateStore == null || templateStore.isEmpty()) - return; - - Paths paths = graph.getPaths(); - HasTemplate = store.identities.createPathToId(paths.HasTemplate); - HasTemplateParameters = store.identities.createPathToId(paths.HasTemplateParameters); - InstanceOf = store.identities.createPathToId(paths.InstanceOf); - StringArray = store.identities.createPathToId(paths.StringArray); - Graph = store.identities.createPathToId(paths.Graph); - - templateStore.forTemplateDefinitions( - new TIntObjectProcedure() { - @Override - public boolean execute(int templateId, TemplateDefinition template) { - if(validateTemplate(templateId, template)) - writeTemplate(templateId, template); - return true; - } - }); - } - - private boolean validateTemplate(int templateId, TemplateDefinition template) { - THashSet parameters = new THashSet(); - parameters.add(""); - for(String parameter : template.getParameters()) - parameters.add(parameter); - boolean isValid = true; - GraphStore templateStore = template.getTemplate(); - for(String root : templateStore.identities.getRoots()) - if(!parameters.contains(root)) { - LocationStore templateLocations = templateStore.getStore(LocationStore.class); - Location location = templateLocations == null ? null - : templateLocations.getLocation(templateStore.identities.getRoot(root)); - if(location == null) { - LocationStore locations = store.getStore(LocationStore.class); - location = locations == null ? null - : locations.getLocation(templateId); - } - problems.add(new Problem( - location, - "Graph template does not have parameter %" + root + "." - )); - isValid = false; - } - return isValid; - } - - private void writeTemplate(int templateId, - TemplateDefinition template) { - TransferableGraph1 tg = template.convert(graph, store, problems); - - int templateValue = store.identities.newResource(); - store.values.setValue(templateValue, new Variant(TransferableGraph1.BINDING, tg)); - store.statements.add(templateId, HasTemplate, templateValue); - store.statements.add(templateValue, InstanceOf, Graph); - - int parameters = store.identities.newResource(); - store.values.setValue(parameters, - new Variant(Bindings.STRING_ARRAY, template.getParameters())); - store.statements.add(templateId, HasTemplateParameters, parameters); - store.statements.add(parameters, InstanceOf, StringArray); - } -} +package org.simantics.graph.compiler.internal.procedures; + +import gnu.trove.procedure.TIntObjectProcedure; +import gnu.trove.set.hash.THashSet; + +import java.util.Collection; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.graph.compiler.internal.store.LocationStore; +import org.simantics.graph.compiler.internal.templates.TemplateDefinition; +import org.simantics.graph.compiler.internal.templates.TemplateDefinitionStore; +import org.simantics.graph.query.CompositeGraph; +import org.simantics.graph.query.Paths; +import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.graph.store.GraphStore; +import org.simantics.ltk.Location; +import org.simantics.ltk.Problem; + +public class CreateTemplates implements Runnable { + + CompositeGraph graph; + GraphStore store; + Collection problems; + + int HasTemplate; + int HasTemplateParameters; + int InstanceOf; + int StringArray; + int Graph; + + public CreateTemplates(CompositeGraph graph, GraphStore store, + Collection problems) { + this.graph = graph; + this.store = store; + this.problems = problems; + } + + @Override + public void run() { + TemplateDefinitionStore templateStore = store.getStore(TemplateDefinitionStore.class); + if(templateStore == null || templateStore.isEmpty()) + return; + + Paths paths = graph.getPaths(); + HasTemplate = store.identities.createPathToId(paths.HasTemplate); + HasTemplateParameters = store.identities.createPathToId(paths.HasTemplateParameters); + InstanceOf = store.identities.createPathToId(paths.InstanceOf); + StringArray = store.identities.createPathToId(paths.StringArray); + Graph = store.identities.createPathToId(paths.Graph); + + templateStore.forTemplateDefinitions( + new TIntObjectProcedure() { + @Override + public boolean execute(int templateId, TemplateDefinition template) { + if(validateTemplate(templateId, template)) + writeTemplate(templateId, template); + return true; + } + }); + } + + private boolean validateTemplate(int templateId, TemplateDefinition template) { + THashSet parameters = new THashSet(); + parameters.add(""); + for(String parameter : template.getParameters()) + parameters.add(parameter); + boolean isValid = true; + GraphStore templateStore = template.getTemplate(); + for(String root : templateStore.identities.getRoots()) + if(!parameters.contains(root)) { + LocationStore templateLocations = templateStore.getStore(LocationStore.class); + Location location = templateLocations == null ? null + : templateLocations.getLocation(templateStore.identities.getRoot(root)); + if(location == null) { + LocationStore locations = store.getStore(LocationStore.class); + location = locations == null ? null + : locations.getLocation(templateId); + } + problems.add(new Problem( + location, + "Graph template does not have parameter %" + root + "." + )); + isValid = false; + } + return isValid; + } + + private void writeTemplate(int templateId, + TemplateDefinition template) { + TransferableGraph1 tg = template.convert(graph, store, problems); + + int templateValue = store.identities.newResource(); + store.values.setValue(templateValue, new Variant(TransferableGraph1.BINDING, tg)); + store.statements.add(templateId, HasTemplate, templateValue); + store.statements.add(templateValue, InstanceOf, Graph); + + int parameters = store.identities.newResource(); + store.values.setValue(parameters, + new Variant(Bindings.STRING_ARRAY, template.getParameters())); + store.statements.add(templateId, HasTemplateParameters, parameters); + store.statements.add(parameters, InstanceOf, StringArray); + } +}