Example usage for com.google.common.graph EndpointPair target

List of usage examples for com.google.common.graph EndpointPair target

Introduction

In this page you can find the example usage for com.google.common.graph EndpointPair target.

Prototype

public abstract N target();

Source Link

Document

If this EndpointPair #isOrdered() , returns the node which is the target.

Usage

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  .  j  ava2s.  c  o m
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.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 ww  .  j av  a 2s .  c  o  m*/
    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

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())));
    }/*  w ww. j  av a 2s  .  c  om*/
    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:dagger.internal.codegen.IncorrectlyInstalledBindsMethodsValidator.java

private ImmutableNetwork<Node, Edge> dependencyGraph(BindingGraph graph) {
    MutableNetwork<Node, Edge> dependencyGraph = NetworkBuilder.from(graph).build();
    for (DependencyEdge dependencyEdge : graph.dependencyEdges()) {
        EndpointPair<Node> endpoint = graph.incidentNodes(dependencyEdge);
        dependencyGraph.addEdge(endpoint.source(), endpoint.target(), dependencyEdge);
    }//from   w  ww . j a v  a 2  s  . c  o m
    return ImmutableNetwork.copyOf(dependencyGraph);
}

From source file:dagger.internal.codegen.BindingCycleValidation.java

/**
 * Returns one of the edges between two nodes that doesn't {@linkplain
 * #breaksCycle(DependencyEdge, BindingGraph) break} a cycle.
 *//*from w  w  w .j  av a 2  s  .  c  o  m*/
private DependencyEdge nonCycleBreakingEdge(EndpointPair<Node> endpointPair, BindingGraph graph) {
    return graph.edgesConnecting(endpointPair.source(), endpointPair.target()).stream()
            .flatMap(instancesOf(DependencyEdge.class)).filter(edge -> !breaksCycle(edge, graph)).findFirst()
            .get();
}

From source file:dagger.internal.codegen.DependencyCycleValidator.java

/**
 * Returns one of the edges between two nodes that doesn't {@linkplain
 * #breaksCycle(DependencyEdge, BindingGraph) break} a cycle.
 *//*  ww  w.  j  a  v  a  2s .  co  m*/
private DependencyEdge nonCycleBreakingEdge(EndpointPair<Node> endpointPair, BindingGraph graph) {
    return graph.network().edgesConnecting(endpointPair.source(), endpointPair.target()).stream()
            .flatMap(instancesOf(DependencyEdge.class)).filter(edge -> !breaksCycle(edge, graph)).findFirst()
            .get();
}

From source file:dagger.internal.codegen.BindingCycleValidation.java

/** Returns the subgraph containing only {@link DependencyEdge}s that would not break a cycle. */
private ImmutableNetwork<Node, DependencyEdge> nonCycleBreakingDependencyGraph(BindingGraph bindingGraph) {
    MutableNetwork<Node, DependencyEdge> dependencyNetwork = NetworkBuilder.from(bindingGraph)
            .expectedNodeCount(bindingGraph.nodes().size())
            .expectedEdgeCount(bindingGraph.dependencyEdges().size()).build();
    bindingGraph.dependencyEdges().stream().filter(edge -> !breaksCycle(edge, bindingGraph)).forEach(edge -> {
        EndpointPair<Node> endpoints = bindingGraph.incidentNodes(edge);
        dependencyNetwork.addEdge(endpoints.source(), endpoints.target(), edge);
    });/*from   w ww.j  a  v  a 2  s . co  m*/
    return ImmutableNetwork.copyOf(dependencyNetwork);
}

From source file:dagger.internal.codegen.DependencyCycleValidator.java

/** Returns the subgraph containing only {@link DependencyEdge}s that would not break a cycle. */
private ImmutableNetwork<Node, DependencyEdge> nonCycleBreakingDependencyGraph(BindingGraph bindingGraph) {
    MutableNetwork<Node, DependencyEdge> dependencyNetwork = NetworkBuilder.from(bindingGraph.network())
            .expectedNodeCount(bindingGraph.network().nodes().size())
            .expectedEdgeCount(bindingGraph.dependencyEdges().size()).build();
    bindingGraph.dependencyEdges().stream().filter(edge -> !breaksCycle(edge, bindingGraph)).forEach(edge -> {
        EndpointPair<Node> endpoints = bindingGraph.network().incidentNodes(edge);
        dependencyNetwork.addEdge(endpoints.source(), endpoints.target(), edge);
    });/*from  w ww.  j ava2  s  .c  om*/
    return ImmutableNetwork.copyOf(dependencyNetwork);
}

From source file:dagger.model.BindingGraph.java

private ImmutableNetwork<Node, DependencyEdge> dependencyGraph() {
    MutableNetwork<Node, DependencyEdge> dependencyGraph = NetworkBuilder.from(network())
            .expectedNodeCount(network().nodes().size()).expectedEdgeCount((int) dependencyEdgeStream().count())
            .build();/*from w w w  .j a  v  a2  s . c  om*/
    dependencyEdgeStream().forEach(edge -> {
        EndpointPair<Node> endpoints = network().incidentNodes(edge);
        dependencyGraph.addEdge(endpoints.source(), endpoints.target(), edge);
    });
    return ImmutableNetwork.copyOf(dependencyGraph);
}

From source file:dagger.internal.codegen.DependencyCycleValidator.java

private Optional<Cycle<Node>> cycleContainingEndpointPair(EndpointPair<Node> endpoints,
        ImmutableNetwork<Node, DependencyEdge> dependencyGraph, Set<EndpointPair<Node>> visited) {
    if (!visited.add(endpoints)) {
        // don't recheck endpoints we already know are part of a cycle
        return Optional.empty();
    }//from  w  w  w .  ja v  a2s .c  o  m

    // If there's a path from the target back to the source, there's a cycle.
    ImmutableList<Node> cycleNodes = shortestPath(dependencyGraph, endpoints.target(), endpoints.source());
    if (cycleNodes.isEmpty()) {
        return Optional.empty();
    }

    Cycle<Node> cycle = Cycle.fromPath(cycleNodes);
    visited.addAll(cycle.endpointPairs()); // no need to check any edge in this cycle again
    return Optional.of(cycle);
}