/
ReferenceGraph.java
394 lines (342 loc) · 11.2 KB
/
ReferenceGraph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* Copyright (c) 2015 Data Harmonisation Panel
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution. If not, see <http://www.gnu.org/licenses/>.
*
* Contributors:
* Data Harmonisation Panel <http://www.dhpanel.eu>
*/
package eu.esdihumboldt.hale.common.instance.graph.reference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import com.tinkerpop.pipes.PipeFunction;
import com.tinkerpop.pipes.branch.LoopPipe.LoopBundle;
import de.fhg.igd.slf4jplus.ALogger;
import de.fhg.igd.slf4jplus.ALoggerFactory;
import eu.esdihumboldt.hale.common.core.report.SimpleLog;
import eu.esdihumboldt.hale.common.instance.graph.reference.internal.ReferencesInstanceCollection;
import eu.esdihumboldt.hale.common.instance.model.Instance;
import eu.esdihumboldt.hale.common.instance.model.InstanceCollection;
import eu.esdihumboldt.hale.common.instance.model.InstanceReference;
import eu.esdihumboldt.hale.common.instance.model.ResourceIterator;
/**
* Graph with associations between instances.
*
* @author Simon Templer
* @param <T> the identifier type, must have a sensible equals implementation
*/
public class ReferenceGraph<T> {
/**
* Custom tinker graph that allows fast access to a random contained vertex
* and to the information if the graph has any vertices.
*
* This class was created because the call to
* {@link TinkerGraph#getVertices()} is very expensive for large graphs.
*/
public class CustomTinkerGraph extends TinkerGraph {
private static final long serialVersionUID = -6470218605426839887L;
/**
* @return a vertex in the graph
*/
@Nullable
public Vertex someVertex() {
if (isEmpty())
return null;
return vertices.values().iterator().next();
}
/**
* @return if the graph is empty
*/
public boolean isEmpty() {
return vertices.isEmpty();
}
}
private static final ALogger logger = ALoggerFactory.getLogger(ReferenceGraph.class);
/**
* Iterator for instance partitions.
*/
public class PartitionIterator implements Iterator<InstanceCollection> {
private final Queue<List<InstanceReference>> candidates = new LinkedList<>();
private final int maxObjects;
private int partCount = 0;
private int partSum = 0;
private int biggestAtom = 0;
private final SimpleLog log;
/**
* @param maxObjects the guiding value for the maximum number of objects
* in a part
* @param log the operation log
*/
public PartitionIterator(int maxObjects, SimpleLog log) {
this.maxObjects = maxObjects;
this.log = log;
}
@Override
public boolean hasNext() {
/*
* There are additional parts if there are candidates in the queue
* and/or vertices left in the graph.
*/
return !candidates.isEmpty() || verticesLeft();
}
private boolean verticesLeft() {
return !graph.isEmpty();
}
@Override
public InstanceCollection next() {
List<InstanceReference> part = candidates.poll();
if (part == null) {
part = new ArrayList<>(maxObjects);
}
Queue<List<InstanceReference>> nextCandidates = new LinkedList<>();
while (verticesLeft() && part.size() < maxObjects) {
// add to part
List<InstanceReference> instances = getNextAtomicPart();
biggestAtom = Math.max(biggestAtom, instances.size());
if (part.size() + instances.size() > maxObjects) {
// add to part candidates for later use
nextCandidates.add(instances);
if (!verticesLeft()) {
// we added everything to candidates and need to
// terminate the loop
break;
}
}
else {
// add to current part
part.addAll(instances);
}
}
// try to add parts from previous candidates
while (!candidates.isEmpty() && part.size() < maxObjects) {
List<InstanceReference> instances = candidates.poll();
if (part.size() + instances.size() > maxObjects) {
// add to part candidates for later use
nextCandidates.add(instances);
}
else {
// add to current part
part.addAll(instances);
}
}
// collected candidates for next attempt
candidates.addAll(nextCandidates);
if (part.isEmpty()) {
// no vertices left
if (!candidates.isEmpty()) {
// yield a previously stored candidate that was to big
// to fit into a request
part = candidates.poll();
}
else {
throw new NoSuchElementException("All parts were retrieved");
}
}
partCount++;
partSum += part.size();
logger.debug("Reference based partitioning - Part {} - {} instances", partCount,
part.size());
if (!hasNext()) {
log.info(
"Completed partitioning of {1} instances in {0} parts, biggest inseparable set of instances was of size {2}.",
partCount, partSum, biggestAtom);
}
return new ReferencesInstanceCollection(part, originalCollection);
}
/**
* @return the next atomic part from the graph as instance references
*/
private List<InstanceReference> getNextAtomicPart() {
// select an arbitrary vertex
Vertex vtx = graph.someVertex();
if (vtx != null) {
// get all vertices associated with that vertex
final Set<Vertex> visited = new LinkedHashSet<>();
if (!vtx.getEdges(Direction.BOTH).iterator().hasNext()) {
// no edges associated - no need to use gremlin
// does not speed up the process by much though
visited.add(vtx);
}
else {
/**
* Example for the Groovy console - getting all associated
* vertices. <code>
* g = TinkerGraphFactory.createTinkerGraph()
* x = new LinkedHashSet()
* g.v(1).as('ref').aggregate(x).both.loop('ref', { !x.contains(it.object) })
* x
* </code>
*/
GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<>();
pipe.start(vtx).as("ref").aggregate(visited).both()
.loop("ref", new PipeFunction<LoopBundle<Vertex>, Boolean>() {
@Override
public Boolean compute(LoopBundle<Vertex> loop) {
return !visited.contains(loop.getObject());
}
}).iterate();
}
List<InstanceReference> result = new ArrayList<>();
for (Vertex associated : visited) {
InstanceReference ref = associated.getProperty(P_INSTANCE_REFERENCE);
if (ref != null) {
result.add(ref);
}
else {
Iterable<Vertex> referers = associated.getVertices(Direction.IN);
Set<String> ids = new HashSet<>();
if (referers != null) {
for (Vertex referer : referers) {
Object ident = referer.getId();
if (ident != null) {
ids.add(ident.toString());
}
}
}
if (ids.isEmpty()) {
log.warn("Encountered referenced object w/o associated instance: "
+ associated.getId());
}
else {
String enumIds = ids.stream().collect(Collectors.joining(", "));
log.warn("Encountered referenced object w/o associated instance: "
+ associated.getId() + " - referenced from " + enumIds);
}
}
}
// remove vertices from graph
for (Vertex v : visited) {
graph.removeVertex(v);
}
return result;
}
else {
return Collections.emptyList();
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
private static final String P_INSTANCE_REFERENCE = "instanceRef";
private static final String E_REFERENCE = "refs";
private final CustomTinkerGraph graph;
private final IdentityReferenceInspector<T> inspector;
/**
* Index for fast retrieval of vertices by identifier.
*/
private final Map<T, Vertex> identifiedVertices = new HashMap<>();
private final InstanceCollection originalCollection;
private boolean partitioned = false;
/**
* Create a new reference graph from the given instance collection.
*
* @param inspector the instance inspector to use
* @param instances the
*/
public ReferenceGraph(IdentityReferenceInspector<T> inspector, InstanceCollection instances) {
this.graph = new CustomTinkerGraph();
this.inspector = inspector;
this.originalCollection = instances;
populate(instances);
// identified vertices no longer needed after populate
identifiedVertices.clear();
}
/**
* Populate the graph with the instances from the given collection.
*
* @param instances an instance collection
*/
protected void populate(InstanceCollection instances) {
try (ResourceIterator<Instance> it = instances.iterator()) {
while (it.hasNext()) {
Instance instance = it.next();
addInstance(instance, instances.getReference(instance));
}
}
}
/**
* Add an instance to the reference graph.
*
* @param instance the instance to add
* @param ref the reference that can be used to retrieve the instance
*/
protected void addInstance(Instance instance, InstanceReference ref) {
// retrieve / create vertex
T id = inspector.getIdentity(instance);
Vertex vertex = getVertex(id);
// store instance reference
vertex.setProperty(P_INSTANCE_REFERENCE, ref);
// create references
Set<T> associations = inspector.getReferencedIdentities(instance);
for (T idRef : associations) {
Vertex assoc = getVertex(idRef);
// add edge between vertices
graph.addEdge(null, vertex, assoc, E_REFERENCE);
}
}
/**
* Partition the collected instances in parts that respectively contain all
* referenced instances.
*
* @param maxObjects the guiding value for the maximum number of objects in
* a part
* @param log the operation log
* @return an iterator of instance collections, each instance collection
* represents a part
*/
public Iterator<InstanceCollection> partition(int maxObjects, SimpleLog log) {
if (!partitioned) {
partitioned = true;
return new PartitionIterator(maxObjects, log);
}
throw new IllegalStateException(
"Partitioning the instance collection can only be done once");
}
/**
* Partition the collected instances in parts that respectively contain all
* referenced instances.
*
* @param maxObjects the guiding value for the maximum number of objects in
* a part
* @return an iterator of instance collections, each instance collection
* represents a part
*/
public Iterator<InstanceCollection> partition(int maxObjects) {
return partition(maxObjects, SimpleLog.fromLogger(logger));
}
private Vertex getVertex(T id) {
Vertex vertex = null;
if (id != null) {
vertex = identifiedVertices.get(id);
}
if (vertex == null) {
vertex = graph.addVertex(id);
identifiedVertices.put(id, vertex);
}
return vertex;
}
}