use of edu.uci.ics.jung.graph.DirectedOrderedSparseMultigraph in project opennms by OpenNMS.
the class HierarchyLayoutAlgorithm method convert.
private edu.uci.ics.jung.graph.DirectedGraph<VertexRef, Edge> convert(final Graph g) {
if (!isFullyLevelAware(g)) {
throw new IllegalStateException("The graph is not LevelAware. Cannot apply Hierarchy Layout. Aborting");
}
// We need to sort the elements. For this purpose we use the DirectedOrderedSparseMultigraph
final edu.uci.ics.jung.graph.DirectedGraph<VertexRef, Edge> jungGraph = new DirectedOrderedSparseMultigraph<>();
final Collection<Vertex> displayVertices = g.getDisplayVertices();
// Sort by level
final List<Vertex> sortedVertices = displayVertices.stream().filter(v -> v instanceof LevelAware).sorted(new Comparator<Vertex>() {
@Override
public int compare(Vertex o1, Vertex o2) {
return Integer.compare(((LevelAware) o1).getLevel(), ((LevelAware) o2).getLevel());
}
}).collect(Collectors.toList());
// Build the graph
for (VertexRef v : sortedVertices) {
jungGraph.addVertex(v);
}
// The order of edges does not matter
for (Edge e : g.getDisplayEdges()) {
jungGraph.addEdge(e, e.getSource().getVertex(), e.getTarget().getVertex());
}
return jungGraph;
}
Aggregations