use of ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator 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());
}
use of ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator in project grakn by graknlabs.
the class LazyQueryCache method remove.
@Override
public void remove(Cache<Q, LazyAnswerIterator> c2, Set<Q> queries) {
c2.getQueries().stream().filter(queries::contains).filter(this::contains).forEach(q -> {
CacheEntry<Q, LazyAnswerIterator> match = this.getEntry(q);
Set<Answer> s = match.cachedElement().stream().collect(Collectors.toSet());
s.removeAll(c2.getAnswerStream(q).collect(Collectors.toSet()));
this.putEntry(match.query(), new LazyAnswerIterator(s.stream()));
});
}
use of ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator in project grakn by graknlabs.
the class LazyQueryCache method reload.
/**
* force stream consumption and reload cache
*/
public void reload() {
Map<Q, CacheEntry<Q, LazyAnswerIterator>> newCache = new HashMap<>();
this.entries().forEach(entry -> newCache.put(entry.query(), new CacheEntry<>(entry.query(), new LazyAnswerIterator(entry.cachedElement().stream().collect(Collectors.toSet()).stream()))));
this.clear();
this.putAll(newCache);
}
use of ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator in project grakn by graknlabs.
the class QueryCacheTest method lazilyGetRetrieveAnswers.
@Test
public void lazilyGetRetrieveAnswers() {
LazyQueryCache<ReasonerAtomicQuery> cache = new LazyQueryCache<>();
cache.record(recordQuery, recordQuery.getQuery().stream());
LazyAnswerIterator retrieveIterator = cache.getAnswers(retrieveQuery);
LazyAnswerIterator recordIterator = cache.getAnswers(recordQuery);
Set<Answer> record = recordIterator.stream().collect(toSet());
Set<Answer> retrieve = retrieveIterator.stream().map(ans -> ans.unify(retrieveToRecordUnifier)).collect(toSet());
assertTrue(!retrieve.isEmpty());
assertEquals(record, retrieve);
}
use of ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator in project grakn by graknlabs.
the class QueryCacheTest method lazilyGetUpdateRetrieveAnswers.
@Test
public void lazilyGetUpdateRetrieveAnswers() {
LazyQueryCache<ReasonerAtomicQuery> cache = new LazyQueryCache<>();
Answer retrieveSingleAnswer = singleAnswer.unify(recordToRetrieveUnifier);
cache.record(recordQuery, recordQuery.getQuery().stream());
LazyAnswerIterator retrieveIterator = cache.getAnswers(retrieveQuery);
LazyAnswerIterator recordIterator = cache.getAnswers(recordQuery);
cache.record(recordQuery, Stream.of(singleAnswer));
Set<Answer> record = recordIterator.stream().collect(toSet());
Set<Answer> retrieve = retrieveIterator.stream().map(ans -> ans.unify(retrieveToRecordUnifier)).collect(toSet());
assertTrue(!retrieve.isEmpty());
assertTrue(!retrieve.contains(singleAnswer));
assertEquals(record, retrieve);
assertTrue(cache.getAnswers(recordQuery).stream().anyMatch(ans -> ans.equals(singleAnswer)));
assertTrue(cache.getAnswers(retrieveQuery).stream().anyMatch(ans -> ans.equals(retrieveSingleAnswer)));
}
Aggregations