Search in sources :

Example 1 with IdListList

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

the class MockComputation method compute0.

@Override
public void compute0(ComputationContext context, Vertex vertex) {
    IdListList value = new IdListList();
    vertex.value(value);
    Edges edges = vertex.edges();
    checkEdgesSize(edges);
    checkEdgesSize(edges);
    if (RANDOM.nextInt() % 10 == 0) {
        vertex.inactivate();
    }
}
Also used : Edges(com.baidu.hugegraph.computer.core.graph.edge.Edges) IdListList(com.baidu.hugegraph.computer.core.graph.value.IdListList)

Example 2 with IdListList

use of com.baidu.hugegraph.computer.core.graph.value.IdListList 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 3 with IdListList

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

the class RingsDetectionOutput method value.

@Override
protected List<String> value(Vertex vertex) {
    IdListList value = vertex.value();
    List<String> propValues = new ArrayList<>();
    for (int i = 0; i < value.size(); i++) {
        propValues.add(value.get(i).toString());
    }
    return propValues;
}
Also used : ArrayList(java.util.ArrayList) IdListList(com.baidu.hugegraph.computer.core.graph.value.IdListList)

Example 4 with IdListList

use of com.baidu.hugegraph.computer.core.graph.value.IdListList 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 5 with IdListList

use of com.baidu.hugegraph.computer.core.graph.value.IdListList 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

IdListList (com.baidu.hugegraph.computer.core.graph.value.IdListList)9 Id (com.baidu.hugegraph.computer.core.graph.id.Id)5 IdList (com.baidu.hugegraph.computer.core.graph.value.IdList)5 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)4 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)2 GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)2 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)2 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 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)2 File (java.io.File)2 Test (org.junit.Test)2 Edges (com.baidu.hugegraph.computer.core.graph.edge.Edges)1 ArrayList (java.util.ArrayList)1