use of ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator in project grakn by graknlabs.
the class LazyQueryCache method getAnswerStreamWithUnifier.
@Override
public Pair<Stream<Answer>, MultiUnifier> getAnswerStreamWithUnifier(Q query) {
CacheEntry<Q, LazyAnswerIterator> match = this.getEntry(query);
if (match != null) {
Q equivalentQuery = match.query();
MultiUnifier multiUnifier = equivalentQuery.getMultiUnifier(query);
Stream<Answer> unified = match.cachedElement().stream().flatMap(a -> a.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<>(answerStream, new MultiUnifierImpl());
}
use of ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator in project grakn by graknlabs.
the class LazyQueryCache method recordRetrieveLazy.
/**
* record answer stream for a specific query and retrieve the updated stream in a lazy iterator
* @param query to be recorded
* @param answers answer stream of the query
* @return lazy iterator of updated answers
*/
public LazyAnswerIterator recordRetrieveLazy(Q query, Stream<Answer> answers) {
CacheEntry<Q, LazyAnswerIterator> match = this.getEntry(query);
if (match != null) {
Q equivalentQuery = match.query();
MultiUnifier multiUnifier = query.getMultiUnifier(equivalentQuery);
Stream<Answer> unifiedStream = answers.flatMap(a -> a.unify(multiUnifier));
// NB: entry overwrite
this.putEntry(match.query(), match.cachedElement().merge(unifiedStream));
return getAnswerIterator(query);
}
LazyAnswerIterator liter = new LazyAnswerIterator(answers);
this.putEntry(query, liter);
return liter;
}
use of ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator in project grakn by graknlabs.
the class QueryAnswerStream method join.
/**
* lazy stream join with quasi- sideways information propagation
* @param stream left stream operand
* @param stream2 right stream operand
* @param joinVars intersection on variables of two streams
* @return joined stream
*/
public static Stream<Answer> join(Stream<Answer> stream, Stream<Answer> stream2, ImmutableSet<Var> joinVars) {
LazyAnswerIterator l2 = new LazyAnswerIterator(stream2);
return stream.flatMap(a1 -> {
Stream<Answer> answerStream = l2.stream();
answerStream = answerStream.filter(ans -> {
for (Var v : joinVars) {
if (!ans.get(v).equals(a1.get(v))) {
return false;
}
}
return true;
});
return answerStream.map(a -> a.merge(a1));
});
}
use of ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator in project grakn by graknlabs.
the class QueryAnswerStream method joinWithInverse.
/**
* lazy stream join with fast lookup from inverse answer map
* @param stream left stream operand
* @param stream2 right stream operand
* @param stream2InverseMap inverse map of right operand from cache
* @param joinVars intersection on variables of two streams
* @return joined stream
*/
static Stream<Answer> joinWithInverse(Stream<Answer> stream, Stream<Answer> stream2, Map<Pair<Var, Concept>, Set<Answer>> stream2InverseMap, ImmutableSet<Var> joinVars) {
if (joinVars.isEmpty()) {
LazyAnswerIterator l2 = new LazyAnswerIterator(stream2);
return stream.flatMap(a1 -> l2.stream().map(a -> a.merge(a1)));
}
return stream.flatMap(a1 -> {
Iterator<Var> vit = joinVars.iterator();
Set<Answer> matchAnswers = findMatchingAnswers(a1, stream2InverseMap, vit.next());
while (vit.hasNext()) {
matchAnswers = Sets.intersection(matchAnswers, findMatchingAnswers(a1, stream2InverseMap, vit.next()));
}
return matchAnswers.stream().map(a -> a.merge(a1));
});
}
Aggregations