Search in sources :

Example 1 with ResolutionState

use of ai.grakn.graql.internal.reasoner.state.ResolutionState 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 ResolutionState

use of ai.grakn.graql.internal.reasoner.state.ResolutionState in project grakn by graknlabs.

the class ResolutionIterator method findNextAnswer.

private Answer findNextAnswer() {
    while (!states.isEmpty()) {
        ResolutionState state = states.pop();
        if (state.isAnswerState() && state.isTopState()) {
            return state.getSubstitution();
        }
        ResolutionState newState = state.generateSubGoal();
        if (newState != null) {
            if (!state.isAnswerState())
                states.push(state);
            states.push(newState);
        }
    }
    return null;
}
Also used : ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState)

Example 3 with ResolutionState

use of ai.grakn.graql.internal.reasoner.state.ResolutionState in project grakn by graknlabs.

the class ReasonerQueryImpl method queryStateIterator.

/**
 * @param parent parent state
 * @param subGoals set of visited sub goals
 * @param cache query cache
 * @return query state iterator (db iter + unifier + state iter) for this query
 */
public Pair<Iterator<ResolutionState>, MultiUnifier> queryStateIterator(QueryStateBase parent, Set<ReasonerAtomicQuery> subGoals, QueryCache<ReasonerAtomicQuery> cache) {
    Iterator<AnswerState> dbIterator;
    Iterator<QueryStateBase> subGoalIterator;
    if (!this.isRuleResolvable()) {
        dbIterator = this.getQuery().stream().map(ans -> ans.explain(new JoinExplanation(this, ans))).map(ans -> new AnswerState(ans, parent.getUnifier(), parent)).iterator();
        subGoalIterator = Collections.emptyIterator();
    } else {
        dbIterator = Collections.emptyIterator();
        LinkedList<ReasonerQueryImpl> subQueries = new ResolutionPlan(this).queryPlan();
        LOG.trace("CQ plan:\n" + subQueries.stream().map(sq -> sq.toString() + (sq.isRuleResolvable() ? "*" : "")).collect(Collectors.joining("\n")));
        subGoalIterator = Iterators.singletonIterator(new CumulativeState(subQueries, new QueryAnswer(), parent.getUnifier(), parent, subGoals, cache));
    }
    return new Pair<>(Iterators.concat(dbIterator, subGoalIterator), new MultiUnifierImpl());
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) LoggerFactory(org.slf4j.LoggerFactory) Type(ai.grakn.concept.Type) ResolutionIterator(ai.grakn.graql.internal.reasoner.ResolutionIterator) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) QueryAnswerStream.join(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.join) Map(java.util.Map) IsaAtom(ai.grakn.graql.internal.reasoner.atom.binary.IsaAtom) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) ConceptId(ai.grakn.concept.ConceptId) Graql.var(ai.grakn.graql.Graql.var) ImmutableSet(com.google.common.collect.ImmutableSet) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ConjunctiveState(ai.grakn.graql.internal.reasoner.state.ConjunctiveState) QueryAnswerStream.joinWithInverse(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.joinWithInverse) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Cache(ai.grakn.graql.internal.reasoner.cache.Cache) Stream(java.util.stream.Stream) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) Var(ai.grakn.graql.Var) AtomicBase(ai.grakn.graql.internal.reasoner.atom.AtomicBase) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Concept(ai.grakn.concept.Concept) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) CumulativeState(ai.grakn.graql.internal.reasoner.state.CumulativeState) JoinExplanation(ai.grakn.graql.internal.reasoner.explanation.JoinExplanation) Iterators(com.google.common.collect.Iterators) QueryAnswerStream.nonEqualsFilter(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.nonEqualsFilter) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) LinkedList(java.util.LinkedList) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Nullable(javax.annotation.Nullable) LinkedHashSet(java.util.LinkedHashSet) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) IsaAtomBase(ai.grakn.graql.internal.reasoner.atom.binary.IsaAtomBase) GetQuery(ai.grakn.graql.GetQuery) AtomicFactory(ai.grakn.graql.internal.reasoner.atom.AtomicFactory) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Schema(ai.grakn.util.Schema) Unifier(ai.grakn.graql.admin.Unifier) ResolutionPlan(ai.grakn.graql.internal.reasoner.plan.ResolutionPlan) Collections(java.util.Collections) JoinExplanation(ai.grakn.graql.internal.reasoner.explanation.JoinExplanation) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) CumulativeState(ai.grakn.graql.internal.reasoner.state.CumulativeState) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) ResolutionPlan(ai.grakn.graql.internal.reasoner.plan.ResolutionPlan) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Aggregations

ResolutionState (ai.grakn.graql.internal.reasoner.state.ResolutionState)3 Concept (ai.grakn.concept.Concept)2 GraqlQueryException (ai.grakn.exception.GraqlQueryException)2 Var (ai.grakn.graql.Var)2 Answer (ai.grakn.graql.admin.Answer)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 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)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 Cache (ai.grakn.graql.internal.reasoner.cache.Cache)2 LazyQueryCache (ai.grakn.graql.internal.reasoner.cache.LazyQueryCache)2 QueryCache (ai.grakn.graql.internal.reasoner.cache.QueryCache)2 InferenceRule (ai.grakn.graql.internal.reasoner.rule.InferenceRule)2