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());
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
}
}
Aggregations