Search in sources :

Example 21 with Unifier

use of ai.grakn.graql.admin.Unifier in project grakn by graknlabs.

the class StructuralCache method get.

/**
 * @param query to be retrieved
 * @return answer stream of provided query
 */
public Stream<Answer> get(Q query) {
    Equivalence.Wrapper<Q> structQuery = equivalence.wrap(query);
    EmbeddedGraknTx<?> tx = query.tx();
    CacheEntry<Q, GraqlTraversal> match = structCache.get(structQuery);
    if (match != null) {
        Q equivalentQuery = match.query();
        GraqlTraversal traversal = match.cachedElement();
        Unifier unifier = equivalentQuery.getMultiUnifier(query, UnifierType.STRUCTURAL).getAny();
        Map<Var, ConceptId> idTransform = equivalentQuery.idTransform(query, unifier);
        ReasonerQueryImpl transformedQuery = equivalentQuery.transformIds(idTransform);
        return MatchBase.streamWithTraversal(transformedQuery.getPattern().commonVars(), tx, traversal.transform(idTransform)).map(ans -> ans.unify(unifier)).map(a -> a.explain(new LookupExplanation(query)));
    }
    GraqlTraversal traversal = GreedyTraversalPlan.createTraversal(query.getPattern(), tx);
    structCache.put(structQuery, new CacheEntry<>(query, traversal));
    return MatchBase.streamWithTraversal(query.getPattern().commonVars(), tx, traversal).map(a -> a.explain(new LookupExplanation(query)));
}
Also used : UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Equivalence(com.google.common.base.Equivalence) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) ReasonerQueryEquivalence(ai.grakn.graql.internal.reasoner.query.ReasonerQueryEquivalence) GreedyTraversalPlan(ai.grakn.graql.internal.gremlin.GreedyTraversalPlan) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) MatchBase(ai.grakn.graql.internal.query.match.MatchBase) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Map(java.util.Map) Var(ai.grakn.graql.Var) ConceptId(ai.grakn.concept.ConceptId) Unifier(ai.grakn.graql.admin.Unifier) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) LookupExplanation(ai.grakn.graql.internal.reasoner.explanation.LookupExplanation) Var(ai.grakn.graql.Var) ConceptId(ai.grakn.concept.ConceptId) Equivalence(com.google.common.base.Equivalence) ReasonerQueryEquivalence(ai.grakn.graql.internal.reasoner.query.ReasonerQueryEquivalence) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) Unifier(ai.grakn.graql.admin.Unifier) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)

Example 22 with Unifier

use of ai.grakn.graql.admin.Unifier in project grakn by graknlabs.

the class ReasonerAtomicQuery method answerStream.

/**
 * resolves the query by performing lookups and rule resolution and returns a stream of new answers
 * @param subGoals visited subGoals (recursive queries)
 * @param cache global query cache
 * @param dCache differential query cache
 * @return stream of differential answers
 */
Stream<Answer> answerStream(Set<ReasonerAtomicQuery> subGoals, Cache<ReasonerAtomicQuery, ?> cache, Cache<ReasonerAtomicQuery, ?> dCache, boolean differentialJoin) {
    boolean queryAdmissible = !subGoals.contains(this);
    LOG.trace("AQ: " + this);
    Stream<Answer> answerStream = cache.contains(this) ? Stream.empty() : dCache.record(this, cache.getAnswerStream(this));
    if (queryAdmissible) {
        Iterator<Pair<InferenceRule, Unifier>> ruleIterator = getRuleStream().iterator();
        while (ruleIterator.hasNext()) {
            Pair<InferenceRule, Unifier> ruleContext = ruleIterator.next();
            Unifier unifier = ruleContext.getValue();
            Unifier unifierInverse = unifier.inverse();
            Answer sub = this.getSubstitution().unify(unifierInverse);
            InferenceRule rule = ruleContext.getKey().propagateConstraints(getAtom(), unifierInverse).withSubstitution(sub);
            Stream<Answer> localStream = resolveViaRule(rule, unifier, subGoals, cache, dCache, differentialJoin);
            answerStream = Stream.concat(answerStream, localStream);
        }
    }
    return dCache.record(this, answerStream);
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) ReasonerUtils.typeUnifier(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.typeUnifier) Unifier(ai.grakn.graql.admin.Unifier) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 23 with Unifier

use of ai.grakn.graql.admin.Unifier in project grakn by graknlabs.

the class ReasonerUtils method typeUnifier.

/**
 * @param childTypes type atoms of child query
 * @param parentTypes type atoms of parent query
 * @param childParentUnifier unifier to unify child with parent
 * @return combined unifier for type atoms
 */
public static Unifier typeUnifier(Set<TypeAtom> childTypes, Set<TypeAtom> parentTypes, Unifier childParentUnifier) {
    Unifier unifier = childParentUnifier;
    for (TypeAtom childType : childTypes) {
        Var childVarName = childType.getVarName();
        Var parentVarName = unifier.containsKey(childVarName) ? Iterables.getOnlyElement(childParentUnifier.get(childVarName)) : childVarName;
        // types are unique so getting one is fine
        TypeAtom parentType = parentTypes.stream().filter(pt -> pt.getVarName().equals(parentVarName)).findFirst().orElse(null);
        if (parentType != null)
            unifier = unifier.merge(childType.getUnifier(parentType));
    }
    return unifier;
}
Also used : ValuePredicate.createValueVar(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate.createValueVar) Var(ai.grakn.graql.Var) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Unifier(ai.grakn.graql.admin.Unifier)

Example 24 with Unifier

use of ai.grakn.graql.admin.Unifier 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)

Example 25 with Unifier

use of ai.grakn.graql.admin.Unifier in project grakn by graknlabs.

the class AtomicQueryTest method testExactUnification_TernaryRelation_ParentRepeatsMetaRoles.

@Test
public void testExactUnification_TernaryRelation_ParentRepeatsMetaRoles() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String parentString = "{(role: $x, role: $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 unifier = childQuery.getMultiUnifier(parentQuery);
    MultiUnifier correctUnifier = new MultiUnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("z"), var("q"), var("y")), ImmutableMultimap.of(var("u"), var("y"), var("v"), var("z"), var("q"), var("x")));
    assertTrue(unifier.containsAll(correctUnifier));
    assertEquals(unifier.size(), 2);
    MultiUnifier unifier2 = childQuery2.getMultiUnifier(parentQuery);
    MultiUnifier correctUnifier2 = new MultiUnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("y"), var("q"), var("z")), ImmutableMultimap.of(var("u"), var("x"), var("v"), var("z"), var("q"), var("y")), ImmutableMultimap.of(var("u"), var("y"), var("v"), var("z"), var("q"), var("x")), ImmutableMultimap.of(var("u"), var("y"), var("v"), var("x"), var("q"), var("z")));
    assertTrue(unifier2.containsAll(correctUnifier2));
    assertEquals(unifier2.size(), 4);
    MultiUnifier unifier3 = childQuery3.getMultiUnifier(parentQuery);
    MultiUnifier correctUnifier3 = 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(unifier3.containsAll(correctUnifier3));
    assertEquals(unifier3.size(), 2);
    Unifier unifier4 = childQuery4.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier4 = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("u"), var("y"), var("q"), var("z")));
    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

Unifier (ai.grakn.graql.admin.Unifier)28 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)23 Test (org.junit.Test)18 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)17 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)16 Answer (ai.grakn.graql.admin.Answer)13 Var (ai.grakn.graql.Var)11 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)9 InferenceRule (ai.grakn.graql.internal.reasoner.rule.InferenceRule)7 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)7 Concept (ai.grakn.concept.Concept)6 Atomic (ai.grakn.graql.admin.Atomic)6 Conjunction (ai.grakn.graql.admin.Conjunction)6 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)6 RelationshipAtom (ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom)6 ResourceAtom (ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom)6 Sets (com.google.common.collect.Sets)6 List (java.util.List)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6