Example usage for com.google.common.graph NetworkBuilder directed

List of usage examples for com.google.common.graph NetworkBuilder directed

Introduction

In this page you can find the example usage for com.google.common.graph NetworkBuilder directed.

Prototype

boolean directed

To view the source code for com.google.common.graph NetworkBuilder directed.

Click Source Link

Usage

From source file:edu.uci.ics.jung.graph.TreeNetworkBuilder.java

/**
 * Returns an empty {@link MutableCTree} with the properties of this {@link TreeNetworkBuilder}.
 *//* www. j  a  va2  s  .c  o  m*/
// TODO(jrtom): decide how we're going to handle different implementations.
// For the graph stuff, we don't really need different implementations, but
// for trees, maybe we do; at least for binary trees vs. trees with no restrictions on outgoing edges...
public <N1 extends N, E1 extends E> MutableCTreeNetwork<N1, E1> build() {
    NetworkBuilder<Object, Object> graphBuilder = NetworkBuilder.directed().allowsSelfLoops(false)
            .allowsParallelEdges(false);
    if (expectedNodeCount.isPresent()) {
        graphBuilder = graphBuilder.expectedNodeCount(expectedNodeCount.get());
    }
    MutableNetwork<N1, E1> delegate = graphBuilder.nodeOrder(nodeOrder).edgeOrder(edgeOrder).build();
    @SuppressWarnings("unchecked")
    Optional<N1> rootCast = (Optional<N1>) root;
    return new DelegateCTreeNetwork<N1, E1>(delegate, rootCast);
}

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

@Override
public MutableNetwork<Node, Edge> apply(MapTask mapTask) {
    List<ParallelInstruction> parallelInstructions = Apiary.listOrEmpty(mapTask.getInstructions());
    MutableNetwork<Node, Edge> network = NetworkBuilder.directed().allowsSelfLoops(false)
            .allowsParallelEdges(true).expectedNodeCount(parallelInstructions.size() * 2).build();

    // Add all the instruction nodes and output nodes
    ParallelInstructionNode[] instructionNodes = new ParallelInstructionNode[parallelInstructions.size()];
    InstructionOutputNode[][] outputNodes = new InstructionOutputNode[parallelInstructions.size()][];
    for (int i = 0; i < parallelInstructions.size(); ++i) {
        // InstructionOutputNode's are the source of truth on instruction outputs.
        // Clear the instruction's outputs to reduce chance for confusion.
        List<InstructionOutput> outputs = Apiary.listOrEmpty(parallelInstructions.get(i).getOutputs());
        outputNodes[i] = new InstructionOutputNode[outputs.size()];

        JsonFactory factory = MoreObjects.firstNonNull(mapTask.getFactory(), Transport.getJsonFactory());
        ParallelInstruction parallelInstruction = clone(factory, parallelInstructions.get(i)).setOutputs(null);

        ParallelInstructionNode instructionNode = ParallelInstructionNode.create(parallelInstruction,
                Nodes.ExecutionLocation.UNKNOWN);
        instructionNodes[i] = instructionNode;
        network.addNode(instructionNode);

        // Connect the instruction node output to the output PCollection node
        for (int j = 0; j < outputs.size(); ++j) {
            InstructionOutput instructionOutput = outputs.get(j);
            InstructionOutputNode outputNode = InstructionOutputNode.create(instructionOutput,
                    "generatedPcollection" + this.idGenerator.getId());
            network.addNode(outputNode);
            if (parallelInstruction.getParDo() != null) {
                network.addEdge(instructionNode, outputNode, MultiOutputInfoEdge
                        .create(parallelInstruction.getParDo().getMultiOutputInfos().get(j)));
            } else {
                network.addEdge(instructionNode, outputNode, DefaultEdge.create());
            }/*w  w w.  j a va2 s  .  com*/
            outputNodes[i][j] = outputNode;
        }
    }

    // Connect PCollections as inputs to instructions
    for (ParallelInstructionNode instructionNode : instructionNodes) {
        ParallelInstruction parallelInstruction = instructionNode.getParallelInstruction();
        if (parallelInstruction.getFlatten() != null) {
            for (InstructionInput input : Apiary.listOrEmpty(parallelInstruction.getFlatten().getInputs())) {
                attachInput(input, network, instructionNode, outputNodes);
            }
        } else if (parallelInstruction.getParDo() != null) {
            attachInput(parallelInstruction.getParDo().getInput(), network, instructionNode, outputNodes);
        } else if (parallelInstruction.getPartialGroupByKey() != null) {
            attachInput(parallelInstruction.getPartialGroupByKey().getInput(), network, instructionNode,
                    outputNodes);
        } else if (parallelInstruction.getRead() != null) {
            // Reads have no inputs so nothing to do
        } else if (parallelInstruction.getWrite() != null) {
            attachInput(parallelInstruction.getWrite().getInput(), network, instructionNode, outputNodes);
        } else {
            throw new IllegalArgumentException(String.format("Unknown type of instruction %s for map task %s",
                    parallelInstruction, mapTask));
        }
    }
    return network;
}

From source file:edu.uci.ics.jung.samples.DrawnIconNodeDemo.java

Network<Integer, Number> createGraph() {
    MutableNetwork<Integer, Number> graph = NetworkBuilder.directed().build();
    graph.addEdge(0, 1, Math.random());
    graph.addEdge(3, 0, Math.random());
    graph.addEdge(0, 4, Math.random());
    graph.addEdge(4, 5, Math.random());
    graph.addEdge(5, 3, Math.random());
    graph.addEdge(2, 1, Math.random());
    graph.addEdge(4, 1, Math.random());
    graph.addEdge(8, 2, Math.random());
    graph.addEdge(3, 8, Math.random());
    graph.addEdge(6, 7, Math.random());
    graph.addEdge(7, 5, Math.random());
    graph.addEdge(0, 9, Math.random());
    graph.addEdge(9, 8, Math.random());
    graph.addEdge(7, 6, Math.random());
    graph.addEdge(6, 5, Math.random());
    graph.addEdge(4, 2, Math.random());
    graph.addEdge(5, 4, Math.random());
    graph.addEdge(4, 10, Math.random());
    graph.addEdge(10, 4, Math.random());

    return graph;
}

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  .  j av  a2s.  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.jgrapht.graph.guava.MutableNetworkAdapter.java

@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
    ois.defaultReadObject();/*from   w  ww .ja v  a2s.co m*/

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

    this.network = (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();
        network.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();
        network.addEdge(s, t, e);
    }
}

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

private MutableNetwork<PipelineNode, PipelineEdge> buildNetwork(Collection<String> transformIds,
        Components components) {/*from  w w  w . jav  a2s .co  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:edu.uci.ics.jung.samples.LensNodeImageShaperDemo.java

Network<Number, Number> createGraph() {
    MutableNetwork<Number, Number> graph = NetworkBuilder.directed().build();
    graph.addEdge(0, 1, Math.random());
    graph.addEdge(3, 0, Math.random());
    graph.addEdge(0, 4, Math.random());
    graph.addEdge(4, 5, Math.random());
    graph.addEdge(5, 3, Math.random());
    graph.addEdge(2, 1, Math.random());
    graph.addEdge(4, 1, Math.random());
    graph.addEdge(8, 2, Math.random());
    graph.addEdge(3, 8, Math.random());
    graph.addEdge(6, 7, Math.random());
    graph.addEdge(7, 5, Math.random());
    graph.addEdge(0, 9, Math.random());
    graph.addEdge(9, 8, Math.random());
    graph.addEdge(7, 6, Math.random());
    graph.addEdge(6, 5, Math.random());
    graph.addEdge(4, 2, Math.random());
    graph.addEdge(5, 4, Math.random());
    graph.addEdge(4, 10, Math.random());
    graph.addEdge(10, 4, Math.random());

    return graph;
}

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;
}