Search in sources :

Example 66 with QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class ReasoningTests method recursiveRelationsWithSharedNeqPredicate_relationsAreEquivalent.

/**
 * Tests a scenario in which the neq predicate binds free variables of two recursive equivalent relations.
 * Corresponds to the following pattern:
 *
 *                     x
 *                   /    \
 *                 /        \
 *               v           v
 *              y     !=      z
 */
@Test
public void recursiveRelationsWithSharedNeqPredicate_relationsAreEquivalent() {
    QueryBuilder qb = testSet29.tx().graql().infer(true);
    String baseQueryString = "match " + "(role1: $x, role2: $y) isa binary-base;" + "(role1: $x, role2: $z) isa binary-base;" + "$y != $z;";
    List<Answer> baseAnswers = qb.<GetQuery>parse(baseQueryString + "get;").execute();
    assertEquals(baseAnswers.size(), 18);
    baseAnswers.forEach(ans -> {
        assertEquals(ans.size(), 3);
        assertNotEquals(ans.get("y"), ans.get("z"));
    });
    String queryString = baseQueryString + "$x has name 'a';";
    String explicitString = "match " + "$x has name 'a';" + "{$y has name 'a';$z has name 'b';} or " + "{$y has name 'a';$z has name 'c';} or " + "{$y has name 'b';$z has name 'a';} or" + "{$y has name 'b';$z has name 'c';} or " + "{$y has name 'c';$z has name 'a';} or " + "{$y has name 'c';$z has name 'b';};";
    List<Answer> answers = qb.<GetQuery>parse(queryString + "get;").execute();
    List<Answer> answers2 = qb.infer(false).<GetQuery>parse(explicitString + "get;").execute();
    assertTrue(baseAnswers.containsAll(answers));
    assertTrue(answers.containsAll(answers2));
    assertTrue(answers2.containsAll(answers));
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 67 with QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class GeoInferenceTest method testTransitiveQuery_withSubstitution.

@Test
public void testTransitiveQuery_withSubstitution() {
    GraknTx graph = geoKB.tx();
    QueryBuilder iqb = graph.graql().infer(true);
    Concept poland = getConcept(graph, "name", "Poland");
    Concept europe = getConcept(graph, "name", "Europe");
    String queryString = "match " + "(geo-entity: $x, entity-location: $y) isa is-located-in;" + "$y id '" + poland.getId().getValue() + "'; get;";
    String queryString2 = "match " + "(geo-entity: $x, entity-location: $y) isa is-located-in;" + "$y id '" + europe.getId().getValue() + "'; get;";
    List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
    answers.forEach(ans -> assertEquals(ans.size(), 2));
    answers.forEach(ans -> assertEquals(ans.get(var("y")).getId().getValue(), poland.getId().getValue()));
    assertEquals(answers.size(), 6);
    List<Answer> answers2 = iqb.materialise(false).<GetQuery>parse(queryString2).execute();
    answers2.forEach(ans -> assertEquals(ans.size(), 2));
    answers2.forEach(ans -> assertEquals(ans.get(var("y")).getId().getValue(), europe.getId().getValue()));
    assertEquals(answers2.size(), 21);
}
Also used : Concept(ai.grakn.concept.Concept) GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 68 with QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class GeoInferenceTest method testLazy.

@Test
public void testLazy() {
    QueryBuilder iqb = geoKB.tx().graql().infer(true);
    String queryString = "match (geo-entity: $x, entity-location: $y) isa is-located-in; limit 1; get;";
    String queryString2 = "match (geo-entity: $x, entity-location: $y) isa is-located-in; limit 22; get;";
    String queryString3 = "match (geo-entity: $x, entity-location: $y) isa is-located-in; get;";
    List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
    List<Answer> answers2 = iqb.materialise(false).<GetQuery>parse(queryString2).execute();
    List<Answer> answers3 = iqb.materialise(false).<GetQuery>parse(queryString3).execute();
    assertTrue(answers3.containsAll(answers));
    assertTrue(answers3.containsAll(answers2));
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 69 with QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class GeoInferenceTest method testTransitiveQuery_Closure_variableRoles.

@Test
public void testTransitiveQuery_Closure_variableRoles() {
    QueryBuilder iqb = geoKB.tx().graql().infer(true);
    String queryString = "match ($r1: $x, $r2: $y) isa is-located-in; get;";
    List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
    answers.forEach(ans -> assertEquals(ans.size(), 4));
    assertEquals(answers.size(), 408);
    List<Answer> answers2 = iqb.materialise(true).<GetQuery>parse(queryString).execute();
    answers2.forEach(ans -> assertEquals(ans.size(), 4));
    assertCollectionsEqual(answers, answers2);
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 70 with QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class GeoInferenceTest method testTransitiveQuery_variableRoles_withSubstitution_withRelationVar.

@Test
public void testTransitiveQuery_variableRoles_withSubstitution_withRelationVar() {
    GraknTx graph = geoKB.tx();
    QueryBuilder iqb = geoKB.tx().graql().infer(true);
    Concept masovia = getConcept(graph, "name", "Masovia");
    String queryString = "match " + "$x ($r1: $x1, $r2: $x2) isa is-located-in;" + "$x2 id '" + masovia.getId().getValue() + "'; get;";
    List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
    assertEquals(answers.size(), 20);
    answers.forEach(ans -> assertEquals(ans.size(), 5));
    List<Answer> answers2 = iqb.materialise(true).<GetQuery>parse(queryString).execute();
    answers2.forEach(ans -> assertEquals(ans.size(), 5));
    assertCollectionsEqual(answers, answers2);
}
Also used : Concept(ai.grakn.concept.Concept) GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Aggregations

QueryBuilder (ai.grakn.graql.QueryBuilder)208 Test (org.junit.Test)202 Answer (ai.grakn.graql.admin.Answer)101 GetQuery (ai.grakn.graql.GetQuery)60 GraknTx (ai.grakn.GraknTx)51 SampleKBContext (ai.grakn.test.rule.SampleKBContext)20 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)18 Concept (ai.grakn.concept.Concept)16 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)9 Ignore (org.junit.Ignore)8 Var (ai.grakn.graql.Var)5 VarPattern (ai.grakn.graql.VarPattern)5 List (java.util.List)5 Label (ai.grakn.concept.Label)4 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)4 Sets (com.google.common.collect.Sets)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 RelationshipType (ai.grakn.concept.RelationshipType)3 Graql (ai.grakn.graql.Graql)3 Graql.var (ai.grakn.graql.Graql.var)3