use of ai.grakn.graql.internal.reasoner.cache.QueryCache in project grakn by graknlabs.
the class ReasonerAtomicQuery method queryStateIterator.
@Override
public Pair<Iterator<ResolutionState>, MultiUnifier> queryStateIterator(QueryStateBase parent, Set<ReasonerAtomicQuery> visitedSubGoals, QueryCache<ReasonerAtomicQuery> cache) {
Pair<Stream<Answer>, MultiUnifier> cacheEntry = cache.getAnswerStreamWithUnifier(this);
MultiUnifier cacheUnifier = cacheEntry.getValue().inverse();
Iterator<AnswerState> dbIterator = cacheEntry.getKey().map(a -> a.explain(a.getExplanation().setQuery(this))).map(ans -> new AnswerState(ans, parent.getUnifier(), parent)).iterator();
Iterator<ResolutionState> subGoalIterator;
// if this is ground and exists in the db then do not resolve further
if (visitedSubGoals.contains(this) || (this.isGround() && dbIterator.hasNext())) {
subGoalIterator = Collections.emptyIterator();
} else {
visitedSubGoals.add(this);
subGoalIterator = this.getRuleStream().map(rulePair -> rulePair.getKey().subGoal(this.getAtom(), rulePair.getValue(), parent, visitedSubGoals, cache)).iterator();
}
return new Pair<>(Iterators.concat(dbIterator, subGoalIterator), cacheUnifier);
}
use of ai.grakn.graql.internal.reasoner.cache.QueryCache in project grakn by graknlabs.
the class QueryCacheTest method getUpdateRetrieveAnswerStream.
@Test
public void getUpdateRetrieveAnswerStream() {
QueryCache<ReasonerAtomicQuery> cache = new QueryCache<>();
Answer answer = recordQuery.getQuery().stream().findFirst().orElse(null);
Answer retrieveAnswer = answer.unify(recordToRetrieveUnifier);
Answer retrieveSingleAnswer = singleAnswer.unify(recordToRetrieveUnifier);
Stream<Answer> recordStream = cache.getAnswerStream(recordQuery);
Stream<Answer> retrieveStream = cache.getAnswerStream(retrieveQuery);
cache.recordAnswer(recordQuery, singleAnswer);
QueryAnswers recordAnswers = new QueryAnswers(recordStream.collect(Collectors.toSet()));
QueryAnswers retrieveAnswers = new QueryAnswers(retrieveStream.collect(Collectors.toSet()));
// NB: not expecting the update in the stream
assertTrue(recordAnswers.contains(answer));
assertTrue(retrieveAnswers.contains(retrieveAnswer));
assertFalse(recordAnswers.contains(singleAnswer));
assertFalse(retrieveAnswers.contains(retrieveSingleAnswer));
assertTrue(cache.getAnswers(recordQuery).contains(singleAnswer));
assertTrue(cache.getAnswers(retrieveQuery).contains(retrieveSingleAnswer));
}
use of ai.grakn.graql.internal.reasoner.cache.QueryCache in project grakn by graknlabs.
the class QueryCacheTest method recordRetrieveSingleAnswer.
@Test
public void recordRetrieveSingleAnswer() {
QueryCache<ReasonerAtomicQuery> cache = new QueryCache<>();
Answer answer = recordQuery.getQuery().stream().findFirst().orElse(null);
Answer retrieveAnswer = answer.unify(recordToRetrieveUnifier);
cache.recordAnswer(recordQuery, answer);
assertEquals(cache.getAnswer(recordQuery, new QueryAnswer()), new QueryAnswer());
assertEquals(cache.getAnswer(recordQuery, answer), answer);
assertEquals(cache.getAnswer(recordQuery, retrieveAnswer), answer);
assertEquals(cache.getAnswer(retrieveQuery, new QueryAnswer()), new QueryAnswer());
assertEquals(cache.getAnswer(retrieveQuery, retrieveAnswer), retrieveAnswer);
assertEquals(cache.getAnswer(retrieveQuery, answer), retrieveAnswer);
}
use of ai.grakn.graql.internal.reasoner.cache.QueryCache in project grakn by graknlabs.
the class QueryCacheTest method recordUpdateRetrieveAnswers.
@Test
public void recordUpdateRetrieveAnswers() {
QueryCache<ReasonerAtomicQuery> cache = new QueryCache<>();
cache.record(recordQuery, new QueryAnswers(recordQuery.getQuery().execute()));
cache.recordAnswer(recordQuery, singleAnswer);
assertTrue(cache.getAnswers(recordQuery).contains(singleAnswer));
assertTrue(cache.getAnswers(retrieveQuery).contains(singleAnswer.unify(recordToRetrieveUnifier)));
}
use of ai.grakn.graql.internal.reasoner.cache.QueryCache in project grakn by graknlabs.
the class QueryCacheTest method getRetrieveAnswerStream.
@Test
public void getRetrieveAnswerStream() {
QueryCache<ReasonerAtomicQuery> cache = new QueryCache<>();
Answer answer = recordQuery.getQuery().stream().findFirst().orElse(null);
Answer retrieveAnswer = answer.unify(recordToRetrieveUnifier);
Stream<Answer> recordStream = cache.getAnswerStream(recordQuery);
Stream<Answer> retrieveStream = cache.getAnswerStream(retrieveQuery);
QueryAnswers recordAnswers = new QueryAnswers(recordStream.collect(Collectors.toSet()));
QueryAnswers retrieveAnswers = new QueryAnswers(retrieveStream.collect(Collectors.toSet()));
assertTrue(recordAnswers.contains(answer));
assertTrue(retrieveAnswers.contains(retrieveAnswer));
}
Aggregations