Search in sources :

Example 21 with ReasonerQueryImpl

use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.

the class ResolutionPlan method queryPlan.

/**
 * compute the query resolution plan - list of queries ordered by their cost as computed by the graql traversal planner
 * @return list of prioritised queries
 */
public LinkedList<ReasonerQueryImpl> queryPlan() {
    LinkedList<ReasonerQueryImpl> queries = new LinkedList<>();
    LinkedList<Atom> atoms = new LinkedList<>(plan);
    List<Atom> nonResolvableAtoms = new ArrayList<>();
    while (!atoms.isEmpty()) {
        Atom top = atoms.remove();
        if (top.isRuleResolvable()) {
            if (!nonResolvableAtoms.isEmpty()) {
                queries.add(ReasonerQueries.create(nonResolvableAtoms, tx));
                nonResolvableAtoms.clear();
            }
            queries.add(ReasonerQueries.atomic(top));
        } else {
            nonResolvableAtoms.add(top);
            if (atoms.isEmpty())
                queries.add(ReasonerQueries.create(nonResolvableAtoms, tx));
        }
    }
    return queries;
}
Also used : ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)

Example 22 with ReasonerQueryImpl

use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.

the class QueryTest method testQueryReiterationCondition_AnotherCyclicalRuleGraph.

@Test
public void testQueryReiterationCondition_AnotherCyclicalRuleGraph() {
    EmbeddedGraknTx<?> graph = snbGraph.tx();
    String patternString = "{($x, $y);}";
    ReasonerQueryImpl query = ReasonerQueries.create(conjunction(patternString, graph), graph);
    assertTrue(query.requiresReiteration());
}
Also used : ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Test(org.junit.Test)

Example 23 with ReasonerQueryImpl

use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.

the class QueryTest method testAlphaEquivalence.

// check two queries are alpha-equivalent - equal up to the choice of free variables
@Test
public void testAlphaEquivalence() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    String patternString = "{" + "$x isa city, has name 'Warsaw';" + "$y isa region;" + "($x, $y) isa is-located-in;" + "($y, $z) isa is-located-in;" + "$z isa country, has name 'Poland';}";
    String patternString2 = "{" + "($r, $ctr) isa is-located-in;" + "($c, $r) isa is-located-in;" + "$c isa city, has name 'Warsaw';" + "$r isa region;" + "$ctr isa country, has name 'Poland';}";
    ReasonerQueryImpl query = ReasonerQueries.create(conjunction(patternString, graph), graph);
    ReasonerQueryImpl query2 = ReasonerQueries.create(conjunction(patternString2, graph), graph);
    queryEquivalence(query, query2, true);
}
Also used : ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Test(org.junit.Test)

Example 24 with ReasonerQueryImpl

use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.

the class QueryTest method testAlphaEquivalence_nonMatchingTypes.

// tests various configurations of alpha-equivalence with extra type atoms present
@Test
public void testAlphaEquivalence_nonMatchingTypes() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    String polandId = getConcept(graph, "name", "Poland").getId().getValue();
    String patternString = "{$y id '" + polandId + "'; $y isa country; (geo-entity: $y1, entity-location: $y), isa is-located-in;}";
    String patternString2 = "{$x1 id '" + polandId + "'; $y isa country; (geo-entity: $x1, entity-location: $x2), isa is-located-in;}";
    String patternString3 = "{$y id '" + polandId + "'; $x isa city; (geo-entity: $x, entity-location: $y), isa is-located-in; $y isa country;}";
    String patternString4 = "{$x isa city; (entity-location: $y1, geo-entity: $x), isa is-located-in;}";
    String patternString5 = "{(geo-entity: $y1, entity-location: $y2), isa is-located-in;}";
    ReasonerQueryImpl query = ReasonerQueries.create(conjunction(patternString, graph), graph);
    ReasonerQueryImpl query2 = ReasonerQueries.create(conjunction(patternString2, graph), graph);
    ReasonerQueryImpl query3 = ReasonerQueries.create(conjunction(patternString3, graph), graph);
    ReasonerQueryImpl query4 = ReasonerQueries.create(conjunction(patternString4, graph), graph);
    ReasonerQueryImpl query5 = ReasonerQueries.create(conjunction(patternString5, graph), graph);
    queryEquivalence(query, query2, false);
    queryEquivalence(query, query3, false);
    queryEquivalence(query2, query3, false);
    queryEquivalence(query, query4, false);
    queryEquivalence(query, query5, false);
    queryEquivalence(query3, query4, false);
    queryEquivalence(query3, query5, false);
    queryEquivalence(query4, query5, false);
}
Also used : ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Test(org.junit.Test)

Example 25 with ReasonerQueryImpl

use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.

the class QueryTest method testWhenReifyingRelation_ExtraAtomIsCreatedWithUserDefinedName.

@Test
public void testWhenReifyingRelation_ExtraAtomIsCreatedWithUserDefinedName() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    String patternString = "{(geo-entity: $x, entity-location: $y) isa is-located-in;}";
    String patternString2 = "{($x, $y) relates geo-entity;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
    ReasonerQueryImpl query = ReasonerQueries.create(pattern, graph);
    ReasonerQueryImpl query2 = ReasonerQueries.create(pattern2, graph);
    assertEquals(query.getAtoms(RelationshipAtom.class).findFirst().orElse(null).isUserDefined(), false);
    assertEquals(query2.getAtoms(RelationshipAtom.class).findFirst().orElse(null).isUserDefined(), true);
    assertEquals(query.getAtoms().size(), 1);
    assertEquals(query2.getAtoms().size(), 2);
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Test(org.junit.Test)

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