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=6a452a9c520d2ec551f7eb7983529c56a23d8b60;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 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 new file mode 100644 index 000000000..6a452a9c5 --- /dev/null +++ b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/CreateTemplates.java @@ -0,0 +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); + } +}