use of ai.grakn.graql.admin.MultiUnifier 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.MultiUnifier 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.MultiUnifier 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.MultiUnifier in project grakn by graknlabs.
the class AtomicQueryTest method testExactUnification_TernaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes_TypeHierarchyInvolved.
// subSubThreeRoleEntity sub subThreeRoleEntity sub threeRoleEntity3
@Test
public void testExactUnification_TernaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes_TypeHierarchyInvolved() {
EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
String parentString = "{$x1 isa threeRoleEntity;$x2 isa subThreeRoleEntity; $x3 isa subSubThreeRoleEntity;($x1, $x2, $x3) isa ternary;}";
String childString = "{$y1 isa threeRoleEntity;$y2 isa subThreeRoleEntity;$y3 isa subSubThreeRoleEntity;($y2, $y3, $y1) isa ternary;}";
String childString2 = "{$y1 isa threeRoleEntity;$y2 isa subThreeRoleEntity;$y3 isa subSubThreeRoleEntity;(role2: $y2, role3: $y3, role1: $y1) isa ternary;}";
Conjunction<VarPatternAdmin> pattern = conjunction(parentString, graph);
Conjunction<VarPatternAdmin> pattern2 = conjunction(childString, graph);
Conjunction<VarPatternAdmin> pattern3 = conjunction(childString2, graph);
ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern2, graph);
ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(pattern3, graph);
MultiUnifier unifier = childQuery.getMultiUnifier(parentQuery);
MultiUnifier unifier2 = childQuery2.getMultiUnifier(parentQuery);
MultiUnifier correctUnifier = new MultiUnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2"), var("y3"), var("x3")), ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x3"), var("y3"), var("x2")), ImmutableMultimap.of(var("y1"), var("x2"), var("y2"), var("x1"), var("y3"), var("x3")), ImmutableMultimap.of(var("y1"), var("x2"), var("y2"), var("x3"), var("y3"), var("x1")), ImmutableMultimap.of(var("y1"), var("x3"), var("y2"), var("x1"), var("y3"), var("x2")), ImmutableMultimap.of(var("y1"), var("x3"), var("y2"), var("x2"), var("y3"), var("x1")));
assertTrue(unifier.containsAll(correctUnifier));
assertTrue(unifier2.containsAll(correctUnifier));
}
use of ai.grakn.graql.admin.MultiUnifier 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));
}
Aggregations