Search in sources :

Example 1 with RelationshipImpl

use of ai.grakn.kb.internal.concept.RelationshipImpl in project grakn by graknlabs.

the class CastingTest method whenCreatingRelation_EnsureRolePlayerContainsInstanceRoleTypeRelationTypeAndRelation.

@Test
public void whenCreatingRelation_EnsureRolePlayerContainsInstanceRoleTypeRelationTypeAndRelation() {
    Entity e1 = entityType.addEntity();
    RelationshipImpl relation = (RelationshipImpl) relationshipType.addRelationship().addRolePlayer(role1, e1);
    Set<Casting> castings = relation.reified().get().castingsRelation().collect(Collectors.toSet());
    castings.forEach(rolePlayer -> {
        assertEquals(e1, rolePlayer.getRolePlayer());
        assertEquals(role1, rolePlayer.getRole());
        assertEquals(relationshipType, rolePlayer.getRelationshipType());
        assertEquals(relation, rolePlayer.getRelationship());
    });
}
Also used : Entity(ai.grakn.concept.Entity) RelationshipImpl(ai.grakn.kb.internal.concept.RelationshipImpl) Test(org.junit.Test)

Example 2 with RelationshipImpl

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

use of ai.grakn.kb.internal.concept.RelationshipImpl in project grakn by graknlabs.

the class ValidateGlobalRulesTest method testValidatePlaysStructureUnique.

@Test
public void testValidatePlaysStructureUnique() {
    Role role1 = tx.putRole("role1");
    Role role2 = tx.putRole("role2");
    RelationshipType relationshipType = tx.putRelationshipType("rt").relates(role1).relates(role2);
    EntityType entityType = tx.putEntityType("et");
    ((EntityTypeImpl) entityType).plays(role1, true);
    ((EntityTypeImpl) entityType).plays(role2, false);
    Entity other1 = entityType.addEntity();
    Entity other2 = entityType.addEntity();
    EntityImpl entity = (EntityImpl) entityType.addEntity();
    RelationshipImpl relation1 = (RelationshipImpl) relationshipType.addRelationship().addRolePlayer(role2, other1).addRolePlayer(role1, entity);
    // Valid with only a single relation
    relation1.reified().get().castingsRelation().forEach(rolePlayer -> assertTrue(ValidateGlobalRules.validatePlaysAndRelatesStructure(rolePlayer).isEmpty()));
    RelationshipImpl relation2 = (RelationshipImpl) relationshipType.addRelationship().addRolePlayer(role2, other2).addRolePlayer(role1, entity);
    // Invalid with multiple relations
    relation1.reified().get().castingsRelation().forEach(rolePlayer -> {
        if (rolePlayer.getRole().equals(role1)) {
            assertFalse(ValidateGlobalRules.validatePlaysAndRelatesStructure(rolePlayer).isEmpty());
        }
    });
    relation2.reified().get().castingsRelation().forEach(rolePlayer -> {
        if (rolePlayer.getRole().equals(role1)) {
            assertFalse(ValidateGlobalRules.validatePlaysAndRelatesStructure(rolePlayer).isEmpty());
        }
    });
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) EntityImpl(ai.grakn.kb.internal.concept.EntityImpl) RelationshipType(ai.grakn.concept.RelationshipType) EntityTypeImpl(ai.grakn.kb.internal.concept.EntityTypeImpl) RelationshipImpl(ai.grakn.kb.internal.concept.RelationshipImpl) Test(org.junit.Test)

Example 4 with RelationshipImpl

use of ai.grakn.kb.internal.concept.RelationshipImpl in project grakn by graknlabs.

the class ValidateGlobalRulesTest method testValidatePlaysStructure.

@Test
public void testValidatePlaysStructure() throws Exception {
    EntityTypeImpl wolf = (EntityTypeImpl) tx.putEntityType("wolf");
    EntityTypeImpl creature = (EntityTypeImpl) tx.putEntityType("creature");
    EntityTypeImpl hunter = (EntityTypeImpl) tx.putEntityType("hunter");
    RelationshipType hunts = tx.putRelationshipType("hunts");
    RoleImpl witcher = (RoleImpl) tx.putRole("witcher");
    RoleImpl monster = (RoleImpl) tx.putRole("monster");
    Thing geralt = hunter.addEntity();
    ThingImpl werewolf = (ThingImpl) wolf.addEntity();
    RelationshipImpl assertion = (RelationshipImpl) hunts.addRelationship().addRolePlayer(witcher, geralt).addRolePlayer(monster, werewolf);
    assertion.reified().get().castingsRelation().forEach(rolePlayer -> assertFalse(ValidateGlobalRules.validatePlaysAndRelatesStructure(rolePlayer).isEmpty()));
    hunter.plays(witcher);
    boolean[] flags = { false, false };
    int count = 0;
    for (Casting casting : assertion.reified().get().castingsRelation().collect(Collectors.toSet())) {
        flags[count] = !ValidateGlobalRules.validatePlaysAndRelatesStructure(casting).isEmpty();
        count++;
    }
    assertTrue(flags[0] && flags[1]);
    wolf.sup(creature);
    creature.plays(monster);
    for (Casting casting : assertion.reified().get().castingsRelation().collect(Collectors.toSet())) {
        assertFalse(ValidateGlobalRules.validatePlaysAndRelatesStructure(casting).isEmpty());
    }
}
Also used : Casting(ai.grakn.kb.internal.structure.Casting) RoleImpl(ai.grakn.kb.internal.concept.RoleImpl) EntityTypeImpl(ai.grakn.kb.internal.concept.EntityTypeImpl) RelationshipType(ai.grakn.concept.RelationshipType) RelationshipImpl(ai.grakn.kb.internal.concept.RelationshipImpl) Thing(ai.grakn.concept.Thing) ThingImpl(ai.grakn.kb.internal.concept.ThingImpl) Test(org.junit.Test)

Example 5 with RelationshipImpl

use of ai.grakn.kb.internal.concept.RelationshipImpl in project grakn by graknlabs.

the class TxCacheTest method whenCreatingSuperTypes_EnsureLogContainsSubTypeCastings.

@Test
public void whenCreatingSuperTypes_EnsureLogContainsSubTypeCastings() {
    Role r1 = tx.putRole("r1");
    Role r2 = tx.putRole("r2");
    EntityType t1 = tx.putEntityType("t1").plays(r1).plays(r2);
    EntityType t2 = tx.putEntityType("t2");
    RelationshipType rt1 = tx.putRelationshipType("rel1").relates(r1).relates(r2);
    Entity i1 = t1.addEntity();
    Entity i2 = t1.addEntity();
    RelationshipImpl relation = (RelationshipImpl) rt1.addRelationship().addRolePlayer(r1, i1).addRolePlayer(r2, i2);
    tx.commit();
    tx = EmbeddedGraknSession.create(tx.keyspace(), Grakn.IN_MEMORY).open(GraknTxType.WRITE);
    assertThat(tx.txCache().getModifiedCastings(), is(empty()));
    t1.sup(t2);
    assertTrue(tx.txCache().getModifiedCastings().containsAll(relation.reified().get().castingsRelation().collect(toSet())));
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) RelationshipImpl(ai.grakn.kb.internal.concept.RelationshipImpl) Test(org.junit.Test)

Aggregations

RelationshipImpl (ai.grakn.kb.internal.concept.RelationshipImpl)5 Test (org.junit.Test)5 Entity (ai.grakn.concept.Entity)4 RelationshipType (ai.grakn.concept.RelationshipType)3 Role (ai.grakn.concept.Role)3 EntityType (ai.grakn.concept.EntityType)2 Thing (ai.grakn.concept.Thing)2 EntityTypeImpl (ai.grakn.kb.internal.concept.EntityTypeImpl)2 EntityImpl (ai.grakn.kb.internal.concept.EntityImpl)1 RoleImpl (ai.grakn.kb.internal.concept.RoleImpl)1 ThingImpl (ai.grakn.kb.internal.concept.ThingImpl)1 Casting (ai.grakn.kb.internal.structure.Casting)1