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;
}
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();
}
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());
}
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;
}
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;
}
Aggregations