Search in sources :

Example 1 with Answer

use of ai.grakn.graql.admin.Answer in project grakn by graknlabs.

the class QueryAnswerStream method knownFilterWithInverse.

static boolean knownFilterWithInverse(Answer answer, Map<Pair<Var, Concept>, Set<Answer>> stream2InverseMap) {
    Iterator<Map.Entry<Var, Concept>> eit = answer.entrySet().iterator();
    Map.Entry<Var, Concept> entry = eit.next();
    Set<Answer> matchAnswers = findMatchingAnswers(entry.getKey(), entry.getValue(), stream2InverseMap);
    while (eit.hasNext()) {
        entry = eit.next();
        matchAnswers = Sets.intersection(matchAnswers, findMatchingAnswers(entry.getKey(), entry.getValue(), stream2InverseMap));
    }
    for (Answer knownAnswer : matchAnswers) {
        if (knownAnswer.entrySet().containsAll(answer.entrySet())) {
            return false;
        }
    }
    return true;
}
Also used : Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Answer(ai.grakn.graql.admin.Answer) Var(ai.grakn.graql.Var) Map(java.util.Map)

Example 2 with Answer

use of ai.grakn.graql.admin.Answer in project grakn by graknlabs.

the class ReasonerAtomicQuery method queryStateIterator.

@Override
public Pair<Iterator<ResolutionState>, MultiUnifier> queryStateIterator(QueryStateBase parent, Set<ReasonerAtomicQuery> visitedSubGoals, QueryCache<ReasonerAtomicQuery> cache) {
    Pair<Stream<Answer>, MultiUnifier> cacheEntry = cache.getAnswerStreamWithUnifier(this);
    MultiUnifier cacheUnifier = cacheEntry.getValue().inverse();
    Iterator<AnswerState> dbIterator = cacheEntry.getKey().map(a -> a.explain(a.getExplanation().setQuery(this))).map(ans -> new AnswerState(ans, parent.getUnifier(), parent)).iterator();
    Iterator<ResolutionState> subGoalIterator;
    // if this is ground and exists in the db then do not resolve further
    if (visitedSubGoals.contains(this) || (this.isGround() && dbIterator.hasNext())) {
        subGoalIterator = Collections.emptyIterator();
    } else {
        visitedSubGoals.add(this);
        subGoalIterator = this.getRuleStream().map(rulePair -> rulePair.getKey().subGoal(this.getAtom(), rulePair.getValue(), parent, visitedSubGoals, cache)).iterator();
    }
    return new Pair<>(Iterators.concat(dbIterator, subGoalIterator), cacheUnifier);
}
Also used : QueryAnswerStream.entityTypeFilter(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.entityTypeFilter) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Concept(ai.grakn.concept.Concept) LoggerFactory(org.slf4j.LoggerFactory) Answer(ai.grakn.graql.admin.Answer) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) Iterators(com.google.common.collect.Iterators) HashSet(java.util.HashSet) ReasonerQueryIterator(ai.grakn.graql.internal.reasoner.iterator.ReasonerQueryIterator) AnswerExplanation(ai.grakn.graql.admin.AnswerExplanation) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Logger(org.slf4j.Logger) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Iterator(java.util.Iterator) Conjunction(ai.grakn.graql.admin.Conjunction) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Sets(com.google.common.collect.Sets) ReasonerUtils.typeUnifier(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.typeUnifier) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Cache(ai.grakn.graql.internal.reasoner.cache.Cache) Stream(java.util.stream.Stream) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) QueryAnswerStream.knownFilterWithInverse(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.knownFilterWithInverse) RuleExplanation(ai.grakn.graql.internal.reasoner.explanation.RuleExplanation) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Preconditions(com.google.common.base.Preconditions) AtomicStateProducer(ai.grakn.graql.internal.reasoner.state.AtomicStateProducer) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) Collections(java.util.Collections) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) Stream(java.util.stream.Stream) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 3 with Answer

use of ai.grakn.graql.admin.Answer in project grakn by graknlabs.

the class AtomicQueryTest method testWhenRoleTypesAreAmbiguous_answersArePermutedCorrectly.

@Test
public void testWhenRoleTypesAreAmbiguous_answersArePermutedCorrectly() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    String childString = "match (geo-entity: $x, entity-location: $y) isa is-located-in; get;";
    String parentString = "match ($x, $y) isa is-located-in; get;";
    QueryBuilder qb = graph.graql().infer(false);
    GetQuery childQuery = qb.parse(childString);
    GetQuery parentQuery = qb.parse(parentString);
    Set<Answer> answers = childQuery.stream().collect(toSet());
    Set<Answer> fullAnswers = parentQuery.stream().collect(toSet());
    Atom childAtom = ReasonerQueries.atomic(conjunction(childQuery.match().admin().getPattern()), graph).getAtom();
    Atom parentAtom = ReasonerQueries.atomic(conjunction(parentQuery.match().admin().getPattern()), graph).getAtom();
    MultiUnifier multiUnifier = childAtom.getMultiUnifier(childAtom, UnifierType.RULE);
    Set<Answer> permutedAnswers = answers.stream().flatMap(a -> multiUnifier.stream().map(a::unify)).collect(Collectors.toSet());
    MultiUnifier multiUnifier2 = childAtom.getMultiUnifier(parentAtom, UnifierType.RULE);
    Set<Answer> permutedAnswers2 = answers.stream().flatMap(a -> multiUnifier2.stream().map(a::unify)).collect(Collectors.toSet());
    assertEquals(fullAnswers, permutedAnswers2);
    assertEquals(answers, permutedAnswers);
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Iterables(com.google.common.collect.Iterables) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) BeforeClass(org.junit.BeforeClass) GraknTestUtil(ai.grakn.util.GraknTestUtil) GraqlTestUtil.assertCollectionsEqual(ai.grakn.util.GraqlTestUtil.assertCollectionsEqual) Concept(ai.grakn.concept.Concept) Answer(ai.grakn.graql.admin.Answer) ReasonerQueryEquivalence(ai.grakn.graql.internal.reasoner.query.ReasonerQueryEquivalence) ArrayList(java.util.ArrayList) GraqlTestUtil.assertExists(ai.grakn.util.GraqlTestUtil.assertExists) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ClassRule(org.junit.ClassRule) ExpectedException(org.junit.rules.ExpectedException) Collectors.toSet(java.util.stream.Collectors.toSet) QueryBuilder(ai.grakn.graql.QueryBuilder) Patterns(ai.grakn.graql.internal.pattern.Patterns) Graql.var(ai.grakn.graql.Graql.var) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Equivalence(com.google.common.base.Equivalence) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) List(java.util.List) Rule(org.junit.Rule) GeoKB(ai.grakn.test.kbs.GeoKB) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) GraqlTestUtil.assertNotExists(ai.grakn.util.GraqlTestUtil.assertNotExists) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Assume.assumeTrue(org.junit.Assume.assumeTrue) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Schema(ai.grakn.util.Schema) Unifier(ai.grakn.graql.admin.Unifier) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Schema(ai.grakn.util.Schema) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Test(org.junit.Test)

Example 4 with Answer

use of ai.grakn.graql.admin.Answer in project grakn by graknlabs.

the class AtomicQueryTest method testExactUnification_BinaryRelationWithSubs.

/**
 * ##################################
 *
 *     Unification Tests
 *
 * ##################################
 */
@Test
public void testExactUnification_BinaryRelationWithSubs() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    Concept x1 = getConceptByResourceValue(graph, "x1");
    Concept x2 = getConceptByResourceValue(graph, "x2");
    String basePatternString = "{($x1, $x2) isa binary;}";
    String basePatternString2 = "{($y1, $y2) isa binary;}";
    ReasonerAtomicQuery xbaseQuery = ReasonerQueries.atomic(conjunction(basePatternString, graph), graph);
    ReasonerAtomicQuery ybaseQuery = ReasonerQueries.atomic(conjunction(basePatternString2, graph), graph);
    Answer xAnswer = new QueryAnswer(ImmutableMap.of(var("x1"), x1, var("x2"), x2));
    Answer flippedXAnswer = new QueryAnswer(ImmutableMap.of(var("x1"), x2, var("x2"), x1));
    Answer yAnswer = new QueryAnswer(ImmutableMap.of(var("y1"), x1, var("y2"), x2));
    Answer flippedYAnswer = new QueryAnswer(ImmutableMap.of(var("y1"), x2, var("y2"), x1));
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(xbaseQuery, xAnswer);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(xbaseQuery, flippedXAnswer);
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("x1"), var("x2"), var("x2"), var("x1")));
    assertTrue(unifier.containsAll(correctUnifier));
    ReasonerAtomicQuery yChildQuery = ReasonerQueries.atomic(ybaseQuery, yAnswer);
    ReasonerAtomicQuery yChildQuery2 = ReasonerQueries.atomic(ybaseQuery, flippedYAnswer);
    Unifier unifier2 = yChildQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier2 = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2")));
    assertTrue(unifier2.containsAll(correctUnifier2));
    Unifier unifier3 = yChildQuery2.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier3 = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x2"), var("y2"), var("x1")));
    assertTrue(unifier3.containsAll(correctUnifier3));
}
Also used : Concept(ai.grakn.concept.Concept) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 5 with Answer

use of ai.grakn.graql.admin.Answer in project grakn by graknlabs.

the class OntologicalQueryTest method allRolesGivenRelationRelates.

@Test
public void allRolesGivenRelationRelates() {
    GraknTx tx = testContext.tx();
    QueryBuilder qb = tx.graql().infer(true);
    String queryString = "match reifying-relation relates $x; get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    assertEquals(answers.stream().map(ans -> ans.get("x")).collect(Collectors.toSet()), tx.getRelationshipType("reifying-relation").relates().collect(Collectors.toSet()));
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Aggregations

Answer (ai.grakn.graql.admin.Answer)193 Test (org.junit.Test)164 QueryBuilder (ai.grakn.graql.QueryBuilder)104 GetQuery (ai.grakn.graql.GetQuery)69 GraknTx (ai.grakn.GraknTx)47 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)37 Concept (ai.grakn.concept.Concept)32 Var (ai.grakn.graql.Var)21 Set (java.util.Set)21 Unifier (ai.grakn.graql.admin.Unifier)19 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)18 Collectors (java.util.stream.Collectors)18 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)17 Sets (com.google.common.collect.Sets)16 List (java.util.List)16 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)15 Stream (java.util.stream.Stream)15 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)14 SampleKBContext (ai.grakn.test.rule.SampleKBContext)14 Pair (ai.grakn.graql.internal.reasoner.utils.Pair)12