Search in sources :

Example 6 with MultiUnifier

use of ai.grakn.graql.admin.MultiUnifier in project grakn by graknlabs.

the class QueryCache method getAnswer.

/**
 * find specific answer to a query in the cache
 * @param query input query
 * @param ans sought specific answer to the query
 * @return found answer if any, otherwise empty answer
 */
public Answer getAnswer(Q query, Answer ans) {
    if (ans.isEmpty())
        return ans;
    CacheEntry<Q, QueryAnswers> match = this.getEntry(query);
    if (match != null) {
        Q equivalentQuery = match.query();
        MultiUnifier multiUnifier = equivalentQuery.getMultiUnifier(query);
        // NB: only used when checking for materialised answer duplicates
        Answer answer = match.cachedElement().stream().flatMap(a -> a.unify(multiUnifier)).filter(a -> a.containsAll(ans)).findFirst().orElse(null);
        if (answer != null)
            return answer;
    }
    // TODO should it create a cache entry?
    List<Answer> answers = ReasonerQueries.create(query, ans).getQuery().execute();
    return answers.isEmpty() ? new QueryAnswer() : answers.iterator().next();
}
Also used : List(java.util.List) 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) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) Set(java.util.Set) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Collectors(java.util.stream.Collectors) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers)

Example 7 with MultiUnifier

use of ai.grakn.graql.admin.MultiUnifier in project grakn by graknlabs.

the class QueryCache method getAnswerStreamWithUnifier.

@Override
public Pair<Stream<Answer>, MultiUnifier> getAnswerStreamWithUnifier(Q query) {
    CacheEntry<Q, QueryAnswers> match = this.getEntry(query);
    if (match != null) {
        Q equivalentQuery = match.query();
        QueryAnswers answers = match.cachedElement();
        MultiUnifier multiUnifier = equivalentQuery.getMultiUnifier(query);
        // lazy version would be answers.stream().flatMap(ans -> ans.unify(multiUnifier))
        return new Pair<>(answers.unify(multiUnifier).stream(), multiUnifier);
    }
    return new Pair<>(structuralCache().get(query), new MultiUnifierImpl());
}
Also used : MultiUnifier(ai.grakn.graql.admin.MultiUnifier) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 8 with MultiUnifier

use of ai.grakn.graql.admin.MultiUnifier in project grakn by graknlabs.

the class QueryCache method recordAnswer.

/**
 * record a specific answer to a given query
 * @param query to which an answer is to be recorded
 * @param answer specific answer to the query
 * @return recorded answer
 */
public Answer recordAnswer(Q query, Answer answer) {
    if (answer.isEmpty())
        return answer;
    CacheEntry<Q, QueryAnswers> match = this.getEntry(query);
    if (match != null) {
        Q equivalentQuery = match.query();
        QueryAnswers answers = match.cachedElement();
        MultiUnifier multiUnifier = query.getMultiUnifier(equivalentQuery);
        answer.unify(multiUnifier).forEach(answers::add);
    } else {
        this.putEntry(query, new QueryAnswers(answer));
    }
    return answer;
}
Also used : MultiUnifier(ai.grakn.graql.admin.MultiUnifier) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers)

Example 9 with MultiUnifier

use of ai.grakn.graql.admin.MultiUnifier 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 10 with MultiUnifier

use of ai.grakn.graql.admin.MultiUnifier in project grakn by graknlabs.

the class AtomicTest method testUnification_MatchAllParentAtom.

@Test
public void testUnification_MatchAllParentAtom() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    String parentString = "{$r($a, $x);}";
    String childString = "{$rel (role1: $z, role2: $b) isa binary;}";
    Atom parent = ReasonerQueries.atomic(conjunction(parentString, graph), graph).getAtom();
    Atom child = ReasonerQueries.atomic(conjunction(childString, graph), graph).getAtom();
    MultiUnifier multiUnifier = child.getMultiUnifier(parent, UnifierType.RULE);
    Unifier correctUnifier = new UnifierImpl(ImmutableMap.of(var("z"), var("a"), var("b"), var("x"), child.getVarName(), parent.getVarName()));
    Unifier correctUnifier2 = new UnifierImpl(ImmutableMap.of(var("z"), var("x"), var("b"), var("a"), child.getVarName(), parent.getVarName()));
    assertEquals(multiUnifier.size(), 2);
    multiUnifier.forEach(u -> assertTrue(u.containsAll(correctUnifier) || u.containsAll(correctUnifier2)));
}
Also used : MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Aggregations

MultiUnifier (ai.grakn.graql.admin.MultiUnifier)17 Unifier (ai.grakn.graql.admin.Unifier)10 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)9 Answer (ai.grakn.graql.admin.Answer)8 MultiUnifierImpl (ai.grakn.graql.internal.reasoner.MultiUnifierImpl)8 Pair (ai.grakn.graql.internal.reasoner.utils.Pair)7 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 Test (org.junit.Test)7 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)6 Stream (java.util.stream.Stream)6 Var (ai.grakn.graql.Var)5 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)5 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 Concept (ai.grakn.concept.Concept)4 GraqlQueryException (ai.grakn.exception.GraqlQueryException)4 Atomic (ai.grakn.graql.admin.Atomic)4 Conjunction (ai.grakn.graql.admin.Conjunction)4