]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ConsistsOfProcess.java
Index tokenized lowercase versions of name and types for UI searches
[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         ConsistsOfProcess process = new ConsistsOfProcess(graph, status, specs, ignoreVirtual);
39         return Pair.make(process.result, process.childrenWithNoName);
40         
41     }
42     
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) {
49                 this.parent = parent;
50                 this.resource = resource;
51         }
52     }
53         
54     private ConsistsOfProcess(ReadGraph graph, ResourceMap<ExtentStatus> status, final Collection<SeedSpec> seeds, final boolean ignoreVirtual) throws DatabaseException {
55
56                 final Layer0 L0 = Layer0.getInstance(graph);
57                 final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
58                 
59                 result = new ArrayList<>();
60                 childrenWithNoName = new HashSet<>();
61                 names = dqs.compileForEachObject(graph, L0.HasName, new AsyncContextMultiProcedure<ConsistsOfProcessEntry, Resource>() {
62
63                         @Override
64                         public void execute(AsyncReadGraph graph, ConsistsOfProcessEntry entry, Resource nameResource) {
65                 
66                                 if(status != null)
67                                         status.put(nameResource, ExtentStatus.EXCLUDED);
68                                 
69                                 graph.forPossibleValue(nameResource, new Procedure<String>() {
70
71                                         @Override
72                                         public void execute(String name) {
73                                             if(!entry.valid) return;
74                                             
75                                             if(name == null) {
76                                                 entry.valid = false;
77                                             } else if (entry.name != null) {
78                                                 entry.valid = false;
79                                             } else {
80                                                 entry.name = name;
81                                             }
82                                         }
83
84                                         @Override
85                                         public void exception(Throwable t) {
86                                                 Logger.defaultLogError(t);
87                                         }
88                                         
89                                 });
90                         }
91
92                         @Override
93                         public void exception(AsyncReadGraph graph, Throwable throwable) {
94                                 Logger.defaultLogError(throwable);
95                         }
96
97                         @Override
98                         public void finished(AsyncReadGraph graph, ConsistsOfProcessEntry entry) {
99                             if(entry.valid) {
100                                 if(entry.name != null) {
101                                     result.add(entry);
102                                 } else {
103                                     // This one did not have a name - not a valid internal
104                                     childrenWithNoName.add(entry.resource);
105                                 }
106                             } else {
107                                 // Something wrong has happened. Do not treat as valid internal
108                                 childrenWithNoName.add(entry.resource);
109                             }
110                         }
111                 });
112                 
113                 structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new AsyncContextMultiProcedure<ConsistsOfProcessEntry, Resource>() {
114
115                         @Override
116                         public void execute(AsyncReadGraph graph, ConsistsOfProcessEntry parent, Resource child) {
117
118                                 if(status != null)
119                                         if(ExtentStatus.EXCLUDED.equals(status.get(child))) return;
120                                 
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);
125                                 }
126                                 
127                         }
128
129                         @Override
130                         public void finished(AsyncReadGraph graph, ConsistsOfProcessEntry parent) {
131                         }
132
133                         @Override
134                         public void exception(AsyncReadGraph graph, Throwable throwable) {
135                                 Logger.defaultLogError(throwable);
136                         }
137
138                 });
139                 
140                 graph.syncRequest(new ReadRequest() {
141
142                         @Override
143                         public void run(ReadGraph graph) throws DatabaseException {
144                                 for(SeedSpec seed  : seeds) {
145                                         
146                                         if(status != null) {
147                                                 ExtentStatus es = status.get(seed.resource);
148                                                 if(ExtentStatus.EXCLUDED.equals(es)) continue;
149                                                 if(ExtentStatus.EXTERNAL.equals(es)) continue;
150                                         }
151                                         
152                                         ConsistsOfProcessEntry entry = new ConsistsOfProcessEntry(null, seed.resource);
153                                         
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);
158                                         }
159                                 }
160                         }
161                         
162                 });
163                 
164         }
165         
166     
167 }