Search in sources :

Example 6 with IdList

use of com.baidu.hugegraph.computer.core.graph.value.IdList in project hugegraph-computer by hugegraph.

the class TriangleCount method triangleCount.

private Integer triangleCount(ComputationContext context, Vertex vertex, Iterator<IdList> messages) {
    IdList neighbors = ((TriangleCountValue) vertex.value()).idList();
    if (context.superstep() == 1) {
        // Collect outgoing neighbors
        Set<Id> outNeighbors = getOutNeighbors(vertex);
        neighbors.addAll(outNeighbors);
        // Collect incoming neighbors
        while (messages.hasNext()) {
            IdList idList = messages.next();
            assert idList.size() == 1;
            Id inId = idList.get(0);
            if (!outNeighbors.contains(inId)) {
                neighbors.add(inId);
            }
        }
        // Send all neighbors to neighbors
        for (Id targetId : neighbors.values()) {
            context.sendMessage(targetId, neighbors);
        }
    } else if (context.superstep() == 2) {
        int count = 0;
        Set<Id> allNeighbors = new HashSet<>(neighbors.values());
        while (messages.hasNext()) {
            IdList twoDegreeNeighbors = messages.next();
            for (Id twoDegreeNeighbor : twoDegreeNeighbors.values()) {
                if (allNeighbors.contains(twoDegreeNeighbor)) {
                    count++;
                }
            }
        }
        return count >> 1;
    }
    return null;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Id(com.baidu.hugegraph.computer.core.graph.id.Id) IdList(com.baidu.hugegraph.computer.core.graph.value.IdList)

Example 7 with IdList

use of com.baidu.hugegraph.computer.core.graph.value.IdList in project hugegraph-computer by hugegraph.

the class RingsDetection method compute.

@Override
public void compute(ComputationContext context, Vertex vertex, Iterator<IdList> messages) {
    Id id = vertex.id();
    boolean halt = true;
    while (messages.hasNext()) {
        halt = false;
        IdList sequence = messages.next();
        if (id.equals(sequence.get(0))) {
            // Use the smallest vertex record ring
            boolean isMin = true;
            for (int i = 1; i < sequence.size(); i++) {
                Id pathVertexValue = sequence.get(i);
                if (id.compareTo(pathVertexValue) > 0) {
                    isMin = false;
                    break;
                }
            }
            if (isMin) {
                sequence.add(id);
                IdListList sequences = vertex.value();
                sequences.add(sequence);
            }
        } else {
            boolean contains = false;
            // Drop sequence if path contains this vertex
            for (int i = 0; i < sequence.size(); i++) {
                Id pathVertexValue = sequence.get(i);
                if (pathVertexValue.equals(vertex.id())) {
                    contains = true;
                    break;
                }
            }
            // Field ringId is smallest vertex id in path
            Id ringId = sequence.get(0);
            if (!contains) {
                sequence.add(vertex.id());
                for (Edge edge : vertex.edges()) {
                    if (ringId.compareTo(edge.targetId()) <= 0) {
                        context.sendMessage(edge.targetId(), sequence);
                    }
                }
            }
        }
    }
    if (halt) {
        vertex.inactivate();
    }
}
Also used : Id(com.baidu.hugegraph.computer.core.graph.id.Id) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge) IdList(com.baidu.hugegraph.computer.core.graph.value.IdList) IdListList(com.baidu.hugegraph.computer.core.graph.value.IdListList)

Example 8 with IdList

use of com.baidu.hugegraph.computer.core.graph.value.IdList in project hugegraph-computer by hugegraph.

the class JsonStructGraphOutputTest method testWriteReadVertexWithProperties.

@Test
public void testWriteReadVertexWithProperties() throws IOException {
    UnitTestBase.updateOptions(ComputerOptions.OUTPUT_WITH_ADJACENT_EDGES, "false", ComputerOptions.OUTPUT_WITH_VERTEX_PROPERTIES, "true", ComputerOptions.OUTPUT_WITH_EDGE_PROPERTIES, "false", ComputerOptions.OUTPUT_RESULT_NAME, "rank");
    ComputerContext context = context();
    GraphFactory factory = context.graphFactory();
    Id longId = BytesId.of(100L);
    IdListList idListList = new IdListList();
    IdList idList1 = new IdList();
    idList1.add(BytesId.of(66L));
    IdList idList2 = new IdList();
    idList2.add(BytesId.of(998L));
    idList2.add(BytesId.of(999L));
    idListList.add(idList1);
    idListList.add(idList2);
    Vertex vertex = factory.createVertex(longId, idListList);
    vertex.properties().put("boolean", new BooleanValue(true));
    vertex.properties().put("byte", new IntValue(127));
    vertex.properties().put("short", new IntValue(16383));
    vertex.properties().put("int", new IntValue(1000000));
    vertex.properties().put("long", new LongValue(10000000000L));
    vertex.properties().put("float", new FloatValue(0.1F));
    vertex.properties().put("double", new DoubleValue(-0.01D));
    vertex.properties().put("idvalue", longId);
    String fileName = "output.json";
    File file = new File(fileName);
    try {
        BufferedFileOutput dos = new BufferedFileOutput(file);
        StructGraphOutput output = (StructGraphOutput) IOFactory.createGraphOutput(context, OutputFormat.JSON, dos);
        output.writeVertex(vertex);
        dos.close();
        @SuppressWarnings("deprecation") String json = FileUtils.readFileToString(file);
        Assert.assertEquals("{\"id\":100,\"rank\":[[66],[998,999]]," + "\"properties\":{\"boolean\":true," + "\"byte\":127,\"double\":-0.01," + "\"short\":16383,\"idvalue\":100," + "\"float\":0.1,\"int\":1000000," + "\"long\":10000000000}}" + System.lineSeparator(), json);
    } finally {
        FileUtils.deleteQuietly(file);
    }
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) IdList(com.baidu.hugegraph.computer.core.graph.value.IdList) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) IdListList(com.baidu.hugegraph.computer.core.graph.value.IdListList) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) BooleanValue(com.baidu.hugegraph.computer.core.graph.value.BooleanValue) 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) FloatValue(com.baidu.hugegraph.computer.core.graph.value.FloatValue) IntValue(com.baidu.hugegraph.computer.core.graph.value.IntValue) File(java.io.File) Test(org.junit.Test)

Example 9 with IdList

use of com.baidu.hugegraph.computer.core.graph.value.IdList in project hugegraph-computer by hugegraph.

the class CsvStructGraphOutputTest method testWriteReadVertexWithEdges.

@Test
public void testWriteReadVertexWithEdges() throws IOException {
    UnitTestBase.updateOptions(ComputerOptions.OUTPUT_WITH_ADJACENT_EDGES, "true", ComputerOptions.OUTPUT_WITH_VERTEX_PROPERTIES, "false", ComputerOptions.OUTPUT_WITH_EDGE_PROPERTIES, "false");
    ComputerContext context = context();
    GraphFactory factory = context.graphFactory();
    Id longId = BytesId.of(100L);
    IdList idList = new IdList();
    idList.add(BytesId.of(998L));
    idList.add(BytesId.of(999L));
    Vertex vertex = factory.createVertex(longId, idList);
    vertex.addEdge(factory.createEdge("knows", BytesId.of(200)));
    vertex.addEdge(factory.createEdge("watch", "1111", BytesId.of(300)));
    String fileName = "output2.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,[998,999],[{200,\"knows\",\"\"}," + "{300,\"watch\",\"1111\"}]" + 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) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) File(java.io.File) IdList(com.baidu.hugegraph.computer.core.graph.value.IdList) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) Test(org.junit.Test)

Example 10 with IdList

use of com.baidu.hugegraph.computer.core.graph.value.IdList in project hugegraph-computer by hugegraph.

the class CsvStructGraphOutputTest method testWriteReadVertexWithProperties.

@Test
public void testWriteReadVertexWithProperties() throws IOException {
    UnitTestBase.updateOptions(ComputerOptions.OUTPUT_WITH_ADJACENT_EDGES, "false", ComputerOptions.OUTPUT_WITH_VERTEX_PROPERTIES, "true", ComputerOptions.OUTPUT_WITH_EDGE_PROPERTIES, "false");
    ComputerContext context = context();
    GraphFactory factory = context.graphFactory();
    Id longId = BytesId.of(100L);
    IdListList idListList = new IdListList();
    IdList idList1 = new IdList();
    idList1.add(BytesId.of(66L));
    IdList idList2 = new IdList();
    idList2.add(BytesId.of(998L));
    idList2.add(BytesId.of(999L));
    idListList.add(idList1);
    idListList.add(idList2);
    Vertex vertex = factory.createVertex(longId, idListList);
    vertex.properties().put("boolean", new BooleanValue(true));
    vertex.properties().put("byte", new IntValue(127));
    vertex.properties().put("short", new IntValue(16383));
    vertex.properties().put("int", new IntValue(1000000));
    vertex.properties().put("long", new LongValue(10000000000L));
    vertex.properties().put("float", new FloatValue(0.1F));
    vertex.properties().put("double", new DoubleValue(-0.01D));
    vertex.properties().put("idvalue", longId);
    String fileName = "output3.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,[[66],[998,999]],{true,127,-0.01,16383," + "100,0.1,1000000,10000000000}" + 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) IdList(com.baidu.hugegraph.computer.core.graph.value.IdList) ComputerContext(com.baidu.hugegraph.computer.core.common.ComputerContext) IdListList(com.baidu.hugegraph.computer.core.graph.value.IdListList) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) BooleanValue(com.baidu.hugegraph.computer.core.graph.value.BooleanValue) 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) FloatValue(com.baidu.hugegraph.computer.core.graph.value.FloatValue) IntValue(com.baidu.hugegraph.computer.core.graph.value.IntValue) File(java.io.File) Test(org.junit.Test)

Aggregations

IdList (com.baidu.hugegraph.computer.core.graph.value.IdList)20 Id (com.baidu.hugegraph.computer.core.graph.id.Id)18 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)14 IdListList (com.baidu.hugegraph.computer.core.graph.value.IdListList)7 ConnectionId (com.baidu.hugegraph.computer.core.network.ConnectionId)7 Test (org.junit.Test)5 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)4 GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)4 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)4 File (java.io.File)4 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)3 Random (java.util.Random)3 BooleanValue (com.baidu.hugegraph.computer.core.graph.value.BooleanValue)2 DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)2 FloatValue (com.baidu.hugegraph.computer.core.graph.value.FloatValue)2 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)2 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)2 KvEntry (com.baidu.hugegraph.computer.core.store.entry.KvEntry)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2