use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.
the class ResolutionPlanTest method prioritiseSpecificResourcesOverRelations.
@Test
public void prioritiseSpecificResourcesOverRelations() {
EmbeddedGraknTx<?> testTx = testContext.tx();
String queryString = "{" + "(role1:$x, role2: $y) isa relation;" + "(role1:$y, role2: $z) isa anotherRelation;" + "(role1:$z, role2: $w) isa yetAnotherRelation;" + "$w has resource 'test';" + "}";
ReasonerQueryImpl query = ReasonerQueries.create(conjunction(queryString, testTx), testTx);
ImmutableList<Atom> correctPlan = ImmutableList.of(getAtom(query, "resource", testTx), getAtom(query, "yetAnotherRelation", testTx), getAtom(query, "anotherRelation", testTx), getAtom(query, "relation", testTx));
ImmutableList<Atom> plan = new ResolutionPlan(query).plan();
assertEquals(plan, correctPlan);
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.
the class ResolutionPlanTest method makeSureConnectednessPreservedWhenRelationsWithSameTypesPresent_longerChain.
@Test
public void makeSureConnectednessPreservedWhenRelationsWithSameTypesPresent_longerChain() {
EmbeddedGraknTx<?> testTx = testContext.tx();
String queryString = "{" + "(role1:$x, role2: $y) isa relation;" + "(role1:$y, role2: $z) isa anotherRelation;" + "(role1:$z, role2: $w) isa yetAnotherRelation;" + "(role1:$w, role2: $u) isa relation;" + "(role1:$u, role2: $v) isa anotherRelation;" + "(role1:$v, role2: $q) isa yetAnotherRelation;" + "}";
ReasonerQueryImpl query = ReasonerQueries.create(conjunction(queryString, testTx), testTx);
ImmutableList<Atom> plan = new ResolutionPlan(query).plan();
UnmodifiableIterator<Atom> iterator = plan.iterator();
Set<Var> vars = new HashSet<>();
vars.addAll(iterator.next().getVarNames());
while (iterator.hasNext()) {
Atom next = iterator.next();
Set<Var> varNames = next.getVarNames();
assertTrue(!Sets.intersection(varNames, vars).isEmpty());
vars.addAll(varNames);
}
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.
the class ResolutionPlanTest method makeSureConnectednessPreservedWhenRelationsWithSameTypesPresent.
@Test
public void makeSureConnectednessPreservedWhenRelationsWithSameTypesPresent() {
EmbeddedGraknTx<?> testTx = testContext.tx();
String queryString = "{" + "(role1:$x, role2: $y) isa relation;" + "(role1:$y, role2: $z) isa anotherRelation;" + "(role1:$z, role2: $w) isa relation;" + "(role1:$w, role2: $u) isa anotherRelation;" + "(role1:$u, role2: $v) isa relation;" + "}";
ReasonerQueryImpl query = ReasonerQueries.create(conjunction(queryString, testTx), testTx);
ImmutableList<Atom> plan = new ResolutionPlan(query).plan();
UnmodifiableIterator<Atom> iterator = plan.iterator();
Set<Var> vars = new HashSet<>();
vars.addAll(iterator.next().getVarNames());
while (iterator.hasNext()) {
Atom next = iterator.next();
Set<Var> varNames = next.getVarNames();
assertTrue(!Sets.intersection(varNames, vars).isEmpty());
vars.addAll(varNames);
}
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl 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.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.
the class GraqlTraversalPlanner method refinedPlan.
/**
* @param query top level query for which the plan is constructed
* @param atoms list of current atoms of interest
* @param subs extra substitutions
* @return an optimally ordered list of provided atoms
*/
private static List<Atom> refinedPlan(ReasonerQueryImpl query, List<Atom> atoms, Set<IdPredicate> subs) {
List<Atom> candidates = subs.isEmpty() ? atoms : atoms.stream().filter(at -> at.getPredicates(IdPredicate.class).findFirst().isPresent()).collect(Collectors.toList());
ImmutableList<Atom> initialPlan = planFromTraversal(atoms, atomsToPattern(atoms, subs), query.tx());
if (candidates.contains(initialPlan.get(0)) || candidates.isEmpty()) {
return initialPlan;
} else {
Atom first = optimalCandidate(candidates);
List<Atom> atomsToPlan = new ArrayList<>(atoms);
if (first != null) {
atomsToPlan.remove(first);
Set<IdPredicate> extraSubs = first.getVarNames().stream().filter(v -> subs.stream().noneMatch(s -> s.getVarName().equals(v))).map(v -> IdPredicate.create(v, ConceptId.of("placeholderId"), query)).collect(Collectors.toSet());
return Stream.concat(Stream.of(first), refinedPlan(query, atomsToPlan, Sets.union(subs, extraSubs)).stream()).collect(Collectors.toList());
} else {
return refinedPlan(query, atomsToPlan, subs);
}
}
}
Aggregations