1 package org.simantics.db.layer0.util;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
9 import org.simantics.db.AsyncReadGraph;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.ResourceMap;
13 import org.simantics.db.common.request.ReadRequest;
14 import org.simantics.db.common.utils.Logger;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
17 import org.simantics.db.procedure.AsyncContextMultiProcedure;
18 import org.simantics.db.procedure.Procedure;
19 import org.simantics.db.service.DirectQuerySupport;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.utils.datastructures.Pair;
23 class ConsistsOfProcess {
25 final List<InternalEntry> result;
26 final Set<Resource> childrenWithNoName;
27 final AsyncContextMultiProcedure<InternalEntry, Resource> structure;
28 final AsyncContextMultiProcedure<InternalEntry, Resource> names;
30 public static Pair<List<InternalEntry>,Set<Resource>> walk(ReadGraph graph, ResourceMap<ExtentStatus> status, Collection<Resource> resources, Set<Resource> exclusions, boolean ignoreVirtual) throws DatabaseException {
31 ConsistsOfProcess process = new ConsistsOfProcess(graph, status, resources, exclusions, ignoreVirtual);
32 return Pair.make(process.result, process.childrenWithNoName);
35 static class InternalEntry {
36 public InternalEntry parent;
37 public Resource resource;
39 public boolean valid = true;
40 InternalEntry(InternalEntry parent, Resource resource, String name) {
42 this.resource = resource;
47 private ConsistsOfProcess(ReadGraph graph, ResourceMap<ExtentStatus> status, final Collection<Resource> resources, final Set<Resource> exclusions, final boolean ignoreVirtual) throws DatabaseException {
49 final Layer0 L0 = Layer0.getInstance(graph);
50 final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
52 result = new ArrayList<InternalEntry>();
53 childrenWithNoName = new HashSet<>();
54 names = dqs.compileForEachObject(graph, L0.HasName, new AsyncContextMultiProcedure<InternalEntry, Resource>() {
57 public void execute(AsyncReadGraph graph, InternalEntry entry, Resource nameResource) {
60 status.put(nameResource, ExtentStatus.EXCLUDED);
62 graph.forPossibleValue(nameResource, new Procedure<String>() {
65 public void execute(String name) {
66 if(!entry.valid) return;
70 } else if (entry.name != null) {
78 public void exception(Throwable t) {
79 Logger.defaultLogError(t);
86 public void exception(AsyncReadGraph graph, Throwable throwable) {
87 Logger.defaultLogError(throwable);
91 public void finished(AsyncReadGraph graph, InternalEntry entry) {
93 if(entry.name != null) {
96 // This one did not have a name - not a valid internal
97 childrenWithNoName.add(entry.resource);
100 // Something wrong has happened. Do not treat as valid internal
101 childrenWithNoName.add(entry.resource);
106 structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new AsyncContextMultiProcedure<InternalEntry, Resource>() {
109 public void execute(AsyncReadGraph graph, InternalEntry parent, Resource child) {
111 if(exclusions.contains(child)) return;
113 if(!ignoreVirtual || child.isPersistent()) {
114 InternalEntry entry = new InternalEntry(parent, child, null);
115 dqs.forEachObjectCompiled(graph, child, entry, structure);
116 dqs.forEachObjectCompiled(graph, child, entry, names);
122 public void finished(AsyncReadGraph graph, InternalEntry parent) {
126 public void exception(AsyncReadGraph graph, Throwable throwable) {
127 Logger.defaultLogError(throwable);
132 graph.syncRequest(new ReadRequest() {
135 public void run(ReadGraph graph) throws DatabaseException {
136 for(Resource r : resources) {
137 InternalEntry root = new InternalEntry(null, r, null);
138 dqs.forEachObjectCompiled(graph, r, root, structure);