use of au.gov.asd.tac.constellation.graph.utilities.wrapper.GraphStep in project constellation by constellation-app.
the class PlaceholderUtilities method collapsePlaceholders.
public static StoreGraph collapsePlaceholders(final StoreGraphRecordStore graph, final Record record, final DatumProcessor<Record, ?> rowProcessor, final Comparator<GraphVertex> dominanceComparator, final boolean cleanupGraph, final boolean debug) throws PluginException, InterruptedException {
graph.complete();
graph.validateKeys();
GraphWrapper g = new GraphWrapper(graph);
// remove all transactions with type 'unknown' and all nodes with identifier 'unknown'
if (cleanupGraph) {
g.streamTransactions().filter(transaction -> transaction.getTypeValue().equals(SchemaTransactionTypeUtilities.getDefaultType())).forEach(GraphTransaction::deferRemove);
g.streamVertices().filter(vertex -> "unknown".equals(vertex.getStringValue(VisualConcept.VertexAttribute.IDENTIFIER))).forEach(GraphVertex::deferRemove);
g.completeDeferred();
}
if (debug) {
GraphOpener.getDefault().openGraph(new DualGraph(graph, true), rowProcessor.getClass().getSimpleName() + "-debug-stage1");
}
// connect all nodes with type 'placeholder'
g.streamVertices().filter(v -> v.getTypeValue().equals(AnalyticConcept.VertexType.PLACEHOLDER)).map(v -> v.walkNeighbours(s -> s.getTransaction().getTypeValue().isSubTypeOf(AnalyticConcept.TransactionType.CORRELATION) && s.getDestinationVertex().getTypeValue().equals(AnalyticConcept.VertexType.PLACEHOLDER), true).map(GraphStep::getDestinationVertex).collect(Collectors.toSet())).distinct().forEach(c -> {
String newIdentifier = c.stream().map(v -> v.getStringValue(VisualConcept.VertexAttribute.IDENTIFIER)).collect(Collectors.joining(SeparatorConstants.HYPHEN));
c.stream().forEach(v -> {
v.setStringValue(VisualConcept.VertexAttribute.IDENTIFIER, newIdentifier);
v.completeWithSchema();
});
});
g.validateKeys();
if (debug) {
GraphOpener.getDefault().openGraph(new DualGraph(graph, true), rowProcessor.getClass().getSimpleName() + "-debug-stage2");
}
// replace nodes with type 'placeholder' with the dominant correlated node
g.streamVertices().filter(v -> v.getTypeValue().equals(AnalyticConcept.VertexType.PLACEHOLDER)).forEach(v -> {
Optional<GraphVertex> dominant = v.streamNeighbours().filter(n -> n.getDirection() != GraphDirection.LOOPBACK).filter(n -> n.getTransaction().getTypeValue().isSubTypeOf(AnalyticConcept.TransactionType.CORRELATION)).map(n -> n.getDestinationVertex()).distinct().sorted(dominanceComparator).findFirst();
// TODO: needed to create a special case for 'unknown' type vertices, but not sure why...
if (dominant.isPresent() && !dominant.get().getTypeValue().equals(SchemaVertexTypeUtilities.getDefaultType())) {
v.setStringValue(VisualConcept.VertexAttribute.IDENTIFIER, dominant.get().getStringValue(VisualConcept.VertexAttribute.IDENTIFIER));
v.setTypeValue(dominant.get().getTypeValue());
v.setRawValue(dominant.get().getRawValue());
v.completeWithSchema();
} else {
v.deferRemove();
}
});
g.completeDeferred();
g.validateKeys();
if (debug) {
GraphOpener.getDefault().openGraph(new DualGraph(graph, true), rowProcessor.getClass().getSimpleName() + "-debug-stage3");
}
// remove transactions that correlate nodes with themselves
g.streamTransactions().filter(t -> t.getTypeValue().isSubTypeOf(AnalyticConcept.TransactionType.CORRELATION)).filter(t -> t.getSourceVertex().equals(t.getDestinationVertex())).forEach(GraphTransaction::deferRemove);
g.completeDeferred();
return graph;
}
Aggregations