Search in sources :

Example 26 with ReasonerAtomicQuery

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

the class AtomicTest method testUnification_ResourceWithIndirectValuePredicate.

@Test
public void testUnification_ResourceWithIndirectValuePredicate() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    String resource = "{$x has resource $r;$r val 'f';}";
    String resource2 = "{$r has resource $x;$x val 'f';}";
    String resource3 = "{$r has resource 'f';}";
    ReasonerAtomicQuery resourceQuery = ReasonerQueries.atomic(conjunction(resource, graph), graph);
    ReasonerAtomicQuery resourceQuery2 = ReasonerQueries.atomic(conjunction(resource2, graph), graph);
    ReasonerAtomicQuery resourceQuery3 = ReasonerQueries.atomic(conjunction(resource3, graph), graph);
    String type = "{$x isa resource;$x id '" + resourceQuery.getQuery().execute().iterator().next().get("r").getId().getValue() + "';}";
    ReasonerAtomicQuery typeQuery = ReasonerQueries.atomic(conjunction(type, graph), graph);
    Atom typeAtom = typeQuery.getAtom();
    Atom resourceAtom = resourceQuery.getAtom();
    Atom resourceAtom2 = resourceQuery2.getAtom();
    Atom resourceAtom3 = resourceQuery3.getAtom();
    Unifier unifier = resourceAtom.getUnifier(typeAtom);
    Unifier unifier2 = resourceAtom2.getUnifier(typeAtom);
    Unifier unifier3 = resourceAtom3.getUnifier(typeAtom);
    Answer typeAnswer = typeQuery.getQuery().execute().iterator().next();
    Answer resourceAnswer = resourceQuery.getQuery().execute().iterator().next();
    Answer resourceAnswer2 = resourceQuery2.getQuery().execute().iterator().next();
    Answer resourceAnswer3 = resourceQuery3.getQuery().execute().iterator().next();
    assertEquals(typeAnswer.get(var("x")), resourceAnswer.unify(unifier).get(var("x")));
    assertEquals(typeAnswer.get(var("x")), resourceAnswer2.unify(unifier2).get(var("x")));
    assertEquals(typeAnswer.get(var("x")), resourceAnswer3.unify(unifier3).get(var("x")));
}
Also used : Answer(ai.grakn.graql.admin.Answer) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 27 with ReasonerAtomicQuery

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

the class AtomicTest method exactUnification.

/**
 * checks the correctness and uniqueness of an exact unifier required to unify child query with parent
 * @param parentQuery parent query
 * @param childQuery child query
 * @param checkInverse flag specifying whether the inverse equality u^{-1}=u(parent, child) of the unifier u(child, parent) should be checked
 * @param checkEquality if true the parent and child answers will be checked for equality, otherwise they are checked for containment of child answers in parent
 */
private void exactUnification(ReasonerAtomicQuery parentQuery, ReasonerAtomicQuery childQuery, boolean checkInverse, boolean checkEquality) {
    Atom childAtom = childQuery.getAtom();
    Atom parentAtom = parentQuery.getAtom();
    Unifier unifier = childAtom.getMultiUnifier(parentAtom, UnifierType.EXACT).getUnifier();
    List<Answer> childAnswers = childQuery.getQuery().execute();
    List<Answer> unifiedAnswers = childAnswers.stream().map(a -> a.unify(unifier)).filter(a -> !a.isEmpty()).collect(Collectors.toList());
    List<Answer> parentAnswers = parentQuery.getQuery().execute();
    if (checkInverse) {
        Unifier unifier2 = parentAtom.getUnifier(childAtom);
        assertEquals(unifier.inverse(), unifier2);
        assertEquals(unifier, unifier2.inverse());
    }
    assertTrue(!childAnswers.isEmpty());
    assertTrue(!unifiedAnswers.isEmpty());
    assertTrue(!parentAnswers.isEmpty());
    if (!checkEquality) {
        assertTrue(parentAnswers.containsAll(unifiedAnswers));
    } else {
        assertCollectionsEqual(parentAnswers, unifiedAnswers);
        Unifier inverse = unifier.inverse();
        List<Answer> parentToChild = parentAnswers.stream().map(a -> a.unify(inverse)).collect(Collectors.toList());
        assertCollectionsEqual(parentToChild, childAnswers);
    }
}
Also used : 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) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Assert.assertThat(org.junit.Assert.assertThat) HashMultimap(com.google.common.collect.HashMultimap) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) ClassRule(org.junit.ClassRule) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) Patterns(ai.grakn.graql.internal.pattern.Patterns) Graql.var(ai.grakn.graql.Graql.var) Matchers.empty(org.hamcrest.Matchers.empty) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Collection(java.util.Collection) 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) List(java.util.List) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) 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) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier)

Example 28 with ReasonerAtomicQuery

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

the class AtomicTest method testUnification_IndirectRoles.

@Test
public void testUnification_IndirectRoles() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    VarPatternAdmin basePattern = var().rel(var("role1").label("subRole1"), var("y1")).rel(var("role2").label("subSubRole2"), var("y2")).isa("binary").admin();
    ReasonerAtomicQuery baseQuery = ReasonerQueries.atomic(Patterns.conjunction(Sets.newHashSet(basePattern)), graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(conjunction("{($r1: $x1, $r2: $x2) isa binary;" + "$r1 label 'subRole1';" + "$r2 label 'subSubRole2';}", graph), graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(conjunction("{($R1: $x, $R2: $y) isa binary;" + "$R1 label 'subRole1';" + "$R2 label 'subSubRole2';}", graph), graph);
    exactUnification(parentQuery, childQuery, true, true);
    exactUnification(baseQuery, parentQuery, true, true);
    exactUnification(baseQuery, childQuery, true, true);
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 29 with ReasonerAtomicQuery

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

the class QueryCacheTest method lazilyGetRetrieveAnswers.

@Test
public void lazilyGetRetrieveAnswers() {
    LazyQueryCache<ReasonerAtomicQuery> cache = new LazyQueryCache<>();
    cache.record(recordQuery, recordQuery.getQuery().stream());
    LazyAnswerIterator retrieveIterator = cache.getAnswers(retrieveQuery);
    LazyAnswerIterator recordIterator = cache.getAnswers(recordQuery);
    Set<Answer> record = recordIterator.stream().collect(toSet());
    Set<Answer> retrieve = retrieveIterator.stream().map(ans -> ans.unify(retrieveToRecordUnifier)).collect(toSet());
    assertTrue(!retrieve.isEmpty());
    assertEquals(record, retrieve);
}
Also used : GraknTestUtil(ai.grakn.util.GraknTestUtil) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) Entity(ai.grakn.concept.Entity) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) GraknTx(ai.grakn.GraknTx) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ClassRule(org.junit.ClassRule) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) Graql.var(ai.grakn.graql.Graql.var) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Stream(java.util.stream.Stream) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) Assert.assertFalse(org.junit.Assert.assertFalse) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) 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) 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) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 30 with ReasonerAtomicQuery

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

the class QueryCacheTest method getRetrieveAnswerStream.

@Test
public void getRetrieveAnswerStream() {
    QueryCache<ReasonerAtomicQuery> cache = new QueryCache<>();
    Answer answer = recordQuery.getQuery().stream().findFirst().orElse(null);
    Answer retrieveAnswer = answer.unify(recordToRetrieveUnifier);
    Stream<Answer> recordStream = cache.getAnswerStream(recordQuery);
    Stream<Answer> retrieveStream = cache.getAnswerStream(retrieveQuery);
    QueryAnswers recordAnswers = new QueryAnswers(recordStream.collect(Collectors.toSet()));
    QueryAnswers retrieveAnswers = new QueryAnswers(retrieveStream.collect(Collectors.toSet()));
    assertTrue(recordAnswers.contains(answer));
    assertTrue(retrieveAnswers.contains(retrieveAnswer));
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) QueryAnswers(ai.grakn.graql.internal.reasoner.query.QueryAnswers) Test(org.junit.Test)

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