Search in sources :

Example 16 with GetQuery

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

the class OntologicalQueryTest method allInstancesOfTypesThatPlayGivenRole.

/**
 * PlaysAtom *
 */
@Test
public void allInstancesOfTypesThatPlayGivenRole() {
    GraknTx tx = testContext.tx();
    QueryBuilder qb = tx.graql().infer(true);
    String queryString = "match $x isa $type; $type plays role1; get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    List<Answer> reifiableRelations = qb.<GetQuery>parse("match $x isa reifiable-relation;get;").execute();
    assertEquals(answers.size(), tx.getEntityType("noRoleEntity").subs().flatMap(EntityType::instances).count() + reifiableRelations.size());
    assertCollectionsEqual(answers, qb.infer(false).<GetQuery>parse(queryString).execute());
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 17 with GetQuery

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

the class OntologicalQueryTest method allInstancesOfRelationsThatRelateGivenRole.

/**
 * RelatesAtom *
 */
@Test
public void allInstancesOfRelationsThatRelateGivenRole() {
    GraknTx tx = testContext.tx();
    QueryBuilder qb = tx.graql().infer(true);
    String queryString = "match $x isa $type; $type relates role1; get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    assertCollectionsEqual(answers, qb.infer(false).<GetQuery>parse(queryString).execute());
    List<Answer> relations = qb.<GetQuery>parse("match $x isa relationship;get;").execute();
    // plus extra 3 cause there are 3 binary relations which are not extra counted as reifiable-relations
    assertEquals(answers.size(), relations.stream().filter(ans -> !ans.get("x").asRelationship().type().isImplicit()).count() + 3);
}
Also used : QueryBuilder(ai.grakn.graql.QueryBuilder) GraqlTestUtil.assertCollectionsEqual(ai.grakn.util.GraqlTestUtil.assertCollectionsEqual) Test(org.junit.Test) Answer(ai.grakn.graql.admin.Answer) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) EntityType(ai.grakn.concept.EntityType) List(java.util.List) Rule(org.junit.Rule) Label(ai.grakn.concept.Label) Ignore(org.junit.Ignore) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) SampleKBContext(ai.grakn.test.rule.SampleKBContext) ExpectedException(org.junit.rules.ExpectedException) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 18 with GetQuery

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

the class OntologicalQueryTest method allInstancesAndTheirType.

// TODO need to correctly return THING and RELATIONSHIP mapping for %type
@Ignore
@Test
public void allInstancesAndTheirType() {
    GraknTx tx = testContext.tx();
    QueryBuilder qb = tx.graql().infer(true);
    String queryString = "match $x isa $type; get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    assertCollectionsEqual(answers, qb.infer(false).<GetQuery>parse(queryString).execute());
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 19 with GetQuery

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

the class ReasonerTest method testReasoningWithQueryContainingRelationTypeVar2.

@Test
public void testReasoningWithQueryContainingRelationTypeVar2() {
    String queryString = "match $y isa product;(recommended-customer: $x, recommended-product: $y) isa $rel; get;";
    String queryString2 = "match $y isa product;(recommended-customer: $x, recommended-product: $y) isa $rel;$rel label recommendation; get;";
    QueryBuilder qb = snbKB.tx().graql();
    GetQuery query = qb.infer(true).parse(queryString);
    GetQuery query2 = qb.infer(true).materialise(true).parse(queryString);
    GetQuery query3 = qb.infer(false).parse(queryString2);
    assertQueriesEqual(query, query2);
    assertQueriesEqual(query2, query3);
}
Also used : GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 20 with GetQuery

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

the class ReasonerTest method testReasoningWithQueryContainingMultiPredResource.

@Test
public void testReasoningWithQueryContainingMultiPredResource() {
    String queryString = "match $p isa person, has age $a;$a val >23; $a val <27;$pr isa product;" + "($p, $pr) isa recommendation; get $p, $pr;";
    String queryString2 = "match $p isa person, has age >23, has age <27;$pr isa product;" + "($p, $pr) isa recommendation; get;";
    QueryBuilder iqb = snbKB.tx().graql().infer(true);
    GetQuery query = iqb.parse(queryString);
    GetQuery query2 = iqb.parse(queryString2);
    assertQueriesEqual(query, query2);
}
Also used : GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Aggregations

GetQuery (ai.grakn.graql.GetQuery)163 Test (org.junit.Test)161 Answer (ai.grakn.graql.admin.Answer)65 QueryBuilder (ai.grakn.graql.QueryBuilder)61 GraknTx (ai.grakn.GraknTx)51 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)23 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)14 SampleKBContext (ai.grakn.test.rule.SampleKBContext)12 Concept (ai.grakn.concept.Concept)9 List (java.util.List)8 Assert.assertEquals (org.junit.Assert.assertEquals)7 Assert.assertTrue (org.junit.Assert.assertTrue)7 Set (java.util.Set)6 Label (ai.grakn.concept.Label)5 RelationshipType (ai.grakn.concept.RelationshipType)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)5 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)5 Sets (com.google.common.collect.Sets)5 Collectors (java.util.stream.Collectors)5 Collectors.toSet (java.util.stream.Collectors.toSet)5