]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ConsistsOfProcess.java
4124add429dd76d4ae2756a6b3e0404e0d214f01
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / ConsistsOfProcess.java
1 package org.simantics.db.layer0.util;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Set;
8
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;
24
25 class ConsistsOfProcess {
26
27         final List<ConsistsOfProcessEntry> result;
28         final Set<Resource> childrenWithNoName;
29         final AsyncContextMultiProcedure<ConsistsOfProcessEntry, Resource> structure;
30         final AsyncContextMultiProcedure<ConsistsOfProcessEntry, Resource> names;
31
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);
34         }
35
36         public static Pair<List<ConsistsOfProcessEntry>,Set<Resource>> walk(ReadGraph graph, ResourceMap<ExtentStatus> status, Collection<SeedSpec> specs, boolean ignoreVirtual) throws DatabaseException {
37                 
38                 Collection<ConsistsOfProcessEntry> entries = new ArrayList<>();
39                 for(SeedSpec r : specs) {
40                         if(SeedSpecType.INTERNAL.equals(r.specType))
41                             entries.add(new ConsistsOfProcessEntry(null, r.resource, true));
42                         else
43                                 entries.add(new ConsistsOfProcessEntry(null, r.resource, false));
44                 }
45         ConsistsOfProcess process = new ConsistsOfProcess(graph, status, entries, ignoreVirtual);
46         return Pair.make(process.result, process.childrenWithNoName);
47         
48     }
49     
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) {
57                 this.parent = parent;
58                 this.resource = resource;
59                 this.internal = internal;
60         }
61     }
62         
63     private ConsistsOfProcess(ReadGraph graph, ResourceMap<ExtentStatus> status, final Collection<ConsistsOfProcessEntry> entries, final boolean ignoreVirtual) throws DatabaseException {
64
65                 final Layer0 L0 = Layer0.getInstance(graph);
66                 final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
67                 
68                 result = new ArrayList<>();
69                 childrenWithNoName = new HashSet<>();
70                 names = dqs.compileForEachObject(graph, L0.HasName, new AsyncContextMultiProcedure<ConsistsOfProcessEntry, Resource>() {
71
72                         @Override
73                         public void execute(AsyncReadGraph graph, ConsistsOfProcessEntry entry, Resource nameResource) {
74                 
75                                 if(status != null)
76                                         status.put(nameResource, ExtentStatus.EXCLUDED);
77                                 
78                                 graph.forPossibleValue(nameResource, new Procedure<String>() {
79
80                                         @Override
81                                         public void execute(String name) {
82                                             if(!entry.valid) return;
83                                             
84                                             if(name == null) {
85                                                 entry.valid = false;
86                                             } else if (entry.name != null) {
87                                                 entry.valid = false;
88                                             } else {
89                                                 entry.name = name;
90                                             }
91                                         }
92
93                                         @Override
94                                         public void exception(Throwable t) {
95                                                 Logger.defaultLogError(t);
96                                         }
97                                         
98                                 });
99                         }
100
101                         @Override
102                         public void exception(AsyncReadGraph graph, Throwable throwable) {
103                                 Logger.defaultLogError(throwable);
104                         }
105
106                         @Override
107                         public void finished(AsyncReadGraph graph, ConsistsOfProcessEntry entry) {
108                             if(entry.valid) {
109                                 if(entry.name != null) {
110                                     result.add(entry);
111                                 } else {
112                                     // This one did not have a name - not a valid internal
113                                     childrenWithNoName.add(entry.resource);
114                                 }
115                             } else {
116                                 // Something wrong has happened. Do not treat as valid internal
117                                 childrenWithNoName.add(entry.resource);
118                             }
119                         }
120                 });
121                 
122                 structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new AsyncContextMultiProcedure<ConsistsOfProcessEntry, Resource>() {
123
124                         @Override
125                         public void execute(AsyncReadGraph graph, ConsistsOfProcessEntry parent, Resource child) {
126
127                                 if(status != null)
128                                         if(ExtentStatus.EXCLUDED.equals(status.get(child))) return;
129                                 
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);
134                                 }
135                                 
136                         }
137
138                         @Override
139                         public void finished(AsyncReadGraph graph, ConsistsOfProcessEntry parent) {
140                         }
141
142                         @Override
143                         public void exception(AsyncReadGraph graph, Throwable throwable) {
144                                 Logger.defaultLogError(throwable);
145                         }
146
147                 });
148                 
149                 graph.syncRequest(new ReadRequest() {
150
151                         @Override
152                         public void run(ReadGraph graph) throws DatabaseException {
153                                 for(ConsistsOfProcessEntry entry  : entries) {
154                                         dqs.forEachObjectCompiled(graph, entry.resource, entry, structure);
155                                         if(entry.internal) {
156                                                 // For roots names are not processed
157                                                 dqs.forEachObjectCompiled(graph, entry.resource, entry, names);
158                                         }
159                                 }
160                         }
161                         
162                 });
163                 
164         }
165         
166     
167 }