Search in sources :

Example 1 with RuleExplanation

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));
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) RuleExplanation(ai.grakn.graql.internal.reasoner.explanation.RuleExplanation) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)

Example 2 with RuleExplanation

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));
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Var(ai.grakn.graql.Var) RuleExplanation(ai.grakn.graql.internal.reasoner.explanation.RuleExplanation) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)

Aggregations

Answer (ai.grakn.graql.admin.Answer)2 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)2 RuleExplanation (ai.grakn.graql.internal.reasoner.explanation.RuleExplanation)2 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)2 Var (ai.grakn.graql.Var)1