Search in sources :

Example 1 with AnswerExplanation

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;
}
Also used : QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ArrayList(java.util.ArrayList) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) AnswerExplanation(ai.grakn.graql.admin.AnswerExplanation)

Example 2 with AnswerExplanation

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);
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) AnswerExplanation(ai.grakn.graql.admin.AnswerExplanation) Test(org.junit.Test)

Aggregations

AnswerExplanation (ai.grakn.graql.admin.AnswerExplanation)2 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)2 GraknTx (ai.grakn.GraknTx)1 GetQuery (ai.grakn.graql.GetQuery)1 QueryBuilder (ai.grakn.graql.QueryBuilder)1 Answer (ai.grakn.graql.admin.Answer)1 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)1 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1