Search in sources :

Example 11 with MultiUnifier

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);
}
Also used : Var(ai.grakn.graql.Var) VarPattern(ai.grakn.graql.VarPattern) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) HashSet(java.util.HashSet)

Example 12 with MultiUnifier

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());
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) LoggerFactory(org.slf4j.LoggerFactory) Type(ai.grakn.concept.Type) ResolutionIterator(ai.grakn.graql.internal.reasoner.ResolutionIterator) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) QueryAnswerStream.join(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.join) Map(java.util.Map) IsaAtom(ai.grakn.graql.internal.reasoner.atom.binary.IsaAtom) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) ConceptId(ai.grakn.concept.ConceptId) Graql.var(ai.grakn.graql.Graql.var) ImmutableSet(com.google.common.collect.ImmutableSet) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ConjunctiveState(ai.grakn.graql.internal.reasoner.state.ConjunctiveState) QueryAnswerStream.joinWithInverse(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.joinWithInverse) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Cache(ai.grakn.graql.internal.reasoner.cache.Cache) Stream(java.util.stream.Stream) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) Var(ai.grakn.graql.Var) AtomicBase(ai.grakn.graql.internal.reasoner.atom.AtomicBase) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Concept(ai.grakn.concept.Concept) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) CumulativeState(ai.grakn.graql.internal.reasoner.state.CumulativeState) JoinExplanation(ai.grakn.graql.internal.reasoner.explanation.JoinExplanation) Iterators(com.google.common.collect.Iterators) QueryAnswerStream.nonEqualsFilter(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.nonEqualsFilter) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) LinkedList(java.util.LinkedList) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Nullable(javax.annotation.Nullable) LinkedHashSet(java.util.LinkedHashSet) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) IsaAtomBase(ai.grakn.graql.internal.reasoner.atom.binary.IsaAtomBase) GetQuery(ai.grakn.graql.GetQuery) AtomicFactory(ai.grakn.graql.internal.reasoner.atom.AtomicFactory) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Schema(ai.grakn.util.Schema) Unifier(ai.grakn.graql.admin.Unifier) ResolutionPlan(ai.grakn.graql.internal.reasoner.plan.ResolutionPlan) Collections(java.util.Collections) JoinExplanation(ai.grakn.graql.internal.reasoner.explanation.JoinExplanation) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) CumulativeState(ai.grakn.graql.internal.reasoner.state.CumulativeState) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) ResolutionPlan(ai.grakn.graql.internal.reasoner.plan.ResolutionPlan) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 13 with MultiUnifier

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());
}
Also used : Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Stream(java.util.stream.Stream) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Map(java.util.Map) Set(java.util.Set) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) Collectors(java.util.stream.Collectors) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) Answer(ai.grakn.graql.admin.Answer) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 14 with MultiUnifier

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;
}
Also used : Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) MultiUnifier(ai.grakn.graql.admin.MultiUnifier)

Example 15 with MultiUnifier

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));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Aggregations

MultiUnifier (ai.grakn.graql.admin.MultiUnifier)17 Unifier (ai.grakn.graql.admin.Unifier)10 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)9 Answer (ai.grakn.graql.admin.Answer)8 MultiUnifierImpl (ai.grakn.graql.internal.reasoner.MultiUnifierImpl)8 Pair (ai.grakn.graql.internal.reasoner.utils.Pair)7 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 Test (org.junit.Test)7 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)6 Stream (java.util.stream.Stream)6 Var (ai.grakn.graql.Var)5 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)5 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 Concept (ai.grakn.concept.Concept)4 GraqlQueryException (ai.grakn.exception.GraqlQueryException)4 Atomic (ai.grakn.graql.admin.Atomic)4 Conjunction (ai.grakn.graql.admin.Conjunction)4