Search in sources :

Example 6 with RelationshipType

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

the class RelationshipTest method whenDeletingRelations_EnsureCastingsRemain.

@Test
public void whenDeletingRelations_EnsureCastingsRemain() {
    Role entityRole = tx.putRole("Entity Role");
    Role degreeRole = tx.putRole("Degree Role");
    EntityType entityType = tx.putEntityType("Entity Type").plays(entityRole);
    AttributeType<Long> degreeType = tx.putAttributeType("Attribute Type", AttributeType.DataType.LONG).plays(degreeRole);
    RelationshipType hasDegree = tx.putRelationshipType("Has Degree").relates(entityRole).relates(degreeRole);
    Entity entity = entityType.addEntity();
    Attribute<Long> degree1 = degreeType.putAttribute(100L);
    Attribute<Long> degree2 = degreeType.putAttribute(101L);
    Relationship relationship1 = hasDegree.addRelationship().addRolePlayer(entityRole, entity).addRolePlayer(degreeRole, degree1);
    hasDegree.addRelationship().addRolePlayer(entityRole, entity).addRolePlayer(degreeRole, degree2);
    assertEquals(2, entity.relationships().count());
    relationship1.delete();
    assertEquals(1, entity.relationships().count());
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 7 with RelationshipType

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

the class RelationshipTest method ensureRelationToStringContainsRolePlayerInformation.

@Test
public void ensureRelationToStringContainsRolePlayerInformation() {
    Role role1 = tx.putRole("role type 1");
    Role role2 = tx.putRole("role type 2");
    RelationshipType relationshipType = tx.putRelationshipType("A relationship Type").relates(role1).relates(role2);
    EntityType type = tx.putEntityType("concept type").plays(role1).plays(role2);
    Thing thing1 = type.addEntity();
    Thing thing2 = type.addEntity();
    Relationship relationship = relationshipType.addRelationship().addRolePlayer(role1, thing1).addRolePlayer(role2, thing2);
    String mainDescription = "ID [" + relationship.getId() + "] Type [" + relationship.type().getLabel() + "] Roles and Role Players:";
    String rolerp1 = "    Role [" + role1.getLabel() + "] played by [" + thing1.getId() + ",]";
    String rolerp2 = "    Role [" + role2.getLabel() + "] played by [" + thing2.getId() + ",]";
    assertTrue("Relationship toString missing main description", relationship.toString().contains(mainDescription));
    assertTrue("Relationship toString missing role and role player definition", relationship.toString().contains(rolerp1));
    assertTrue("Relationship toString missing role and role player definition", relationship.toString().contains(rolerp2));
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Matchers.containsString(org.hamcrest.Matchers.containsString) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 8 with RelationshipType

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

the class RelationshipTest method whenAttributeLinkedToRelationshipIsInferred_EnsureItIsMarkedAsInferred.

@Test
public void whenAttributeLinkedToRelationshipIsInferred_EnsureItIsMarkedAsInferred() {
    AttributeType attributeType = tx.putAttributeType("Another thing of sorts", AttributeType.DataType.STRING);
    RelationshipType relationshipType = tx.putRelationshipType("A thing of sorts").attribute(attributeType);
    Attribute attribute = attributeType.putAttribute("Things");
    Relationship relationship = relationshipType.addRelationship();
    RelationshipImpl.from(relationship).attributeInferred(attribute);
    assertTrue(relationship.relationships().findAny().get().isInferred());
}
Also used : Attribute(ai.grakn.concept.Attribute) AttributeType(ai.grakn.concept.AttributeType) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 9 with RelationshipType

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

the class RoleTest method whenAddingRoleTypeToMultipleRelationTypes_EnsureItLinkedToBothRelationTypes.

@Test
public void whenAddingRoleTypeToMultipleRelationTypes_EnsureItLinkedToBothRelationTypes() throws InvalidKBException {
    Role roleA = tx.putRole("roleA");
    Role roleB = tx.putRole("roleB");
    relationshipType.relates(roleA).relates(role);
    RelationshipType relationshipType2 = tx.putRelationshipType("relationshipType2").relates(roleB).relates(role);
    tx.commit();
    assertThat(roleA.relationshipTypes().collect(toSet()), containsInAnyOrder(relationshipType));
    assertThat(roleB.relationshipTypes().collect(toSet()), containsInAnyOrder(relationshipType2));
    assertThat(role.relationshipTypes().collect(toSet()), containsInAnyOrder(relationshipType, relationshipType2));
}
Also used : Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 10 with RelationshipType

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

the class RoleTest method whenDeletingRoleTypeWithRolePlayers_Throw.

@Test
public void whenDeletingRoleTypeWithRolePlayers_Throw() {
    Role roleA = tx.putRole("roleA");
    Role roleB = tx.putRole("roleB");
    RelationshipType relationshipType = tx.putRelationshipType("relationTypes").relates(roleA).relates(roleB);
    EntityType entityType = tx.putEntityType("entityType").plays(roleA).plays(roleB);
    Entity a = entityType.addEntity();
    Entity b = entityType.addEntity();
    relationshipType.addRelationship().addRolePlayer(roleA, a).addRolePlayer(roleB, b);
    expectedException.expect(GraknTxOperationException.class);
    expectedException.expectMessage(GraknTxOperationException.cannotBeDeleted(roleA).getMessage());
    roleA.delete();
}
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)

Aggregations

RelationshipType (ai.grakn.concept.RelationshipType)127 Role (ai.grakn.concept.Role)105 Test (org.junit.Test)91 EntityType (ai.grakn.concept.EntityType)80 Entity (ai.grakn.concept.Entity)52 GraknTx (ai.grakn.GraknTx)39 Relationship (ai.grakn.concept.Relationship)25 ConceptId (ai.grakn.concept.ConceptId)23 Label (ai.grakn.concept.Label)21 HashSet (java.util.HashSet)20 Set (java.util.Set)20 AttributeType (ai.grakn.concept.AttributeType)17 Thing (ai.grakn.concept.Thing)17 Attribute (ai.grakn.concept.Attribute)16 Schema (ai.grakn.util.Schema)12 Collectors (java.util.stream.Collectors)12 List (java.util.List)11 GraknSession (ai.grakn.GraknSession)10 GraknTxType (ai.grakn.GraknTxType)10 Concept (ai.grakn.concept.Concept)10