Search in sources :

Example 41 with Entity

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

the class EntityTypeTest method whenGettingTheInstancesOfType_ReturnAllInstances.

@Test
public void whenGettingTheInstancesOfType_ReturnAllInstances() {
    EntityType e1 = tx.putEntityType("e1");
    EntityType e2 = tx.putEntityType("e2").sup(e1);
    EntityType e3 = tx.putEntityType("e3").sup(e1);
    Entity e2_child1 = e2.addEntity();
    Entity e2_child2 = e2.addEntity();
    Entity e3_child1 = e3.addEntity();
    Entity e3_child2 = e3.addEntity();
    Entity e3_child3 = e3.addEntity();
    assertThat(e1.instances().collect(toSet()), containsInAnyOrder(e2_child1, e2_child2, e3_child1, e3_child2, e3_child3));
    assertThat(e2.instances().collect(toSet()), containsInAnyOrder(e2_child1, e2_child2));
    assertThat(e3.instances().collect(toSet()), containsInAnyOrder(e3_child1, e3_child2, e3_child3));
}
Also used : EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Test(org.junit.Test)

Example 42 with Entity

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

the class RemoteConceptsTest method whenCallingAddEntity_ExecuteAConceptMethod.

@Test
public void whenCallingAddEntity_ExecuteAConceptMethod() {
    Entity entity = RemoteConcepts.createEntity(tx, A);
    mockConceptMethod(ConceptMethods.ADD_ENTITY, entity);
    assertEquals(entity, entityType.addEntity());
}
Also used : Entity(ai.grakn.concept.Entity) Test(org.junit.Test)

Example 43 with Entity

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

the class GrpcServerTest method whenRemovingRolePlayer_RolePlayerIsRemoved.

@Test
public void whenRemovingRolePlayer_RolePlayerIsRemoved() throws InterruptedException {
    ConceptId conceptId = ConceptId.of("V123456");
    ConceptId roleId = ConceptId.of("ROLE");
    ConceptId playerId = ConceptId.of("PLAYER");
    Concept concept = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(tx.getConcept(conceptId)).thenReturn(concept);
    when(concept.isRelationship()).thenReturn(true);
    Role role = mock(Role.class, RETURNS_DEEP_STUBS);
    when(tx.getConcept(roleId)).thenReturn(role);
    when(role.isRole()).thenReturn(true);
    when(role.asRole()).thenReturn(role);
    when(role.getId()).thenReturn(roleId);
    Entity player = mock(Entity.class, RETURNS_DEEP_STUBS);
    when(tx.getConcept(playerId)).thenReturn(player);
    when(player.isEntity()).thenReturn(true);
    when(player.asEntity()).thenReturn(player);
    when(player.isThing()).thenReturn(true);
    when(player.asThing()).thenReturn(player);
    when(player.getId()).thenReturn(playerId);
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.READ));
        tx.receive().ok();
        ConceptMethod<Void> conceptMethod = ConceptMethods.removeRolePlayer(RolePlayer.create(role, player));
        tx.send(GrpcUtil.runConceptMethodRequest(conceptId, conceptMethod));
        tx.receive().ok();
        verify(concept.asRelationship()).removeRolePlayer(role, player);
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) Role(ai.grakn.concept.Role) Entity(ai.grakn.concept.Entity) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 44 with Entity

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

the class ValidateGlobalRulesTest method testValidatePlaysStructureUnique.

@Test
public void testValidatePlaysStructureUnique() {
    Role role1 = tx.putRole("role1");
    Role role2 = tx.putRole("role2");
    RelationshipType relationshipType = tx.putRelationshipType("rt").relates(role1).relates(role2);
    EntityType entityType = tx.putEntityType("et");
    ((EntityTypeImpl) entityType).plays(role1, true);
    ((EntityTypeImpl) entityType).plays(role2, false);
    Entity other1 = entityType.addEntity();
    Entity other2 = entityType.addEntity();
    EntityImpl entity = (EntityImpl) entityType.addEntity();
    RelationshipImpl relation1 = (RelationshipImpl) relationshipType.addRelationship().addRolePlayer(role2, other1).addRolePlayer(role1, entity);
    // Valid with only a single relation
    relation1.reified().get().castingsRelation().forEach(rolePlayer -> assertTrue(ValidateGlobalRules.validatePlaysAndRelatesStructure(rolePlayer).isEmpty()));
    RelationshipImpl relation2 = (RelationshipImpl) relationshipType.addRelationship().addRolePlayer(role2, other2).addRolePlayer(role1, entity);
    // Invalid with multiple relations
    relation1.reified().get().castingsRelation().forEach(rolePlayer -> {
        if (rolePlayer.getRole().equals(role1)) {
            assertFalse(ValidateGlobalRules.validatePlaysAndRelatesStructure(rolePlayer).isEmpty());
        }
    });
    relation2.reified().get().castingsRelation().forEach(rolePlayer -> {
        if (rolePlayer.getRole().equals(role1)) {
            assertFalse(ValidateGlobalRules.validatePlaysAndRelatesStructure(rolePlayer).isEmpty());
        }
    });
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) EntityImpl(ai.grakn.kb.internal.concept.EntityImpl) RelationshipType(ai.grakn.concept.RelationshipType) EntityTypeImpl(ai.grakn.kb.internal.concept.EntityTypeImpl) RelationshipImpl(ai.grakn.kb.internal.concept.RelationshipImpl) Test(org.junit.Test)

Example 45 with Entity

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

Aggregations

Entity (ai.grakn.concept.Entity)99 Test (org.junit.Test)81 EntityType (ai.grakn.concept.EntityType)74 Role (ai.grakn.concept.Role)53 RelationshipType (ai.grakn.concept.RelationshipType)50 GraknTx (ai.grakn.GraknTx)44 Attribute (ai.grakn.concept.Attribute)18 Set (java.util.Set)16 Relationship (ai.grakn.concept.Relationship)14 Label (ai.grakn.concept.Label)11 ConceptId (ai.grakn.concept.ConceptId)10 HashSet (java.util.HashSet)10 ArrayList (java.util.ArrayList)8 AttributeType (ai.grakn.concept.AttributeType)7 List (java.util.List)7 GraknSession (ai.grakn.GraknSession)6 Before (org.junit.Before)6 GraknTxType (ai.grakn.GraknTxType)5 Concept (ai.grakn.concept.Concept)5 GraqlQueryException (ai.grakn.exception.GraqlQueryException)5