Example usage for com.google.common.graph Network edges

List of usage examples for com.google.common.graph Network edges

Introduction

In this page you can find the example usage for com.google.common.graph Network edges.

Prototype

Set<E> edges();

Source Link

Document

Returns all edges in this network.

Usage

From source file:org.apache.beam.runners.dataflow.worker.graph.Networks.java

public static <N, E> String toDot(Network<N, E> network) {
    StringBuilder builder = new StringBuilder();
    builder.append("digraph network {\n");
    Map<N, String> nodeName = Maps.newIdentityHashMap();
    network.nodes().forEach(node -> nodeName.put(node, "n" + nodeName.size()));
    for (Entry<N, String> nodeEntry : nodeName.entrySet()) {
        builder.append(String.format("  %s [fontname=\"Courier New\" label=\"%s\"];\n", nodeEntry.getValue(),
                escapeDot(nodeEntry.getKey().toString())));
    }/*from  w w  w  . j  a  va 2  s  .c  om*/
    for (E edge : network.edges()) {
        EndpointPair<N> endpoints = network.incidentNodes(edge);
        builder.append(String.format("  %s -> %s [fontname=\"Courier New\" label=\"%s\"];\n",
                nodeName.get(endpoints.source()), nodeName.get(endpoints.target()),
                escapeDot(edge.toString())));
    }
    builder.append("}");
    return builder.toString();
}

From source file:org.apache.beam.runners.core.construction.graph.Networks.java

/**
 * Return a set of nodes in sorted topological order.
 *
 * <p>Note that back edges within directed graphs are "broken" returning a topological order for a
 * directed acyclic network which approximates the original network.
 *
 * <p>Nodes will be considered in the order specified by the {@link
 * ElementOrder#sorted(Comparator) sorted ElementOrder} created with the provided comparator.
 *//*w w w.  ja  v  a 2 s.c om*/
public static <NodeT, EdgeT> Iterable<NodeT> topologicalOrder(Network<NodeT, EdgeT> network,
        Comparator<NodeT> nodeOrder) {
    // Copy the characteristics of the network to ensure that the result network can represent the
    // original network, just with the provided suborder
    MutableNetwork<NodeT, EdgeT> orderedNetwork = NetworkBuilder.from(network)
            .nodeOrder(ElementOrder.sorted(nodeOrder)).build();
    for (NodeT node : network.nodes()) {
        orderedNetwork.addNode(node);
    }
    for (EdgeT edge : network.edges()) {
        EndpointPair<NodeT> incident = network.incidentNodes(edge);
        orderedNetwork.addEdge(incident.source(), incident.target(), edge);
    }
    return computeTopologicalOrder(orderedNetwork);
}

From source file:org.apache.beam.runners.core.construction.graph.Networks.java

public static <NodeT, EdgeT> String toDot(Network<NodeT, EdgeT> network) {
    StringBuilder builder = new StringBuilder();
    builder.append(String.format("digraph network {%n"));
    Map<NodeT, String> nodeName = Maps.newIdentityHashMap();
    network.nodes().forEach(node -> nodeName.put(node, "n" + nodeName.size()));
    for (Entry<NodeT, String> nodeEntry : nodeName.entrySet()) {
        builder.append(String.format("  %s [fontname=\"Courier New\" label=\"%s\"];%n", nodeEntry.getValue(),
                escapeDot(nodeEntry.getKey().toString())));
    }//www .j  a  v  a2  s  .co  m
    for (EdgeT edge : network.edges()) {
        EndpointPair<NodeT> endpoints = network.incidentNodes(edge);
        builder.append(String.format("  %s -> %s [fontname=\"Courier New\" label=\"%s\"];%n",
                nodeName.get(endpoints.source()), nodeName.get(endpoints.target()),
                escapeDot(edge.toString())));
    }
    builder.append("}");
    return builder.toString();
}

From source file:edu.uci.ics.jung.visualization.renderers.BasicRenderer.java

@Override
public void render(RenderContext<N, E> renderContext, VisualizationModel<N, E> visualizationModel) {
    Network<N, E> network = visualizationModel.getNetwork();
    // paint all the edges
    try {//w w w  .ja va  2s  .c o  m
        for (E e : network.edges()) {
            renderEdge(renderContext, visualizationModel, e);
            renderEdgeLabel(renderContext, visualizationModel, e);
        }
    } catch (ConcurrentModificationException cme) {
        renderContext.getScreenDevice().repaint();
    }

    // paint all the nodes
    try {
        for (N v : network.nodes()) {
            renderNode(renderContext, visualizationModel, v);
            renderNodeLabel(renderContext, visualizationModel, v);
        }
    } catch (ConcurrentModificationException cme) {
        renderContext.getScreenDevice().repaint();
    }
}

From source file:edu.uci.ics.jung.visualization.BaseVisualizationModel.java

public void setNetwork(Network<N, E> network, boolean forceUpdate) {
    log.trace("setNetwork to n:{} e:{}", network.nodes(), network.edges());
    this.network = network;
    this.layoutModel.setGraph(network.asGraph());
    if (forceUpdate && this.layoutAlgorithm != null) {
        log.trace("will accept {}", layoutAlgorithm);
        layoutModel.accept(this.layoutAlgorithm);
        log.trace("will fire stateChanged");
        changeSupport.fireStateChanged();
        log.trace("fired stateChanged");
    }//from   ww w.  j a v  a2 s.  c o m
}

From source file:edu.uci.ics.jung.visualization.renderers.BasicRenderer.java

public void render(RenderContext<N, E> renderContext, VisualizationModel<N, E> visualizationModel,
        Spatial<N> nodeSpatial, Spatial<E> edgeSpatial) {
    if (nodeSpatial == null) {
        render(renderContext, visualizationModel);
        return;//  ww  w  . jav a 2  s .  c o m
    }
    Iterable<N> visibleNodes = null;
    Iterable<E> visibleEdges = null;

    try {
        visibleNodes = nodeSpatial
                .getVisibleElements(((VisualizationServer) renderContext.getScreenDevice()).viewOnLayout());

        if (edgeSpatial != null) {
            visibleEdges = edgeSpatial
                    .getVisibleElements(((VisualizationServer) renderContext.getScreenDevice()).viewOnLayout());
        } else {
            visibleEdges = visualizationModel.getNetwork().edges();
        }
    } catch (ConcurrentModificationException ex) {
        // skip rendering until graph node index is stable,
        // this can happen if the layout relax thread is changing locations while the
        // visualization is rendering
        log.info("got {} so returning", ex);
        return;
    }

    try {
        Network<N, E> network = visualizationModel.getNetwork();
        // paint all the edges
        log.trace("the visibleEdges are {}", visibleEdges);
        for (E e : visibleEdges) {
            if (network.edges().contains(e)) {
                renderEdge(renderContext, visualizationModel, e);
                renderEdgeLabel(renderContext, visualizationModel, e);
            }
        }
    } catch (ConcurrentModificationException cme) {
        renderContext.getScreenDevice().repaint();
    }

    // paint all the nodes
    try {
        log.trace("the visibleNodes are {}", visibleNodes);

        for (N v : visibleNodes) {
            renderNode(renderContext, visualizationModel, v);
            renderNodeLabel(renderContext, visualizationModel, v);
        }
    } catch (ConcurrentModificationException cme) {
        renderContext.getScreenDevice().repaint();
    }
}

From source file:dagger.model.BindingGraph.java

/**
 * Returns the edges for entry points that transitively depend on a binding or missing binding for
 * a key. Never returns an empty set./* w w w.j a  v  a  2  s  .  c  om*/
 */
public final ImmutableSet<DependencyEdge> entryPointEdgesDependingOnBinding(MaybeBinding binding) {
    ImmutableNetwork<Node, DependencyEdge> dependencyGraph = dependencyGraph();
    Network<Node, DependencyEdge> subgraphDependingOnBinding = inducedSubgraph(dependencyGraph,
            reachableNodes(transpose(dependencyGraph).asGraph(), binding));
    ImmutableSet<DependencyEdge> entryPointEdges = intersection(entryPointEdges(),
            subgraphDependingOnBinding.edges()).immutableCopy();
    verify(!entryPointEdges.isEmpty(), "No entry points depend on binding %s", binding);
    return entryPointEdges;
}