Search in sources :

Example 66 with Role

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

the class GraknTxTest method whenAttemptingToMutateReadOnlyGraph_Throw.

@Test
public void whenAttemptingToMutateReadOnlyGraph_Throw() {
    Keyspace keyspace = Keyspace.of("myreadonlygraph");
    String entityType = "My Entity Type";
    String roleType1 = "My Role Type 1";
    String roleType2 = "My Role Type 2";
    String relationType1 = "My Relationship Type 1";
    String relationType2 = "My Relationship Type 2";
    String resourceType = "My Attribute Type";
    // Fail Some Mutations
    tx = EmbeddedGraknSession.create(keyspace, Grakn.IN_MEMORY).open(GraknTxType.READ);
    failMutation(tx, () -> tx.putEntityType(entityType));
    failMutation(tx, () -> tx.putRole(roleType1));
    failMutation(tx, () -> tx.putRelationshipType(relationType1));
    // Pass some mutations
    tx.close();
    tx = EmbeddedGraknSession.create(keyspace, Grakn.IN_MEMORY).open(GraknTxType.WRITE);
    EntityType entityT = tx.putEntityType(entityType);
    entityT.addEntity();
    Role roleT1 = tx.putRole(roleType1);
    Role roleT2 = tx.putRole(roleType2);
    RelationshipType relationT1 = tx.putRelationshipType(relationType1).relates(roleT1);
    RelationshipType relationT2 = tx.putRelationshipType(relationType2).relates(roleT2);
    AttributeType<String> resourceT = tx.putAttributeType(resourceType, AttributeType.DataType.STRING);
    tx.commit();
    // Fail some mutations again
    tx = EmbeddedGraknSession.create(keyspace, Grakn.IN_MEMORY).open(GraknTxType.READ);
    failMutation(tx, entityT::addEntity);
    failMutation(tx, () -> resourceT.putAttribute("A resource"));
    failMutation(tx, () -> tx.putEntityType(entityType));
    failMutation(tx, () -> entityT.plays(roleT1));
    failMutation(tx, () -> relationT1.relates(roleT2));
    failMutation(tx, () -> relationT2.relates(roleT1));
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) Keyspace(ai.grakn.Keyspace) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 67 with Role

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

the class GrpcServerIT method whenDefiningASchema_TheSchemaIsDefined.

@Test
public void whenDefiningASchema_TheSchemaIsDefined() {
    try (GraknTx tx = remoteSession.open(GraknTxType.WRITE)) {
        EntityType animal = tx.putEntityType("animal");
        EntityType dog = tx.putEntityType("dog").sup(animal);
        EntityType cat = tx.putEntityType("cat");
        animal.sub(cat);
        cat.setLabel(Label.of("feline"));
        dog.setAbstract(true).setAbstract(false);
        cat.setAbstract(true);
        RelationshipType chases = tx.putRelationshipType("chases");
        Role chased = tx.putRole("chased");
        Role chaser = tx.putRole("chaser");
        chases.relates(chased).relates(chaser);
        Role pointlessRole = tx.putRole("pointless-role");
        tx.putRelationshipType("pointless").relates(pointlessRole);
        chases.relates(pointlessRole).deleteRelates(pointlessRole);
        dog.plays(chaser);
        cat.plays(chased);
        AttributeType<String> name = tx.putAttributeType("name", DataType.STRING);
        AttributeType<String> id = tx.putAttributeType("id", DataType.STRING).setRegex("(good|bad)-dog");
        AttributeType<Long> age = tx.putAttributeType("age", DataType.LONG);
        animal.attribute(name);
        animal.key(id);
        dog.attribute(age).deleteAttribute(age);
        cat.key(age).deleteKey(age);
        cat.plays(chaser).deletePlays(chaser);
        Entity dunstan = dog.addEntity();
        Attribute<String> dunstanId = id.putAttribute("good-dog");
        assertNotNull(dunstan.attributeRelationship(dunstanId));
        Attribute<String> dunstanName = name.putAttribute("Dunstan");
        dunstan.attribute(dunstanName).deleteAttribute(dunstanName);
        chases.addRelationship().addRolePlayer(chaser, dunstan);
        tx.commit();
    }
    try (GraknTx tx = localSession.open(GraknTxType.READ)) {
        EntityType animal = tx.getEntityType("animal");
        EntityType dog = tx.getEntityType("dog");
        EntityType cat = tx.getEntityType("feline");
        RelationshipType chases = tx.getRelationshipType("chases");
        Role chased = tx.getRole("chased");
        Role chaser = tx.getRole("chaser");
        AttributeType<String> name = tx.getAttributeType("name");
        AttributeType<String> id = tx.getAttributeType("id");
        Entity dunstan = Iterators.getOnlyElement(dog.instances().iterator());
        Relationship aChase = Iterators.getOnlyElement(chases.instances().iterator());
        assertEquals(animal, dog.sup());
        assertEquals(animal, cat.sup());
        assertEquals(ImmutableSet.of(chased, chaser), chases.relates().collect(toSet()));
        assertEquals(ImmutableSet.of(chaser), dog.plays().filter(role -> !role.isImplicit()).collect(toSet()));
        assertEquals(ImmutableSet.of(chased), cat.plays().filter(role -> !role.isImplicit()).collect(toSet()));
        assertEquals(ImmutableSet.of(name, id), animal.attributes().collect(toSet()));
        assertEquals(ImmutableSet.of(id), animal.keys().collect(toSet()));
        assertEquals(ImmutableSet.of(name, id), dog.attributes().collect(toSet()));
        assertEquals(ImmutableSet.of(id), dog.keys().collect(toSet()));
        assertEquals(ImmutableSet.of(name, id), cat.attributes().collect(toSet()));
        assertEquals(ImmutableSet.of(id), cat.keys().collect(toSet()));
        assertEquals("good-dog", Iterables.getOnlyElement(dunstan.keys(id).collect(toSet())).getValue());
        ImmutableMap<Role, ImmutableSet<?>> expectedRolePlayers = ImmutableMap.of(chaser, ImmutableSet.of(dunstan), chased, ImmutableSet.of());
        assertEquals(expectedRolePlayers, aChase.allRolePlayers());
        assertEquals("(good|bad)-dog", id.getRegex());
        assertFalse(dog.isAbstract());
        assertTrue(cat.isAbstract());
    }
}
Also used : Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) ImmutableSet(com.google.common.collect.ImmutableSet) Relationship(ai.grakn.concept.Relationship) Test(org.junit.Test)

Example 68 with Role

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

the class BatchExecutorClientIT method loader.

private BatchExecutorClient loader(int maxDelay) {
    // load schema
    try (EmbeddedGraknTx<?> graph = session.open(GraknTxType.WRITE)) {
        Role role = graph.putRole("some-role");
        graph.putRelationshipType("some-relationship").relates(role);
        EntityType nameTag = graph.putEntityType("name_tag");
        AttributeType<String> nameTagString = graph.putAttributeType("name_tag_string", AttributeType.DataType.STRING);
        AttributeType<String> nameTagId = graph.putAttributeType("name_tag_id", AttributeType.DataType.STRING);
        nameTag.attribute(nameTagString);
        nameTag.attribute(nameTagId);
        graph.commitSubmitNoLogs();
        GraknClient graknClient = GraknClient.of(engine.uri());
        return spy(BatchExecutorClient.newBuilder().taskClient(graknClient).maxDelay(maxDelay).requestLogEnabled(true).build());
    }
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) GraknClient(ai.grakn.client.GraknClient)

Example 69 with Role

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

the class GrpcServerIT method whenGettingARole_TheInformationOnTheRoleIsCorrect.

@Test
public void whenGettingARole_TheInformationOnTheRoleIsCorrect() {
    try (GraknTx remoteTx = remoteSession.open(GraknTxType.READ);
        GraknTx localTx = localSession.open(GraknTxType.READ)) {
        GetQuery query = remoteTx.graql().match(var("x").label("actor")).get();
        Role remoteConcept = query.stream().findAny().get().get("x").asRole();
        Role localConcept = localTx.getConcept(remoteConcept.getId()).asRole();
        assertEqualConcepts(localConcept, remoteConcept, Role::playedByTypes);
        assertEqualConcepts(localConcept, remoteConcept, Role::relationshipTypes);
    }
}
Also used : Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Example 70 with Role

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

the class DiagonalKB method buildExtensionalDB.

private void buildExtensionalDB(GraknTx tx, int n, int m) {
    Role relFrom = tx.getRole("rel-from");
    Role relTo = tx.getRole("rel-to");
    EntityType entity1 = tx.getEntityType("entity1");
    RelationshipType horizontal = tx.getRelationshipType("horizontal");
    RelationshipType vertical = tx.getRelationshipType("vertical");
    ConceptId[][] instanceIds = new ConceptId[n][m];
    long inserts = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            instanceIds[i][j] = putEntityWithResource(tx, "a" + i + "," + j, entity1, key).getId();
            inserts++;
            if (inserts % 100 == 0)
                System.out.println("inst inserts: " + inserts);
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (i < n - 1) {
                vertical.addRelationship().addRolePlayer(relFrom, tx.getConcept(instanceIds[i][j])).addRolePlayer(relTo, tx.getConcept(instanceIds[i + 1][j]));
                inserts++;
            }
            if (j < m - 1) {
                horizontal.addRelationship().addRolePlayer(relFrom, tx.getConcept(instanceIds[i][j])).addRolePlayer(relTo, tx.getConcept(instanceIds[i][j + 1]));
                inserts++;
            }
            if (inserts % 100 == 0)
                System.out.println("rel inserts: " + inserts);
        }
    }
    System.out.println("Extensional DB loaded.");
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId)

Aggregations

Role (ai.grakn.concept.Role)189 Test (org.junit.Test)124 RelationshipType (ai.grakn.concept.RelationshipType)114 EntityType (ai.grakn.concept.EntityType)92 Entity (ai.grakn.concept.Entity)55 GraknTx (ai.grakn.GraknTx)48 Relationship (ai.grakn.concept.Relationship)37 Set (java.util.Set)36 ConceptId (ai.grakn.concept.ConceptId)33 Label (ai.grakn.concept.Label)33 Thing (ai.grakn.concept.Thing)32 HashSet (java.util.HashSet)31 Var (ai.grakn.graql.Var)25 AttributeType (ai.grakn.concept.AttributeType)21 Concept (ai.grakn.concept.Concept)20 Schema (ai.grakn.util.Schema)17 Collectors (java.util.stream.Collectors)17 Attribute (ai.grakn.concept.Attribute)16 SchemaConcept (ai.grakn.concept.SchemaConcept)16 Type (ai.grakn.concept.Type)16