List of usage examples for com.google.common.graph MutableNetwork addNode
@CanIgnoreReturnValue
boolean addNode(N node);
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. *//*from ww w .j a va 2s . 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
/** * Applies the {@code function} to all nodes within the {@code network}. Replaces any node which * is not {@link #equals(Object)} to the original node, maintaining all existing edges between * nodes.//from ww w. j ava 2s .c o m */ public static <N, E> void replaceDirectedNetworkNodes(MutableNetwork<N, E> network, Function<N, N> function) { checkArgument(network.isDirected(), "Only directed networks are supported, given %s", network); checkArgument(!network.allowsSelfLoops(), "Only networks without self loops are supported, given %s", network); // A map from the existing node to the replacement node Map<N, N> oldNodesToNewNodes = new HashMap<>(network.nodes().size()); for (N currentNode : network.nodes()) { N newNode = function.apply(currentNode); // Skip updating the network if the old node is equivalent to the new node if (!currentNode.equals(newNode)) { oldNodesToNewNodes.put(currentNode, newNode); } } // For each replacement, connect up the existing predecessors and successors to the new node // and then remove the old node. for (Map.Entry<N, N> entry : oldNodesToNewNodes.entrySet()) { N oldNode = entry.getKey(); N newNode = entry.getValue(); network.addNode(newNode); for (N predecessor : ImmutableSet.copyOf(network.predecessors(oldNode))) { for (E edge : ImmutableSet.copyOf(network.edgesConnecting(predecessor, oldNode))) { network.removeEdge(edge); network.addEdge(predecessor, newNode, edge); } } for (N successor : ImmutableSet.copyOf(network.successors(oldNode))) { for (E edge : ImmutableSet.copyOf(network.edgesConnecting(oldNode, successor))) { network.removeEdge(edge); network.addEdge(newNode, successor, edge); } } network.removeNode(oldNode); } }
From source file:edu.uci.ics.jung.samples.SpatialLensLargeGraphDemo.java
Network<String, Number> buildOneNode() {
MutableNetwork<String, Number> graph = NetworkBuilder.directed().allowsParallelEdges(true).build();
graph.addNode("A");
return graph;
}
From source file:org.apache.beam.runners.core.construction.graph.Networks.java
/** * Applies the {@code function} to all nodes within the {@code network}. Replaces any node which * is not {@link #equals(Object)} to the original node, maintaining all existing edges between * nodes.//from ww w. j av a2 s .com */ public static <NodeT, EdgeT> void replaceDirectedNetworkNodes(MutableNetwork<NodeT, EdgeT> network, Function<NodeT, NodeT> function) { checkArgument(network.isDirected(), "Only directed networks are supported, given %s", network); checkArgument(!network.allowsSelfLoops(), "Only networks without self loops are supported, given %s", network); // A map from the existing node to the replacement node Map<NodeT, NodeT> oldNodesToNewNodes = new HashMap<>(network.nodes().size()); for (NodeT currentNode : network.nodes()) { NodeT newNode = function.apply(currentNode); // Skip updating the network if the old node is equivalent to the new node if (!currentNode.equals(newNode)) { oldNodesToNewNodes.put(currentNode, newNode); } } // For each replacement, connect up the existing predecessors and successors to the new node // and then remove the old node. for (Map.Entry<NodeT, NodeT> entry : oldNodesToNewNodes.entrySet()) { NodeT oldNode = entry.getKey(); NodeT newNode = entry.getValue(); network.addNode(newNode); for (NodeT predecessor : ImmutableSet.copyOf(network.predecessors(oldNode))) { for (EdgeT edge : ImmutableSet.copyOf(network.edgesConnecting(predecessor, oldNode))) { network.removeEdge(edge); network.addEdge(predecessor, newNode, edge); } } for (NodeT successor : ImmutableSet.copyOf(network.successors(oldNode))) { for (EdgeT edge : ImmutableSet.copyOf(network.edgesConnecting(oldNode, successor))) { network.removeEdge(edge); network.addEdge(newNode, successor, edge); } } network.removeNode(oldNode); } }
From source file:edu.uci.ics.jung.samples.SubLayoutDemo.java
private void cluster(boolean state) { if (state) {/*from w w w . j a v a 2 s. com*/ // put the picked nodes into a new sublayout Collection<String> picked = ps.getPicked(); if (picked.size() > 1) { Point2D center = new Point2D.Double(); double x = 0; double y = 0; for (String node : picked) { Point p = clusteringLayoutModel.apply(node); x += p.x; y += p.y; } x /= picked.size(); y /= picked.size(); center.setLocation(x, y); MutableNetwork<String, Number> subGraph; try { subGraph = NetworkBuilder.from(graph).build(); for (String node : picked) { subGraph.addNode(node); for (Number edge : graph.incidentEdges(node)) { EndpointPair<String> endpoints = graph.incidentNodes(edge); String nodeU = endpoints.nodeU(); String nodeV = endpoints.nodeV(); if (picked.contains(nodeU) && picked.contains(nodeV)) { // put this edge into the subgraph subGraph.addEdge(nodeU, nodeV, edge); } } } LayoutAlgorithm<String> subLayoutAlgorithm = getLayoutAlgorithmFor(subLayoutType); LayoutModel<String> newLayoutModel = LoadingCacheLayoutModel.<String>builder() .setGraph(subGraph.asGraph()).setSize(subLayoutSize.width, subLayoutSize.height) .setInitializer( new RandomLocationTransformer<>(subLayoutSize.width, subLayoutSize.height, 0)) .build(); clusteringLayoutModel.put(newLayoutModel, Point.of(center.getX(), center.getY())); newLayoutModel.accept(subLayoutAlgorithm); vv.repaint(); } catch (Exception e) { e.printStackTrace(); } } } else { // remove all sublayouts this.clusteringLayoutModel.removeAll(); vv.repaint(); } }
From source file:org.jgrapht.graph.guava.ImmutableNetworkAdapter.java
@SuppressWarnings("unchecked") private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject();/*from w w w .java 2 s .c o m*/ 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.CloneAmbiguousFlattensFunction.java
/** * A helper function which performs the actual cloning procedure, which means creating the runner * and SDK versions of both the ambiguous flatten and its PCollection, attaching the old flatten's * predecessors and successors properly, and then removing the ambiguous flatten from the network. *//* w w w. j a v a2 s . c o m*/ private void cloneFlatten(Node flatten, MutableNetwork<Node, Edge> network) { // Start by creating the clones of the flatten and its PCollection. InstructionOutputNode flattenOut = (InstructionOutputNode) Iterables .getOnlyElement(network.successors(flatten)); ParallelInstruction flattenInstruction = ((ParallelInstructionNode) flatten).getParallelInstruction(); Node runnerFlatten = ParallelInstructionNode.create(flattenInstruction, ExecutionLocation.RUNNER_HARNESS); Node runnerFlattenOut = InstructionOutputNode.create(flattenOut.getInstructionOutput(), flattenOut.getPcollectionId()); network.addNode(runnerFlatten); network.addNode(runnerFlattenOut); Node sdkFlatten = ParallelInstructionNode.create(flattenInstruction, ExecutionLocation.SDK_HARNESS); Node sdkFlattenOut = InstructionOutputNode.create(flattenOut.getInstructionOutput(), flattenOut.getPcollectionId()); network.addNode(sdkFlatten); network.addNode(sdkFlattenOut); for (Edge edge : ImmutableList.copyOf(network.edgesConnecting(flatten, flattenOut))) { network.addEdge(runnerFlatten, runnerFlattenOut, edge.clone()); network.addEdge(sdkFlatten, sdkFlattenOut, edge.clone()); } // Copy over predecessor edges to both cloned nodes. for (Node predecessor : network.predecessors(flatten)) { for (Edge edge : ImmutableList.copyOf(network.edgesConnecting(predecessor, flatten))) { network.addEdge(predecessor, runnerFlatten, edge.clone()); network.addEdge(predecessor, sdkFlatten, edge.clone()); } } // Copy over successor edges depending on execution locations of successors. for (Node successor : network.successors(flattenOut)) { // Connect successor to SDK harness only if sure it executes in SDK. Node selectedOutput = executesInSdkHarness(successor) ? sdkFlattenOut : runnerFlattenOut; for (Edge edge : ImmutableList.copyOf(network.edgesConnecting(flattenOut, successor))) { network.addEdge(selectedOutput, successor, edge.clone()); } } network.removeNode(flatten); network.removeNode(flattenOut); }
From source file:org.apache.beam.runners.core.construction.graph.QueryablePipeline.java
private MutableNetwork<PipelineNode, PipelineEdge> buildNetwork(Collection<String> transformIds, Components components) {//from ww w .j ava2s . c o m MutableNetwork<PipelineNode, PipelineEdge> network = NetworkBuilder.directed().allowsParallelEdges(true) .allowsSelfLoops(false).build(); Set<PCollectionNode> unproducedCollections = new HashSet<>(); for (String transformId : transformIds) { PTransform transform = components.getTransformsOrThrow(transformId); PTransformNode transformNode = PipelineNode.pTransform(transformId, this.components.getTransformsOrThrow(transformId)); network.addNode(transformNode); for (String produced : transform.getOutputsMap().values()) { PCollectionNode producedNode = PipelineNode.pCollection(produced, components.getPcollectionsOrThrow(produced)); network.addNode(producedNode); network.addEdge(transformNode, producedNode, new PerElementEdge()); checkArgument(network.inDegree(producedNode) == 1, "A %s should have exactly one producing %s, but found %s:\nPCollection:\n%s\nProducers:\n%s", PCollectionNode.class.getSimpleName(), PTransformNode.class.getSimpleName(), network.predecessors(producedNode).size(), producedNode, network.predecessors(producedNode)); unproducedCollections.remove(producedNode); } for (Map.Entry<String, String> consumed : transform.getInputsMap().entrySet()) { // This loop may add an edge between the consumed PCollection and the current PTransform. // The local name of the transform must be used to determine the type of edge. String pcollectionId = consumed.getValue(); PCollectionNode consumedNode = PipelineNode.pCollection(pcollectionId, this.components.getPcollectionsOrThrow(pcollectionId)); if (network.addNode(consumedNode)) { // This node has been added to the network for the first time, so it has no producer. unproducedCollections.add(consumedNode); } if (getLocalSideInputNames(transform).contains(consumed.getKey())) { network.addEdge(consumedNode, transformNode, new SingletonEdge()); } else { network.addEdge(consumedNode, transformNode, new PerElementEdge()); } } } checkArgument(unproducedCollections.isEmpty(), "%ss %s were consumed but never produced", PCollectionNode.class.getSimpleName(), unproducedCollections); return network; }
From source file:org.apache.beam.runners.dataflow.worker.graph.CreateRegisterFnOperationFunction.java
/** * Rewires the given set of predecessors and successors across a gRPC port surrounded by output * nodes. Edges to the remaining successors are copied over to the new output node that is placed * before the port node. For example://from ww w . j a va 2 s .c om * * <pre><code> * predecessors --> outputNode --> successors * \--> existingSuccessors * </pre></code> becomes: * * <pre><code> * * outputNode -------------------------------\ * \ \ * |-> existingSuccessors \ * / \ * predecessors --> newPredecessorOutputNode --> portNode --> portOutputNode --> successors}. * </code></pre> */ private Node rewireAcrossSdkRunnerPortNode(MutableNetwork<Node, Edge> network, InstructionOutputNode outputNode, Set<Node> predecessors, Set<Node> successors) { InstructionOutputNode newPredecessorOutputNode = InstructionOutputNode .create(outputNode.getInstructionOutput(), outputNode.getPcollectionId()); InstructionOutputNode portOutputNode = InstructionOutputNode.create(outputNode.getInstructionOutput(), outputNode.getPcollectionId()); String predecessorPortEdgeId = idGenerator.getId(); String successorPortEdgeId = idGenerator.getId(); Node portNode = portSupplier.apply(predecessorPortEdgeId, successorPortEdgeId); network.addNode(newPredecessorOutputNode); network.addNode(portNode); for (Node predecessor : predecessors) { for (Edge edge : ImmutableList.copyOf(network.edgesConnecting(predecessor, outputNode))) { network.removeEdge(edge); network.addEdge(predecessor, newPredecessorOutputNode, edge); } } // Maintain edges for existing successors. List<Node> existingSuccessors = ImmutableList .copyOf(Sets.difference(network.successors(outputNode), successors)); for (Node existingSuccessor : existingSuccessors) { List<Edge> existingSuccessorEdges = ImmutableList .copyOf(network.edgesConnecting(outputNode, existingSuccessor)); for (Edge existingSuccessorEdge : existingSuccessorEdges) { network.addEdge(newPredecessorOutputNode, existingSuccessor, existingSuccessorEdge.clone()); } } // Rewire the requested successors over the port node. network.addEdge(newPredecessorOutputNode, portNode, MultiOutputInfoEdge.create(new MultiOutputInfo().setTag(predecessorPortEdgeId))); network.addEdge(portNode, portOutputNode, MultiOutputInfoEdge.create(new MultiOutputInfo().setTag(successorPortEdgeId))); for (Node successor : successors) { for (Edge edge : ImmutableList.copyOf(network.edgesConnecting(outputNode, successor))) { network.addEdge(portOutputNode, successor, edge.clone()); } } return portNode; }
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 ww . j ava 2 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; }