-package org.simantics.document.server.request;\r
-\r
-import gnu.trove.set.hash.THashSet;\r
-\r
-import java.util.Collections;\r
-import java.util.Set;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.request.VariableRead;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.document.base.ontology.DocumentationResource;\r
-\r
-public class NodesRequest2 extends VariableRead<Set<Variable>> {\r
-\r
- public NodesRequest2(Variable var) {\r
- super(var);\r
- }\r
-\r
- @Override\r
- public Set<Variable> perform(ReadGraph graph) throws DatabaseException {\r
- \r
- long s = System.nanoTime();\r
- \r
- DocumentationResource DOC = DocumentationResource.getInstance(graph);\r
-\r
- Resource type = variable.getPossibleType(graph);\r
- if(type == null) return Collections.emptySet();\r
- \r
- if(!graph.isInheritedFrom(type, DOC.Components_Component)) return Collections.emptySet();\r
-\r
- Boolean pathExists = variable.getPossiblePropertyValue(graph, DOC.Properties_pathExists, Bindings.BOOLEAN);\r
- if(pathExists != null && !pathExists) return Collections.emptySet();\r
-\r
- if(graph.isInheritedFrom(type, DOC.Components_PrimitiveComponent)) {\r
- return Collections.singleton(variable);\r
- } else {\r
- Set<Variable> result = new THashSet<Variable>();\r
- for(Variable child : variable.getChildren(graph)) {\r
- Set<Variable> nodes = graph.syncRequest(new NodesRequest2(child));\r
- result.addAll(nodes);\r
- }\r
-\r
- if(DocumentRequest.PROFILE) {\r
- long dura = System.nanoTime()-s;\r
- System.err.println("NodesRequest2 " + System.identityHashCode(this) + " " + variable.getURI(graph) + " in " + 1e-6*dura + "ms.");\r
- }\r
- \r
- return result;\r
- }\r
-\r
- }\r
-\r
+package org.simantics.document.server.request;
+
+import gnu.trove.set.hash.THashSet;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Set;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.AsyncReadRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.VariableChildren;
+import org.simantics.db.layer0.request.VariableRead;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.document.base.ontology.DocumentationResource;
+import org.simantics.utils.threads.logger.ITask;
+import org.simantics.utils.threads.logger.ThreadLogger;
+
+public class NodesRequest2 extends VariableRead<Set<Variable>> {
+
+ public NodesRequest2(Variable var) {
+ super(var);
+ }
+
+ @Override
+ public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
+
+ ITask task = DocumentRequest.PROFILE ? ThreadLogger.task(this) : null;
+
+ DocumentationResource DOC = DocumentationResource.getInstance(graph);
+
+ Resource type = variable.getPossibleType(graph);
+ if(type == null) {
+ if(DocumentRequest.PROFILE) task.finish();
+ return Collections.emptySet();
+ }
+
+ if(!graph.isInheritedFrom(type, DOC.Components_Component)) {
+ if(DocumentRequest.PROFILE) task.finish();
+ return Collections.emptySet();
+ }
+
+ Boolean pathExists = variable.getPossiblePropertyValue(graph, DOC.Properties_pathExists, Bindings.BOOLEAN);
+ if(pathExists != null && !pathExists) {
+ if(DocumentRequest.PROFILE) task.finish();
+ return Collections.emptySet();
+ }
+
+ if(graph.isInheritedFrom(type, DOC.Components_PrimitiveComponent)) {
+ if(DocumentRequest.PROFILE) task.finish();
+ return Collections.singleton(variable);
+ } else {
+ Set<Variable> result = new THashSet<Variable>();
+ Collection<Variable> children = graph.syncRequest(new VariableChildren(variable));
+ graph.syncRequest(new AsyncReadRequest() {
+
+ @Override
+ public void run(AsyncReadGraph graph) throws DatabaseException {
+
+ for(Variable child : children) {
+ graph.asyncRequest(new NodesRequest2(child), new AsyncProcedure<Set<Variable>>() {
+
+ @Override
+ public void execute(AsyncReadGraph graph, Set<Variable> vars) {
+ synchronized(result) {
+ result.addAll(vars);
+ }
+ }
+
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable throwable) {
+ }
+
+ });
+ }
+
+ }
+
+ });
+
+ if(DocumentRequest.PROFILE) task.finish();
+ return result;
+
+ }
+
+ }
+
}
\ No newline at end of file