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());
}
});
}
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());
}
}
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()));
}
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));
}
Aggregations