Search in sources :

Example 1 with DegreeQuery

use of ai.grakn.graql.analytics.DegreeQuery in project grakn by graknlabs.

the class TinkerComputeQueryRunner method run.

public ComputeJob<Map<Long, Set<String>>> run(DegreeQuery query) {
    return runCompute(query, tinkerComputeQuery -> {
        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);
                return type.subs();
            }).map(SchemaConcept::getLabel).collect(Collectors.toSet());
        }
        Set<Label> subLabels = Sets.union(tinkerComputeQuery.subLabels(), ofLabels);
        if (!tinkerComputeQuery.selectedTypesHaveInstance()) {
            return Collections.emptyMap();
        }
        Set<LabelId> subLabelIds = convertLabelsToIds(subLabels);
        Set<LabelId> ofLabelIds = convertLabelsToIds(ofLabels);
        ComputerResult result = tinkerComputeQuery.compute(new DegreeVertexProgram(ofLabelIds), new DegreeDistributionMapReduce(ofLabelIds, DegreeVertexProgram.DEGREE), subLabelIds);
        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) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) Label(ai.grakn.concept.Label) DegreeVertexProgram(ai.grakn.graql.internal.analytics.DegreeVertexProgram) SchemaConcept(ai.grakn.concept.SchemaConcept) LabelId(ai.grakn.concept.LabelId)

Example 2 with DegreeQuery

use of ai.grakn.graql.analytics.DegreeQuery in project grakn by graknlabs.

the class GraqlTest method testDegrees.

@Test
public void testDegrees() throws Exception {
    addSchemaAndEntities();
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        Map<Long, Set<String>> degrees = graph.graql().<DegreeQuery>parse("compute degrees;").execute();
        Map<String, Long> correctDegrees = new HashMap<>();
        correctDegrees.put(entityId1, 1L);
        correctDegrees.put(entityId2, 2L);
        correctDegrees.put(entityId3, 0L);
        correctDegrees.put(entityId4, 1L);
        correctDegrees.put(relationId12, 2L);
        correctDegrees.put(relationId24, 2L);
        assertTrue(!degrees.isEmpty());
        degrees.forEach((key, value) -> value.forEach(id -> {
            assertTrue(correctDegrees.containsKey(id));
            assertEquals(correctDegrees.get(id), key);
        }));
    }
}
Also used : DegreeQuery(ai.grakn.graql.analytics.DegreeQuery) InvalidKBException(ai.grakn.exception.InvalidKBException) Arrays(java.util.Arrays) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) ConnectedComponentQuery(ai.grakn.graql.analytics.ConnectedComponentQuery) HashMap(java.util.HashMap) GraqlSyntaxException(ai.grakn.exception.GraqlSyntaxException) EntityType(ai.grakn.concept.EntityType) SessionContext(ai.grakn.test.rule.SessionContext) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) Label(ai.grakn.concept.Label) PathQuery(ai.grakn.graql.analytics.PathQuery) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) MeanQuery(ai.grakn.graql.analytics.MeanQuery) GraknTx(ai.grakn.GraknTx) Map(java.util.Map) SumQuery(ai.grakn.graql.analytics.SumQuery) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) MinQuery(ai.grakn.graql.analytics.MinQuery) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) MaxQuery(ai.grakn.graql.analytics.MaxQuery) GraqlQueryException(ai.grakn.exception.GraqlQueryException) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TestCase.assertNull(junit.framework.TestCase.assertNull) Collectors(java.util.stream.Collectors) Query(ai.grakn.graql.Query) MedianQuery(ai.grakn.graql.analytics.MedianQuery) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) PathsQuery(ai.grakn.graql.analytics.PathsQuery) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

AttributeType (ai.grakn.concept.AttributeType)2 Concept (ai.grakn.concept.Concept)2 ConceptId (ai.grakn.concept.ConceptId)2 Label (ai.grakn.concept.Label)2 GraqlQueryException (ai.grakn.exception.GraqlQueryException)2 ConnectedComponentQuery (ai.grakn.graql.analytics.ConnectedComponentQuery)2 DegreeQuery (ai.grakn.graql.analytics.DegreeQuery)2 MaxQuery (ai.grakn.graql.analytics.MaxQuery)2 MeanQuery (ai.grakn.graql.analytics.MeanQuery)2 MedianQuery (ai.grakn.graql.analytics.MedianQuery)2 MinQuery (ai.grakn.graql.analytics.MinQuery)2 PathQuery (ai.grakn.graql.analytics.PathQuery)2 PathsQuery (ai.grakn.graql.analytics.PathsQuery)2 SumQuery (ai.grakn.graql.analytics.SumQuery)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ComputeJob (ai.grakn.ComputeJob)1