Search in sources :

Example 36 with EntityType

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

the class ValidatorTest method whenManuallyCreatingCorrectBinaryRelation_Commit.

@Test
public void whenManuallyCreatingCorrectBinaryRelation_Commit() throws InvalidKBException {
    Role characterBeingPlayed = tx.putRole("Character being played");
    Role personPlayingCharacter = tx.putRole("Person Playing Char");
    RelationshipType playsChar = tx.putRelationshipType("Plays Char").relates(characterBeingPlayed).relates(personPlayingCharacter);
    EntityType person = tx.putEntityType("person").plays(characterBeingPlayed).plays(personPlayingCharacter);
    EntityType character = tx.putEntityType("character").plays(characterBeingPlayed);
    Entity matt = person.addEntity();
    Entity walker = character.addEntity();
    playsChar.addRelationship().addRolePlayer(personPlayingCharacter, matt).addRolePlayer(characterBeingPlayed, walker);
    tx.commit();
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 37 with EntityType

use of ai.grakn.concept.EntityType 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 38 with EntityType

use of ai.grakn.concept.EntityType 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 39 with EntityType

use of ai.grakn.concept.EntityType 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 40 with EntityType

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

the class GraknTxPutPropertyTest method whenCallingPutEntityType_CreateATypeWithSuperTypeEntity.

@Property
public void whenCallingPutEntityType_CreateATypeWithSuperTypeEntity(@Open GraknTx graph, @Unused Label label) {
    EntityType entityType = graph.putEntityType(label);
    assertEquals(graph.admin().getMetaEntityType(), entityType.sup());
}
Also used : EntityType(ai.grakn.concept.EntityType) Property(com.pholser.junit.quickcheck.Property)

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