Search in sources :

Example 1 with LazyQueryCache

use of ai.grakn.graql.internal.reasoner.cache.LazyQueryCache 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);
}
Also used : GraknTestUtil(ai.grakn.util.GraknTestUtil) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) Entity(ai.grakn.concept.Entity) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) GraknTx(ai.grakn.GraknTx) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ClassRule(org.junit.ClassRule) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) Graql.var(ai.grakn.graql.Graql.var) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Stream(java.util.stream.Stream) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) Assert.assertFalse(org.junit.Assert.assertFalse) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Assume.assumeTrue(org.junit.Assume.assumeTrue) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Unifier(ai.grakn.graql.admin.Unifier) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 2 with LazyQueryCache

use of ai.grakn.graql.internal.reasoner.cache.LazyQueryCache 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)));
}
Also used : GraknTestUtil(ai.grakn.util.GraknTestUtil) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) Entity(ai.grakn.concept.Entity) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) GraknTx(ai.grakn.GraknTx) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ClassRule(org.junit.ClassRule) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) Graql.var(ai.grakn.graql.Graql.var) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Stream(java.util.stream.Stream) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) Assert.assertFalse(org.junit.Assert.assertFalse) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Assume.assumeTrue(org.junit.Assume.assumeTrue) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Unifier(ai.grakn.graql.admin.Unifier) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Aggregations

GraknTx (ai.grakn.GraknTx)2 Entity (ai.grakn.concept.Entity)2 Graql.var (ai.grakn.graql.Graql.var)2 Answer (ai.grakn.graql.admin.Answer)2 Conjunction (ai.grakn.graql.admin.Conjunction)2 Unifier (ai.grakn.graql.admin.Unifier)2 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)2 Patterns (ai.grakn.graql.internal.pattern.Patterns)2 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)2 LazyQueryCache (ai.grakn.graql.internal.reasoner.cache.LazyQueryCache)2 QueryCache (ai.grakn.graql.internal.reasoner.cache.QueryCache)2 LazyAnswerIterator (ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator)2 QueryAnswers (ai.grakn.graql.internal.reasoner.query.QueryAnswers)2 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)2 ReasonerQueries (ai.grakn.graql.internal.reasoner.query.ReasonerQueries)2 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)2 SampleKBContext (ai.grakn.test.rule.SampleKBContext)2 GraknTestUtil (ai.grakn.util.GraknTestUtil)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Set (java.util.Set)2