Search in sources :

Example 71 with QueryBuilder

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

the class GeoInferenceTest method testTransitiveQuery_withSpecificResource.

@Test
public void testTransitiveQuery_withSpecificResource() {
    GraknTx graph = geoKB.tx();
    QueryBuilder iqb = graph.graql().infer(true);
    String queryString = "match " + "(geo-entity: $x, entity-location: $y) isa is-located-in;" + "$y has name 'Poland'; get;";
    String queryString2 = "match " + "(geo-entity: $x, entity-location: $y) isa is-located-in;" + "$y has name 'Europe'; get;";
    Concept poland = getConcept(graph, "name", "Poland");
    Concept europe = getConcept(graph, "name", "Europe");
    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 72 with QueryBuilder

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

the class GeoInferenceTest method testTransitiveQuery_withSubstitution_noRoles.

@Test
public void testTransitiveQuery_withSubstitution_noRoles() {
    GraknTx graph = geoKB.tx();
    QueryBuilder iqb = graph.graql().infer(true);
    Concept masovia = getConcept(graph, "name", "Masovia");
    String queryString = "match " + "($x, $y) isa is-located-in;" + "$y id '" + masovia.getId().getValue() + "'; get;";
    String queryString2 = "match " + "{(geo-entity: $x, entity-location: $y) isa is-located-in or " + "(geo-entity: $y, entity-location: $x) isa is-located-in;};" + "$y id '" + masovia.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(), masovia.getId().getValue()));
    assertEquals(answers.size(), 5);
    List<Answer> answers2 = iqb.materialise(false).<GetQuery>parse(queryString2).execute();
    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)

Example 73 with QueryBuilder

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

the class GeoInferenceTest method testTransitiveQuery_Closure.

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

Example 74 with QueryBuilder

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

the class SNBInferenceTest method testBook.

@Test
public void testBook() {
    QueryBuilder qb = snbGraph.tx().graql().infer(false);
    QueryBuilder iqb = snbGraph.tx().graql().infer(true);
    String queryString = "match $x isa person;" + "($x, $y) isa recommendation;" + "$c isa category;$c has name 'book';" + "($y, $c) isa typing;" + "get $x, $y;";
    String explicitQuery = "match $x isa person, has name $nx;$y isa product, has name $ny;" + "{$nx val 'Alice';$ny val 'War of the Worlds';} or" + "{$nx val 'Karl Fischer';$ny val 'Faust';} or " + "{$nx val 'Denis';{$ny val 'Colour of Magic';} or {$ny val 'Dorian Gray';};};get $x, $y;";
    assertQueriesEqual(iqb.materialise(false).parse(queryString), qb.parse(explicitQuery));
    assertQueriesEqual(iqb.materialise(true).parse(queryString), qb.parse(explicitQuery));
}
Also used : QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 75 with QueryBuilder

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

the class SNBInferenceTest method testTransitivity.

/**
 * Tests transitivity
 */
@Test
public void testTransitivity() {
    QueryBuilder qb = snbGraph.tx().graql().infer(false);
    QueryBuilder iqb = snbGraph.tx().graql().infer(true);
    String queryString = "match " + "$x isa university;$y isa country;(located-subject: $x, subject-location: $y) isa resides; get;";
    String explicitQuery = "match $x isa university, has name 'University of Cambridge';" + "$y isa country, has name 'UK'; get;";
    assertQueriesEqual(iqb.materialise(false).parse(queryString), qb.parse(explicitQuery));
    assertQueriesEqual(iqb.materialise(true).parse(queryString), qb.parse(explicitQuery));
}
Also used : 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