Search in sources :

Example 11 with Edge

use of com.baidu.hugegraph.computer.core.graph.edge.Edge in project hugegraph-computer by hugegraph.

the class ClosenessCentrality method compute0.

@Override
public void compute0(ComputationContext context, Vertex vertex) {
    // Set empty map as initial value
    vertex.value(new ClosenessValue());
    // Send messages to adjacent edges
    for (Edge edge : vertex.edges()) {
        Id senderId = vertex.id();
        // Get property value
        double value = this.weightValue(edge.property(this.weightProp));
        DoubleValue distance = new DoubleValue(value);
        ClosenessMessage message = new ClosenessMessage(senderId, senderId, distance);
        context.sendMessage(edge.targetId(), message);
    }
}
Also used : DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) Id(com.baidu.hugegraph.computer.core.graph.id.Id) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge)

Example 12 with Edge

use of com.baidu.hugegraph.computer.core.graph.edge.Edge in project hugegraph-computer by hugegraph.

the class EdgeMessageRecvPartitionTest method addTenDuplicateEdgeBuffer.

private static void addTenDuplicateEdgeBuffer(Consumer<NetworkBuffer> consumer) throws IOException {
    for (long i = 0L; i < 10L; i++) {
        Vertex vertex = graphFactory().createVertex();
        vertex.id(BytesId.of(i));
        Edges edges = graphFactory().createEdges(2);
        for (long j = i + 1; j < i + 3; j++) {
            Edge edge = graphFactory().createEdge();
            edge.targetId(BytesId.of(j));
            Properties properties = graphFactory().createProperties();
            properties.put("p1", new LongValue(i));
            edge.properties(properties);
            edges.add(edge);
        }
        vertex.edges(edges);
        ReceiverUtil.consumeBuffer(writeEdges(vertex), consumer);
    }
    for (long i = 0L; i < 10L; i++) {
        Vertex vertex = graphFactory().createVertex();
        vertex.id(BytesId.of(i));
        Edges edges = graphFactory().createEdges(2);
        for (long j = i + 1; j < i + 3; j++) {
            Edge edge = graphFactory().createEdge();
            edge.targetId(BytesId.of(j));
            Properties properties = graphFactory().createProperties();
            properties.put("p2", new LongValue(2L * i));
            edge.properties(properties);
            edges.add(edge);
        }
        vertex.edges(edges);
        ReceiverUtil.consumeBuffer(writeEdges(vertex), consumer);
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties) Edges(com.baidu.hugegraph.computer.core.graph.edge.Edges) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge)

Example 13 with Edge

use of com.baidu.hugegraph.computer.core.graph.edge.Edge in project hugegraph-computer by hugegraph.

the class EdgeMessageRecvPartitionTest method writeEdges.

private static byte[] writeEdges(Vertex vertex) throws IOException {
    BytesOutput bytesOutput = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
    EntryOutput entryOutput = new EntryOutputImpl(bytesOutput);
    Id id = vertex.id();
    KvEntryWriter subKvWriter = entryOutput.writeEntry(out -> {
        id.write(out);
    });
    for (Edge edge : vertex.edges()) {
        Id targetId = edge.targetId();
        subKvWriter.writeSubKv(out -> {
            targetId.write(out);
        }, out -> {
            edge.properties().write(out);
        });
    }
    subKvWriter.writeFinish();
    return bytesOutput.toByteArray();
}
Also used : EntryOutput(com.baidu.hugegraph.computer.core.store.entry.EntryOutput) EntryOutputImpl(com.baidu.hugegraph.computer.core.store.entry.EntryOutputImpl) BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) KvEntryWriter(com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge)

Example 14 with Edge

use of com.baidu.hugegraph.computer.core.graph.edge.Edge in project hugegraph-computer by hugegraph.

the class EdgeMessageRecvPartitionTest method addTenEdgeBuffer.

public static void addTenEdgeBuffer(Consumer<NetworkBuffer> consumer) throws IOException {
    for (long i = 0L; i < 10L; i++) {
        Vertex vertex = graphFactory().createVertex();
        vertex.id(BytesId.of(i));
        Edges edges = graphFactory().createEdges(2);
        for (long j = i + 1; j < i + 3; j++) {
            Edge edge = graphFactory().createEdge();
            edge.targetId(BytesId.of(j));
            Properties properties = graphFactory().createProperties();
            properties.put("p1", new LongValue(i));
            edge.properties(properties);
            edges.add(edge);
        }
        vertex.edges(edges);
        ReceiverUtil.consumeBuffer(writeEdges(vertex), consumer);
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties) Edges(com.baidu.hugegraph.computer.core.graph.edge.Edges) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge)

Example 15 with Edge

use of com.baidu.hugegraph.computer.core.graph.edge.Edge in project hugegraph-computer by hugegraph.

the class DegreeCentrality method compute0.

@Override
public void compute0(ComputationContext context, Vertex vertex) {
    if (!this.calculateByWeightProperty) {
        vertex.value(new DoubleValue(vertex.numEdges()));
    } else {
        /*
             *  TODO: Here we use doubleValue type now, we will use BigDecimal
             *  and output "BigDecimalValue" to resolve double type overflow
             *  int the future;
             */
        double totalWeight = 0.0;
        Iterator<Edge> edges = vertex.edges().iterator();
        while (edges.hasNext()) {
            Edge edge = edges.next();
            double weight = weightValue(edge.property(this.weightProperty));
            totalWeight += weight;
            if (Double.isInfinite(totalWeight)) {
                throw new ComputerException("Calculate weight overflow," + "current is %s, edge '%s' " + "is %s", totalWeight, edge, weight);
            }
        }
        vertex.value(new DoubleValue(totalWeight));
    }
    vertex.inactivate();
}
Also used : DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Aggregations

Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)28 Id (com.baidu.hugegraph.computer.core.graph.id.Id)12 Edges (com.baidu.hugegraph.computer.core.graph.edge.Edges)11 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)11 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)10 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)8 ComputerException (com.baidu.hugegraph.computer.core.common.exception.ComputerException)5 IdListList (com.baidu.hugegraph.computer.core.graph.value.IdListList)4 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)3 DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)3 IdList (com.baidu.hugegraph.computer.core.graph.value.IdList)3 IOException (java.io.IOException)3 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)2 ComputerOptions (com.baidu.hugegraph.computer.core.config.ComputerOptions)2 EdgeFrequency (com.baidu.hugegraph.computer.core.config.EdgeFrequency)2 Value (com.baidu.hugegraph.computer.core.graph.value.Value)2 EntryOutput (com.baidu.hugegraph.computer.core.store.entry.EntryOutput)2 KvEntryWriter (com.baidu.hugegraph.computer.core.store.entry.KvEntryWriter)2 Config (com.baidu.hugegraph.computer.core.config.Config)1 GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)1