use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery 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.internal.reasoner.query.ReasonerAtomicQuery 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.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class AtomicTest method testUnification_IndirectRoles_NoRelationType.
@Test
public void testUnification_IndirectRoles_NoRelationType() {
EmbeddedGraknTx<?> graph = unificationTestSet.tx();
VarPatternAdmin basePattern = var().rel(var("role1").label("subRole1"), var("y1")).rel(var("role2").label("subSubRole2"), var("y2")).admin();
ReasonerAtomicQuery baseQuery = ReasonerQueries.atomic(Patterns.conjunction(Sets.newHashSet(basePattern)), graph);
ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(conjunction("{($r1: $x1, $r2: $x2);" + "$r1 label 'subRole1';" + "$r2 label 'subSubRole2';}", graph), graph);
ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(conjunction("{($R1: $x, $R2: $y);" + "$R1 label 'subRole1';" + "$R2 label 'subSubRole2';}", graph), graph);
exactUnification(parentQuery, childQuery, true, true);
exactUnification(baseQuery, parentQuery, true, true);
exactUnification(baseQuery, childQuery, true, true);
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery 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.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class QueryTest method testAlphaEquivalence_RelationsWithSubstitution.
@Test
public void testAlphaEquivalence_RelationsWithSubstitution() {
EmbeddedGraknTx<?> graph = geoKB.tx();
String patternString = "{(role: $x, role: $y);$x id 'V666';}";
String patternString2 = "{(role: $x, role: $y);$y id 'V666';}";
String patternString3 = "{(role: $x, role: $y);$x id 'V666';$y id 'V667';}";
String patternString4 = "{(role: $x, role: $y);$y id 'V666';$x id 'V667';}";
String patternString5 = "{(entity-location: $x, geo-entity: $y);$x id 'V666';$y id 'V667';}";
String patternString6 = "{(entity-location: $x, geo-entity: $y);$y id 'V666';$x id 'V667';}";
String patternString7 = "{(role: $x, role: $y);$x id 'V666';$y id 'V666';}";
Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
Conjunction<VarPatternAdmin> pattern3 = conjunction(patternString3, graph);
Conjunction<VarPatternAdmin> pattern4 = conjunction(patternString4, graph);
Conjunction<VarPatternAdmin> pattern5 = conjunction(patternString5, graph);
Conjunction<VarPatternAdmin> pattern6 = conjunction(patternString6, graph);
Conjunction<VarPatternAdmin> pattern7 = conjunction(patternString7, graph);
ReasonerAtomicQuery query = ReasonerQueries.atomic(pattern, graph);
ReasonerAtomicQuery query2 = ReasonerQueries.atomic(pattern2, graph);
ReasonerAtomicQuery query3 = ReasonerQueries.atomic(pattern3, graph);
ReasonerAtomicQuery query4 = ReasonerQueries.atomic(pattern4, graph);
ReasonerAtomicQuery query5 = ReasonerQueries.atomic(pattern5, graph);
ReasonerAtomicQuery query6 = ReasonerQueries.atomic(pattern6, graph);
ReasonerAtomicQuery query7 = ReasonerQueries.atomic(pattern7, graph);
queryEquivalence(query, query2, true);
queryEquivalence(query, query3, false);
queryEquivalence(query, query4, false);
queryEquivalence(query, query5, false);
queryEquivalence(query, query6, false);
queryEquivalence(query, query7, false);
queryEquivalence(query2, query3, false);
queryEquivalence(query2, query4, false);
queryEquivalence(query2, query5, false);
queryEquivalence(query2, query6, false);
queryEquivalence(query2, query7, false);
queryEquivalence(query3, query4, true);
queryEquivalence(query3, query5, false);
queryEquivalence(query3, query6, false);
queryEquivalence(query3, query7, false);
queryEquivalence(query4, query5, false);
queryEquivalence(query4, query6, false);
queryEquivalence(query4, query7, false);
queryEquivalence(query5, query6, false);
queryEquivalence(query5, query7, false);
queryEquivalence(query6, query7, false);
}
Aggregations