use of ai.grakn.concept.Role in project grakn by graknlabs.
the class BenchmarkTests method nonRecursiveChainOfRules.
/**
* Executes a scalability test defined in terms of the number of rules in the system. Creates a simple rule chain:
*
* R_i(x, y) := R_{i-1}(x, y); i e [1, N]
*
* with a single initial relation instance R_0(a ,b)
*/
@Test
public void nonRecursiveChainOfRules() {
final int N = 200;
LOG.debug(new Object() {
}.getClass().getEnclosingMethod().getName());
GraknSession graknSession = sessionContext.newSession();
// NB: loading data here as defining it as KB and using graql api leads to circular dependencies
try (GraknTx tx = graknSession.open(GraknTxType.WRITE)) {
Role fromRole = tx.putRole("fromRole");
Role toRole = tx.putRole("toRole");
RelationshipType relation0 = tx.putRelationshipType("relation0").relates(fromRole).relates(toRole);
for (int i = 1; i <= N; i++) {
tx.putRelationshipType("relation" + i).relates(fromRole).relates(toRole);
}
EntityType genericEntity = tx.putEntityType("genericEntity").plays(fromRole).plays(toRole);
Entity fromEntity = genericEntity.addEntity();
Entity toEntity = genericEntity.addEntity();
relation0.addRelationship().addRolePlayer(fromRole, fromEntity).addRolePlayer(toRole, toEntity);
for (int i = 1; i <= N; i++) {
Var fromVar = Graql.var().asUserDefined();
Var toVar = Graql.var().asUserDefined();
VarPattern rulePattern = Graql.label("rule" + i).when(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa("relation" + (i - 1)))).then(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa("relation" + i)));
tx.graql().define(rulePattern).execute();
}
tx.commit();
}
try (GraknTx tx = graknSession.open(GraknTxType.READ)) {
final long limit = 1;
String queryPattern = "(fromRole: $x, toRole: $y) isa relation" + N + ";";
String queryString = "match " + queryPattern + " get;";
String limitedQueryString = "match " + queryPattern + "limit " + limit + ";" + "get;";
assertEquals(executeQuery(queryString, tx, "full").size(), limit);
assertEquals(executeQuery(limitedQueryString, tx, "limit").size(), limit);
}
}
use of ai.grakn.concept.Role in project grakn by graknlabs.
the class AtomicTest method testRoleInference_RelationHasVerticalRoleHierarchy.
// relation relates a single role so instead of assigning metarole this role should be assigned
@Test
public void testRoleInference_RelationHasVerticalRoleHierarchy() {
EmbeddedGraknTx<?> graph = ruleApplicabilitySet.tx();
String relationString = "{($x, $y) isa reifying-relation;}";
RelationshipAtom relation = (RelationshipAtom) ReasonerQueries.atomic(conjunction(relationString, graph), graph).getAtom();
ImmutableSetMultimap<Role, Var> roleMap = ImmutableSetMultimap.of(graph.getRole("role1"), var("x"), graph.getRole("role1"), var("y"));
assertEquals(roleMap, roleSetMap(relation.getRoleVarMap()));
}
use of ai.grakn.concept.Role in project grakn by graknlabs.
the class AtomicTest method testRuleApplicability_AmbiguousRoleMapping.
@Test
public void testRuleApplicability_AmbiguousRoleMapping() {
EmbeddedGraknTx<?> graph = ruleApplicabilitySet.tx();
// although singleRoleEntity plays only one role it can also play an implicit role of the resource so mapping ambiguous
String relationString = "{($x, $y, $z);$x isa singleRoleEntity; $y isa anotherTwoRoleEntity; $z isa twoRoleEntity;}";
RelationshipAtom relation = (RelationshipAtom) ReasonerQueries.atomic(conjunction(relationString, graph), graph).getAtom();
ImmutableSetMultimap<Role, Var> roleMap = ImmutableSetMultimap.of(graph.getRole("role"), var("x"), graph.getRole("role"), var("y"), graph.getRole("role"), var("z"));
assertEquals(roleMap, roleSetMap((relation.getRoleVarMap())));
assertEquals(3, relation.getApplicableRules().count());
}
use of ai.grakn.concept.Role in project grakn by graknlabs.
the class SchemaMutationTest method whenChangingTheSuperTypeOfAnEntityTypeWhichHasAResource_EnsureTheResourceIsStillAccessibleViaTheRelationTypeInstances_ByPreventingChange.
@Test
public void whenChangingTheSuperTypeOfAnEntityTypeWhichHasAResource_EnsureTheResourceIsStillAccessibleViaTheRelationTypeInstances_ByPreventingChange() {
AttributeType<String> name = tx.putAttributeType("name", AttributeType.DataType.STRING);
// Create a person and allow person to have a name
EntityType person = tx.putEntityType("person").attribute(name);
// Create a man which is a person and is therefore allowed to have a name
EntityType man = tx.putEntityType("man").sup(person);
RelationshipType has_name = tx.getRelationshipType("@has-name");
// Create a Man and name him Bob
Attribute<String> nameBob = name.putAttribute("Bob");
man.addEntity().attribute(nameBob);
// Get The Relationship which says that our man is name bob
Relationship expectedEdge = Iterables.getOnlyElement(has_name.instances().collect(toSet()));
Role hasNameOwner = tx.getRole("@has-name-owner");
assertThat(expectedEdge.type().instances().collect(toSet()), hasItem(expectedEdge));
expectedException.expect(GraknTxOperationException.class);
expectedException.expectMessage(GraknTxOperationException.changingSuperWillDisconnectRole(person, tx.admin().getMetaEntityType(), hasNameOwner).getMessage());
// Man is no longer a person and therefore is not allowed to have a name
man.sup(tx.admin().getMetaEntityType());
}
use of ai.grakn.concept.Role 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));
}
Aggregations