Search in sources :

Example 1 with EntityType

use of ai.grakn.concept.EntityType in project grakn by graknlabs.

the class BenchmarkTests method nonRecursiveChainOfRules.

/**
 * Executes a scalability test defined in terms of the number of rules in the system. Creates a simple rule chain:
 *
 * R_i(x, y) := R_{i-1}(x, y);     i e [1, N]
 *
 * with a single initial relation instance R_0(a ,b)
 */
@Test
public void nonRecursiveChainOfRules() {
    final int N = 200;
    LOG.debug(new Object() {
    }.getClass().getEnclosingMethod().getName());
    GraknSession graknSession = sessionContext.newSession();
    // NB: loading data here as defining it as KB and using graql api leads to circular dependencies
    try (GraknTx tx = graknSession.open(GraknTxType.WRITE)) {
        Role fromRole = tx.putRole("fromRole");
        Role toRole = tx.putRole("toRole");
        RelationshipType relation0 = tx.putRelationshipType("relation0").relates(fromRole).relates(toRole);
        for (int i = 1; i <= N; i++) {
            tx.putRelationshipType("relation" + i).relates(fromRole).relates(toRole);
        }
        EntityType genericEntity = tx.putEntityType("genericEntity").plays(fromRole).plays(toRole);
        Entity fromEntity = genericEntity.addEntity();
        Entity toEntity = genericEntity.addEntity();
        relation0.addRelationship().addRolePlayer(fromRole, fromEntity).addRolePlayer(toRole, toEntity);
        for (int i = 1; i <= N; i++) {
            Var fromVar = Graql.var().asUserDefined();
            Var toVar = Graql.var().asUserDefined();
            VarPattern rulePattern = Graql.label("rule" + i).when(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa("relation" + (i - 1)))).then(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa("relation" + i)));
            tx.graql().define(rulePattern).execute();
        }
        tx.commit();
    }
    try (GraknTx tx = graknSession.open(GraknTxType.READ)) {
        final long limit = 1;
        String queryPattern = "(fromRole: $x, toRole: $y) isa relation" + N + ";";
        String queryString = "match " + queryPattern + " get;";
        String limitedQueryString = "match " + queryPattern + "limit " + limit + ";" + "get;";
        assertEquals(executeQuery(queryString, tx, "full").size(), limit);
        assertEquals(executeQuery(limitedQueryString, tx, "limit").size(), limit);
    }
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Var(ai.grakn.graql.Var) GraknSession(ai.grakn.GraknSession) RelationshipType(ai.grakn.concept.RelationshipType) VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 2 with EntityType

use of ai.grakn.concept.EntityType in project grakn by graknlabs.

the class SchemaMutationTest method whenChangingSuperTypesUsingBatchGraph_Throw.

@Test
public void whenChangingSuperTypesUsingBatchGraph_Throw() {
    String entityTypeId1 = "entityType1";
    String entityTypeId2 = "entityType2";
    tx.putEntityType(entityTypeId1);
    tx.putEntityType(entityTypeId2);
    EmbeddedGraknTx<?> graknGraphBatch = batchTx();
    EntityType entityType1 = graknGraphBatch.getEntityType(entityTypeId1);
    EntityType entityType2 = graknGraphBatch.getEntityType(entityTypeId2);
    expectedException.expect(GraknTxOperationException.class);
    expectedException.expectMessage(GraknTxOperationException.schemaMutation().getMessage());
    entityType1.sup(entityType2);
}
Also used : EntityType(ai.grakn.concept.EntityType) Test(org.junit.Test)

Example 3 with EntityType

use of ai.grakn.concept.EntityType in project grakn by graknlabs.

the class SchemaMutationTest method whenChangingTheSuperTypeOfAnEntityTypeWhichHasAResource_EnsureTheResourceIsStillAccessibleViaTheRelationTypeInstances_ByPreventingChange.

@Test
public void whenChangingTheSuperTypeOfAnEntityTypeWhichHasAResource_EnsureTheResourceIsStillAccessibleViaTheRelationTypeInstances_ByPreventingChange() {
    AttributeType<String> name = tx.putAttributeType("name", AttributeType.DataType.STRING);
    // Create a person and allow person to have a name
    EntityType person = tx.putEntityType("person").attribute(name);
    // Create a man which is a person and is therefore allowed to have a name
    EntityType man = tx.putEntityType("man").sup(person);
    RelationshipType has_name = tx.getRelationshipType("@has-name");
    // Create a Man and name him Bob
    Attribute<String> nameBob = name.putAttribute("Bob");
    man.addEntity().attribute(nameBob);
    // Get The Relationship which says that our man is name bob
    Relationship expectedEdge = Iterables.getOnlyElement(has_name.instances().collect(toSet()));
    Role hasNameOwner = tx.getRole("@has-name-owner");
    assertThat(expectedEdge.type().instances().collect(toSet()), hasItem(expectedEdge));
    expectedException.expect(GraknTxOperationException.class);
    expectedException.expectMessage(GraknTxOperationException.changingSuperWillDisconnectRole(person, tx.admin().getMetaEntityType(), hasNameOwner).getMessage());
    // Man is no longer a person and therefore is not allowed to have a name
    man.sup(tx.admin().getMetaEntityType());
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 4 with EntityType

use of ai.grakn.concept.EntityType in project grakn by graknlabs.

the class PostProcessingTest method whenShardingThresholdIsBreached_ShardTypes.

@Test
public void whenShardingThresholdIsBreached_ShardTypes() {
    Keyspace keyspace = SampleKBLoader.randomKeyspace();
    EntityType et1;
    EntityType et2;
    // Create Simple GraknTx
    try (EmbeddedGraknTx<?> graknTx = EmbeddedGraknSession.create(keyspace, engine.uri().toString()).open(GraknTxType.WRITE)) {
        et1 = graknTx.putEntityType("et1");
        et2 = graknTx.putEntityType("et2");
        graknTx.commitSubmitNoLogs();
    }
    checkShardCount(keyspace, et1, 1);
    checkShardCount(keyspace, et2, 1);
    // Add new counts
    createAndUploadCountCommitLog(keyspace, et1.getId(), 99_999L);
    createAndUploadCountCommitLog(keyspace, et2.getId(), 99_999L);
    checkShardCount(keyspace, et1, 1);
    checkShardCount(keyspace, et2, 1);
    // Add new counts
    createAndUploadCountCommitLog(keyspace, et1.getId(), 2L);
    createAndUploadCountCommitLog(keyspace, et2.getId(), 1L);
    checkShardCount(keyspace, et1, 2);
    checkShardCount(keyspace, et2, 1);
}
Also used : EntityType(ai.grakn.concept.EntityType) Keyspace(ai.grakn.Keyspace) Test(org.junit.Test)

Example 5 with EntityType

use of ai.grakn.concept.EntityType in project grakn by graknlabs.

the class EntityTest method whenCreatingAnEntityAndNotLinkingARequiredKey_Throw.

@Test
public void whenCreatingAnEntityAndNotLinkingARequiredKey_Throw() throws InvalidKBException {
    String resourceTypeId = "A Attribute Thing";
    EntityType entityType = tx.putEntityType("A Thing");
    AttributeType<String> attributeType = tx.putAttributeType(resourceTypeId, AttributeType.DataType.STRING);
    entityType.key(attributeType);
    entityType.addEntity();
    expectedException.expect(InvalidKBException.class);
    tx.commit();
}
Also used : EntityType(ai.grakn.concept.EntityType) Test(org.junit.Test)

Aggregations

EntityType (ai.grakn.concept.EntityType)168 Test (org.junit.Test)141 Role (ai.grakn.concept.Role)83 RelationshipType (ai.grakn.concept.RelationshipType)82 Entity (ai.grakn.concept.Entity)74 GraknTx (ai.grakn.GraknTx)45 Relationship (ai.grakn.concept.Relationship)21 Attribute (ai.grakn.concept.Attribute)19 AttributeType (ai.grakn.concept.AttributeType)17 ConceptId (ai.grakn.concept.ConceptId)17 Label (ai.grakn.concept.Label)17 Set (java.util.Set)16 HashSet (java.util.HashSet)15 Thing (ai.grakn.concept.Thing)14 GraknTxType (ai.grakn.GraknTxType)8 ArrayList (java.util.ArrayList)8 GraknSession (ai.grakn.GraknSession)7 Concept (ai.grakn.concept.Concept)7 HashMap (java.util.HashMap)7 Before (org.junit.Before)7