1 package org.simantics.graph.compiler.internal.procedures;
3 import gnu.trove.procedure.TIntObjectProcedure;
4 import gnu.trove.set.hash.THashSet;
6 import java.util.Collection;
8 import org.simantics.databoard.Bindings;
9 import org.simantics.databoard.binding.mutable.Variant;
10 import org.simantics.graph.compiler.internal.store.LocationStore;
11 import org.simantics.graph.compiler.internal.templates.TemplateDefinition;
12 import org.simantics.graph.compiler.internal.templates.TemplateDefinitionStore;
13 import org.simantics.graph.query.CompositeGraph;
14 import org.simantics.graph.query.Paths;
15 import org.simantics.graph.representation.TransferableGraph1;
16 import org.simantics.graph.store.GraphStore;
17 import org.simantics.ltk.Location;
18 import org.simantics.ltk.Problem;
20 public class CreateTemplates implements Runnable {
24 Collection<Problem> problems;
27 int HasTemplateParameters;
32 public CreateTemplates(CompositeGraph graph, GraphStore store,
33 Collection<Problem> problems) {
36 this.problems = problems;
41 TemplateDefinitionStore templateStore = store.getStore(TemplateDefinitionStore.class);
42 if(templateStore == null || templateStore.isEmpty())
45 Paths paths = graph.getPaths();
46 HasTemplate = store.identities.createPathToId(paths.HasTemplate);
47 HasTemplateParameters = store.identities.createPathToId(paths.HasTemplateParameters);
48 InstanceOf = store.identities.createPathToId(paths.InstanceOf);
49 StringArray = store.identities.createPathToId(paths.StringArray);
50 Graph = store.identities.createPathToId(paths.Graph);
52 templateStore.forTemplateDefinitions(
53 new TIntObjectProcedure<TemplateDefinition>() {
55 public boolean execute(int templateId, TemplateDefinition template) {
56 if(validateTemplate(templateId, template))
57 writeTemplate(templateId, template);
63 private boolean validateTemplate(int templateId, TemplateDefinition template) {
64 THashSet<String> parameters = new THashSet<String>();
66 for(String parameter : template.getParameters())
67 parameters.add(parameter);
68 boolean isValid = true;
69 GraphStore templateStore = template.getTemplate();
70 for(String root : templateStore.identities.getRoots())
71 if(!parameters.contains(root)) {
72 LocationStore templateLocations = templateStore.getStore(LocationStore.class);
73 Location location = templateLocations == null ? null
74 : templateLocations.getLocation(templateStore.identities.getRoot(root));
75 if(location == null) {
76 LocationStore locations = store.getStore(LocationStore.class);
77 location = locations == null ? null
78 : locations.getLocation(templateId);
80 problems.add(new Problem(
82 "Graph template does not have parameter %" + root + "."
89 private void writeTemplate(int templateId,
90 TemplateDefinition template) {
91 TransferableGraph1 tg = template.convert(graph, store, problems);
93 int templateValue = store.identities.newResource();
94 store.values.setValue(templateValue, new Variant(TransferableGraph1.BINDING, tg));
95 store.statements.add(templateId, HasTemplate, templateValue);
96 store.statements.add(templateValue, InstanceOf, Graph);
98 int parameters = store.identities.newResource();
99 store.values.setValue(parameters,
100 new Variant(Bindings.STRING_ARRAY, template.getParameters()));
101 store.statements.add(templateId, HasTemplateParameters, parameters);
102 store.statements.add(parameters, InstanceOf, StringArray);