Search in sources :

Example 41 with Role

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

the class ValidatorTest method whenARoleInARelationIsPlayedAZillionTimes_TheGraphIsValid.

@Test
public void whenARoleInARelationIsPlayedAZillionTimes_TheGraphIsValid() {
    Role role1 = tx.putRole("role-1");
    Role role2 = tx.putRole("role-2");
    RelationshipType relationshipType = tx.putRelationshipType("my-relationship").relates(role1).relates(role2);
    EntityType entityType = tx.putEntityType("my-entity").plays(role1);
    Relationship relationship = relationshipType.addRelationship();
    Set<Thing> things = new HashSet<>();
    int oneZillion = 100;
    for (int i = 0; i < oneZillion; i++) {
        Thing thing = entityType.addEntity();
        things.add(thing);
        relationship.addRolePlayer(role1, thing);
    }
    assertEquals(things, relationship.rolePlayers(role1).collect(toSet()));
    tx.commit();
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 42 with Role

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

the class ValidatorTest method whenARelationTypeHasASubTypeHierarchy_EnsureThatWhenARelationTypeHasMatchingRoleTypes2.

@Test
public void whenARelationTypeHasASubTypeHierarchy_EnsureThatWhenARelationTypeHasMatchingRoleTypes2() throws InvalidKBException {
    Role relative = tx.putRole("relative");
    Role parent = tx.putRole("parent").sup(relative);
    Role father = tx.putRole("father").sup(parent);
    Role mother = tx.putRole("mother").sup(parent);
    Role pChild = tx.putRole("pChild").sup(relative);
    Role fmChild = tx.putRole("fChild").sup(pChild);
    // This is to bypass a specific validation rule
    tx.putRelationshipType("filler").relates(relative);
    tx.putEntityType("animal").plays(relative).plays(parent).plays(father).plays(mother).plays(pChild).plays(fmChild);
    RelationshipType parenthood = tx.putRelationshipType("parenthood").relates(parent).relates(pChild);
    tx.putRelationshipType("fathermotherhood").sup(parenthood).relates(father).relates(mother).relates(fmChild);
    tx.commit();
}
Also used : Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 43 with Role

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

the class ValidatorTest method whenRemovingInvalidRolePlayers_EnsureValidationPasses.

@Test
public void whenRemovingInvalidRolePlayers_EnsureValidationPasses() {
    Role chased = tx.putRole("chased");
    Role chaser = tx.putRole("chaser");
    RelationshipType chases = tx.putRelationshipType("chases").relates(chased).relates(chaser);
    EntityType puppy = tx.putEntityType("puppy").plays(chaser);
    Entity dunstan = puppy.addEntity();
    Relationship rel = chases.addRelationship();
    rel.addRolePlayer(chaser, dunstan);
    rel.addRolePlayer(chased, dunstan).removeRolePlayer(chased, dunstan);
    tx.commit();
}
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 44 with Role

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

the class ValidatorTest method whenCreatingRelationWithSubTypeHierarchyAndNoMatchingRoleTypeHierarchy_Throw1.

@Test
public void whenCreatingRelationWithSubTypeHierarchyAndNoMatchingRoleTypeHierarchy_Throw1() throws InvalidKBException {
    Role pChild = tx.putRole("pChild");
    Role fChild = tx.putRole("fChild").sup(pChild);
    Role parent = tx.putRole("parent");
    Role father = tx.putRole("father").sup(parent);
    Role inContext = tx.putRole("in-context");
    tx.putEntityType("animal").plays(parent).plays(father).plays(pChild).plays(fChild);
    tx.putEntityType("context").plays(inContext);
    RelationshipType parenthood = tx.putRelationshipType("parenthood").relates(parent).relates(pChild);
    RelationshipType fatherhood = tx.putRelationshipType("fatherhood").sup(parenthood).relates(father).relates(fChild).relates(inContext);
    expectedException.expect(InvalidKBException.class);
    expectedException.expectMessage(ErrorMessage.VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(inContext.getLabel(), fatherhood.getLabel(), "super", "super", parenthood.getLabel()));
    tx.commit();
}
Also used : Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 45 with Role

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

the class ValidatorTest method whenManuallyCreatingCorrectBinaryRelation_Commit.

@Test
public void whenManuallyCreatingCorrectBinaryRelation_Commit() throws InvalidKBException {
    Role characterBeingPlayed = tx.putRole("Character being played");
    Role personPlayingCharacter = tx.putRole("Person Playing Char");
    RelationshipType playsChar = tx.putRelationshipType("Plays Char").relates(characterBeingPlayed).relates(personPlayingCharacter);
    EntityType person = tx.putEntityType("person").plays(characterBeingPlayed).plays(personPlayingCharacter);
    EntityType character = tx.putEntityType("character").plays(characterBeingPlayed);
    Entity matt = person.addEntity();
    Entity walker = character.addEntity();
    playsChar.addRelationship().addRolePlayer(personPlayingCharacter, matt).addRolePlayer(characterBeingPlayed, walker);
    tx.commit();
}
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

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