1 package org.simantics.db.layer0.util;
3 import java.util.ArrayList;
4 import java.util.Collection;
8 import org.simantics.db.AsyncReadGraph;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.ResourceMap;
12 import org.simantics.db.common.request.ReadRequest;
13 import org.simantics.db.common.utils.Logger;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
16 import org.simantics.db.procedure.AsyncContextMultiProcedure;
17 import org.simantics.db.procedure.Procedure;
18 import org.simantics.db.service.DirectQuerySupport;
19 import org.simantics.layer0.Layer0;
21 class ConsistsOfProcess {
23 final List<InternalEntry> result;
24 final AsyncContextMultiProcedure<InternalEntry, Resource> structure;
25 final AsyncContextMultiProcedure<InternalEntry, Resource> names;
27 public static List<InternalEntry> walk(ReadGraph graph, ResourceMap<ExtentStatus> status, Collection<Resource> resources, Set<Resource> exclusions, boolean ignoreVirtual) throws DatabaseException {
28 ConsistsOfProcess process = new ConsistsOfProcess(graph, status, resources, exclusions, ignoreVirtual);
29 return process.result;
32 static class InternalEntry {
33 public InternalEntry parent;
34 public Resource resource;
36 InternalEntry(InternalEntry parent, Resource resource, String name) {
38 this.resource = resource;
43 private ConsistsOfProcess(ReadGraph graph, ResourceMap<ExtentStatus> status, final Collection<Resource> resources, final Set<Resource> exclusions, final boolean ignoreVirtual) throws DatabaseException {
45 final Layer0 L0 = Layer0.getInstance(graph);
46 final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
48 result = new ArrayList<InternalEntry>();
50 names = dqs.compileForEachObject(graph, L0.HasName, new AsyncContextMultiProcedure<InternalEntry, Resource>() {
53 public void execute(AsyncReadGraph graph, InternalEntry entry, Resource nameResource) {
56 status.put(nameResource, ExtentStatus.EXCLUDED);
58 graph.forPossibleValue(nameResource, new Procedure<String>() {
61 public void execute(String result) {
66 public void exception(Throwable t) {
67 Logger.defaultLogError(t);
74 public void exception(AsyncReadGraph graph, Throwable throwable) {
75 Logger.defaultLogError(throwable);
79 public void finished(AsyncReadGraph graph) {
84 structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new AsyncContextMultiProcedure<InternalEntry, Resource>() {
87 public void execute(AsyncReadGraph graph, InternalEntry parent, Resource child) {
89 if(exclusions.contains(child)) return;
91 if(!ignoreVirtual || child.isPersistent()) {
92 InternalEntry entry = new InternalEntry(parent, child, null);
93 if(result.add(entry)) {
94 dqs.forEachObjectCompiled(graph, child, entry, structure);
95 dqs.forEachObjectCompiled(graph, child, entry, names);
102 public void finished(AsyncReadGraph graph) {
106 public void exception(AsyncReadGraph graph, Throwable throwable) {
107 Logger.defaultLogError(throwable);
112 graph.syncRequest(new ReadRequest() {
115 public void run(ReadGraph graph) throws DatabaseException {
116 for(Resource r : resources) {
117 InternalEntry root = new InternalEntry(null, r, null);
118 dqs.forEachObjectCompiled(graph, r, root, structure);