Search in sources :

Example 36 with Entity

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

the class AttributeTest method whenLinkingResourcesToThings_EnsureTheRelationIsAnEdge.

@Test
public void whenLinkingResourcesToThings_EnsureTheRelationIsAnEdge() {
    AttributeType<String> attributeType = tx.putAttributeType("My attribute type", AttributeType.DataType.STRING);
    Attribute<String> attribute = attributeType.putAttribute("A String");
    EntityType entityType = tx.putEntityType("My entity type").attribute(attributeType);
    Entity entity = entityType.addEntity();
    entity.attribute(attribute);
    RelationshipStructure relationshipStructure = RelationshipImpl.from(Iterables.getOnlyElement(entity.relationships().collect(toSet()))).structure();
    assertThat(relationshipStructure, instanceOf(RelationshipEdge.class));
    assertTrue("Edge Relationship id not starting with [" + Schema.PREFIX_EDGE + "]", relationshipStructure.getId().getValue().startsWith(Schema.PREFIX_EDGE));
    assertEquals(entity, attribute.owner());
    assertThat(entity.attributes().collect(toSet()), containsInAnyOrder(attribute));
}
Also used : EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Test(org.junit.Test)

Example 37 with Entity

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

the class AttributeTest method whenInsertingAThingWithTwoKeys_Throw.

@Test
public void whenInsertingAThingWithTwoKeys_Throw() {
    AttributeType<String> attributeType = tx.putAttributeType("Key Thingy", AttributeType.DataType.STRING);
    EntityType entityType = tx.putEntityType("Entity Type Thingy").key(attributeType);
    Entity entity = entityType.addEntity();
    Attribute<String> key1 = attributeType.putAttribute("key 1");
    Attribute<String> key2 = attributeType.putAttribute("key 2");
    entity.attribute(key1);
    entity.attribute(key2);
    expectedException.expect(InvalidKBException.class);
    tx.commit();
}
Also used : EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Test(org.junit.Test)

Example 38 with Entity

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

the class AttributeTest method whenAddingRolePlayerToRelationEdge_RelationAutomaticallyReifies.

@Test
public void whenAddingRolePlayerToRelationEdge_RelationAutomaticallyReifies() {
    // Create boring attribute which creates a relation edge
    AttributeType<String> attributeType = tx.putAttributeType("My attribute type", AttributeType.DataType.STRING);
    Attribute<String> attribute = attributeType.putAttribute("A String");
    EntityType entityType = tx.putEntityType("My entity type").attribute(attributeType);
    Entity entity = entityType.addEntity();
    entity.attribute(attribute);
    RelationshipImpl relation = RelationshipImpl.from(entity.relationships().iterator().next());
    // Check it's a relation edge.
    RelationshipStructure relationshipStructureBefore = relation.structure();
    assertThat(relationshipStructureBefore, instanceOf(RelationshipEdge.class));
    // Get the roles and role players via the relation edge:
    Map<Role, Set<Thing>> allRolePlayerBefore = relationshipStructureBefore.allRolePlayers();
    // Expand Schema to allow new role
    Role newRole = tx.putRole("My New Role");
    entityType.plays(newRole);
    relation.type().relates(newRole);
    Entity newEntity = entityType.addEntity();
    // Now actually add the new role player
    relation.addRolePlayer(newRole, newEntity);
    // Check it's a relation reified now.
    RelationshipStructure relationshipStructureAfter = relation.structure();
    assertThat(relationshipStructureAfter, instanceOf(RelationshipReified.class));
    // Check IDs are equal
    assertEquals(relationshipStructureBefore.getId(), relation.getId());
    assertEquals(relationshipStructureBefore.getId(), relationshipStructureAfter.getId());
    // Check Role Players have been transferred
    allRolePlayerBefore.forEach((role, player) -> assertEquals(player, relationshipStructureAfter.rolePlayers(role).collect(toSet())));
    // Check Type Has Been Transferred
    assertEquals(relationshipStructureBefore.type(), relationshipStructureAfter.type());
    // Check new role player has been added as well
    assertEquals(newEntity, Iterables.getOnlyElement(relationshipStructureAfter.rolePlayers(newRole).collect(toSet())));
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) Entity(ai.grakn.concept.Entity) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) Test(org.junit.Test)

Example 39 with Entity

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

the class EntityTypePropertyTest method whenAddingAnEntity_TheEntityHasNoResources.

@Property
public void whenAddingAnEntity_TheEntityHasNoResources(@NonMeta @NonAbstract EntityType type) {
    Entity entity = type.addEntity();
    assertThat(entity.attributes().collect(toSet()), empty());
}
Also used : Entity(ai.grakn.concept.Entity) Property(com.pholser.junit.quickcheck.Property)

Example 40 with Entity

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

the class EntityTypePropertyTest method whenAddingAnEntity_TheEntityIsInNoRelations.

@Property
public void whenAddingAnEntity_TheEntityIsInNoRelations(@NonMeta @NonAbstract EntityType type) {
    Entity entity = type.addEntity();
    assertThat(entity.relationships().collect(toSet()), empty());
}
Also used : Entity(ai.grakn.concept.Entity) Property(com.pholser.junit.quickcheck.Property)

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