Search in sources :

Example 56 with Id

use of com.baidu.hugegraph.computer.core.graph.id.Id in project hugegraph-computer by hugegraph.

the class StreamGraphOutputInputTest method testWriteReadMessage.

@Test
public void testWriteReadMessage() throws IOException {
    UnitTestBase.updateOptions(ComputerOptions.ALGORITHM_MESSAGE_CLASS, DoubleValue.class.getName());
    Id id = BytesId.of(999L);
    Value value = new DoubleValue(0.85D);
    byte[] bytes;
    try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
        StreamGraphOutput output = newStreamGraphOutput(bao);
        output.writeMessage(id, value);
        bytes = bao.toByteArray();
        System.out.println(Arrays.toString(bytes));
    }
    try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
        StreamGraphInput input = newStreamGraphInput(bai);
        Assert.assertEquals(Pair.of(id, value), input.readMessage());
    }
}
Also used : DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) Value(com.baidu.hugegraph.computer.core.graph.value.Value) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) Test(org.junit.Test)

Example 57 with Id

use of com.baidu.hugegraph.computer.core.graph.id.Id in project hugegraph-computer by hugegraph.

the class StreamGraphOutputInputTest method testWriteReadEdgesWithSinglePerLabelFrequency.

@Test
public void testWriteReadEdgesWithSinglePerLabelFrequency() throws Exception {
    UnitTestBase.updateOptions(ComputerOptions.INPUT_EDGE_FREQ, "SINGLE_PER_LABEL");
    ComputerContext context = ComputerContext.instance();
    GraphFactory graphFactory = context.graphFactory();
    Id longId = BytesId.of(100L);
    LongValue longValue = new LongValue(999L);
    Vertex vertex = graphFactory().createVertex(longId, longValue);
    vertex.addEdge(graphFactory.createEdge(BytesId.of(2L)));
    vertex.addEdge(graphFactory.createEdge("knows", BytesId.of(3L)));
    vertex.addEdge(graphFactory.createEdge("watch", BytesId.of(3L)));
    vertex.addEdge(graphFactory.createEdge("watch", "1111", BytesId.of(4L)));
    vertex.addEdge(graphFactory.createEdge("watch", "2222", BytesId.of(4L)));
    byte[] bytes;
    try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
        StreamGraphOutput output = newStreamGraphOutput(bao);
        output.writeEdges(vertex);
        bytes = bao.toByteArray();
        bytes = reweaveBytes(bytes);
    }
    try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
        StreamGraphInput input = newStreamGraphInput(bai);
        assertEdgesEqual(vertex, input.readEdges(), EdgeFrequency.SINGLE_PER_LABEL);
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) Test(org.junit.Test)

Example 58 with Id

use of com.baidu.hugegraph.computer.core.graph.id.Id in project hugegraph-computer by hugegraph.

the class StreamGraphOutputInputTest method testWriteReadEdgesWithMultipleFrequency.

@Test
public void testWriteReadEdgesWithMultipleFrequency() throws Exception {
    UnitTestBase.updateOptions(ComputerOptions.INPUT_EDGE_FREQ, "MULTIPLE");
    ComputerContext context = ComputerContext.instance();
    GraphFactory graphFactory = context.graphFactory();
    Id longId = BytesId.of(100L);
    LongValue longValue = new LongValue(999L);
    Vertex vertex = graphFactory().createVertex(longId, longValue);
    vertex.addEdge(graphFactory.createEdge(BytesId.of(2L)));
    vertex.addEdge(graphFactory.createEdge("knows", BytesId.of(3L)));
    vertex.addEdge(graphFactory.createEdge("watch", BytesId.of(3L)));
    vertex.addEdge(graphFactory.createEdge("watch", "1111", BytesId.of(4L)));
    vertex.addEdge(graphFactory.createEdge("watch", "2222", BytesId.of(4L)));
    byte[] bytes;
    try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
        StreamGraphOutput output = newStreamGraphOutput(bao);
        output.writeEdges(vertex);
        bytes = bao.toByteArray();
        bytes = reweaveBytes(bytes);
    }
    try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
        StreamGraphInput input = newStreamGraphInput(bai);
        assertEdgesEqual(vertex, input.readEdges(), EdgeFrequency.MULTIPLE);
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) Test(org.junit.Test)

Example 59 with Id

use of com.baidu.hugegraph.computer.core.graph.id.Id in project hugegraph-computer by hugegraph.

the class CsvStructGraphOutputTest method testWriteReadVertexOnlyIdAndValue.

@Test
public void testWriteReadVertexOnlyIdAndValue() throws IOException {
    UnitTestBase.updateOptions(ComputerOptions.OUTPUT_WITH_ADJACENT_EDGES, "false", ComputerOptions.OUTPUT_WITH_VERTEX_PROPERTIES, "false", ComputerOptions.OUTPUT_WITH_EDGE_PROPERTIES, "false");
    ComputerContext context = context();
    GraphFactory factory = context.graphFactory();
    Id longId = BytesId.of(100L);
    Value value = BytesId.of(999L);
    Vertex vertex = factory.createVertex(longId, value);
    String fileName = "output.csv";
    File file = new File(fileName);
    try {
        BufferedFileOutput dos = new BufferedFileOutput(file);
        StructGraphOutput output = (StructGraphOutput) IOFactory.createGraphOutput(context, OutputFormat.CSV, dos);
        output.writeVertex(vertex);
        dos.close();
        @SuppressWarnings("deprecation") String text = FileUtils.readFileToString(file);
        Assert.assertEquals("100,999" + System.lineSeparator(), text);
    } finally {
        FileUtils.deleteQuietly(file);
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) FloatValue(com.baidu.hugegraph.computer.core.graph.value.FloatValue) Value(com.baidu.hugegraph.computer.core.graph.value.Value) IntValue(com.baidu.hugegraph.computer.core.graph.value.IntValue) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) BooleanValue(com.baidu.hugegraph.computer.core.graph.value.BooleanValue) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) File(java.io.File) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) Test(org.junit.Test)

Example 60 with Id

use of com.baidu.hugegraph.computer.core.graph.id.Id in project hugegraph-computer by hugegraph.

the class BetweennessCentrality method forward.

private void forward(ComputationContext context, Vertex vertex, IdList sequence, IdSet arrivingVertices) {
    if (sequence.size() == 0) {
        return;
    }
    BetweennessValue value = vertex.value();
    IdSet arrivedVertices = value.arrivedVertices();
    Id source = sequence.get(0);
    // The source vertex is arriving at first time
    if (!arrivedVertices.contains(source)) {
        arrivingVertices.add(source);
        SeqCount seqCount = this.seqTable.computeIfAbsent(source, k -> new SeqCount());
        seqCount.totalCount++;
        // Accumulate the number of shortest paths for intermediate vertices
        for (int i = 1; i < sequence.size(); i++) {
            Id id = sequence.get(i);
            Map<Id, Integer> idCounts = seqCount.idCount;
            idCounts.put(id, idCounts.getOrDefault(id, 0) + 1);
        }
        Id selfId = vertex.id();
        sequence.add(selfId);
        BetweennessMessage newMessage = new BetweennessMessage(sequence);
        for (Edge edge : vertex.edges()) {
            Id targetId = edge.targetId();
            if (this.sample(selfId, targetId, edge) && !sequence.contains(targetId)) {
                context.sendMessage(targetId, newMessage);
            }
        }
    }
}
Also used : IdSet(com.baidu.hugegraph.computer.core.graph.value.IdSet) Id(com.baidu.hugegraph.computer.core.graph.id.Id) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge)

Aggregations

Id (com.baidu.hugegraph.computer.core.graph.id.Id)74 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)51 Test (org.junit.Test)29 IdList (com.baidu.hugegraph.computer.core.graph.value.IdList)18 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)12 DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)12 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)12 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)12 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)11 GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)10 KvEntry (com.baidu.hugegraph.computer.core.store.entry.KvEntry)9 ConnectionId (com.baidu.hugegraph.computer.core.network.ConnectionId)8 IdListList (com.baidu.hugegraph.computer.core.graph.value.IdListList)7 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)6 File (java.io.File)6 Value (com.baidu.hugegraph.computer.core.graph.value.Value)5 BytesInput (com.baidu.hugegraph.computer.core.io.BytesInput)5 Config (com.baidu.hugegraph.computer.core.config.Config)4 BooleanValue (com.baidu.hugegraph.computer.core.graph.value.BooleanValue)4 FloatValue (com.baidu.hugegraph.computer.core.graph.value.FloatValue)4