use of ai.grakn.graql.admin.Answer in project grakn by graknlabs.
the class QueryAnswerStream method knownFilterWithInverse.
static boolean knownFilterWithInverse(Answer answer, Map<Pair<Var, Concept>, Set<Answer>> stream2InverseMap) {
Iterator<Map.Entry<Var, Concept>> eit = answer.entrySet().iterator();
Map.Entry<Var, Concept> entry = eit.next();
Set<Answer> matchAnswers = findMatchingAnswers(entry.getKey(), entry.getValue(), stream2InverseMap);
while (eit.hasNext()) {
entry = eit.next();
matchAnswers = Sets.intersection(matchAnswers, findMatchingAnswers(entry.getKey(), entry.getValue(), stream2InverseMap));
}
for (Answer knownAnswer : matchAnswers) {
if (knownAnswer.entrySet().containsAll(answer.entrySet())) {
return false;
}
}
return true;
}
use of ai.grakn.graql.admin.Answer in project grakn by graknlabs.
the class ReasonerAtomicQuery method queryStateIterator.
@Override
public Pair<Iterator<ResolutionState>, MultiUnifier> queryStateIterator(QueryStateBase parent, Set<ReasonerAtomicQuery> visitedSubGoals, QueryCache<ReasonerAtomicQuery> cache) {
Pair<Stream<Answer>, MultiUnifier> cacheEntry = cache.getAnswerStreamWithUnifier(this);
MultiUnifier cacheUnifier = cacheEntry.getValue().inverse();
Iterator<AnswerState> dbIterator = cacheEntry.getKey().map(a -> a.explain(a.getExplanation().setQuery(this))).map(ans -> new AnswerState(ans, parent.getUnifier(), parent)).iterator();
Iterator<ResolutionState> subGoalIterator;
// if this is ground and exists in the db then do not resolve further
if (visitedSubGoals.contains(this) || (this.isGround() && dbIterator.hasNext())) {
subGoalIterator = Collections.emptyIterator();
} else {
visitedSubGoals.add(this);
subGoalIterator = this.getRuleStream().map(rulePair -> rulePair.getKey().subGoal(this.getAtom(), rulePair.getValue(), parent, visitedSubGoals, cache)).iterator();
}
return new Pair<>(Iterators.concat(dbIterator, subGoalIterator), cacheUnifier);
}
use of ai.grakn.graql.admin.Answer in project grakn by graknlabs.
the class AtomicQueryTest method testWhenRoleTypesAreAmbiguous_answersArePermutedCorrectly.
@Test
public void testWhenRoleTypesAreAmbiguous_answersArePermutedCorrectly() {
EmbeddedGraknTx<?> graph = geoKB.tx();
String childString = "match (geo-entity: $x, entity-location: $y) isa is-located-in; get;";
String parentString = "match ($x, $y) isa is-located-in; get;";
QueryBuilder qb = graph.graql().infer(false);
GetQuery childQuery = qb.parse(childString);
GetQuery parentQuery = qb.parse(parentString);
Set<Answer> answers = childQuery.stream().collect(toSet());
Set<Answer> fullAnswers = parentQuery.stream().collect(toSet());
Atom childAtom = ReasonerQueries.atomic(conjunction(childQuery.match().admin().getPattern()), graph).getAtom();
Atom parentAtom = ReasonerQueries.atomic(conjunction(parentQuery.match().admin().getPattern()), graph).getAtom();
MultiUnifier multiUnifier = childAtom.getMultiUnifier(childAtom, UnifierType.RULE);
Set<Answer> permutedAnswers = answers.stream().flatMap(a -> multiUnifier.stream().map(a::unify)).collect(Collectors.toSet());
MultiUnifier multiUnifier2 = childAtom.getMultiUnifier(parentAtom, UnifierType.RULE);
Set<Answer> permutedAnswers2 = answers.stream().flatMap(a -> multiUnifier2.stream().map(a::unify)).collect(Collectors.toSet());
assertEquals(fullAnswers, permutedAnswers2);
assertEquals(answers, permutedAnswers);
}
use of ai.grakn.graql.admin.Answer 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.Answer in project grakn by graknlabs.
the class OntologicalQueryTest method allRolesGivenRelationRelates.
@Test
public void allRolesGivenRelationRelates() {
GraknTx tx = testContext.tx();
QueryBuilder qb = tx.graql().infer(true);
String queryString = "match reifying-relation relates $x; get;";
List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
assertEquals(answers.stream().map(ans -> ans.get("x")).collect(Collectors.toSet()), tx.getRelationshipType("reifying-relation").relates().collect(Collectors.toSet()));
}
Aggregations