use of ai.grakn.graql.admin.Unifier in project grakn by graknlabs.
the class ReasonerAtomicQuery method getMultiUnifier.
/**
* @throws IllegalArgumentException if passed a {@link ReasonerQuery} that is not a {@link ReasonerAtomicQuery}.
*/
@Override
public MultiUnifier getMultiUnifier(ReasonerQuery p, UnifierComparison unifierType) {
if (p == this)
return new MultiUnifierImpl();
Preconditions.checkArgument(p instanceof ReasonerAtomicQuery);
ReasonerAtomicQuery parent = (ReasonerAtomicQuery) p;
MultiUnifier multiUnifier = this.getAtom().getMultiUnifier(parent.getAtom(), unifierType);
Set<TypeAtom> childTypes = this.getAtom().getTypeConstraints().collect(Collectors.toSet());
if (childTypes.isEmpty())
return multiUnifier;
// get corresponding type unifiers
Set<TypeAtom> parentTypes = parent.getAtom().getTypeConstraints().collect(Collectors.toSet());
if (multiUnifier.isEmpty())
return new MultiUnifierImpl(typeUnifier(childTypes, parentTypes, new UnifierImpl()));
Set<Unifier> unifiers = multiUnifier.unifiers().stream().map(unifier -> typeUnifier(childTypes, parentTypes, unifier)).collect(Collectors.toSet());
return new MultiUnifierImpl(unifiers);
}
use of ai.grakn.graql.admin.Unifier in project grakn by graknlabs.
the class AtomicQueryTest method testExactUnification_TernaryRelationWithTypes_RepeatingRelationPlayers_withMetaRoles.
@Test
public void testExactUnification_TernaryRelationWithTypes_RepeatingRelationPlayers_withMetaRoles() {
EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
String patternString = "{$x1 isa threeRoleEntity;$x3 isa threeRoleEntity3;($x1, $x2, $x3) isa ternary;}";
String patternString2 = "{$y3 isa threeRoleEntity3;$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 testWhenUnifiyingAtomWithItself_UnifierIsIdentity.
@Test
public void testWhenUnifiyingAtomWithItself_UnifierIsIdentity() {
EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
String patternString = "{$x1 isa twoRoleEntity;$x2 isa twoRoleEntity2;($x1, $x2) isa binary;}";
Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern, graph);
Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
Unifier unifier2 = parentQuery.getMultiUnifier(parentQuery).getUnifier();
Unifier unifier3 = childQuery.getMultiUnifier(childQuery).getUnifier();
assertTrue(Sets.intersection(unifier.keySet(), Sets.newHashSet(var("x"), var("y"))).isEmpty());
assertTrue(unifier2.isEmpty());
assertTrue(unifier3.isEmpty());
}
use of ai.grakn.graql.admin.Unifier in project grakn by graknlabs.
the class AtomicQueryTest method testExactUnification_TernaryRelationWithTypes_SomeVarsHaveTypes_UnifierMatchesTypes.
@Test
public void testExactUnification_TernaryRelationWithTypes_SomeVarsHaveTypes_UnifierMatchesTypes() {
EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
String patternString = "{$x1 isa threeRoleEntity;$x3 isa threeRoleEntity3;($x1, $x2, $x3) isa ternary;}";
String patternString2 = "{$y3 isa threeRoleEntity3;$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_BinaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes.
// only a single unifier exists
@Test
public void testExactUnification_BinaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes() {
EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
String patternString = "{$x1 isa twoRoleEntity;$x2 isa twoRoleEntity2;($x1, $x2) isa binary;}";
String patternString2 = "{$y1 isa twoRoleEntity;$y2 isa twoRoleEntity2;($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));
}
Aggregations