Search in sources :

Example 1 with LookupExplanation

use of ai.grakn.graql.internal.reasoner.explanation.LookupExplanation in project grakn by graknlabs.

the class LazyQueryCache method getAnswersWithUnifier.

@Override
public Pair<LazyAnswerIterator, MultiUnifier> getAnswersWithUnifier(Q query) {
    CacheEntry<Q, LazyAnswerIterator> match = this.getEntry(query);
    if (match != null) {
        Q equivalentQuery = match.query();
        MultiUnifier multiUnifier = equivalentQuery.getMultiUnifier(query);
        LazyAnswerIterator unified = match.cachedElement().unify(multiUnifier);
        return new Pair<>(unified, multiUnifier);
    }
    Stream<Answer> answerStream = record(query, query.getQuery().stream().map(a -> a.explain(new LookupExplanation(query))));
    return new Pair<>(new LazyAnswerIterator(answerStream), new MultiUnifierImpl());
}
Also used : Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Stream(java.util.stream.Stream) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Map(java.util.Map) Set(java.util.Set) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) Collectors(java.util.stream.Collectors) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) Answer(ai.grakn.graql.admin.Answer) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 2 with LookupExplanation

use of ai.grakn.graql.internal.reasoner.explanation.LookupExplanation in project grakn by graknlabs.

the class LazyQueryCache method getAnswerStreamWithUnifier.

@Override
public Pair<Stream<Answer>, MultiUnifier> getAnswerStreamWithUnifier(Q query) {
    CacheEntry<Q, LazyAnswerIterator> match = this.getEntry(query);
    if (match != null) {
        Q equivalentQuery = match.query();
        MultiUnifier multiUnifier = equivalentQuery.getMultiUnifier(query);
        Stream<Answer> unified = match.cachedElement().stream().flatMap(a -> a.unify(multiUnifier));
        return new Pair<>(unified, multiUnifier);
    }
    Stream<Answer> answerStream = record(query, query.getQuery().stream().map(a -> a.explain(new LookupExplanation(query))));
    return new Pair<>(answerStream, new MultiUnifierImpl());
}
Also used : Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Stream(java.util.stream.Stream) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Map(java.util.Map) Set(java.util.Set) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) Collectors(java.util.stream.Collectors) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) Answer(ai.grakn.graql.admin.Answer) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 3 with LookupExplanation

use of ai.grakn.graql.internal.reasoner.explanation.LookupExplanation in project grakn by graknlabs.

the class StructuralCache method get.

/**
 * @param query to be retrieved
 * @return answer stream of provided query
 */
public Stream<Answer> get(Q query) {
    Equivalence.Wrapper<Q> structQuery = equivalence.wrap(query);
    EmbeddedGraknTx<?> tx = query.tx();
    CacheEntry<Q, GraqlTraversal> match = structCache.get(structQuery);
    if (match != null) {
        Q equivalentQuery = match.query();
        GraqlTraversal traversal = match.cachedElement();
        Unifier unifier = equivalentQuery.getMultiUnifier(query, UnifierType.STRUCTURAL).getAny();
        Map<Var, ConceptId> idTransform = equivalentQuery.idTransform(query, unifier);
        ReasonerQueryImpl transformedQuery = equivalentQuery.transformIds(idTransform);
        return MatchBase.streamWithTraversal(transformedQuery.getPattern().commonVars(), tx, traversal.transform(idTransform)).map(ans -> ans.unify(unifier)).map(a -> a.explain(new LookupExplanation(query)));
    }
    GraqlTraversal traversal = GreedyTraversalPlan.createTraversal(query.getPattern(), tx);
    structCache.put(structQuery, new CacheEntry<>(query, traversal));
    return MatchBase.streamWithTraversal(query.getPattern().commonVars(), tx, traversal).map(a -> a.explain(new LookupExplanation(query)));
}
Also used : UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Equivalence(com.google.common.base.Equivalence) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) ReasonerQueryEquivalence(ai.grakn.graql.internal.reasoner.query.ReasonerQueryEquivalence) GreedyTraversalPlan(ai.grakn.graql.internal.gremlin.GreedyTraversalPlan) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) MatchBase(ai.grakn.graql.internal.query.match.MatchBase) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Map(java.util.Map) Var(ai.grakn.graql.Var) ConceptId(ai.grakn.concept.ConceptId) Unifier(ai.grakn.graql.admin.Unifier) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) Var(ai.grakn.graql.Var) ConceptId(ai.grakn.concept.ConceptId) Equivalence(com.google.common.base.Equivalence) ReasonerQueryEquivalence(ai.grakn.graql.internal.reasoner.query.ReasonerQueryEquivalence) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) Unifier(ai.grakn.graql.admin.Unifier) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)

Aggregations

Answer (ai.grakn.graql.admin.Answer)3 LookupExplanation (ai.grakn.graql.internal.reasoner.explanation.LookupExplanation)3 ReasonerQueryImpl (ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Stream (java.util.stream.Stream)3 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)2 MultiUnifierImpl (ai.grakn.graql.internal.reasoner.MultiUnifierImpl)2 LazyAnswerIterator (ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator)2 Pair (ai.grakn.graql.internal.reasoner.utils.Pair)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ConceptId (ai.grakn.concept.ConceptId)1 Var (ai.grakn.graql.Var)1 ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)1 Unifier (ai.grakn.graql.admin.Unifier)1 GraqlTraversal (ai.grakn.graql.internal.gremlin.GraqlTraversal)1 GreedyTraversalPlan (ai.grakn.graql.internal.gremlin.GreedyTraversalPlan)1 MatchBase (ai.grakn.graql.internal.query.match.MatchBase)1 UnifierType (ai.grakn.graql.internal.reasoner.UnifierType)1