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