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