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())));
}
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();
}
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));
}
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));
}
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));
}
Aggregations