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