Search in sources :

Example 1 with EntityTypeImpl

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

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

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

the class GraknTxTest method whenBuildingAConceptFromAVertex_ReturnConcept.

@Test
public void whenBuildingAConceptFromAVertex_ReturnConcept() {
    EntityTypeImpl et = (EntityTypeImpl) tx.putEntityType("Sample Entity Type");
    assertEquals(et, tx.factory().buildConcept(et.vertex()));
}
Also used : EntityTypeImpl(ai.grakn.kb.internal.concept.EntityTypeImpl) Test(org.junit.Test)

Example 4 with EntityTypeImpl

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

the class GraknTxTest method whenShardingSuperNode_EnsureNewInstancesGoToNewShard.

@Test
public void whenShardingSuperNode_EnsureNewInstancesGoToNewShard() {
    EntityTypeImpl entityType = (EntityTypeImpl) tx.putEntityType("The Special Type");
    Shard s1 = entityType.currentShard();
    // Add 3 instances to first shard
    Entity s1_e1 = entityType.addEntity();
    Entity s1_e2 = entityType.addEntity();
    Entity s1_e3 = entityType.addEntity();
    tx.shard(entityType.getId());
    Shard s2 = entityType.currentShard();
    // Add 5 instances to second shard
    Entity s2_e1 = entityType.addEntity();
    Entity s2_e2 = entityType.addEntity();
    Entity s2_e3 = entityType.addEntity();
    Entity s2_e4 = entityType.addEntity();
    Entity s2_e5 = entityType.addEntity();
    tx.shard(entityType.getId());
    Shard s3 = entityType.currentShard();
    // Add 2 instances to 3rd shard
    Entity s3_e1 = entityType.addEntity();
    Entity s3_e2 = entityType.addEntity();
    // Check Type was sharded correctly
    assertThat(entityType.shards().collect(toSet()), containsInAnyOrder(s1, s2, s3));
    // Check shards have correct instances
    assertThat(s1.links().collect(toSet()), containsInAnyOrder(s1_e1, s1_e2, s1_e3));
    assertThat(s2.links().collect(toSet()), containsInAnyOrder(s2_e1, s2_e2, s2_e3, s2_e4, s2_e5));
    assertThat(s3.links().collect(toSet()), containsInAnyOrder(s3_e1, s3_e2));
}
Also used : Entity(ai.grakn.concept.Entity) EntityTypeImpl(ai.grakn.kb.internal.concept.EntityTypeImpl) Shard(ai.grakn.kb.internal.structure.Shard) Test(org.junit.Test)

Aggregations

EntityTypeImpl (ai.grakn.kb.internal.concept.EntityTypeImpl)4 Test (org.junit.Test)4 Entity (ai.grakn.concept.Entity)2 RelationshipType (ai.grakn.concept.RelationshipType)2 RelationshipImpl (ai.grakn.kb.internal.concept.RelationshipImpl)2 EntityType (ai.grakn.concept.EntityType)1 Role (ai.grakn.concept.Role)1 Thing (ai.grakn.concept.Thing)1 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 Shard (ai.grakn.kb.internal.structure.Shard)1