use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class AtomicTest method testUnification_ResourceWithIndirectValuePredicate.
@Test
public void testUnification_ResourceWithIndirectValuePredicate() {
EmbeddedGraknTx<?> graph = unificationTestSet.tx();
String resource = "{$x has resource $r;$r val 'f';}";
String resource2 = "{$r has resource $x;$x val 'f';}";
String resource3 = "{$r has resource 'f';}";
ReasonerAtomicQuery resourceQuery = ReasonerQueries.atomic(conjunction(resource, graph), graph);
ReasonerAtomicQuery resourceQuery2 = ReasonerQueries.atomic(conjunction(resource2, graph), graph);
ReasonerAtomicQuery resourceQuery3 = ReasonerQueries.atomic(conjunction(resource3, graph), graph);
String type = "{$x isa resource;$x id '" + resourceQuery.getQuery().execute().iterator().next().get("r").getId().getValue() + "';}";
ReasonerAtomicQuery typeQuery = ReasonerQueries.atomic(conjunction(type, graph), graph);
Atom typeAtom = typeQuery.getAtom();
Atom resourceAtom = resourceQuery.getAtom();
Atom resourceAtom2 = resourceQuery2.getAtom();
Atom resourceAtom3 = resourceQuery3.getAtom();
Unifier unifier = resourceAtom.getUnifier(typeAtom);
Unifier unifier2 = resourceAtom2.getUnifier(typeAtom);
Unifier unifier3 = resourceAtom3.getUnifier(typeAtom);
Answer typeAnswer = typeQuery.getQuery().execute().iterator().next();
Answer resourceAnswer = resourceQuery.getQuery().execute().iterator().next();
Answer resourceAnswer2 = resourceQuery2.getQuery().execute().iterator().next();
Answer resourceAnswer3 = resourceQuery3.getQuery().execute().iterator().next();
assertEquals(typeAnswer.get(var("x")), resourceAnswer.unify(unifier).get(var("x")));
assertEquals(typeAnswer.get(var("x")), resourceAnswer2.unify(unifier2).get(var("x")));
assertEquals(typeAnswer.get(var("x")), resourceAnswer3.unify(unifier3).get(var("x")));
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class AtomicTest method exactUnification.
/**
* 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 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 exactUnification(ReasonerAtomicQuery parentQuery, ReasonerAtomicQuery childQuery, boolean checkInverse, boolean checkEquality) {
Atom childAtom = childQuery.getAtom();
Atom parentAtom = parentQuery.getAtom();
Unifier unifier = childAtom.getMultiUnifier(parentAtom, UnifierType.EXACT).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 unifier2 = parentAtom.getUnifier(childAtom);
assertEquals(unifier.inverse(), unifier2);
assertEquals(unifier, unifier2.inverse());
}
assertTrue(!childAnswers.isEmpty());
assertTrue(!unifiedAnswers.isEmpty());
assertTrue(!parentAnswers.isEmpty());
if (!checkEquality) {
assertTrue(parentAnswers.containsAll(unifiedAnswers));
} else {
assertCollectionsEqual(parentAnswers, unifiedAnswers);
Unifier inverse = unifier.inverse();
List<Answer> parentToChild = parentAnswers.stream().map(a -> a.unify(inverse)).collect(Collectors.toList());
assertCollectionsEqual(parentToChild, childAnswers);
}
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class AtomicTest method testUnification_IndirectRoles.
@Test
public void testUnification_IndirectRoles() {
EmbeddedGraknTx<?> graph = unificationTestSet.tx();
VarPatternAdmin basePattern = var().rel(var("role1").label("subRole1"), var("y1")).rel(var("role2").label("subSubRole2"), var("y2")).isa("binary").admin();
ReasonerAtomicQuery baseQuery = ReasonerQueries.atomic(Patterns.conjunction(Sets.newHashSet(basePattern)), graph);
ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(conjunction("{($r1: $x1, $r2: $x2) isa binary;" + "$r1 label 'subRole1';" + "$r2 label 'subSubRole2';}", graph), graph);
ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(conjunction("{($R1: $x, $R2: $y) isa binary;" + "$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 QueryCacheTest method lazilyGetRetrieveAnswers.
@Test
public void lazilyGetRetrieveAnswers() {
LazyQueryCache<ReasonerAtomicQuery> cache = new LazyQueryCache<>();
cache.record(recordQuery, recordQuery.getQuery().stream());
LazyAnswerIterator retrieveIterator = cache.getAnswers(retrieveQuery);
LazyAnswerIterator recordIterator = cache.getAnswers(recordQuery);
Set<Answer> record = recordIterator.stream().collect(toSet());
Set<Answer> retrieve = retrieveIterator.stream().map(ans -> ans.unify(retrieveToRecordUnifier)).collect(toSet());
assertTrue(!retrieve.isEmpty());
assertEquals(record, retrieve);
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery in project grakn by graknlabs.
the class QueryCacheTest method getRetrieveAnswerStream.
@Test
public void getRetrieveAnswerStream() {
QueryCache<ReasonerAtomicQuery> cache = new QueryCache<>();
Answer answer = recordQuery.getQuery().stream().findFirst().orElse(null);
Answer retrieveAnswer = answer.unify(recordToRetrieveUnifier);
Stream<Answer> recordStream = cache.getAnswerStream(recordQuery);
Stream<Answer> retrieveStream = cache.getAnswerStream(retrieveQuery);
QueryAnswers recordAnswers = new QueryAnswers(recordStream.collect(Collectors.toSet()));
QueryAnswers retrieveAnswers = new QueryAnswers(retrieveStream.collect(Collectors.toSet()));
assertTrue(recordAnswers.contains(answer));
assertTrue(retrieveAnswers.contains(retrieveAnswer));
}
Aggregations