Search in sources :

Example 6 with JanusGraphComputer

use of org.janusgraph.core.JanusGraphComputer in project janusgraph by JanusGraph.

the class OLAPTest method testShortestDistance.

@Test
public void testShortestDistance() throws Exception {
    PropertyKey distance = mgmt.makePropertyKey("distance").dataType(Integer.class).cardinality(Cardinality.SINGLE).make();
    mgmt.makeEdgeLabel("connect").signature(distance).multiplicity(Multiplicity.MULTI).make();
    finishSchema();
    int maxDepth = 16;
    int maxBranch = 5;
    JanusGraphVertex vertex = tx.addVertex();
    // Grow a star-shaped graph around vertex which will be the single-source for this shortest path computation
    final int numV = growVertex(vertex, 0, maxDepth, maxBranch);
    final int numE = numV - 1;
    assertCount(numV, tx.query().vertices());
    assertCount(numE, tx.query().edges());
    log.debug("seed inE count: {}", vertex.query().direction(Direction.IN).edgeCount());
    log.debug("seed outE count: {}", vertex.query().direction(Direction.OUT).edgeCount());
    clopen();
    final JanusGraphComputer computer = graph.compute();
    computer.resultMode(JanusGraphComputer.ResultMode.NONE);
    computer.workers(4);
    computer.program(ShortestDistanceVertexProgram.build().seed((long) vertex.id()).maxDepth(maxDepth + 4).create(graph));
    computer.mapReduce(ShortestDistanceMapReduce.build().create());
    ComputerResult result = computer.submit().get();
    Iterator<KeyValue<Long, Long>> distances = result.memory().get(ShortestDistanceMapReduce.DEFAULT_MEMORY_KEY);
    int vertexCount = 0;
    while (distances.hasNext()) {
        final KeyValue<Long, Long> kv = distances.next();
        final long dist = kv.getValue();
        assertTrue(dist >= 0 && dist < Integer.MAX_VALUE, "Invalid distance: " + dist);
        JanusGraphVertex v = getV(tx, kv.getKey());
        assertEquals(v.<Integer>value("distance").intValue(), dist);
        vertexCount++;
    }
    assertEquals(numV, vertexCount);
    assertTrue(0 < vertexCount);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyValue(org.apache.tinkerpop.gremlin.process.computer.KeyValue) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) PropertyKey(org.janusgraph.core.PropertyKey) JanusGraphComputer(org.janusgraph.core.JanusGraphComputer) Test(org.junit.jupiter.api.Test) JanusGraphBaseTest(org.janusgraph.graphdb.JanusGraphBaseTest)

Aggregations

JanusGraphComputer (org.janusgraph.core.JanusGraphComputer)6 ComputerResult (org.apache.tinkerpop.gremlin.process.computer.ComputerResult)5 JanusGraphBaseTest (org.janusgraph.graphdb.JanusGraphBaseTest)5 Test (org.junit.jupiter.api.Test)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 KeyValue (org.apache.tinkerpop.gremlin.process.computer.KeyValue)2 HashSet (java.util.HashSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)1 PropertyKey (org.janusgraph.core.PropertyKey)1 Transaction (org.janusgraph.core.Transaction)1 IDManager (org.janusgraph.graphdb.idmanagement.IDManager)1 OLAPTest (org.janusgraph.olap.OLAPTest)1