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));
}
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));
}
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));
}
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()));
}
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);
}
Aggregations