use of ai.grakn.graql.admin.MultiUnifier in project grakn by graknlabs.
the class RelationshipAtom method getMultiUnifier.
@Override
public MultiUnifier getMultiUnifier(Atom pAtom, UnifierComparison unifierType) {
Unifier baseUnifier = super.getUnifier(pAtom);
Set<Unifier> unifiers = new HashSet<>();
if (pAtom.isRelation()) {
// This is safe due to the check above
assert (pAtom instanceof RelationshipAtom);
RelationshipAtom parentAtom = (RelationshipAtom) pAtom;
// this is important for cases like unifying ($r1: $x, $r2: $y) with itself
if (this.equals(parentAtom) && this.getPartialSubstitutions().collect(toSet()).equals(parentAtom.getPartialSubstitutions().collect(toSet())) && this.getTypeConstraints().collect(toSet()).equals(parentAtom.getTypeConstraints().collect(toSet()))) {
return new MultiUnifierImpl();
}
boolean unifyRoleVariables = parentAtom.getRelationPlayers().stream().map(RelationPlayer::getRole).flatMap(CommonUtil::optionalToStream).anyMatch(rp -> rp.var().isUserDefinedName());
getRelationPlayerMappings(parentAtom, unifierType).forEach(mappingList -> {
Multimap<Var, Var> varMappings = HashMultimap.create();
mappingList.forEach(rpm -> {
// add role player mapping
varMappings.put(rpm.getKey().getRolePlayer().var(), rpm.getValue().getRolePlayer().var());
// add role var mapping if needed
VarPattern childRolePattern = rpm.getKey().getRole().orElse(null);
VarPattern parentRolePattern = rpm.getValue().getRole().orElse(null);
if (parentRolePattern != null && childRolePattern != null && unifyRoleVariables) {
varMappings.put(childRolePattern.admin().var(), parentRolePattern.admin().var());
}
});
unifiers.add(baseUnifier.merge(new UnifierImpl(varMappings)));
});
} else {
unifiers.add(baseUnifier);
}
return new MultiUnifierImpl(unifiers);
}
use of ai.grakn.graql.admin.MultiUnifier in project grakn by graknlabs.
the class ReasonerQueryImpl method queryStateIterator.
/**
* @param parent parent state
* @param subGoals set of visited sub goals
* @param cache query cache
* @return query state iterator (db iter + unifier + state iter) for this query
*/
public Pair<Iterator<ResolutionState>, MultiUnifier> queryStateIterator(QueryStateBase parent, Set<ReasonerAtomicQuery> subGoals, QueryCache<ReasonerAtomicQuery> cache) {
Iterator<AnswerState> dbIterator;
Iterator<QueryStateBase> subGoalIterator;
if (!this.isRuleResolvable()) {
dbIterator = this.getQuery().stream().map(ans -> ans.explain(new JoinExplanation(this, ans))).map(ans -> new AnswerState(ans, parent.getUnifier(), parent)).iterator();
subGoalIterator = Collections.emptyIterator();
} else {
dbIterator = Collections.emptyIterator();
LinkedList<ReasonerQueryImpl> subQueries = new ResolutionPlan(this).queryPlan();
LOG.trace("CQ plan:\n" + subQueries.stream().map(sq -> sq.toString() + (sq.isRuleResolvable() ? "*" : "")).collect(Collectors.joining("\n")));
subGoalIterator = Iterators.singletonIterator(new CumulativeState(subQueries, new QueryAnswer(), parent.getUnifier(), parent, subGoals, cache));
}
return new Pair<>(Iterators.concat(dbIterator, subGoalIterator), new MultiUnifierImpl());
}
use of ai.grakn.graql.admin.MultiUnifier in project grakn by graknlabs.
the class LazyQueryCache method getAnswerStreamWithUnifier.
@Override
public Pair<Stream<Answer>, MultiUnifier> getAnswerStreamWithUnifier(Q query) {
CacheEntry<Q, LazyAnswerIterator> match = this.getEntry(query);
if (match != null) {
Q equivalentQuery = match.query();
MultiUnifier multiUnifier = equivalentQuery.getMultiUnifier(query);
Stream<Answer> unified = match.cachedElement().stream().flatMap(a -> a.unify(multiUnifier));
return new Pair<>(unified, multiUnifier);
}
Stream<Answer> answerStream = record(query, query.getQuery().stream().map(a -> a.explain(new LookupExplanation(query))));
return new Pair<>(answerStream, new MultiUnifierImpl());
}
use of ai.grakn.graql.admin.MultiUnifier in project grakn by graknlabs.
the class LazyQueryCache method recordRetrieveLazy.
/**
* record answer stream for a specific query and retrieve the updated stream in a lazy iterator
* @param query to be recorded
* @param answers answer stream of the query
* @return lazy iterator of updated answers
*/
public LazyAnswerIterator recordRetrieveLazy(Q query, Stream<Answer> answers) {
CacheEntry<Q, LazyAnswerIterator> match = this.getEntry(query);
if (match != null) {
Q equivalentQuery = match.query();
MultiUnifier multiUnifier = query.getMultiUnifier(equivalentQuery);
Stream<Answer> unifiedStream = answers.flatMap(a -> a.unify(multiUnifier));
// NB: entry overwrite
this.putEntry(match.query(), match.cachedElement().merge(unifiedStream));
return getAnswerIterator(query);
}
LazyAnswerIterator liter = new LazyAnswerIterator(answers);
this.putEntry(query, liter);
return liter;
}
use of ai.grakn.graql.admin.MultiUnifier in project grakn by graknlabs.
the class AtomicQueryTest method testExactUnification_TernaryRelation_ParentRepeatsMetaRoles_ParentRepeatsRPs.
@Test
public void testExactUnification_TernaryRelation_ParentRepeatsMetaRoles_ParentRepeatsRPs() {
EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
String parentString = "{(role: $x, role: $x, role2: $y) 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);
Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("q"), var("x"), var("u"), var("x"), var("v"), var("y")));
assertTrue(unifier.containsAll(correctUnifier));
MultiUnifier unifier2 = childQuery2.getMultiUnifier(parentQuery);
MultiUnifier correctUnifier2 = new MultiUnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("q"), var("x"), var("v"), var("y")), ImmutableMultimap.of(var("u"), var("x"), var("v"), var("x"), var("q"), var("y")));
assertTrue(unifier2.containsAll(correctUnifier2));
assertEquals(unifier2.size(), 2);
Unifier unifier3 = childQuery3.getMultiUnifier(parentQuery).getUnifier();
Unifier correctUnifier3 = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("x"), var("q"), var("y")));
assertTrue(unifier3.containsAll(correctUnifier3));
Unifier unifier4 = childQuery4.getMultiUnifier(parentQuery).getUnifier();
Unifier correctUnifier4 = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("q"), var("y")));
assertTrue(unifier4.containsAll(correctUnifier4));
}
Aggregations