]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/CreateTemplates.java
3b3e217016a9855c7f7abdc4f0ee7d0db89367c4
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / procedures / CreateTemplates.java
1 package org.simantics.graph.compiler.internal.procedures;
2
3 import gnu.trove.procedure.TIntObjectProcedure;
4 import gnu.trove.set.hash.THashSet;
5
6 import java.util.Collection;
7
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;
19
20 public class CreateTemplates implements Runnable {
21         
22         CompositeGraph graph;
23         GraphStore store;
24         Collection<Problem> problems;
25         
26         int HasTemplate;
27         int HasTemplateParameters;
28         int InstanceOf;
29         int StringArray;
30         int Graph;
31         
32         public CreateTemplates(CompositeGraph graph, GraphStore store,
33                         Collection<Problem> problems) {
34                 this.graph = graph;
35                 this.store = store;
36                 this.problems = problems;
37         }
38
39         @Override
40         public void run() {
41                 TemplateDefinitionStore templateStore = store.getStore(TemplateDefinitionStore.class);
42                 if(templateStore == null || templateStore.isEmpty())
43                         return;
44                 
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);
51                                 
52                 templateStore.forTemplateDefinitions(
53                         new TIntObjectProcedure<TemplateDefinition>() {
54                                 @Override
55                                 public boolean execute(int templateId, TemplateDefinition template) {
56                                         if(validateTemplate(templateId, template))
57                                                 writeTemplate(templateId, template);
58                                         return true;
59                                 }                                                       
60                         });
61         }       
62         
63         private boolean validateTemplate(int templateId, TemplateDefinition template) {
64                 THashSet<String> parameters = new THashSet<String>();
65                 parameters.add("");
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);                            
79                                 }
80                                 problems.add(new Problem(
81                                                 location, 
82                                                 "Graph template does not have parameter %" + root + "."
83                                         ));
84                                 isValid = false;
85                         }
86                 return isValid;
87         }       
88         
89         private void writeTemplate(int templateId,
90                         TemplateDefinition template) {
91             TransferableGraph1 tg = template.convert(graph, store, problems);
92
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);
97
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);
103         }
104 }