Search in sources :

Example 1 with Thing

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

the class CastingTest method whenUpdatingRelation_EnsureRolePlayersAreUpdated.

@Test
public void whenUpdatingRelation_EnsureRolePlayersAreUpdated() {
    Entity e1 = entityType.addEntity();
    Entity e3 = entityType.addEntity();
    RelationshipImpl relation = (RelationshipImpl) relationshipType.addRelationship().addRolePlayer(role1, e1);
    Set<Thing> things = relation.reified().get().castingsRelation().map(Casting::getRolePlayer).collect(Collectors.toSet());
    Set<Role> roles = relation.reified().get().castingsRelation().map(Casting::getRole).collect(Collectors.toSet());
    assertThat(things, containsInAnyOrder(e1));
    assertThat(roles, containsInAnyOrder(role1));
    // Now Update
    relation.addRolePlayer(role2, e1).addRolePlayer(role3, e3);
    things = relation.reified().get().castingsRelation().map(Casting::getRolePlayer).collect(Collectors.toSet());
    roles = relation.reified().get().castingsRelation().map(Casting::getRole).collect(Collectors.toSet());
    assertThat(things, containsInAnyOrder(e1, e3));
    assertThat(roles, containsInAnyOrder(role1, role2, role3));
}
Also used : Role(ai.grakn.concept.Role) Entity(ai.grakn.concept.Entity) RelationshipImpl(ai.grakn.kb.internal.concept.RelationshipImpl) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 2 with Thing

use of ai.grakn.concept.Thing 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 3 with Thing

use of ai.grakn.concept.Thing 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 4 with Thing

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

the class JsonMigratorMainTest method runAndAssertDataCorrect.

private void runAndAssertDataCorrect(String... args) {
    run(args);
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        EntityType personType = graph.getEntityType("person");
        assertEquals(1, personType.instances().count());
        Entity person = personType.instances().iterator().next();
        Entity address = getProperty(graph, person, "has-address").asEntity();
        Entity streetAddress = getProperty(graph, address, "address-has-street").asEntity();
        Attribute number = getResource(graph, streetAddress, Label.of("number"));
        assertEquals(21L, number.getValue());
        Collection<Thing> phoneNumbers = getProperties(graph, person, "has-phone");
        assertEquals(2, phoneNumbers.size());
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) Thing(ai.grakn.concept.Thing)

Example 5 with Thing

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

the class ValidatorTest method whenCommittingGraphWhichFollowsValidationRules_Commit.

@Test
public void whenCommittingGraphWhichFollowsValidationRules_Commit() {
    RelationshipType cast = tx.putRelationshipType("Cast");
    Role feature = tx.putRole("Feature");
    Role actor = tx.putRole("Actor");
    EntityType movie = tx.putEntityType("Movie");
    EntityType person = tx.putEntityType("Person");
    Thing pacino = person.addEntity();
    Thing godfather = movie.addEntity();
    EntityType genre = tx.putEntityType("Genre");
    Role movieOfGenre = tx.putRole("Movie of Genre");
    Role movieGenre = tx.putRole("Movie Genre");
    Thing crime = genre.addEntity();
    RelationshipType movieHasGenre = tx.putRelationshipType("Movie Has Genre");
    // Construction
    cast.relates(feature);
    cast.relates(actor);
    cast.addRelationship().addRolePlayer(feature, godfather).addRolePlayer(actor, pacino);
    movieHasGenre.addRelationship().addRolePlayer(movieOfGenre, godfather).addRolePlayer(movieGenre, crime);
    movieHasGenre.relates(movieOfGenre);
    movieHasGenre.relates(movieGenre);
    movie.plays(movieOfGenre);
    person.plays(actor);
    movie.plays(feature);
    genre.plays(movieGenre);
    tx.commit();
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Aggregations

Thing (ai.grakn.concept.Thing)47 Role (ai.grakn.concept.Role)30 Test (org.junit.Test)29 RelationshipType (ai.grakn.concept.RelationshipType)17 EntityType (ai.grakn.concept.EntityType)14 Relationship (ai.grakn.concept.Relationship)12 HashSet (java.util.HashSet)9 Set (java.util.Set)9 GraknTx (ai.grakn.GraknTx)7 Attribute (ai.grakn.concept.Attribute)7 ConceptId (ai.grakn.concept.ConceptId)7 AttributeType (ai.grakn.concept.AttributeType)6 Concept (ai.grakn.concept.Concept)6 Entity (ai.grakn.concept.Entity)6 Collectors (java.util.stream.Collectors)5 Label (ai.grakn.concept.Label)4 Schema (ai.grakn.util.Schema)4 Collection (java.util.Collection)4 Map (java.util.Map)4 GraknSession (ai.grakn.GraknSession)3