Search in sources :

Example 11 with ReasonerAtomicQuery

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

the class AtomicQueryTest method testExactUnification_TernaryRelation_ParentRepeatsRoles.

@Test
public void testExactUnification_TernaryRelation_ParentRepeatsRoles() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String parentString = "{(role1: $x, role1: $y, role2: $z) isa ternary;}";
    String childString = "{(role1: $u, role2: $v, role3: $q) isa ternary;}";
    String childString2 = "{(role1: $u, role2: $v, role2: $q) isa ternary;}";
    String childString3 = "{(role1: $u, role1: $v, role2: $q) isa ternary;}";
    String childString4 = "{(role1: $u, role1: $u, role2: $q) isa ternary;}";
    Conjunction<VarPatternAdmin> parentPattern = conjunction(parentString, graph);
    Conjunction<VarPatternAdmin> childPattern = conjunction(childString, graph);
    Conjunction<VarPatternAdmin> childPattern2 = conjunction(childString2, graph);
    Conjunction<VarPatternAdmin> childPattern3 = conjunction(childString3, graph);
    Conjunction<VarPatternAdmin> childPattern4 = conjunction(childString4, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(parentPattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(childPattern, graph);
    ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(childPattern2, graph);
    ReasonerAtomicQuery childQuery3 = ReasonerQueries.atomic(childPattern3, graph);
    ReasonerAtomicQuery childQuery4 = ReasonerQueries.atomic(childPattern4, graph);
    MultiUnifier emptyUnifier = childQuery.getMultiUnifier(parentQuery);
    MultiUnifier emptyUnifier2 = childQuery2.getMultiUnifier(parentQuery);
    assertTrue(emptyUnifier.isEmpty());
    assertTrue(emptyUnifier2.isEmpty());
    MultiUnifier unifier = childQuery3.getMultiUnifier(parentQuery);
    MultiUnifier correctUnifier = new MultiUnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("y"), var("q"), var("z")), ImmutableMultimap.of(var("u"), var("y"), var("v"), var("x"), var("q"), var("z")));
    assertTrue(unifier.containsAll(correctUnifier));
    assertEquals(unifier.size(), 2);
    Unifier unifier2 = childQuery4.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier2 = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("u"), var("y"), var("q"), var("z")));
    assertTrue(unifier2.containsAll(correctUnifier2));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) 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 12 with ReasonerAtomicQuery

use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery 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 13 with ReasonerAtomicQuery

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

the class AtomicTest method testUnification_IndirectRoles_NoRelationType.

@Test
public void testUnification_IndirectRoles_NoRelationType() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    VarPatternAdmin basePattern = var().rel(var("role1").label("subRole1"), var("y1")).rel(var("role2").label("subSubRole2"), var("y2")).admin();
    ReasonerAtomicQuery baseQuery = ReasonerQueries.atomic(Patterns.conjunction(Sets.newHashSet(basePattern)), graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(conjunction("{($r1: $x1, $r2: $x2);" + "$r1 label 'subRole1';" + "$r2 label 'subSubRole2';}", graph), graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(conjunction("{($R1: $x, $R2: $y);" + "$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 14 with ReasonerAtomicQuery

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

the class AtomicTest method testUnification_ParentHasFewerRelationPlayers.

@Test
public void testUnification_ParentHasFewerRelationPlayers() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    String childString = "{(subRole1: $y, subRole2: $x) isa binary;}";
    String parentString = "{(subRole1: $x) isa binary;}";
    String parentString2 = "{(subRole2: $y) isa binary;}";
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(conjunction(childString, graph), graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(conjunction(parentString, graph), graph);
    ReasonerAtomicQuery parentQuery2 = ReasonerQueries.atomic(conjunction(parentString2, graph), graph);
    Atom childAtom = childQuery.getAtom();
    Atom parentAtom = parentQuery.getAtom();
    Atom parentAtom2 = parentQuery2.getAtom();
    List<Answer> childAnswers = childQuery.getQuery().execute();
    List<Answer> parentAnswers = parentQuery.getQuery().execute();
    List<Answer> parentAnswers2 = parentQuery2.getQuery().execute();
    Unifier unifier = childAtom.getUnifier(parentAtom);
    Unifier unifier2 = childAtom.getUnifier(parentAtom2);
    assertCollectionsEqual(parentAnswers, childAnswers.stream().map(a -> a.unify(unifier)).map(a -> a.project(parentQuery.getVarNames())).distinct().collect(Collectors.toList()));
    assertCollectionsEqual(parentAnswers2, childAnswers.stream().map(a -> a.unify(unifier2)).map(a -> a.project(parentQuery2.getVarNames())).distinct().collect(Collectors.toList()));
}
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) 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 15 with ReasonerAtomicQuery

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

the class QueryTest method testAlphaEquivalence_RelationsWithSubstitution.

@Test
public void testAlphaEquivalence_RelationsWithSubstitution() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    String patternString = "{(role: $x, role: $y);$x id 'V666';}";
    String patternString2 = "{(role: $x, role: $y);$y id 'V666';}";
    String patternString3 = "{(role: $x, role: $y);$x id 'V666';$y id 'V667';}";
    String patternString4 = "{(role: $x, role: $y);$y id 'V666';$x id 'V667';}";
    String patternString5 = "{(entity-location: $x, geo-entity: $y);$x id 'V666';$y id 'V667';}";
    String patternString6 = "{(entity-location: $x, geo-entity: $y);$y id 'V666';$x id 'V667';}";
    String patternString7 = "{(role: $x, role: $y);$x id 'V666';$y id 'V666';}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
    Conjunction<VarPatternAdmin> pattern3 = conjunction(patternString3, graph);
    Conjunction<VarPatternAdmin> pattern4 = conjunction(patternString4, graph);
    Conjunction<VarPatternAdmin> pattern5 = conjunction(patternString5, graph);
    Conjunction<VarPatternAdmin> pattern6 = conjunction(patternString6, graph);
    Conjunction<VarPatternAdmin> pattern7 = conjunction(patternString7, graph);
    ReasonerAtomicQuery query = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery query2 = ReasonerQueries.atomic(pattern2, graph);
    ReasonerAtomicQuery query3 = ReasonerQueries.atomic(pattern3, graph);
    ReasonerAtomicQuery query4 = ReasonerQueries.atomic(pattern4, graph);
    ReasonerAtomicQuery query5 = ReasonerQueries.atomic(pattern5, graph);
    ReasonerAtomicQuery query6 = ReasonerQueries.atomic(pattern6, graph);
    ReasonerAtomicQuery query7 = ReasonerQueries.atomic(pattern7, graph);
    queryEquivalence(query, query2, true);
    queryEquivalence(query, query3, false);
    queryEquivalence(query, query4, false);
    queryEquivalence(query, query5, false);
    queryEquivalence(query, query6, false);
    queryEquivalence(query, query7, false);
    queryEquivalence(query2, query3, false);
    queryEquivalence(query2, query4, false);
    queryEquivalence(query2, query5, false);
    queryEquivalence(query2, query6, false);
    queryEquivalence(query2, query7, false);
    queryEquivalence(query3, query4, true);
    queryEquivalence(query3, query5, false);
    queryEquivalence(query3, query6, false);
    queryEquivalence(query3, query7, false);
    queryEquivalence(query4, query5, false);
    queryEquivalence(query4, query6, false);
    queryEquivalence(query4, query7, false);
    queryEquivalence(query5, query6, false);
    queryEquivalence(query5, query7, false);
    queryEquivalence(query6, query7, false);
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) 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