use of ai.grakn.graql.admin.AnswerExplanation 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.admin.AnswerExplanation in project grakn by graknlabs.
the class ExplanationTest method testExplainingMixedAtomicQueries.
@Test
public void testExplainingMixedAtomicQueries() {
GraknTx expGraph = explanationKB.tx();
QueryBuilder eiqb = expGraph.graql().infer(true);
String queryString = "match " + "$x has value 'high';" + "($x, $y) isa carried-relation;" + "get;";
GetQuery query = eiqb.parse(queryString);
List<Answer> answers = query.execute();
testExplanation(answers);
Answer inferredAnswer = answers.stream().filter(ans -> ans.getExplanations().stream().filter(AnswerExplanation::isRuleExplanation).findFirst().isPresent()).findFirst().orElse(null);
Set<AnswerExplanation> explanations = inferredAnswer.getExplanations();
assertEquals(explanations.stream().filter(AnswerExplanation::isRuleExplanation).count(), 2);
assertEquals(explanations.stream().filter(AnswerExplanation::isLookupExplanation).count(), 4);
}
Aggregations