Search in sources :

Example 11 with Value

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

the class DefaultPropertiesTest method testOverwrite.

@Test
public void testOverwrite() {
    DefaultProperties properties = new DefaultProperties(graphFactory());
    Assert.assertEquals(0, properties.get().size());
    properties.put("p1", new LongValue(1L));
    properties.put("p2", new DoubleValue(2.0D));
    Assert.assertEquals(new LongValue(1L), properties.get("p1"));
    properties.put("p1", new LongValue(2L));
    Assert.assertEquals(new LongValue(2L), properties.get("p1"));
    Map<String, Value> props = properties.get();
    Assert.assertEquals(2, props.size());
    Assert.assertEquals(new LongValue(2L), props.get("p1"));
    Assert.assertEquals(new DoubleValue(2.0D), props.get("p2"));
}
Also used : DefaultProperties(com.baidu.hugegraph.computer.core.graph.properties.DefaultProperties) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) Value(com.baidu.hugegraph.computer.core.graph.value.Value) Test(org.junit.Test)

Example 12 with Value

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

the class JsonStructGraphOutputTest 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", ComputerOptions.OUTPUT_RESULT_NAME, "rank");
    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.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\":999}" + 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) 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 13 with Value

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

the class UnitTestBase method assertValueEqualAfterWriteAndRead.

public static void assertValueEqualAfterWriteAndRead(Value oldValue) throws IOException {
    byte[] bytes;
    try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
        oldValue.write(bao);
        bytes = bao.toByteArray();
    }
    Value newValue = graphFactory().createValue(oldValue.valueType());
    try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
        newValue.read(bai);
        Assert.assertEquals(oldValue, newValue);
    }
}
Also used : BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) Value(com.baidu.hugegraph.computer.core.graph.value.Value)

Example 14 with Value

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

the class PointerCombinerTest method testVertexPropertiesCombiner.

@Test
public void testVertexPropertiesCombiner() throws IOException {
    Config config = UnitTestBase.updateWithRequiredOptions(ComputerOptions.WORKER_COMBINER_CLASS, DoubleValueSumCombiner.class.getName(), ComputerOptions.WORKER_VERTEX_PROPERTIES_COMBINER_CLASS, MergeOldPropertiesCombiner.class.getName());
    Combiner<Properties> valueCombiner = config.createObject(ComputerOptions.WORKER_VERTEX_PROPERTIES_COMBINER_CLASS);
    GraphFactory graphFactory = graphFactory();
    PointerCombiner combiner = SorterTestUtil.createPointerCombiner(graphFactory::createProperties, valueCombiner);
    try (BytesOutput bytesOutput1 = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE);
        BytesOutput bytesOutput2 = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
        Properties value1 = graphFactory.createProperties();
        value1.put("p1", new LongValue(1L));
        Properties value2 = graphFactory.createProperties();
        value2.put("p2", new LongValue(2L));
        value1.write(bytesOutput1);
        value2.write(bytesOutput2);
        Pointer pointer1 = new InlinePointer(bytesOutput1.buffer(), bytesOutput1.position());
        Pointer pointer2 = new InlinePointer(bytesOutput2.buffer(), bytesOutput2.position());
        Pointer pointer = combiner.combine(pointer1, pointer2);
        BytesInput input = IOFactory.createBytesInput(pointer.bytes());
        Properties combinedValue = graphFactory.createProperties();
        combinedValue.read(input);
        Map<String, Value> map = combinedValue.get();
        Assert.assertEquals(2, map.size());
        Assert.assertEquals(new LongValue(1L), map.get("p1"));
        Assert.assertEquals(new LongValue(2L), map.get("p2"));
    }
}
Also used : GraphFactory(com.baidu.hugegraph.computer.core.graph.GraphFactory) BytesOutput(com.baidu.hugegraph.computer.core.io.BytesOutput) BytesInput(com.baidu.hugegraph.computer.core.io.BytesInput) Config(com.baidu.hugegraph.computer.core.config.Config) InlinePointer(com.baidu.hugegraph.computer.core.store.entry.InlinePointer) Pointer(com.baidu.hugegraph.computer.core.store.entry.Pointer) InlinePointer(com.baidu.hugegraph.computer.core.store.entry.InlinePointer) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) 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) Test(org.junit.Test)

Example 15 with Value

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

Aggregations

Value (com.baidu.hugegraph.computer.core.graph.value.Value)19 DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)7 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)7 Test (org.junit.Test)6 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)5 ComputerException (com.baidu.hugegraph.computer.core.common.exception.ComputerException)3 GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)3 Edges (com.baidu.hugegraph.computer.core.graph.edge.Edges)3 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)3 Id (com.baidu.hugegraph.computer.core.graph.id.Id)3 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)3 BooleanValue (com.baidu.hugegraph.computer.core.graph.value.BooleanValue)3 FloatValue (com.baidu.hugegraph.computer.core.graph.value.FloatValue)3 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)3 IOException (java.io.IOException)3 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)2 Config (com.baidu.hugegraph.computer.core.config.Config)2 DefaultProperties (com.baidu.hugegraph.computer.core.graph.properties.DefaultProperties)2 ListValue (com.baidu.hugegraph.computer.core.graph.value.ListValue)2 BytesInput (com.baidu.hugegraph.computer.core.io.BytesInput)2