Search in sources :

Example 1 with QueryAnswers

use of ai.grakn.graql.internal.reasoner.query.QueryAnswers in project grakn by graknlabs.

the class QueryCache method recordAnswerWithUnifier.

/**
 * record a specific answer to a given query with a known cache unifier
 * @param query to which an answer is to be recorded
 * @param answer answer specific answer to the query
 * @param unifier between the cached and input query
 * @return recorded answer
 */
public Answer recordAnswerWithUnifier(Q query, Answer answer, MultiUnifier unifier) {
    if (answer.isEmpty())
        return answer;
    CacheEntry<Q, QueryAnswers> match = this.getEntry(query);
    if (match != null) {
        QueryAnswers answers = match.cachedElement();
        answer.unify(unifier).forEach(answers::add);
    } else {
        this.putEntry(query, new QueryAnswers(answer));
    }
    return answer;
}
Also used : QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers)

Example 2 with QueryAnswers

use of ai.grakn.graql.internal.reasoner.query.QueryAnswers in project grakn by graknlabs.

the class QueryCache method getAnswer.

/**
 * find specific answer to a query in the cache
 * @param query input query
 * @param ans sought specific answer to the query
 * @return found answer if any, otherwise empty answer
 */
public Answer getAnswer(Q query, Answer ans) {
    if (ans.isEmpty())
        return ans;
    CacheEntry<Q, QueryAnswers> match = this.getEntry(query);
    if (match != null) {
        Q equivalentQuery = match.query();
        MultiUnifier multiUnifier = equivalentQuery.getMultiUnifier(query);
        // NB: only used when checking for materialised answer duplicates
        Answer answer = match.cachedElement().stream().flatMap(a -> a.unify(multiUnifier)).filter(a -> a.containsAll(ans)).findFirst().orElse(null);
        if (answer != null)
            return answer;
    }
    // TODO should it create a cache entry?
    List<Answer> answers = ReasonerQueries.create(query, ans).getQuery().execute();
    return answers.isEmpty() ? new QueryAnswer() : answers.iterator().next();
}
Also used : List(java.util.List) 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) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) Set(java.util.Set) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Collectors(java.util.stream.Collectors) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers)

Example 3 with QueryAnswers

use of ai.grakn.graql.internal.reasoner.query.QueryAnswers in project grakn by graknlabs.

the class QueryCache method getAnswerStreamWithUnifier.

@Override
public Pair<Stream<Answer>, MultiUnifier> getAnswerStreamWithUnifier(Q query) {
    CacheEntry<Q, QueryAnswers> match = this.getEntry(query);
    if (match != null) {
        Q equivalentQuery = match.query();
        QueryAnswers answers = match.cachedElement();
        MultiUnifier multiUnifier = equivalentQuery.getMultiUnifier(query);
        // lazy version would be answers.stream().flatMap(ans -> ans.unify(multiUnifier))
        return new Pair<>(answers.unify(multiUnifier).stream(), multiUnifier);
    }
    return new Pair<>(structuralCache().get(query), new MultiUnifierImpl());
}
Also used : MultiUnifier(ai.grakn.graql.admin.MultiUnifier) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 4 with QueryAnswers

use of ai.grakn.graql.internal.reasoner.query.QueryAnswers in project grakn by graknlabs.

the class QueryCache method recordAnswer.

/**
 * record a specific answer to a given query
 * @param query to which an answer is to be recorded
 * @param answer specific answer to the query
 * @return recorded answer
 */
public Answer recordAnswer(Q query, Answer answer) {
    if (answer.isEmpty())
        return answer;
    CacheEntry<Q, QueryAnswers> match = this.getEntry(query);
    if (match != null) {
        Q equivalentQuery = match.query();
        QueryAnswers answers = match.cachedElement();
        MultiUnifier multiUnifier = query.getMultiUnifier(equivalentQuery);
        answer.unify(multiUnifier).forEach(answers::add);
    } else {
        this.putEntry(query, new QueryAnswers(answer));
    }
    return answer;
}
Also used : MultiUnifier(ai.grakn.graql.admin.MultiUnifier) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers)

Example 5 with QueryAnswers

use of ai.grakn.graql.internal.reasoner.query.QueryAnswers in project grakn by graknlabs.

the class QueryCache method record.

@Override
public QueryAnswers record(Q query, QueryAnswers answers) {
    CacheEntry<Q, QueryAnswers> match = this.getEntry(query);
    if (match != null) {
        Q equivalentQuery = match.query();
        QueryAnswers unifiedAnswers = answers.unify(query.getMultiUnifier(equivalentQuery));
        this.getEntry(query).cachedElement().addAll(unifiedAnswers);
        return getAnswers(query);
    }
    this.putEntry(query, answers);
    return answers;
}
Also used : QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers)

Aggregations

QueryAnswers (ai.grakn.graql.internal.reasoner.query.QueryAnswers)9 LazyQueryCache (ai.grakn.graql.internal.reasoner.cache.LazyQueryCache)4 QueryCache (ai.grakn.graql.internal.reasoner.cache.QueryCache)4 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)4 Test (org.junit.Test)4 Answer (ai.grakn.graql.admin.Answer)3 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)3 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)3 MultiUnifierImpl (ai.grakn.graql.internal.reasoner.MultiUnifierImpl)2 Pair (ai.grakn.graql.internal.reasoner.utils.Pair)2 ReasonerQueries (ai.grakn.graql.internal.reasoner.query.ReasonerQueries)1 ReasonerQueryImpl (ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1