Search in sources :

Example 21 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class ExplanationTest method testExplanationTreeCorrect_TransitiveClosureWithSpecificResourceAndTypes.

@Test
public void testExplanationTreeCorrect_TransitiveClosureWithSpecificResourceAndTypes() {
    String queryString = "match $x isa university;" + "(geo-entity: $x, entity-location: $y) isa is-located-in;" + "$y isa country;$y has name 'Poland'; get;";
    Answer answer1 = new QueryAnswer(ImmutableMap.of(var("x"), polibuda, var("y"), poland));
    Answer answer2 = new QueryAnswer(ImmutableMap.of(var("x"), uw, var("y"), poland));
    List<Answer> answers = iqb.<GetQuery>parse(queryString).execute();
    testExplanation(answers);
    Answer queryAnswer1 = findAnswer(answer1, answers);
    Answer queryAnswer2 = findAnswer(answer2, answers);
    assertEquals(queryAnswer1, answer1);
    assertEquals(queryAnswer2, answer2);
    assertTrue(queryAnswer1.getExplanation().isJoinExplanation());
    assertTrue(queryAnswer2.getExplanation().isJoinExplanation());
    // (res), (uni, ctr) - (region, ctr)
    // - (uni, region) - {(city, region), (uni, city)
    assertEquals(queryAnswer1.getPartialAnswers().size(), 6);
    assertEquals(queryAnswer2.getPartialAnswers().size(), 6);
    assertEquals(4, getLookupExplanations(queryAnswer1).size());
    assertEquals(4, queryAnswer1.getExplicitPath().size());
    assertEquals(4, getLookupExplanations(queryAnswer2).size());
    assertEquals(4, queryAnswer2.getExplicitPath().size());
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Test(org.junit.Test)

Example 22 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class ExplanationTest method testExplanationTreeCorrect_TransitiveClosure.

@Test
public void testExplanationTreeCorrect_TransitiveClosure() {
    String queryString = "match (geo-entity: $x, entity-location: $y) isa is-located-in; get;";
    Answer answer1 = new QueryAnswer(ImmutableMap.of(var("x"), polibuda, var("y"), warsaw));
    Answer answer2 = new QueryAnswer(ImmutableMap.of(var("x"), polibuda, var("y"), masovia));
    Answer answer3 = new QueryAnswer(ImmutableMap.of(var("x"), polibuda, var("y"), poland));
    Answer answer4 = new QueryAnswer(ImmutableMap.of(var("x"), polibuda, var("y"), europe));
    List<Answer> answers = iqb.<GetQuery>parse(queryString).execute();
    testExplanation(answers);
    Answer queryAnswer1 = findAnswer(answer1, answers);
    Answer queryAnswer2 = findAnswer(answer2, answers);
    Answer queryAnswer3 = findAnswer(answer3, answers);
    Answer queryAnswer4 = findAnswer(answer4, answers);
    assertEquals(queryAnswer1, answer1);
    assertEquals(queryAnswer2, answer2);
    assertEquals(queryAnswer3, answer3);
    assertEquals(queryAnswer4, answer4);
    assertEquals(queryAnswer1.getPartialAnswers().size(), 1);
    assertEquals(queryAnswer2.getPartialAnswers().size(), 3);
    assertEquals(queryAnswer3.getPartialAnswers().size(), 5);
    assertEquals(queryAnswer4.getPartialAnswers().size(), 7);
    assertTrue(queryAnswer1.getExplanation().isLookupExplanation());
    assertTrue(queryAnswer2.getExplanation().isRuleExplanation());
    assertEquals(2, getLookupExplanations(queryAnswer2).size());
    assertEquals(2, queryAnswer2.getExplicitPath().size());
    assertTrue(queryAnswer3.getExplanation().isRuleExplanation());
    assertEquals(2, getRuleExplanations(queryAnswer3).size());
    assertEquals(3, queryAnswer3.getExplicitPath().size());
    assertTrue(queryAnswer4.getExplanation().isRuleExplanation());
    assertEquals(3, getRuleExplanations(queryAnswer4).size());
    assertEquals(4, queryAnswer4.getExplicitPath().size());
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Test(org.junit.Test)

Example 23 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class ReasonerQueryImpl method getSubstitution.

/**
 * @return substitution obtained from all id predicates (including internal) in the query
 */
public Answer getSubstitution() {
    if (substitution == null) {
        Set<Var> varNames = getVarNames();
        Set<IdPredicate> predicates = getAtoms(IsaAtomBase.class).map(IsaAtomBase::getTypePredicate).filter(Objects::nonNull).filter(p -> varNames.contains(p.getVarName())).collect(Collectors.toSet());
        getAtoms(IdPredicate.class).forEach(predicates::add);
        HashMap<Var, Concept> answerMap = new HashMap<>();
        predicates.forEach(p -> {
            Concept concept = tx().getConcept(p.getPredicate());
            if (concept == null)
                throw GraqlQueryException.idNotFound(p.getPredicate());
            answerMap.put(p.getVarName(), concept);
        });
        substitution = new QueryAnswer(answerMap);
    }
    return substitution;
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) LoggerFactory(org.slf4j.LoggerFactory) Type(ai.grakn.concept.Type) ResolutionIterator(ai.grakn.graql.internal.reasoner.ResolutionIterator) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) QueryAnswerStream.join(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.join) Map(java.util.Map) IsaAtom(ai.grakn.graql.internal.reasoner.atom.binary.IsaAtom) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) ConceptId(ai.grakn.concept.ConceptId) Graql.var(ai.grakn.graql.Graql.var) ImmutableSet(com.google.common.collect.ImmutableSet) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ConjunctiveState(ai.grakn.graql.internal.reasoner.state.ConjunctiveState) QueryAnswerStream.joinWithInverse(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.joinWithInverse) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Cache(ai.grakn.graql.internal.reasoner.cache.Cache) Stream(java.util.stream.Stream) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) Var(ai.grakn.graql.Var) AtomicBase(ai.grakn.graql.internal.reasoner.atom.AtomicBase) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Concept(ai.grakn.concept.Concept) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) CumulativeState(ai.grakn.graql.internal.reasoner.state.CumulativeState) JoinExplanation(ai.grakn.graql.internal.reasoner.explanation.JoinExplanation) Iterators(com.google.common.collect.Iterators) QueryAnswerStream.nonEqualsFilter(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.nonEqualsFilter) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) LinkedList(java.util.LinkedList) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Nullable(javax.annotation.Nullable) LinkedHashSet(java.util.LinkedHashSet) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) IsaAtomBase(ai.grakn.graql.internal.reasoner.atom.binary.IsaAtomBase) GetQuery(ai.grakn.graql.GetQuery) AtomicFactory(ai.grakn.graql.internal.reasoner.atom.AtomicFactory) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Schema(ai.grakn.util.Schema) Unifier(ai.grakn.graql.admin.Unifier) ResolutionPlan(ai.grakn.graql.internal.reasoner.plan.ResolutionPlan) Collections(java.util.Collections) Concept(ai.grakn.concept.Concept) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) HashMap(java.util.HashMap) Var(ai.grakn.graql.Var) Objects(java.util.Objects)

Example 24 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class ReasonerQueryImpl method queryStateIterator.

/**
 * @param parent parent state
 * @param subGoals set of visited sub goals
 * @param cache query cache
 * @return query state iterator (db iter + unifier + state iter) for this query
 */
public Pair<Iterator<ResolutionState>, MultiUnifier> queryStateIterator(QueryStateBase parent, Set<ReasonerAtomicQuery> subGoals, QueryCache<ReasonerAtomicQuery> cache) {
    Iterator<AnswerState> dbIterator;
    Iterator<QueryStateBase> subGoalIterator;
    if (!this.isRuleResolvable()) {
        dbIterator = this.getQuery().stream().map(ans -> ans.explain(new JoinExplanation(this, ans))).map(ans -> new AnswerState(ans, parent.getUnifier(), parent)).iterator();
        subGoalIterator = Collections.emptyIterator();
    } else {
        dbIterator = Collections.emptyIterator();
        LinkedList<ReasonerQueryImpl> subQueries = new ResolutionPlan(this).queryPlan();
        LOG.trace("CQ plan:\n" + subQueries.stream().map(sq -> sq.toString() + (sq.isRuleResolvable() ? "*" : "")).collect(Collectors.joining("\n")));
        subGoalIterator = Iterators.singletonIterator(new CumulativeState(subQueries, new QueryAnswer(), parent.getUnifier(), parent, subGoals, cache));
    }
    return new Pair<>(Iterators.concat(dbIterator, subGoalIterator), new MultiUnifierImpl());
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) LoggerFactory(org.slf4j.LoggerFactory) Type(ai.grakn.concept.Type) ResolutionIterator(ai.grakn.graql.internal.reasoner.ResolutionIterator) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) QueryAnswerStream.join(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.join) Map(java.util.Map) IsaAtom(ai.grakn.graql.internal.reasoner.atom.binary.IsaAtom) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) ConceptId(ai.grakn.concept.ConceptId) Graql.var(ai.grakn.graql.Graql.var) ImmutableSet(com.google.common.collect.ImmutableSet) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ConjunctiveState(ai.grakn.graql.internal.reasoner.state.ConjunctiveState) QueryAnswerStream.joinWithInverse(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.joinWithInverse) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Cache(ai.grakn.graql.internal.reasoner.cache.Cache) Stream(java.util.stream.Stream) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) Var(ai.grakn.graql.Var) AtomicBase(ai.grakn.graql.internal.reasoner.atom.AtomicBase) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Concept(ai.grakn.concept.Concept) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) CumulativeState(ai.grakn.graql.internal.reasoner.state.CumulativeState) JoinExplanation(ai.grakn.graql.internal.reasoner.explanation.JoinExplanation) Iterators(com.google.common.collect.Iterators) QueryAnswerStream.nonEqualsFilter(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.nonEqualsFilter) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) LinkedList(java.util.LinkedList) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Nullable(javax.annotation.Nullable) LinkedHashSet(java.util.LinkedHashSet) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) IsaAtomBase(ai.grakn.graql.internal.reasoner.atom.binary.IsaAtomBase) GetQuery(ai.grakn.graql.GetQuery) AtomicFactory(ai.grakn.graql.internal.reasoner.atom.AtomicFactory) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Schema(ai.grakn.util.Schema) Unifier(ai.grakn.graql.admin.Unifier) ResolutionPlan(ai.grakn.graql.internal.reasoner.plan.ResolutionPlan) Collections(java.util.Collections) JoinExplanation(ai.grakn.graql.internal.reasoner.explanation.JoinExplanation) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) CumulativeState(ai.grakn.graql.internal.reasoner.state.CumulativeState) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) ResolutionPlan(ai.grakn.graql.internal.reasoner.plan.ResolutionPlan) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 25 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class JacksonPrinterTest method whenPrintingMap_MapGraqlStringOverKeyAndValues.

@Test
public void whenPrintingMap_MapGraqlStringOverKeyAndValues() {
    QueryAnswer ans = new QueryAnswer();
    Map<?, ?> map = ImmutableMap.of(ans, ImmutableList.of(ans));
    Map<?, ?> expected = ImmutableMap.of(Answer.create(ans), ImmutableList.of(Answer.create(ans)));
    assertEquals(expected, printer.build(map));
}
Also used : QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Test(org.junit.Test)

Aggregations

QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)29 Answer (ai.grakn.graql.admin.Answer)15 Concept (ai.grakn.concept.Concept)13 Test (org.junit.Test)13 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)9 Var (ai.grakn.graql.Var)8 GetQuery (ai.grakn.graql.GetQuery)6 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)5 ConceptId (ai.grakn.concept.ConceptId)4 Type (ai.grakn.concept.Type)4 Unifier (ai.grakn.graql.admin.Unifier)4 MultiUnifierImpl (ai.grakn.graql.internal.reasoner.MultiUnifierImpl)4 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)4 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)4 GraqlQueryException (ai.grakn.exception.GraqlQueryException)3 RelationshipAtom (ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 List (java.util.List)3 Set (java.util.Set)3