Search in sources :

Example 1 with QueryCache

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);
}
Also used : QueryAnswerStream.entityTypeFilter(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.entityTypeFilter) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Concept(ai.grakn.concept.Concept) LoggerFactory(org.slf4j.LoggerFactory) Answer(ai.grakn.graql.admin.Answer) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) Iterators(com.google.common.collect.Iterators) HashSet(java.util.HashSet) ReasonerQueryIterator(ai.grakn.graql.internal.reasoner.iterator.ReasonerQueryIterator) AnswerExplanation(ai.grakn.graql.admin.AnswerExplanation) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Logger(org.slf4j.Logger) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Iterator(java.util.Iterator) Conjunction(ai.grakn.graql.admin.Conjunction) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Sets(com.google.common.collect.Sets) ReasonerUtils.typeUnifier(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.typeUnifier) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Cache(ai.grakn.graql.internal.reasoner.cache.Cache) Stream(java.util.stream.Stream) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) QueryAnswerStream.knownFilterWithInverse(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.knownFilterWithInverse) RuleExplanation(ai.grakn.graql.internal.reasoner.explanation.RuleExplanation) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Preconditions(com.google.common.base.Preconditions) AtomicStateProducer(ai.grakn.graql.internal.reasoner.state.AtomicStateProducer) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) Collections(java.util.Collections) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) Stream(java.util.stream.Stream) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 2 with QueryCache

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));
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) Test(org.junit.Test)

Example 3 with QueryCache

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);
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 4 with QueryCache

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)));
}
Also used : QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) Test(org.junit.Test)

Example 5 with QueryCache

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));
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) Test(org.junit.Test)

Aggregations

LazyQueryCache (ai.grakn.graql.internal.reasoner.cache.LazyQueryCache)7 QueryCache (ai.grakn.graql.internal.reasoner.cache.QueryCache)7 Answer (ai.grakn.graql.admin.Answer)5 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)5 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)5 Test (org.junit.Test)5 QueryAnswers (ai.grakn.graql.internal.reasoner.query.QueryAnswers)4 Concept (ai.grakn.concept.Concept)2 GraqlQueryException (ai.grakn.exception.GraqlQueryException)2 Var (ai.grakn.graql.Var)2 Atomic (ai.grakn.graql.admin.Atomic)2 Conjunction (ai.grakn.graql.admin.Conjunction)2 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)2 ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)2 Unifier (ai.grakn.graql.admin.Unifier)2 UnifierComparison (ai.grakn.graql.admin.UnifierComparison)2 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)2 MultiUnifierImpl (ai.grakn.graql.internal.reasoner.MultiUnifierImpl)2 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)2 NeqPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate)2