Example usage for com.google.common.graph ImmutableNetwork copyOf

List of usage examples for com.google.common.graph ImmutableNetwork copyOf

Introduction

In this page you can find the example usage for com.google.common.graph ImmutableNetwork copyOf.

Prototype

@Deprecated
public static <N, E> ImmutableNetwork<N, E> copyOf(ImmutableNetwork<N, E> graph) 

Source Link

Document

Simply returns its argument.

Usage

From source file:dagger.model.BindingGraph.java

static BindingGraph create(Network<Node, Edge> network, boolean isModuleBindingGraph,
        boolean isPartialBindingGraph) {
    return new AutoValue_BindingGraph(ImmutableNetwork.copyOf(network), isModuleBindingGraph,
            isPartialBindingGraph);//ww  w. j av a 2 s. c  om
}

From source file:org.jgrapht.graph.guava.ImmutableNetworkAdapter.java

/**
 * Returns a shallow copy of this graph instance. Neither edges nor vertices are cloned.
 *
 * @return a shallow copy of this graph.
 *
 * @throws RuntimeException in case the clone is not supported
 *
 * @see java.lang.Object#clone()/*from   www  .j a  v  a 2  s . c o m*/
 */
@Override
public Object clone() {
    try {
        ImmutableNetworkAdapter<V, E> newGraph = TypeUtil.uncheckedCast(super.clone());

        newGraph.vertexSupplier = this.vertexSupplier;
        newGraph.edgeSupplier = this.edgeSupplier;
        newGraph.unmodifiableVertexSet = null;
        newGraph.unmodifiableEdgeSet = null;
        newGraph.network = ImmutableNetwork.copyOf(Graphs.copyOf(this.network));

        return newGraph;
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
        throw new RuntimeException();
    }
}

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  w w.j a v  a  2s .c o m*/
    return ImmutableNetwork.copyOf(dependencyGraph);
}

From source file:org.jgrapht.graph.guava.ImmutableNetworkAdapter.java

@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
    ois.defaultReadObject();/*from ww w.  j  a  va 2  s  .  c  om*/

    GraphType type = (GraphType) ois.readObject();
    if (type.isMixed()) {
        throw new IOException("Graph type not supported");
    }

    MutableNetwork<V, E> mutableNetwork = (type.isDirected() ? NetworkBuilder.directed()
            : NetworkBuilder.undirected()).allowsParallelEdges(type.isAllowingMultipleEdges())
                    .allowsSelfLoops(type.isAllowingSelfLoops()).build();

    // read vertices
    int n = ois.readInt();
    for (int i = 0; i < n; i++) {
        V v = (V) ois.readObject();
        mutableNetwork.addNode(v);
    }

    // read edges
    int m = ois.readInt();
    for (int i = 0; i < m; i++) {
        V s = (V) ois.readObject();
        V t = (V) ois.readObject();
        E e = (E) ois.readObject();
        mutableNetwork.addEdge(s, t, e);
    }

    // setup the immutable copy
    this.network = ImmutableNetwork.copyOf(mutableNetwork);
}

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

@Override
public MutableNetwork<Node, Edge> apply(MutableNetwork<Node, Edge> network) {

    // Record all SDK nodes, and all root nodes.
    Set<Node> runnerRootNodes = new HashSet<>();
    Set<Node> sdkNodes = new HashSet<>();
    Set<Node> sdkRootNodes = new HashSet<>();
    for (ParallelInstructionNode node : Iterables.filter(network.nodes(), ParallelInstructionNode.class)) {
        if (executesInSdkHarness(node)) {
            sdkNodes.add(node);//from w w  w .j  a v  a2  s  .  c o m
            if (network.inDegree(node) == 0) {
                sdkRootNodes.add(node);
            }
        } else if (network.inDegree(node) == 0) {
            runnerRootNodes.add(node);
        }
    }

    // If nothing executes within the SDK harness, return the original network.
    if (sdkNodes.isEmpty()) {
        return network;
    }

    // Represents the set of nodes which represent gRPC boundaries from the Runner to the SDK.
    Set<Node> runnerToSdkBoundaries = new HashSet<>();
    // Represents the set of nodes which represent gRPC boundaries from the SDK to the Runner.
    Set<Node> sdkToRunnerBoundaries = new HashSet<>();

    ImmutableNetwork<Node, Edge> originalNetwork = ImmutableNetwork.copyOf(network);

    // Update the network with outputs which are meant to bridge the instructions
    // that execute in different harnesses. One output per direction of information
    // flow from runner to SDK and SDK to runner per original output node.
    for (InstructionOutputNode outputNode : Iterables.filter(originalNetwork.nodes(),
            InstructionOutputNode.class)) {

        // Categorize all predecessor instructions
        Set<Node> predecessorRunnerInstructions = new HashSet<>();
        Set<Node> predecessorSdkInstructions = new HashSet<>();
        for (Node predecessorInstruction : originalNetwork.predecessors(outputNode)) {
            if (sdkNodes.contains(predecessorInstruction)) {
                predecessorSdkInstructions.add(predecessorInstruction);
            } else {
                predecessorRunnerInstructions.add(predecessorInstruction);
            }
        }

        // Categorize all successor instructions
        Set<Node> successorRunnerInstructions = new HashSet<>();
        Set<Node> successorSdkInstructions = new HashSet<>();
        for (Node successorInstruction : originalNetwork.successors(outputNode)) {
            if (sdkNodes.contains(successorInstruction)) {
                successorSdkInstructions.add(successorInstruction);
            } else {
                successorRunnerInstructions.add(successorInstruction);
            }
        }

        // If there is data that will be flowing from the Runner to the SDK, rewire network to have
        // nodes connected across a gRPC node. Also add the gRPC node as an SDK root.
        if (!predecessorRunnerInstructions.isEmpty() && !successorSdkInstructions.isEmpty()) {
            runnerToSdkBoundaries.add(rewireAcrossSdkRunnerPortNode(network, outputNode,
                    predecessorRunnerInstructions, successorSdkInstructions));
        }

        // If there is data that will be flowing from the SDK to the Runner, rewire network to have
        // nodes connected across a gRPC node.
        if (!predecessorSdkInstructions.isEmpty() && !successorRunnerInstructions.isEmpty()) {
            sdkToRunnerBoundaries.add(rewireAcrossSdkRunnerPortNode(network, outputNode,
                    predecessorSdkInstructions, successorRunnerInstructions));
        }

        // Remove original output node if it was rewired because it will have become disconnected
        // through the new output node.
        if (network.inDegree(outputNode) == 0) {
            network.removeNode(outputNode);
        }
    }

    // Create the subnetworks that represent potentially multiple fused SDK portions and a single
    // fused Runner portion replacing the SDK portion that is embedded within the Runner portion
    // with a RegisterFnOperation, adding edges to maintain proper happens before relationships.
    Set<Node> allRunnerNodes = Networks.reachableNodes(network,
            Sets.union(runnerRootNodes, sdkToRunnerBoundaries), runnerToSdkBoundaries);
    if (this.useExecutableStageBundleExecution) {
        // When using shared library, there is no grpc node in runner graph.
        allRunnerNodes = Sets.difference(allRunnerNodes,
                Sets.union(runnerToSdkBoundaries, sdkToRunnerBoundaries));
    }
    MutableNetwork<Node, Edge> runnerNetwork = Graphs.inducedSubgraph(network, allRunnerNodes);

    // TODO: Reduce the amount of 'copying' of SDK nodes by breaking potential cycles
    // between the SDK networks and the Runner network. Cycles can occur because entire
    // SDK subnetworks are replaced by a singular node within the Runner network.
    // khines@ suggested to look at go/priority-based-fusion for an algorithm based upon
    // using poison paths.
    for (Node sdkRoot : Sets.union(sdkRootNodes, runnerToSdkBoundaries)) {
        Set<Node> sdkSubnetworkNodes = Networks.reachableNodes(network, ImmutableSet.of(sdkRoot),
                sdkToRunnerBoundaries);
        MutableNetwork<Node, Edge> sdkNetwork = Graphs.inducedSubgraph(network, sdkSubnetworkNodes);
        Node registerFnNode = registerFnOperationFunction.apply(sdkNetwork);

        runnerNetwork.addNode(registerFnNode);
        // Create happens before relationships between all Runner and SDK nodes which are in the
        // SDK subnetwork; direction dependent on whether its a predecessor of the SDK subnetwork or
        // a successor.
        if (this.useExecutableStageBundleExecution) {
            // When using shared library, there is no gprc node in runner graph. Then the registerFnNode
            // should be linked directly to 2 OutputInstruction nodes.
            for (Node predecessor : Sets.intersection(sdkSubnetworkNodes, runnerToSdkBoundaries)) {
                predecessor = network.predecessors(predecessor).iterator().next();
                runnerNetwork.addEdge(predecessor, registerFnNode, HappensBeforeEdge.create());
            }
            for (Node successor : Sets.intersection(sdkSubnetworkNodes, sdkToRunnerBoundaries)) {
                successor = network.successors(successor).iterator().next();
                runnerNetwork.addEdge(registerFnNode, successor, HappensBeforeEdge.create());
            }
        } else {
            for (Node predecessor : Sets.intersection(sdkSubnetworkNodes, runnerToSdkBoundaries)) {
                runnerNetwork.addEdge(predecessor, registerFnNode, HappensBeforeEdge.create());
            }
            for (Node successor : Sets.intersection(sdkSubnetworkNodes, sdkToRunnerBoundaries)) {
                runnerNetwork.addEdge(registerFnNode, successor, HappensBeforeEdge.create());
            }
        }
    }

    return runnerNetwork;
}

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);
    });/* w  w  w.  j  ava 2 s . c  o 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);
    });/*  w  ww . j a  va2 s.c  o  m*/
    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  ww  .  j  a  v  a2  s  .com
    dependencyEdgeStream().forEach(edge -> {
        EndpointPair<Node> endpoints = network().incidentNodes(edge);
        dependencyGraph.addEdge(endpoints.source(), endpoints.target(), edge);
    });
    return ImmutableNetwork.copyOf(dependencyGraph);
}