Search in sources :

Example 1 with DirectedOrderedSparseMultigraph

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;
}
Also used : Vertex(org.opennms.features.topology.api.topo.Vertex) DirectedOrderedSparseMultigraph(edu.uci.ics.jung.graph.DirectedOrderedSparseMultigraph) DirectedOrderedSparseMultigraph(edu.uci.ics.jung.graph.DirectedOrderedSparseMultigraph) Comparator(java.util.Comparator) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Edge(org.opennms.features.topology.api.topo.Edge) LevelAware(org.opennms.features.topology.api.topo.LevelAware)

Aggregations

DirectedOrderedSparseMultigraph (edu.uci.ics.jung.graph.DirectedOrderedSparseMultigraph)1 Comparator (java.util.Comparator)1 Edge (org.opennms.features.topology.api.topo.Edge)1 LevelAware (org.opennms.features.topology.api.topo.LevelAware)1 Vertex (org.opennms.features.topology.api.topo.Vertex)1 VertexRef (org.opennms.features.topology.api.topo.VertexRef)1