Search in sources :

Example 1 with LabelId

use of ai.grakn.concept.LabelId in project grakn by graknlabs.

the class TinkerComputeQueryRunner method run.

public ComputeJob<Long> run(CountQuery query) {
    return runCompute(query, tinkerComputeQuery -> {
        if (!tinkerComputeQuery.selectedTypesHaveInstance()) {
            LOG.debug("Count = 0");
            return 0L;
        }
        Set<LabelId> typeLabelIds = convertLabelsToIds(tinkerComputeQuery.subLabels());
        Map<Integer, Long> count;
        Set<LabelId> rolePlayerLabelIds = tinkerComputeQuery.getRolePlayerLabelIds();
        rolePlayerLabelIds.addAll(typeLabelIds);
        ComputerResult result = tinkerComputeQuery.compute(new CountVertexProgram(), new CountMapReduceWithAttribute(), rolePlayerLabelIds, false);
        count = result.memory().get(CountMapReduceWithAttribute.class.getName());
        long finalCount = count.keySet().stream().filter(id -> typeLabelIds.contains(LabelId.of(id))).mapToLong(count::get).sum();
        if (count.containsKey(GraknMapReduce.RESERVED_TYPE_LABEL_KEY)) {
            finalCount += count.get(GraknMapReduce.RESERVED_TYPE_LABEL_KEY);
        }
        LOG.debug("Count = " + finalCount);
        return finalCount;
    });
}
Also used : CountVertexProgram(ai.grakn.graql.internal.analytics.CountVertexProgram) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) LabelId(ai.grakn.concept.LabelId) CountMapReduceWithAttribute(ai.grakn.graql.internal.analytics.CountMapReduceWithAttribute)

Example 2 with LabelId

use of ai.grakn.concept.LabelId in project grakn by graknlabs.

the class TinkerComputeQueryRunner method run.

public ComputeJob<Optional<Number>> run(MedianQuery query) {
    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());
        ComputerResult result = tinkerComputeQuery.compute(new MedianVertexProgram(statisticsResourceLabelIds, dataType), null, allSubLabelIds);
        Number finalResult = result.memory().get(MedianVertexProgram.MEDIAN);
        LOG.debug("Median = " + finalResult);
        return Optional.of(finalResult);
    });
}
Also used : AttributeType(ai.grakn.concept.AttributeType) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) LabelId(ai.grakn.concept.LabelId) MedianVertexProgram(ai.grakn.graql.internal.analytics.MedianVertexProgram)

Example 3 with LabelId

use of ai.grakn.concept.LabelId 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 4 with LabelId

use of ai.grakn.concept.LabelId in project grakn by graknlabs.

the class DegreeStatisticsVertexProgram method degreeStatisticsStepResourceOwner.

static void degreeStatisticsStepResourceOwner(Vertex vertex, Messenger<Long> messenger, Set<LabelId> ofLabelIds) {
    LabelId labelId = Utility.getVertexTypeId(vertex);
    if (labelId.isValid() && !ofLabelIds.contains(labelId)) {
        messenger.sendMessage(messageScopeShortcutIn, 1L);
        messenger.sendMessage(messageScopeResourceOut, 1L);
    }
}
Also used : LabelId(ai.grakn.concept.LabelId)

Example 5 with LabelId

use of ai.grakn.concept.LabelId in project grakn by graknlabs.

the class TinkerComputeQueryRunner method run.

public <T> ComputeJob<T> run(ConnectedComponentQuery<T> query) {
    return runCompute(query, tinkerComputeQuery -> {
        if (!tinkerComputeQuery.selectedTypesHaveInstance()) {
            LOG.info("Selected types don't have instances");
            return (T) Collections.emptyMap();
        }
        Set<LabelId> subLabelIds = convertLabelsToIds(tinkerComputeQuery.subLabels());
        GraknVertexProgram<?> vertexProgram;
        if (query.sourceId().isPresent()) {
            ConceptId conceptId = query.sourceId().get();
            if (!tinkerComputeQuery.verticesExistInSubgraph(conceptId)) {
                throw GraqlQueryException.instanceDoesNotExist();
            }
            vertexProgram = new ConnectedComponentVertexProgram(conceptId);
        } else {
            vertexProgram = new ConnectedComponentsVertexProgram();
        }
        Long clusterSize = query.clusterSize();
        GraknMapReduce<?> mapReduce;
        if (query.isMembersSet()) {
            mapReduce = new ClusterMemberMapReduce(ConnectedComponentsVertexProgram.CLUSTER_LABEL, clusterSize);
        } else {
            mapReduce = new ClusterSizeMapReduce(ConnectedComponentsVertexProgram.CLUSTER_LABEL, clusterSize);
        }
        Memory memory = tinkerComputeQuery.compute(vertexProgram, mapReduce, subLabelIds).memory();
        return memory.get(mapReduce.getClass().getName());
    });
}
Also used : ConnectedComponentVertexProgram(ai.grakn.graql.internal.analytics.ConnectedComponentVertexProgram) Memory(org.apache.tinkerpop.gremlin.process.computer.Memory) ClusterSizeMapReduce(ai.grakn.graql.internal.analytics.ClusterSizeMapReduce) LabelId(ai.grakn.concept.LabelId) ConnectedComponentsVertexProgram(ai.grakn.graql.internal.analytics.ConnectedComponentsVertexProgram) ClusterMemberMapReduce(ai.grakn.graql.internal.analytics.ClusterMemberMapReduce) ConceptId(ai.grakn.concept.ConceptId)

Aggregations

LabelId (ai.grakn.concept.LabelId)10 ComputerResult (org.apache.tinkerpop.gremlin.process.computer.ComputerResult)7 AttributeType (ai.grakn.concept.AttributeType)4 ConceptId (ai.grakn.concept.ConceptId)4 SchemaConcept (ai.grakn.concept.SchemaConcept)4 ClusterMemberMapReduce (ai.grakn.graql.internal.analytics.ClusterMemberMapReduce)4 Concept (ai.grakn.concept.Concept)3 Label (ai.grakn.concept.Label)3 ClusterSizeMapReduce (ai.grakn.graql.internal.analytics.ClusterSizeMapReduce)3 ConnectedComponentVertexProgram (ai.grakn.graql.internal.analytics.ConnectedComponentVertexProgram)3 ConnectedComponentsVertexProgram (ai.grakn.graql.internal.analytics.ConnectedComponentsVertexProgram)3 CountMapReduceWithAttribute (ai.grakn.graql.internal.analytics.CountMapReduceWithAttribute)3 CountVertexProgram (ai.grakn.graql.internal.analytics.CountVertexProgram)3 DegreeStatisticsVertexProgram (ai.grakn.graql.internal.analytics.DegreeStatisticsVertexProgram)3 KCoreVertexProgram (ai.grakn.graql.internal.analytics.KCoreVertexProgram)3 NoResultException (ai.grakn.graql.internal.analytics.NoResultException)3 ComputeJob (ai.grakn.ComputeJob)2 GraknComputer (ai.grakn.GraknComputer)2 Type (ai.grakn.concept.Type)2 GraqlQueryException (ai.grakn.exception.GraqlQueryException)2