Search in sources :

Example 11 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class OntologicalQueryTest method allInstancesOfTypesThatAreSubTypeOfGivenType_needInferenceToGetAllResults.

@Test
public void allInstancesOfTypesThatAreSubTypeOfGivenType_needInferenceToGetAllResults() {
    GraknTx tx = testContext.tx();
    QueryBuilder qb = tx.graql().infer(true);
    String queryString = "match $x isa $type; $type sub relationship; get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    assertEquals(answers.size(), tx.getRelationshipType("relationship").subs().flatMap(RelationshipType::instances).count());
    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) RelationshipType(ai.grakn.concept.RelationshipType) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 12 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class OntologicalQueryTest method allInstancesOfMetaRelation.

@Test
public void allInstancesOfMetaRelation() {
    GraknTx tx = testContext.tx();
    QueryBuilder qb = tx.graql().infer(true);
    String queryString = "match $x isa relationship;get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    assertEquals(answers.size(), 13);
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 13 with GraknTx

use of ai.grakn.GraknTx 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 14 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class OntologicalQueryTest method allTypesOfRolePlayerInASpecificRelationWithUnspecifiedRoles.

@Test
public void allTypesOfRolePlayerInASpecificRelationWithUnspecifiedRoles() {
    GraknTx tx = testContext.tx();
    QueryBuilder qb = tx.graql().infer(true);
    String queryString = "match ($x, $y) isa reifiable-relation;$x isa $type; get;";
    // 3 instances * {anotherTwoRoleEntity, anotherSingleRoleEntity, noRoleEntity, entity, Thing} * arity
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    assertEquals(answers.size(), qb.<GetQuery>parse("match $x isa reifiable-relation; get;").stream().count() * 5 * 2);
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 15 with GraknTx

use of ai.grakn.GraknTx 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)

Aggregations

GraknTx (ai.grakn.GraknTx)243 Test (org.junit.Test)189 EntityType (ai.grakn.concept.EntityType)54 GetQuery (ai.grakn.graql.GetQuery)52 Entity (ai.grakn.concept.Entity)51 QueryBuilder (ai.grakn.graql.QueryBuilder)49 Role (ai.grakn.concept.Role)48 RelationshipType (ai.grakn.concept.RelationshipType)46 Answer (ai.grakn.graql.admin.Answer)44 Set (java.util.Set)44 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)33 Label (ai.grakn.concept.Label)28 Attribute (ai.grakn.concept.Attribute)27 Concept (ai.grakn.concept.Concept)27 HashSet (java.util.HashSet)26 GraknSession (ai.grakn.GraknSession)22 AttributeType (ai.grakn.concept.AttributeType)22 ConceptId (ai.grakn.concept.ConceptId)22 List (java.util.List)19 GraknTxType (ai.grakn.GraknTxType)17