Search in sources :

Example 16 with ReasonerQueryImpl

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);
}
Also used : Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) ResolutionPlan(ai.grakn.graql.internal.reasoner.plan.ResolutionPlan) Test(org.junit.Test)

Example 17 with ReasonerQueryImpl

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);
    }
}
Also used : Var(ai.grakn.graql.Var) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) ResolutionPlan(ai.grakn.graql.internal.reasoner.plan.ResolutionPlan) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with ReasonerQueryImpl

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);
    }
}
Also used : Var(ai.grakn.graql.Var) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) ResolutionPlan(ai.grakn.graql.internal.reasoner.plan.ResolutionPlan) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with ReasonerQueryImpl

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)));
}
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 20 with ReasonerQueryImpl

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);
        }
    }
}
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) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) ArrayList(java.util.ArrayList) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) OntologicalAtom(ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom)

Aggregations

ReasonerQueryImpl (ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)28 Test (org.junit.Test)20 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)11 ResolutionPlan (ai.grakn.graql.internal.reasoner.plan.ResolutionPlan)7 Var (ai.grakn.graql.Var)5 HashSet (java.util.HashSet)5 ConceptId (ai.grakn.concept.ConceptId)4 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)4 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 Stream (java.util.stream.Stream)4 GraknTx (ai.grakn.GraknTx)3 Rule (ai.grakn.concept.Rule)3 SchemaConcept (ai.grakn.concept.SchemaConcept)3 Answer (ai.grakn.graql.admin.Answer)3 Atomic (ai.grakn.graql.admin.Atomic)3 Unifier (ai.grakn.graql.admin.Unifier)3 UnifierType (ai.grakn.graql.internal.reasoner.UnifierType)3 ValuePredicate (ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate)3 Sets (com.google.common.collect.Sets)3