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 Collection<ConsistsOfProcessEntry> entries = new ArrayList<>(specs.size());
39 for(SeedSpec r : specs) {
40 if(SeedSpecType.INTERNAL.equals(r.specType))
41 entries.add(new ConsistsOfProcessEntry(null, r.resource, true));
43 entries.add(new ConsistsOfProcessEntry(null, r.resource, false));
45 ConsistsOfProcess process = new ConsistsOfProcess(graph, status, entries, ignoreVirtual);
46 return Pair.make(process.result, process.childrenWithNoName);
50 static class ConsistsOfProcessEntry {
51 public ConsistsOfProcessEntry parent;
52 public Resource resource;
53 public boolean internal;
54 public boolean valid = true;
55 public String name = null;
56 ConsistsOfProcessEntry(ConsistsOfProcessEntry parent, Resource resource, boolean internal) {
58 this.resource = resource;
59 this.internal = internal;
63 private ConsistsOfProcess(ReadGraph graph, ResourceMap<ExtentStatus> status, final Collection<ConsistsOfProcessEntry> entries, final boolean ignoreVirtual) throws DatabaseException {
65 final Layer0 L0 = Layer0.getInstance(graph);
66 final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
68 result = new ArrayList<>();
69 childrenWithNoName = new HashSet<>();
70 names = dqs.compileForEachObject(graph, L0.HasName, new AsyncContextMultiProcedure<ConsistsOfProcessEntry, Resource>() {
73 public void execute(AsyncReadGraph graph, ConsistsOfProcessEntry entry, Resource nameResource) {
76 status.put(nameResource, ExtentStatus.EXCLUDED);
78 graph.forPossibleValue(nameResource, new Procedure<String>() {
81 public void execute(String name) {
82 if(!entry.valid) return;
86 } else if (entry.name != null) {
94 public void exception(Throwable t) {
95 Logger.defaultLogError(t);
102 public void exception(AsyncReadGraph graph, Throwable throwable) {
103 Logger.defaultLogError(throwable);
107 public void finished(AsyncReadGraph graph, ConsistsOfProcessEntry entry) {
109 if(entry.name != null) {
112 // This one did not have a name - not a valid internal
113 childrenWithNoName.add(entry.resource);
116 // Something wrong has happened. Do not treat as valid internal
117 childrenWithNoName.add(entry.resource);
122 structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new AsyncContextMultiProcedure<ConsistsOfProcessEntry, Resource>() {
125 public void execute(AsyncReadGraph graph, ConsistsOfProcessEntry parent, Resource child) {
128 if(ExtentStatus.EXCLUDED.equals(status.get(child))) return;
130 if(!ignoreVirtual || child.isPersistent()) {
131 ConsistsOfProcessEntry entry = new ConsistsOfProcessEntry(parent, child, false);
132 dqs.forEachObjectCompiled(graph, child, entry, structure);
133 dqs.forEachObjectCompiled(graph, child, entry, names);
139 public void finished(AsyncReadGraph graph, ConsistsOfProcessEntry parent) {
143 public void exception(AsyncReadGraph graph, Throwable throwable) {
144 Logger.defaultLogError(throwable);
149 graph.syncRequest(new ReadRequest() {
152 public void run(ReadGraph graph) throws DatabaseException {
153 for(ConsistsOfProcessEntry entry : entries) {
154 dqs.forEachObjectCompiled(graph, entry.resource, entry, structure);
156 // For roots names are not processed
157 dqs.forEachObjectCompiled(graph, entry.resource, entry, names);