Search in sources :

Example 26 with Unifier

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

the class AtomicQueryTest method testExactUnification_BinaryRelationWithTypes_SomeVarsHaveTypes_UnifierMatchesTypes.

// only a single unifier exists
@Test
public void testExactUnification_BinaryRelationWithTypes_SomeVarsHaveTypes_UnifierMatchesTypes() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String patternString = "{$x1 isa twoRoleEntity;($x1, $x2) isa binary;}";
    String patternString2 = "{$y1 isa twoRoleEntity;($y1, $y2) isa binary;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern2, graph);
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2")));
    assertTrue(unifier.containsAll(correctUnifier));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) 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 27 with Unifier

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

the class AtomicQueryTest method testExactUnification_TernaryRelation_ParentRepeatsRoles_ParentRepeatsRPs.

@Test
public void testExactUnification_TernaryRelation_ParentRepeatsRoles_ParentRepeatsRPs() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String parentString = "{(role1: $x, role1: $x, role2: $y) 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());
    Unifier unifier = childQuery3.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("x"), var("q"), var("y")));
    assertTrue(unifier.containsAll(correctUnifier));
    Unifier unifier2 = childQuery4.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier2 = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("q"), var("y")));
    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 28 with Unifier

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

the class AtomicQueryTest method queryUnification.

/**
 * 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 ignoreTypes flag specifying whether the types should be disregarded and only role players checked for containment
 * @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 queryUnification(ReasonerAtomicQuery parentQuery, ReasonerAtomicQuery childQuery, boolean checkInverse, boolean checkEquality, boolean ignoreTypes) {
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).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 inverse = parentQuery.getMultiUnifier(childQuery).getUnifier();
        assertEquals(unifier.inverse(), inverse);
        assertEquals(unifier, inverse.inverse());
    }
    assertTrue(!childAnswers.isEmpty());
    assertTrue(!unifiedAnswers.isEmpty());
    assertTrue(!parentAnswers.isEmpty());
    Set<Var> parentNonTypeVariables = Sets.difference(parentQuery.getAtom().getVarNames(), Sets.newHashSet(parentQuery.getAtom().getPredicateVariable()));
    if (!checkEquality) {
        if (!ignoreTypes) {
            assertTrue(parentAnswers.containsAll(unifiedAnswers));
        } else {
            List<Answer> projectedParentAnswers = parentAnswers.stream().map(ans -> ans.project(parentNonTypeVariables)).collect(Collectors.toList());
            List<Answer> projectedUnified = unifiedAnswers.stream().map(ans -> ans.project(parentNonTypeVariables)).collect(Collectors.toList());
            assertTrue(projectedParentAnswers.containsAll(projectedUnified));
        }
    } else {
        Unifier inverse = unifier.inverse();
        if (!ignoreTypes) {
            assertCollectionsEqual(parentAnswers, unifiedAnswers);
            List<Answer> parentToChild = parentAnswers.stream().map(a -> a.unify(inverse)).collect(Collectors.toList());
            assertCollectionsEqual(parentToChild, childAnswers);
        } else {
            Set<Var> childNonTypeVariables = Sets.difference(childQuery.getAtom().getVarNames(), Sets.newHashSet(childQuery.getAtom().getPredicateVariable()));
            List<Answer> projectedParentAnswers = parentAnswers.stream().map(ans -> ans.project(parentNonTypeVariables)).collect(Collectors.toList());
            List<Answer> projectedUnified = unifiedAnswers.stream().map(ans -> ans.project(parentNonTypeVariables)).collect(Collectors.toList());
            List<Answer> projectedChild = childAnswers.stream().map(ans -> ans.project(childNonTypeVariables)).collect(Collectors.toList());
            assertCollectionsEqual(projectedParentAnswers, projectedUnified);
            List<Answer> projectedParentToChild = projectedParentAnswers.stream().map(a -> a.unify(inverse)).map(ans -> ans.project(childNonTypeVariables)).collect(Collectors.toList());
            assertCollectionsEqual(projectedParentToChild, projectedChild);
        }
    }
}
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) Var(ai.grakn.graql.Var) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier)

Aggregations

Unifier (ai.grakn.graql.admin.Unifier)28 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)23 Test (org.junit.Test)18 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)17 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)16 Answer (ai.grakn.graql.admin.Answer)13 Var (ai.grakn.graql.Var)11 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)9 InferenceRule (ai.grakn.graql.internal.reasoner.rule.InferenceRule)7 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)7 Concept (ai.grakn.concept.Concept)6 Atomic (ai.grakn.graql.admin.Atomic)6 Conjunction (ai.grakn.graql.admin.Conjunction)6 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)6 RelationshipAtom (ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom)6 ResourceAtom (ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom)6 Sets (com.google.common.collect.Sets)6 List (java.util.List)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6