Search in sources :

Example 16 with ReasonerAtomicQuery

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;
}
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 17 with ReasonerAtomicQuery

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

Example 18 with ReasonerAtomicQuery

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));
}
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 19 with ReasonerAtomicQuery

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));
}
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)

Example 20 with ReasonerAtomicQuery

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;
}
Also used : Iterables(com.google.common.collect.Iterables) Concept(ai.grakn.concept.Concept) Type(ai.grakn.concept.Type) Answer(ai.grakn.graql.admin.Answer) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) CollectionUtils(org.apache.commons.collections.CollectionUtils) ConceptId(ai.grakn.concept.ConceptId) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) GraqlTestUtil(ai.grakn.util.GraqlTestUtil) ExpectedException(org.junit.rules.ExpectedException) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Collectors.toSet(java.util.stream.Collectors.toSet) QueryBuilder(ai.grakn.graql.QueryBuilder) Patterns(ai.grakn.graql.internal.pattern.Patterns) Conjunction(ai.grakn.graql.admin.Conjunction) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) List(java.util.List) Rule(org.junit.Rule) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) SampleKBContext(ai.grakn.test.rule.SampleKBContext) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Schema(ai.grakn.util.Schema) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Type(ai.grakn.concept.Type) RelationshipType(ai.grakn.concept.RelationshipType) GetQuery(ai.grakn.graql.GetQuery) ArrayList(java.util.ArrayList) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)

Aggregations

ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)43 Test (org.junit.Test)35 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)24 Unifier (ai.grakn.graql.admin.Unifier)19 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)18 Answer (ai.grakn.graql.admin.Answer)17 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)17 QueryCache (ai.grakn.graql.internal.reasoner.cache.QueryCache)8 Concept (ai.grakn.concept.Concept)7 Conjunction (ai.grakn.graql.admin.Conjunction)7 Patterns (ai.grakn.graql.internal.pattern.Patterns)7 LazyQueryCache (ai.grakn.graql.internal.reasoner.cache.LazyQueryCache)7 ReasonerQueries (ai.grakn.graql.internal.reasoner.query.ReasonerQueries)7 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)7 Set (java.util.Set)7 Collectors.toSet (java.util.stream.Collectors.toSet)7 GetQuery (ai.grakn.graql.GetQuery)6 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)6 QueryAnswers (ai.grakn.graql.internal.reasoner.query.QueryAnswers)6 SampleKBContext (ai.grakn.test.rule.SampleKBContext)6