Search in sources :

Example 1 with LazyAnswerIterator

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());
}
Also used : 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) Map(java.util.Map) Set(java.util.Set) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) Collectors(java.util.stream.Collectors) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) Answer(ai.grakn.graql.admin.Answer) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 2 with LazyAnswerIterator

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()));
    });
}
Also used : Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator)

Example 3 with LazyAnswerIterator

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);
}
Also used : HashMap(java.util.HashMap) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator)

Example 4 with LazyAnswerIterator

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);
}
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 5 with LazyAnswerIterator

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

LazyAnswerIterator (ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator)9 Answer (ai.grakn.graql.admin.Answer)8 Set (java.util.Set)6 Stream (java.util.stream.Stream)6 Pair (ai.grakn.graql.internal.reasoner.utils.Pair)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)3 GraknTx (ai.grakn.GraknTx)2 Concept (ai.grakn.concept.Concept)2 Entity (ai.grakn.concept.Entity)2 SchemaConcept (ai.grakn.concept.SchemaConcept)2 Graql.var (ai.grakn.graql.Graql.var)2 Var (ai.grakn.graql.Var)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 TypeAtom (ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom)2