Search in sources :

Example 76 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class ResourceAtom method getUnifier.

@Override
public Unifier getUnifier(Atom parentAtom) {
    if (!(parentAtom instanceof ResourceAtom)) {
        return new UnifierImpl(ImmutableMap.of(this.getPredicateVariable(), parentAtom.getVarName()));
    }
    Unifier unifier = super.getUnifier(parentAtom);
    ResourceAtom parent = (ResourceAtom) parentAtom;
    // unify relation vars
    Var childRelationVarName = this.getRelationVariable();
    Var parentRelationVarName = parent.getRelationVariable();
    if (parentRelationVarName.isUserDefinedName() && !childRelationVarName.equals(parentRelationVarName)) {
        unifier = unifier.merge(new UnifierImpl(ImmutableMap.of(childRelationVarName, parentRelationVarName)));
    }
    return unifier;
}
Also used : Var(ai.grakn.graql.Var) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) Unifier(ai.grakn.graql.admin.Unifier)

Example 77 with Var

use of ai.grakn.graql.Var 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 78 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class QueryAnswerStream method entityTypeFilter.

static boolean entityTypeFilter(Answer answer, Set<TypeAtom> types) {
    if (types.isEmpty())
        return true;
    for (TypeAtom type : types) {
        Var var = type.getVarName();
        SchemaConcept t = type.getSchemaConcept();
        if (t.subs().noneMatch(sub -> sub.equals(answer.get(var).asThing().type()))) {
            return false;
        }
    }
    return true;
}
Also used : Var(ai.grakn.graql.Var) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 79 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class QueryAnswerStream method join.

/**
 * lazy stream join with quasi- sideways information propagation
 * @param stream left stream operand
 * @param stream2 right stream operand
 * @param joinVars intersection on variables of two streams
 * @return joined stream
 */
public static Stream<Answer> join(Stream<Answer> stream, Stream<Answer> stream2, ImmutableSet<Var> joinVars) {
    LazyAnswerIterator l2 = new LazyAnswerIterator(stream2);
    return stream.flatMap(a1 -> {
        Stream<Answer> answerStream = l2.stream();
        answerStream = answerStream.filter(ans -> {
            for (Var v : joinVars) {
                if (!ans.get(v).equals(a1.get(v))) {
                    return false;
                }
            }
            return true;
        });
        return answerStream.map(a -> a.merge(a1));
    });
}
Also used : Pair(ai.grakn.graql.internal.reasoner.utils.Pair) ImmutableSet(com.google.common.collect.ImmutableSet) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Iterator(java.util.Iterator) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Set(java.util.Set) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) Sets(com.google.common.collect.Sets) HashSet(java.util.HashSet) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Stream(java.util.stream.Stream) Map(java.util.Map) Var(ai.grakn.graql.Var) Answer(ai.grakn.graql.admin.Answer) Var(ai.grakn.graql.Var) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator)

Example 80 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class QueryAnswerStream method joinWithInverse.

/**
 * lazy stream join with fast lookup from inverse answer map
 * @param stream left stream operand
 * @param stream2 right stream operand
 * @param stream2InverseMap inverse map of right operand from cache
 * @param joinVars intersection on variables of two streams
 * @return joined stream
 */
static Stream<Answer> joinWithInverse(Stream<Answer> stream, Stream<Answer> stream2, Map<Pair<Var, Concept>, Set<Answer>> stream2InverseMap, ImmutableSet<Var> joinVars) {
    if (joinVars.isEmpty()) {
        LazyAnswerIterator l2 = new LazyAnswerIterator(stream2);
        return stream.flatMap(a1 -> l2.stream().map(a -> a.merge(a1)));
    }
    return stream.flatMap(a1 -> {
        Iterator<Var> vit = joinVars.iterator();
        Set<Answer> matchAnswers = findMatchingAnswers(a1, stream2InverseMap, vit.next());
        while (vit.hasNext()) {
            matchAnswers = Sets.intersection(matchAnswers, findMatchingAnswers(a1, stream2InverseMap, vit.next()));
        }
        return matchAnswers.stream().map(a -> a.merge(a1));
    });
}
Also used : Pair(ai.grakn.graql.internal.reasoner.utils.Pair) ImmutableSet(com.google.common.collect.ImmutableSet) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Iterator(java.util.Iterator) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Set(java.util.Set) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) Sets(com.google.common.collect.Sets) HashSet(java.util.HashSet) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Stream(java.util.stream.Stream) Map(java.util.Map) Var(ai.grakn.graql.Var) Answer(ai.grakn.graql.admin.Answer) Var(ai.grakn.graql.Var) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator)

Aggregations

Var (ai.grakn.graql.Var)100 Test (org.junit.Test)29 Answer (ai.grakn.graql.admin.Answer)28 ConceptId (ai.grakn.concept.ConceptId)26 Concept (ai.grakn.concept.Concept)25 Role (ai.grakn.concept.Role)25 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)24 HashSet (java.util.HashSet)24 Set (java.util.Set)24 VarPattern (ai.grakn.graql.VarPattern)21 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)21 Sets (com.google.common.collect.Sets)20 Map (java.util.Map)20 Stream (java.util.stream.Stream)20 GraqlQueryException (ai.grakn.exception.GraqlQueryException)19 ImmutableSet (com.google.common.collect.ImmutableSet)19 HashMap (java.util.HashMap)19 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)18 List (java.util.List)18 SchemaConcept (ai.grakn.concept.SchemaConcept)17