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