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