use of ai.grakn.graql.internal.reasoner.explanation.RuleExplanation 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.explanation.RuleExplanation 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));
}
Aggregations