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();
}
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);
}
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)));
}
Aggregations