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 {
-
- DocumentationResource DOC = DocumentationResource.getInstance(graph);
-
- Resource type = variable.getPossibleType(graph);
- if(type == null) return Collections.emptySet();
-
- if(!graph.isInheritedFrom(type, DOC.Components_Component)) return Collections.emptySet();
-
- Boolean pathExists = variable.getPossiblePropertyValue(graph, DOC.Properties_pathExists, Bindings.BOOLEAN);
- if(pathExists != null && !pathExists) return Collections.emptySet();
-
- if(graph.isInheritedFrom(type, DOC.Components_PrimitiveComponent)) {
- return Collections.singleton(variable);
- } else {
- Set<Variable> result = new THashSet<Variable>();
- for(Variable child : variable.getChildren(graph)) {
- Set<Variable> nodes = graph.syncRequest(new NodesRequest2(child));
- result.addAll(nodes);
- }
-
- return result;
- }
-
- }
+ }
+
+ @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