Search in sources :

Example 1 with GraqlTraversal

use of ai.grakn.graql.internal.gremlin.GraqlTraversal in project grakn by graknlabs.

the class GraqlTraversalPlanner method planFromTraversal.

/**
 * @param atoms list of current atoms of interest
 * @param queryPattern corresponding pattern
 * @return an optimally ordered list of provided atoms
 */
static ImmutableList<Atom> planFromTraversal(List<Atom> atoms, PatternAdmin queryPattern, EmbeddedGraknTx<?> tx) {
    Multimap<VarProperty, Atom> propertyMap = HashMultimap.create();
    atoms.stream().filter(at -> !(at instanceof OntologicalAtom)).forEach(at -> at.getVarProperties().forEach(p -> propertyMap.put(p, at)));
    Set<VarProperty> properties = propertyMap.keySet();
    GraqlTraversal graqlTraversal = GreedyTraversalPlan.createTraversal(queryPattern, tx);
    ImmutableList<Fragment> fragments = graqlTraversal.fragments().iterator().next();
    ImmutableList.Builder<Atom> builder = ImmutableList.builder();
    builder.addAll(atoms.stream().filter(at -> at instanceof OntologicalAtom).iterator());
    builder.addAll(fragments.stream().map(Fragment::varProperty).filter(Objects::nonNull).filter(properties::contains).distinct().flatMap(p -> propertyMap.get(p).stream()).distinct().iterator());
    return builder.build();
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) GreedyTraversalPlan(ai.grakn.graql.internal.gremlin.GreedyTraversalPlan) OntologicalAtom(ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom) HashMultimap(com.google.common.collect.HashMultimap) ImmutableList(com.google.common.collect.ImmutableList) ConceptId(ai.grakn.concept.ConceptId) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Nullable(javax.annotation.Nullable) Patterns(ai.grakn.graql.internal.pattern.Patterns) Conjunction(ai.grakn.graql.admin.Conjunction) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) VarProperty(ai.grakn.graql.admin.VarProperty) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Stream(java.util.stream.Stream) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Comparator(java.util.Comparator) VarProperty(ai.grakn.graql.admin.VarProperty) ImmutableList(com.google.common.collect.ImmutableList) Objects(java.util.Objects) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) OntologicalAtom(ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom) OntologicalAtom(ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom)

Example 2 with GraqlTraversal

use of ai.grakn.graql.internal.gremlin.GraqlTraversal in project grakn by graknlabs.

the class MatchBase method stream.

@Override
public Stream<Answer> stream(Optional<EmbeddedGraknTx<?>> optionalGraph) {
    EmbeddedGraknTx<?> tx = optionalGraph.orElseThrow(GraqlQueryException::noTx);
    validatePattern(tx);
    GraqlTraversal graqlTraversal = GreedyTraversalPlan.createTraversal(pattern, tx);
    LOG.trace("Created query plan");
    LOG.trace(graqlTraversal.toString());
    return streamWithTraversal(this.getPattern().commonVars(), tx, graqlTraversal);
}
Also used : GraqlQueryException(ai.grakn.exception.GraqlQueryException) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal)

Example 3 with GraqlTraversal

use of ai.grakn.graql.internal.gremlin.GraqlTraversal 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)

Aggregations

GraqlTraversal (ai.grakn.graql.internal.gremlin.GraqlTraversal)3 ConceptId (ai.grakn.concept.ConceptId)2 GreedyTraversalPlan (ai.grakn.graql.internal.gremlin.GreedyTraversalPlan)2 ReasonerQueryImpl (ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)2 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)2 Stream (java.util.stream.Stream)2 GraqlQueryException (ai.grakn.exception.GraqlQueryException)1 Var (ai.grakn.graql.Var)1 Answer (ai.grakn.graql.admin.Answer)1 Atomic (ai.grakn.graql.admin.Atomic)1 Conjunction (ai.grakn.graql.admin.Conjunction)1 PatternAdmin (ai.grakn.graql.admin.PatternAdmin)1 ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)1 Unifier (ai.grakn.graql.admin.Unifier)1 VarProperty (ai.grakn.graql.admin.VarProperty)1 Fragment (ai.grakn.graql.internal.gremlin.fragment.Fragment)1 Patterns (ai.grakn.graql.internal.pattern.Patterns)1 MatchBase (ai.grakn.graql.internal.query.match.MatchBase)1 UnifierType (ai.grakn.graql.internal.reasoner.UnifierType)1 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)1