Search in sources :

Example 46 with Role

use of ai.grakn.concept.Role 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 47 with Role

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

the class ValidatorTest method whenCommittingRelationWithoutSpecifyingSchema_ThrowOnCommit.

@Test
public void whenCommittingRelationWithoutSpecifyingSchema_ThrowOnCommit() {
    EntityType fakeType = tx.putEntityType("Fake Concept");
    RelationshipType relationshipType = tx.putRelationshipType("kicks");
    Role kicker = tx.putRole("kicker");
    Role kickee = tx.putRole("kickee");
    Thing kyle = fakeType.addEntity();
    Thing icke = fakeType.addEntity();
    relationshipType.addRelationship().addRolePlayer(kicker, kyle).addRolePlayer(kickee, icke);
    String error1 = ErrorMessage.VALIDATION_CASTING.getMessage(kyle.type().getLabel(), kyle.getId(), kicker.getLabel());
    String error2 = ErrorMessage.VALIDATION_CASTING.getMessage(icke.type().getLabel(), icke.getId(), kickee.getLabel());
    expectedException.expect(InvalidKBException.class);
    expectedException.expectMessage(allOf(containsString(error1), containsString(error2)));
    tx.commit();
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Matchers.containsString(org.hamcrest.Matchers.containsString) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 48 with Role

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

the class EntityTypeTest method whenRemovingRoleFromEntityType_TheRoleCanNoLongerBePlayed.

@Test
public void whenRemovingRoleFromEntityType_TheRoleCanNoLongerBePlayed() {
    Role role1 = tx.putRole("A Role 1");
    Role role2 = tx.putRole("A Role 2");
    EntityType type = tx.putEntityType("A Concept Type").plays(role1).plays(role2);
    assertThat(type.plays().collect(toSet()), containsInAnyOrder(role1, role2));
    type.deletePlays(role1);
    assertThat(type.plays().collect(toSet()), containsInAnyOrder(role2));
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Test(org.junit.Test)

Example 49 with Role

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

the class RemoteConceptsTest method whenCallingDeleteRelates_ExecuteAConceptMethod.

@Test
public void whenCallingDeleteRelates_ExecuteAConceptMethod() {
    Role role = RemoteConcepts.createRole(tx, A);
    assertEquals(relationshipType, relationshipType.deleteRelates(role));
    verifyConceptMethodCalled(ConceptMethods.unsetRelatedRole(role));
}
Also used : Role(ai.grakn.concept.Role) Test(org.junit.Test)

Example 50 with Role

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

the class RemoteConceptsTest method whenCallingRelationshipsWithRoles_GetTheExpectedResult.

@Test
public void whenCallingRelationshipsWithRoles_GetTheExpectedResult() {
    Role foo = RemoteConcepts.createRole(tx, ConceptId.of("foo"));
    Role bar = RemoteConcepts.createRole(tx, ConceptId.of("bar"));
    Role baz = RemoteConcepts.createRole(tx, ConceptId.of("baz"));
    Relationship a = RemoteConcepts.createRelationship(tx, A);
    Relationship b = RemoteConcepts.createRelationship(tx, B);
    Relationship c = RemoteConcepts.createRelationship(tx, C);
    mockConceptMethod(ConceptMethods.getRelationshipsByRoles(foo, bar, baz), Stream.of(a, b, c));
    assertThat(thing.relationships(foo, bar, baz).collect(toSet()), containsInAnyOrder(a, b, c));
}
Also used : Role(ai.grakn.concept.Role) Relationship(ai.grakn.concept.Relationship) Test(org.junit.Test)

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