-package org.simantics.graph.compiler.internal.procedures;\r
-\r
-import gnu.trove.procedure.TIntObjectProcedure;\r
-import gnu.trove.set.hash.THashSet;\r
-\r
-import java.util.Collection;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.binding.mutable.Variant;\r
-import org.simantics.graph.compiler.internal.store.LocationStore;\r
-import org.simantics.graph.compiler.internal.templates.TemplateDefinition;\r
-import org.simantics.graph.compiler.internal.templates.TemplateDefinitionStore;\r
-import org.simantics.graph.query.CompositeGraph;\r
-import org.simantics.graph.query.Paths;\r
-import org.simantics.graph.representation.TransferableGraph1;\r
-import org.simantics.graph.store.GraphStore;\r
-import org.simantics.ltk.Location;\r
-import org.simantics.ltk.Problem;\r
-\r
-public class CreateTemplates implements Runnable {\r
- \r
- CompositeGraph graph;\r
- GraphStore store;\r
- Collection<Problem> problems;\r
- \r
- int HasTemplate;\r
- int HasTemplateParameters;\r
- int InstanceOf;\r
- int StringArray;\r
- int Graph;\r
- \r
- public CreateTemplates(CompositeGraph graph, GraphStore store,\r
- Collection<Problem> problems) {\r
- this.graph = graph;\r
- this.store = store;\r
- this.problems = problems;\r
- }\r
-\r
- @Override\r
- public void run() {\r
- TemplateDefinitionStore templateStore = store.getStore(TemplateDefinitionStore.class);\r
- if(templateStore == null || templateStore.isEmpty())\r
- return;\r
- \r
- Paths paths = graph.getPaths();\r
- HasTemplate = store.identities.createPathToId(paths.HasTemplate);\r
- HasTemplateParameters = store.identities.createPathToId(paths.HasTemplateParameters);\r
- InstanceOf = store.identities.createPathToId(paths.InstanceOf);\r
- StringArray = store.identities.createPathToId(paths.StringArray);\r
- Graph = store.identities.createPathToId(paths.Graph);\r
- \r
- templateStore.forTemplateDefinitions(\r
- new TIntObjectProcedure<TemplateDefinition>() {\r
- @Override\r
- public boolean execute(int templateId, TemplateDefinition template) {\r
- if(validateTemplate(templateId, template))\r
- writeTemplate(templateId, template);\r
- return true;\r
- } \r
- });\r
- } \r
- \r
- private boolean validateTemplate(int templateId, TemplateDefinition template) {\r
- THashSet<String> parameters = new THashSet<String>();\r
- parameters.add("");\r
- for(String parameter : template.getParameters())\r
- parameters.add(parameter);\r
- boolean isValid = true;\r
- GraphStore templateStore = template.getTemplate();\r
- for(String root : templateStore.identities.getRoots())\r
- if(!parameters.contains(root)) {\r
- LocationStore templateLocations = templateStore.getStore(LocationStore.class);\r
- Location location = templateLocations == null ? null\r
- : templateLocations.getLocation(templateStore.identities.getRoot(root));\r
- if(location == null) {\r
- LocationStore locations = store.getStore(LocationStore.class);\r
- location = locations == null ? null \r
- : locations.getLocation(templateId); \r
- }\r
- problems.add(new Problem(\r
- location, \r
- "Graph template does not have parameter %" + root + "."\r
- ));\r
- isValid = false;\r
- }\r
- return isValid;\r
- } \r
- \r
- private void writeTemplate(int templateId,\r
- TemplateDefinition template) {\r
- TransferableGraph1 tg = template.convert(graph, store, problems);\r
-\r
- int templateValue = store.identities.newResource();\r
- store.values.setValue(templateValue, new Variant(TransferableGraph1.BINDING, tg));\r
- store.statements.add(templateId, HasTemplate, templateValue);\r
- store.statements.add(templateValue, InstanceOf, Graph);\r
-\r
- int parameters = store.identities.newResource();\r
- store.values.setValue(parameters, \r
- new Variant(Bindings.STRING_ARRAY, template.getParameters()));\r
- store.statements.add(templateId, HasTemplateParameters, parameters);\r
- store.statements.add(parameters, InstanceOf, StringArray);\r
- }\r
-}\r
+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<Problem> problems;
+
+ int HasTemplate;
+ int HasTemplateParameters;
+ int InstanceOf;
+ int StringArray;
+ int Graph;
+
+ public CreateTemplates(CompositeGraph graph, GraphStore store,
+ Collection<Problem> 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<TemplateDefinition>() {
+ @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<String> parameters = new THashSet<String>();
+ 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);
+ }
+}