Search in sources :

Example 11 with Entity

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

the class RelationshipTest method whenAddingRolePlayerToRelation_RelationIsExpanded.

@Test
public void whenAddingRolePlayerToRelation_RelationIsExpanded() {
    Relationship relationship = relationshipType.addRelationship();
    Role role = tx.putRole("A role");
    Entity entity1 = type.addEntity();
    relationship.addRolePlayer(role, entity1);
    assertThat(relationship.allRolePlayers().keySet(), containsInAnyOrder(role1, role2, role3, role));
    assertThat(relationship.allRolePlayers().get(role), containsInAnyOrder(entity1));
}
Also used : Role(ai.grakn.concept.Role) Entity(ai.grakn.concept.Entity) Relationship(ai.grakn.concept.Relationship) Test(org.junit.Test)

Example 12 with Entity

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

the class RelationshipTest method whenRemovingRolePlayerFromRelationship_EnsureRolePlayerIsRemoved.

@Test
public void whenRemovingRolePlayerFromRelationship_EnsureRolePlayerIsRemoved() {
    Role role1 = tx.putRole("dark");
    Role role2 = tx.putRole("souls");
    RelationshipType relationshipType = tx.putRelationshipType("Dark Souls").relates(role1).relates(role2);
    EntityType entityType = tx.putEntityType("Dead Guys").plays(role1).plays(role2);
    Entity e1 = entityType.addEntity();
    Entity e2 = entityType.addEntity();
    Entity e3 = entityType.addEntity();
    Entity e4 = entityType.addEntity();
    Entity e5 = entityType.addEntity();
    Entity e6 = entityType.addEntity();
    Relationship relationship = relationshipType.addRelationship().addRolePlayer(role1, e1).addRolePlayer(role1, e2).addRolePlayer(role1, e3).addRolePlayer(role2, e4).addRolePlayer(role2, e5).addRolePlayer(role2, e6);
    assertThat(relationship.rolePlayers().collect(Collectors.toSet()), containsInAnyOrder(e1, e2, e3, e4, e5, e6));
    relationship.removeRolePlayer(role1, e2);
    relationship.removeRolePlayer(role2, e1);
    assertThat(relationship.rolePlayers().collect(Collectors.toSet()), containsInAnyOrder(e1, e3, e4, e5, e6));
    relationship.removeRolePlayer(role2, e6);
    assertThat(relationship.rolePlayers().collect(Collectors.toSet()), containsInAnyOrder(e1, e3, e4, e5));
}
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 13 with Entity

use of ai.grakn.concept.Entity 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 14 with Entity

use of ai.grakn.concept.Entity 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)

Example 15 with Entity

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

the class AddWithCommitBenchmark method addRelation.

@Benchmark
public void addRelation() {
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        Entity entity1 = entityType.addEntity();
        Entity entity2 = entityType.addEntity();
        relationshipType.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2);
        graph.commit();
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Benchmark(org.openjdk.jmh.annotations.Benchmark)

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