Search in sources :

Example 6 with Role

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

the class MigratorTestUtils method getResources.

public static Stream<Attribute> getResources(GraknTx graph, Thing thing, Label label) {
    Role roleOwner = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(label));
    Role roleOther = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(label));
    Stream<Relationship> relations = thing.relationships(roleOwner);
    return relations.flatMap(r -> r.rolePlayers(roleOther)).map(Concept::asAttribute);
}
Also used : Role(ai.grakn.concept.Role) InvalidKBException(ai.grakn.exception.InvalidKBException) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) Relationship(ai.grakn.concept.Relationship) GraknTxType(ai.grakn.GraknTxType) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) Schema(ai.grakn.util.Schema) TestCase.assertEquals(junit.framework.TestCase.assertEquals) Concept(ai.grakn.concept.Concept) Relationship(ai.grakn.concept.Relationship)

Example 7 with Role

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

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

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

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

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