Search in sources :

Example 6 with LazyAnswerIterator

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

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

Example 8 with LazyAnswerIterator

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));
    });
}
Also used : Pair(ai.grakn.graql.internal.reasoner.utils.Pair) ImmutableSet(com.google.common.collect.ImmutableSet) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Iterator(java.util.Iterator) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Set(java.util.Set) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) Sets(com.google.common.collect.Sets) HashSet(java.util.HashSet) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Stream(java.util.stream.Stream) Map(java.util.Map) Var(ai.grakn.graql.Var) Answer(ai.grakn.graql.admin.Answer) Var(ai.grakn.graql.Var) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator)

Example 9 with LazyAnswerIterator

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));
    });
}
Also used : Pair(ai.grakn.graql.internal.reasoner.utils.Pair) ImmutableSet(com.google.common.collect.ImmutableSet) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Iterator(java.util.Iterator) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Set(java.util.Set) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) Sets(com.google.common.collect.Sets) HashSet(java.util.HashSet) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Stream(java.util.stream.Stream) Map(java.util.Map) Var(ai.grakn.graql.Var) Answer(ai.grakn.graql.admin.Answer) Var(ai.grakn.graql.Var) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator)

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