Search in sources :

Example 6 with ComputerResult

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

use of org.apache.tinkerpop.gremlin.process.computer.ComputerResult in project titan by thinkaurelius.

the class TitanPartitionGraphTest method testVertexPartitionOlap.

private void testVertexPartitionOlap(CommitMode commitMode) throws Exception {
    Object[] options = { option(GraphDatabaseConfiguration.IDS_FLUSH), false };
    clopen(options);
    //        int[] groupDegrees = {10,20,30};
    int[] groupDegrees = { 2 };
    int numVertices = setupGroupClusters(groupDegrees, commitMode);
    Map<Long, Integer> degreeMap = new HashMap<>(groupDegrees.length);
    for (int i = 0; i < groupDegrees.length; i++) {
        degreeMap.put(getOnlyVertex(tx.query().has("groupid", "group" + i)).longId(), groupDegrees[i]);
    }
    clopen(options);
    //Test OLAP works with partitioned vertices
    TitanGraphComputer computer = graph.compute(FulgoraGraphComputer.class);
    computer.resultMode(TitanGraphComputer.ResultMode.NONE);
    computer.workers(1);
    computer.program(new OLAPTest.DegreeCounter());
    computer.mapReduce(new OLAPTest.DegreeMapper());
    ComputerResult result = computer.submit().get();
    assertTrue(result.memory().exists(OLAPTest.DegreeMapper.DEGREE_RESULT));
    Map<Long, Integer> degrees = result.memory().get(OLAPTest.DegreeMapper.DEGREE_RESULT);
    assertNotNull(degrees);
    assertEquals(numVertices, degrees.size());
    final IDManager idManager = graph.getIDManager();
    for (Map.Entry<Long, Integer> entry : degrees.entrySet()) {
        long vid = entry.getKey();
        Integer degree = entry.getValue();
        if (idManager.isPartitionedVertex(vid)) {
            //                System.out.println("Partitioned: " + degree );
            assertEquals(degreeMap.get(vid), degree);
        } else {
            assertEquals(1, (long) degree);
        }
    }
}
Also used : IDManager(com.thinkaurelius.titan.graphdb.idmanagement.IDManager) OLAPTest(com.thinkaurelius.titan.olap.OLAPTest) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult)

Example 8 with ComputerResult

use of org.apache.tinkerpop.gremlin.process.computer.ComputerResult in project grakn by graknlabs.

the class TinkerComputeQueryRunner method run.

public ComputeJob<Map<String, Set<String>>> run(KCoreQuery query) {
    return runCompute(query, tinkerComputeQuery -> {
        long k = query.kValue();
        if (k < 2L)
            throw GraqlQueryException.kValueSmallerThanTwo();
        if (!tinkerComputeQuery.selectedTypesHaveInstance()) {
            return Collections.emptyMap();
        }
        ComputerResult result;
        Set<LabelId> subLabelIds = convertLabelsToIds(tinkerComputeQuery.subLabels());
        try {
            result = tinkerComputeQuery.compute(new KCoreVertexProgram(k), new ClusterMemberMapReduce(KCoreVertexProgram.K_CORE_LABEL), subLabelIds);
        } catch (NoResultException e) {
            return Collections.emptyMap();
        }
        return result.memory().get(ClusterMemberMapReduce.class.getName());
    });
}
Also used : KCoreVertexProgram(ai.grakn.graql.internal.analytics.KCoreVertexProgram) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) LabelId(ai.grakn.concept.LabelId) NoResultException(ai.grakn.graql.internal.analytics.NoResultException) ClusterMemberMapReduce(ai.grakn.graql.internal.analytics.ClusterMemberMapReduce)

Example 9 with ComputerResult

use of org.apache.tinkerpop.gremlin.process.computer.ComputerResult in project grakn by graknlabs.

the class TinkerComputeQueryRunner method run.

public ComputeJob<Map<Long, Set<String>>> run(CorenessQuery query) {
    return runCompute(query, tinkerComputeQuery -> {
        long k = query.minK();
        if (k < 2L)
            throw GraqlQueryException.kValueSmallerThanTwo();
        Set<Label> ofLabels;
        // Check if ofType is valid before returning emptyMap
        if (query.targetLabels().isEmpty()) {
            ofLabels = tinkerComputeQuery.subLabels();
        } else {
            ofLabels = query.targetLabels().stream().flatMap(typeLabel -> {
                Type type = tx.getSchemaConcept(typeLabel);
                if (type == null)
                    throw GraqlQueryException.labelNotFound(typeLabel);
                if (type.isRelationshipType())
                    throw GraqlQueryException.kCoreOnRelationshipType(typeLabel);
                return type.subs();
            }).map(SchemaConcept::getLabel).collect(Collectors.toSet());
        }
        Set<Label> subLabels = Sets.union(tinkerComputeQuery.subLabels(), ofLabels);
        if (!tinkerComputeQuery.selectedTypesHaveInstance()) {
            return Collections.emptyMap();
        }
        ComputerResult result;
        Set<LabelId> subLabelIds = convertLabelsToIds(subLabels);
        Set<LabelId> ofLabelIds = convertLabelsToIds(ofLabels);
        try {
            result = tinkerComputeQuery.compute(new CorenessVertexProgram(k), new DegreeDistributionMapReduce(ofLabelIds, CorenessVertexProgram.CORENESS), subLabelIds);
        } catch (NoResultException e) {
            return Collections.emptyMap();
        }
        return result.memory().get(DegreeDistributionMapReduce.class.getName());
    });
}
Also used : MaxMapReduce(ai.grakn.graql.internal.analytics.MaxMapReduce) LoggerFactory(org.slf4j.LoggerFactory) ConnectedComponentQuery(ai.grakn.graql.analytics.ConnectedComponentQuery) Type(ai.grakn.concept.Type) KCoreVertexProgram(ai.grakn.graql.internal.analytics.KCoreVertexProgram) CountMapReduceWithAttribute(ai.grakn.graql.internal.analytics.CountMapReduceWithAttribute) Label(ai.grakn.concept.Label) LabelId(ai.grakn.concept.LabelId) Map(java.util.Map) CorenessVertexProgram(ai.grakn.graql.internal.analytics.CorenessVertexProgram) ComputeQuery(ai.grakn.graql.ComputeQuery) ConceptId(ai.grakn.concept.ConceptId) MinQuery(ai.grakn.graql.analytics.MinQuery) CorenessQuery(ai.grakn.graql.analytics.CorenessQuery) MaxQuery(ai.grakn.graql.analytics.MaxQuery) ConnectedComponentVertexProgram(ai.grakn.graql.internal.analytics.ConnectedComponentVertexProgram) Set(java.util.Set) DegreeStatisticsVertexProgram(ai.grakn.graql.internal.analytics.DegreeStatisticsVertexProgram) GraknMapReduce(ai.grakn.graql.internal.analytics.GraknMapReduce) MeanMapReduce(ai.grakn.graql.internal.analytics.MeanMapReduce) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) SumMapReduce(ai.grakn.graql.internal.analytics.SumMapReduce) Serializable(java.io.Serializable) List(java.util.List) Memory(org.apache.tinkerpop.gremlin.process.computer.Memory) MedianVertexProgram(ai.grakn.graql.internal.analytics.MedianVertexProgram) Optional(java.util.Optional) DegreeDistributionMapReduce(ai.grakn.graql.internal.analytics.DegreeDistributionMapReduce) DegreeQuery(ai.grakn.graql.analytics.DegreeQuery) ComputeJob(ai.grakn.ComputeJob) StdQuery(ai.grakn.graql.analytics.StdQuery) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Multimap(com.google.common.collect.Multimap) Function(java.util.function.Function) StatisticsQuery(ai.grakn.graql.StatisticsQuery) ConnectedComponentsVertexProgram(ai.grakn.graql.internal.analytics.ConnectedComponentsVertexProgram) ClusterSizeMapReduce(ai.grakn.graql.internal.analytics.ClusterSizeMapReduce) PathQuery(ai.grakn.graql.analytics.PathQuery) AttributeType(ai.grakn.concept.AttributeType) NoResultException(ai.grakn.graql.internal.analytics.NoResultException) GraknComputer(ai.grakn.GraknComputer) MeanQuery(ai.grakn.graql.analytics.MeanQuery) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) GraknVertexProgram(ai.grakn.graql.internal.analytics.GraknVertexProgram) SumQuery(ai.grakn.graql.analytics.SumQuery) GraqlQueryException(ai.grakn.exception.GraqlQueryException) DegreeVertexProgram(ai.grakn.graql.internal.analytics.DegreeVertexProgram) Logger(org.slf4j.Logger) ShortestPathVertexProgram(ai.grakn.graql.internal.analytics.ShortestPathVertexProgram) MedianQuery(ai.grakn.graql.analytics.MedianQuery) MinMapReduce(ai.grakn.graql.internal.analytics.MinMapReduce) CountVertexProgram(ai.grakn.graql.internal.analytics.CountVertexProgram) ClusterMemberMapReduce(ai.grakn.graql.internal.analytics.ClusterMemberMapReduce) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) CountQuery(ai.grakn.graql.analytics.CountQuery) PathsQuery(ai.grakn.graql.analytics.PathsQuery) KCoreQuery(ai.grakn.graql.analytics.KCoreQuery) MapReduce(org.apache.tinkerpop.gremlin.process.computer.MapReduce) StdMapReduce(ai.grakn.graql.internal.analytics.StdMapReduce) Collections(java.util.Collections) Type(ai.grakn.concept.Type) AttributeType(ai.grakn.concept.AttributeType) DegreeDistributionMapReduce(ai.grakn.graql.internal.analytics.DegreeDistributionMapReduce) CorenessVertexProgram(ai.grakn.graql.internal.analytics.CorenessVertexProgram) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept) LabelId(ai.grakn.concept.LabelId) NoResultException(ai.grakn.graql.internal.analytics.NoResultException)

Example 10 with ComputerResult

use of org.apache.tinkerpop.gremlin.process.computer.ComputerResult in project grakn by graknlabs.

the class TinkerComputeQueryRunner method execWithMapReduce.

private <T, S, Q extends StatisticsQuery<?>> TinkerComputeJob<Optional<S>> execWithMapReduce(Q query, MapReduceFactory<T> mapReduceFactory, Function<T, S> operator) {
    return runStatistics(query, tinkerComputeQuery -> {
        AttributeType.DataType<?> dataType = tinkerComputeQuery.getDataTypeOfSelectedResourceTypes();
        if (!tinkerComputeQuery.selectedResourceTypesHaveInstance()) {
            return Optional.empty();
        }
        Set<LabelId> allSubLabelIds = convertLabelsToIds(tinkerComputeQuery.getCombinedSubTypes());
        Set<LabelId> statisticsResourceLabelIds = convertLabelsToIds(tinkerComputeQuery.statisticsResourceLabels());
        GraknMapReduce<T> mapReduce = mapReduceFactory.get(statisticsResourceLabelIds, dataType, DegreeVertexProgram.DEGREE);
        ComputerResult result = tinkerComputeQuery.compute(new DegreeStatisticsVertexProgram(statisticsResourceLabelIds), mapReduce, allSubLabelIds);
        Map<Serializable, T> map = result.memory().get(mapReduce.getClass().getName());
        LOG.debug("Result = " + map.get(MapReduce.NullObject.instance()));
        return Optional.of(operator.apply(map.get(MapReduce.NullObject.instance())));
    });
}
Also used : Serializable(java.io.Serializable) AttributeType(ai.grakn.concept.AttributeType) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) DegreeStatisticsVertexProgram(ai.grakn.graql.internal.analytics.DegreeStatisticsVertexProgram) LabelId(ai.grakn.concept.LabelId)

Aggregations

ComputerResult (org.apache.tinkerpop.gremlin.process.computer.ComputerResult)15 LabelId (ai.grakn.concept.LabelId)7 Map (java.util.Map)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 JanusGraphComputer (org.janusgraph.core.JanusGraphComputer)5 AttributeType (ai.grakn.concept.AttributeType)4 List (java.util.List)4 MapReduce (org.apache.tinkerpop.gremlin.process.computer.MapReduce)4 Concept (ai.grakn.concept.Concept)3 ConceptId (ai.grakn.concept.ConceptId)3 SchemaConcept (ai.grakn.concept.SchemaConcept)3 ClusterMemberMapReduce (ai.grakn.graql.internal.analytics.ClusterMemberMapReduce)3 NoResultException (ai.grakn.graql.internal.analytics.NoResultException)3 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)3 JanusGraphBaseTest (org.janusgraph.graphdb.JanusGraphBaseTest)3 Test (org.junit.jupiter.api.Test)3 ComputeJob (ai.grakn.ComputeJob)2 GraknComputer (ai.grakn.GraknComputer)2 Label (ai.grakn.concept.Label)2 Type (ai.grakn.concept.Type)2