Search in sources :

Example 1 with NoResultException

use of ai.grakn.graql.internal.analytics.NoResultException 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 2 with NoResultException

use of ai.grakn.graql.internal.analytics.NoResultException 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 3 with NoResultException

use of ai.grakn.graql.internal.analytics.NoResultException in project grakn by graknlabs.

the class TinkerComputeQueryRunner method run.

public TinkerComputeJob<List<List<Concept>>> run(PathsQuery query) {
    return runCompute(query, tinkerComputeQuery -> {
        ConceptId sourceId = query.from();
        ConceptId destinationId = query.to();
        if (!tinkerComputeQuery.verticesExistInSubgraph(sourceId, destinationId)) {
            throw GraqlQueryException.instanceDoesNotExist();
        }
        if (sourceId.equals(destinationId)) {
            return Collections.singletonList(Collections.singletonList(tx.getConcept(sourceId)));
        }
        ComputerResult result;
        Set<LabelId> subLabelIds = convertLabelsToIds(tinkerComputeQuery.subLabels());
        try {
            result = tinkerComputeQuery.compute(new ShortestPathVertexProgram(sourceId, destinationId), null, subLabelIds);
        } catch (NoResultException e) {
            return Collections.emptyList();
        }
        Multimap<Concept, Concept> predecessorMapFromSource = tinkerComputeQuery.getPredecessorMap(result);
        List<List<Concept>> allPaths = tinkerComputeQuery.getAllPaths(predecessorMapFromSource, sourceId);
        if (tinkerComputeQuery.isAttributeIncluded()) {
            // this can be slow
            return tinkerComputeQuery.getExtendedPaths(allPaths);
        }
        LOG.info("Number of paths: " + allPaths.size());
        return allPaths;
    });
}
Also used : Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) ShortestPathVertexProgram(ai.grakn.graql.internal.analytics.ShortestPathVertexProgram) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) List(java.util.List) LabelId(ai.grakn.concept.LabelId) NoResultException(ai.grakn.graql.internal.analytics.NoResultException) ConceptId(ai.grakn.concept.ConceptId)

Aggregations

LabelId (ai.grakn.concept.LabelId)3 NoResultException (ai.grakn.graql.internal.analytics.NoResultException)3 ComputerResult (org.apache.tinkerpop.gremlin.process.computer.ComputerResult)3 Concept (ai.grakn.concept.Concept)2 ConceptId (ai.grakn.concept.ConceptId)2 SchemaConcept (ai.grakn.concept.SchemaConcept)2 ClusterMemberMapReduce (ai.grakn.graql.internal.analytics.ClusterMemberMapReduce)2 KCoreVertexProgram (ai.grakn.graql.internal.analytics.KCoreVertexProgram)2 ShortestPathVertexProgram (ai.grakn.graql.internal.analytics.ShortestPathVertexProgram)2 List (java.util.List)2 ComputeJob (ai.grakn.ComputeJob)1 GraknComputer (ai.grakn.GraknComputer)1 AttributeType (ai.grakn.concept.AttributeType)1 Label (ai.grakn.concept.Label)1 Type (ai.grakn.concept.Type)1 GraqlQueryException (ai.grakn.exception.GraqlQueryException)1 ComputeQuery (ai.grakn.graql.ComputeQuery)1 StatisticsQuery (ai.grakn.graql.StatisticsQuery)1 ConnectedComponentQuery (ai.grakn.graql.analytics.ConnectedComponentQuery)1 CorenessQuery (ai.grakn.graql.analytics.CorenessQuery)1