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.layer0.util.TransferableGraphConfiguration2.SeedSpec;
18 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec.SeedSpecType;
19 import org.simantics.db.procedure.AsyncContextMultiProcedure;
20 import org.simantics.db.procedure.Procedure;
21 import org.simantics.db.service.DirectQuerySupport;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.utils.datastructures.Pair;
25 class ConsistsOfProcess {
27 final List<ConsistsOfProcessEntry> result;
28 final Set<Resource> childrenWithNoName;
29 final AsyncContextMultiProcedure<ConsistsOfProcessEntry, Resource> structure;
30 final AsyncContextMultiProcedure<ConsistsOfProcessEntry, Resource> names;
32 public static Pair<List<ConsistsOfProcessEntry>,Set<Resource>> walk(ReadGraph graph, Collection<SeedSpec> specs, boolean ignoreVirtual) throws DatabaseException {
33 return walk(graph, null, specs, ignoreVirtual);
36 public static Pair<List<ConsistsOfProcessEntry>,Set<Resource>> walk(ReadGraph graph, ResourceMap<ExtentStatus> status, Collection<SeedSpec> specs, boolean ignoreVirtual) throws DatabaseException {
38 ConsistsOfProcess process = new ConsistsOfProcess(graph, status, specs, ignoreVirtual);
39 return Pair.make(process.result, process.childrenWithNoName);
43 static class ConsistsOfProcessEntry {
44 public ConsistsOfProcessEntry parent;
45 public Resource resource;
46 public boolean valid = true;
47 public String name = null;
48 ConsistsOfProcessEntry(ConsistsOfProcessEntry parent, Resource resource) {
50 this.resource = resource;
54 private ConsistsOfProcess(ReadGraph graph, ResourceMap<ExtentStatus> status, final Collection<SeedSpec> seeds, final boolean ignoreVirtual) throws DatabaseException {
56 final Layer0 L0 = Layer0.getInstance(graph);
57 final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
59 result = new ArrayList<>();
60 childrenWithNoName = new HashSet<>();
61 names = dqs.compileForEachObject(graph, L0.HasName, new AsyncContextMultiProcedure<ConsistsOfProcessEntry, Resource>() {
64 public void execute(AsyncReadGraph graph, ConsistsOfProcessEntry entry, Resource nameResource) {
67 status.put(nameResource, ExtentStatus.EXCLUDED);
69 graph.forPossibleValue(nameResource, new Procedure<String>() {
72 public void execute(String name) {
73 if(!entry.valid) return;
77 } else if (entry.name != null) {
85 public void exception(Throwable t) {
86 Logger.defaultLogError(t);
93 public void exception(AsyncReadGraph graph, Throwable throwable) {
94 Logger.defaultLogError(throwable);
98 public void finished(AsyncReadGraph graph, ConsistsOfProcessEntry entry) {
100 if(entry.name != null) {
103 // This one did not have a name - not a valid internal
104 childrenWithNoName.add(entry.resource);
107 // Something wrong has happened. Do not treat as valid internal
108 childrenWithNoName.add(entry.resource);
113 structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new AsyncContextMultiProcedure<ConsistsOfProcessEntry, Resource>() {
116 public void execute(AsyncReadGraph graph, ConsistsOfProcessEntry parent, Resource child) {
119 if(ExtentStatus.EXCLUDED.equals(status.get(child))) return;
121 if(!ignoreVirtual || child.isPersistent()) {
122 ConsistsOfProcessEntry entry = new ConsistsOfProcessEntry(parent, child);
123 dqs.forEachObjectCompiled(graph, child, entry, structure);
124 dqs.forEachObjectCompiled(graph, child, entry, names);
130 public void finished(AsyncReadGraph graph, ConsistsOfProcessEntry parent) {
134 public void exception(AsyncReadGraph graph, Throwable throwable) {
135 Logger.defaultLogError(throwable);
140 graph.syncRequest(new ReadRequest() {
143 public void run(ReadGraph graph) throws DatabaseException {
144 for(SeedSpec seed : seeds) {
147 ExtentStatus es = status.get(seed.resource);
148 if(ExtentStatus.EXCLUDED.equals(es)) continue;
149 if(ExtentStatus.EXTERNAL.equals(es)) continue;
152 ConsistsOfProcessEntry entry = new ConsistsOfProcessEntry(null, seed.resource);
154 dqs.forEachObjectCompiled(graph, seed.resource, entry, structure);
155 if(SeedSpecType.INTERNAL.equals(seed.specType)) {
156 // Process names only for internal seeds
157 dqs.forEachObjectCompiled(graph, seed.resource, entry, names);