Search in sources :

Example 6 with Unifier

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

the class AtomicQueryTest method testExactUnification_TernaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes.

@Test
public void testExactUnification_TernaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String patternString = "{$x1 isa threeRoleEntity;$x2 isa threeRoleEntity2; $x3 isa threeRoleEntity3;($x1, $x2, $x3) isa ternary;}";
    String patternString2 = "{$y3 isa threeRoleEntity3;$y2 isa threeRoleEntity2;$y1 isa threeRoleEntity;($y2, $y3, $y1) isa ternary;}";
    String patternString3 = "{$y3 isa threeRoleEntity3;$y2 isa threeRoleEntity2;$y1 isa threeRoleEntity;(role2: $y2, role3: $y3, role1: $y1) isa ternary;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
    Conjunction<VarPatternAdmin> pattern3 = conjunction(patternString3, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern2, graph);
    ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(pattern3, graph);
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier unifier2 = childQuery2.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2"), var("y3"), var("x3")));
    assertTrue(unifier.containsAll(correctUnifier));
    assertTrue(unifier2.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 7 with Unifier

use of ai.grakn.graql.admin.Unifier 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 8 with Unifier

use of ai.grakn.graql.admin.Unifier 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 9 with Unifier

use of ai.grakn.graql.admin.Unifier 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 10 with Unifier

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

the class InferenceRule method subGoal.

/**
 * @param parentAtom atom to which this rule is applied
 * @param ruleUnifier unifier with parent state
 * @param parent parent state
 * @param visitedSubGoals set of visited sub goals
 * @param cache query cache
 * @return resolution subGoal formed from this rule
 */
public ResolutionState subGoal(Atom parentAtom, Unifier ruleUnifier, QueryStateBase parent, Set<ReasonerAtomicQuery> visitedSubGoals, QueryCache<ReasonerAtomicQuery> cache) {
    Unifier ruleUnifierInverse = ruleUnifier.inverse();
    // delta' = theta . thetaP . delta
    Answer partialSubPrime = parentAtom.getParentQuery().getSubstitution().unify(ruleUnifierInverse);
    return new RuleState(this.propagateConstraints(parentAtom, ruleUnifierInverse), partialSubPrime, ruleUnifier, parent, visitedSubGoals, cache);
}
Also used : Answer(ai.grakn.graql.admin.Answer) RuleState(ai.grakn.graql.internal.reasoner.state.RuleState) 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