Search in sources :

Example 1 with KeyValue

use of org.apache.tinkerpop.gremlin.process.computer.KeyValue in project janusgraph by JanusGraph.

the class OLAPTest method testPageRank.

@Test
public void testPageRank() throws ExecutionException, InterruptedException {
    mgmt.makePropertyKey("distance").dataType(Integer.class).cardinality(Cardinality.SINGLE).make();
    mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.MULTI).make();
    mgmt.makeEdgeLabel("likes").multiplicity(Multiplicity.MULTI).make();
    finishSchema();
    final int branch = 6;
    final int diameter = 5;
    final double alpha = 0.85d;
    int numV = (int) ((Math.pow(branch, diameter + 1) - 1) / (branch - 1));
    JanusGraphVertex v = tx.addVertex();
    expand(v, 0, diameter, branch);
    clopen();
    assertCount(numV, tx.query().vertices());
    log.debug("PR test numV: {}", numV);
    newTx();
    // Precompute correct PR results:
    double[] correctPR = new double[diameter + 1];
    for (int i = diameter; i >= 0; i--) {
        double pr = (1.0D - alpha) / numV;
        if (i < diameter)
            pr += alpha * branch * correctPR[i + 1];
        log.debug("diameter={} pr={}", diameter, pr);
        correctPR[i] = pr;
    }
    double correctPRSum = 0;
    for (final JanusGraphVertex janusGraphVertex : tx.query().vertices()) {
        correctPRSum += correctPR[janusGraphVertex.<Integer>value("distance")];
    }
    final JanusGraphComputer computer = graph.compute();
    computer.resultMode(JanusGraphComputer.ResultMode.NONE);
    computer.workers(4);
    computer.program(PageRankVertexProgram.build().iterations(10).vertexCount(numV).dampingFactor(alpha).create(graph));
    computer.mapReduce(PageRankMapReduce.build().create());
    ComputerResult result = computer.submit().get();
    Iterator<KeyValue<Long, Double>> ranks = result.memory().get(PageRankMapReduce.DEFAULT_MEMORY_KEY);
    assertNotNull(ranks);
    int vertexCounter = 0;
    double computedPRSum = 0;
    correctPRSum = 0;
    final Set<Long> vertexIDs = new HashSet<>(numV);
    while (ranks.hasNext()) {
        final KeyValue<Long, Double> rank = ranks.next();
        final Long vertexID = rank.getKey();
        final Double computedPR = rank.getValue();
        assertNotNull(vertexID);
        assertNotNull(computedPR);
        final JanusGraphVertex u = getV(tx, vertexID);
        final int distance = u.<Integer>value("distance");
        vertexCounter++;
        // assertEquals("Incorrect PR on vertex #" + vertexCounter, correctPR[distance], computedPR, EPSILON);
        computedPRSum += computedPR;
        correctPRSum += correctPR[distance];
        assertFalse(vertexIDs.contains(vertexID));
        vertexIDs.add(vertexID);
        log.debug("vertexID={} computedPR={}", vertexID, computedPR);
    }
    assertEquals(numV, vertexCounter);
    assertEquals(correctPRSum, computedPRSum, 0.001);
}
Also used : KeyValue(org.apache.tinkerpop.gremlin.process.computer.KeyValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) JanusGraphComputer(org.janusgraph.core.JanusGraphComputer) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) JanusGraphBaseTest(org.janusgraph.graphdb.JanusGraphBaseTest)

Example 2 with KeyValue

use of org.apache.tinkerpop.gremlin.process.computer.KeyValue 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

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ComputerResult (org.apache.tinkerpop.gremlin.process.computer.ComputerResult)2 KeyValue (org.apache.tinkerpop.gremlin.process.computer.KeyValue)2 JanusGraphComputer (org.janusgraph.core.JanusGraphComputer)2 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)2 JanusGraphBaseTest (org.janusgraph.graphdb.JanusGraphBaseTest)2 Test (org.junit.jupiter.api.Test)2 HashSet (java.util.HashSet)1 PropertyKey (org.janusgraph.core.PropertyKey)1