use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class ExplanationBuilder method buildExplanation.
public static List<Answer> buildExplanation(ai.grakn.graql.admin.Answer queryAnswer) {
final List<Answer> explanation = new ArrayList<>();
queryAnswer.getExplanation().getAnswers().forEach(answer -> {
AnswerExplanation expl = answer.getExplanation();
Atom atom = ((ReasonerAtomicQuery) expl.getQuery()).getAtom();
ai.grakn.graql.admin.Answer inferredAnswer = new QueryAnswer();
if (expl.isLookupExplanation()) {
ReasonerAtomicQuery rewrittenQuery = ReasonerQueries.atomic(atom.rewriteWithRelationVariable());
inferredAnswer = ReasonerQueries.atomic(rewrittenQuery, answer).getQuery().stream().findFirst().orElse(new QueryAnswer());
} else if (expl.isRuleExplanation()) {
Atom headAtom = ((RuleExplanation) expl).getRule().getHead().getAtom();
ReasonerAtomicQuery rewrittenQuery = ReasonerQueries.atomic(headAtom.rewriteWithRelationVariable());
inferredAnswer = headAtom.getMultiUnifier(atom, UnifierType.RULE).stream().map(Unifier::inverse).flatMap(unifier -> rewrittenQuery.materialise(answer.unify(unifier))).findFirst().orElse(new QueryAnswer());
}
explanation.add(Answer.create(inferredAnswer));
});
return explanation;
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class AtomicState method propagateAnswer.
@Override
ResolutionState propagateAnswer(AnswerState state) {
Answer answer = state.getAnswer();
ReasonerAtomicQuery query = getQuery();
if (answer.isEmpty())
return null;
if (state.getRule() != null && query.getAtom().requiresRoleExpansion()) {
return new RoleExpansionState(answer, getUnifier(), query.getAtom().getRoleExpansionVariables(), getParentState());
}
return new AnswerState(answer, getUnifier(), getParentState());
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class AtomicState method ruleAnswer.
private Answer ruleAnswer(Answer baseAnswer, InferenceRule rule, Unifier unifier) {
ReasonerAtomicQuery query = getQuery();
Answer answer = baseAnswer.merge(rule.getHead().getRoleSubstitution()).unify(unifier);
if (answer.isEmpty())
return answer;
return answer.merge(query.getSubstitution()).project(query.getVarNames()).explain(new RuleExplanation(query, rule));
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class AtomicState method materialisedAnswer.
private Answer materialisedAnswer(Answer baseAnswer, InferenceRule rule, Unifier unifier) {
Answer answer = baseAnswer;
ReasonerAtomicQuery query = getQuery();
QueryCache<ReasonerAtomicQuery> cache = getCache();
ReasonerAtomicQuery subbedQuery = ReasonerQueries.atomic(query, answer);
ReasonerAtomicQuery ruleHead = ReasonerQueries.atomic(rule.getHead(), answer);
Set<Var> queryVars = query.getVarNames().size() < ruleHead.getVarNames().size() ? unifier.keySet() : ruleHead.getVarNames();
boolean queryEquivalentToHead = subbedQuery.isEquivalent(ruleHead);
// check if the specific answer to ruleHead already in cache/db
Answer headAnswer = cache.getAnswer(ruleHead, answer).project(queryVars).unify(unifier);
// if not and query different than rule head do the same with the query
Answer queryAnswer = headAnswer.isEmpty() && queryEquivalentToHead ? cache.getAnswer(query, answer) : new QueryAnswer();
// ensure no duplicates created - only materialise answer if it doesn't exist in the db
if (headAnswer.isEmpty() && queryAnswer.isEmpty()) {
Answer materialisedSub = ruleHead.materialise(answer).findFirst().orElse(null);
if (!queryEquivalentToHead)
cache.recordAnswer(ruleHead, materialisedSub);
answer = materialisedSub.project(queryVars).unify(unifier);
} else {
answer = headAnswer.isEmpty() ? queryAnswer : headAnswer;
}
if (answer.isEmpty())
return answer;
return answer.merge(query.getSubstitution()).explain(new RuleExplanation(query, rule));
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class TypeInferenceQueryTest method typedAnswers.
private List<Answer> typedAnswers(List<RelationshipType> possibleTypes, String pattern, EmbeddedGraknTx<?> graph) {
List<Answer> answers = new ArrayList<>();
ReasonerAtomicQuery query = ReasonerQueries.atomic(conjunction(pattern, graph), graph);
for (Type type : possibleTypes) {
GetQuery typedQuery = graph.graql().match(ReasonerQueries.atomic(query.getAtom().addType(type)).getPattern()).get();
typedQuery.stream().filter(ans -> !answers.contains(ans)).forEach(answers::add);
}
return answers;
}
Aggregations