use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class RelationshipTest method whenAddingDuplicateRelationsWithDifferentKeys_EnsureTheyCanBeCommitted.
@Test
public void whenAddingDuplicateRelationsWithDifferentKeys_EnsureTheyCanBeCommitted() {
Role role1 = tx.putRole("dark");
Role role2 = tx.putRole("souls");
AttributeType<Long> attributeType = tx.putAttributeType("Death Number", AttributeType.DataType.LONG);
RelationshipType relationshipType = tx.putRelationshipType("Dark Souls").relates(role1).relates(role2).key(attributeType);
EntityType entityType = tx.putEntityType("Dead Guys").plays(role1).plays(role2);
Entity e1 = entityType.addEntity();
Entity e2 = entityType.addEntity();
Attribute<Long> r1 = attributeType.putAttribute(1000000L);
Attribute<Long> r2 = attributeType.putAttribute(2000000L);
Relationship rel1 = relationshipType.addRelationship().addRolePlayer(role1, e1).addRolePlayer(role2, e2);
Relationship rel2 = relationshipType.addRelationship().addRolePlayer(role1, e1).addRolePlayer(role2, e2);
// Set the keys and commit. Without this step it should fail
rel1.attribute(r1);
rel2.attribute(r2);
tx.commit();
tx = session.open(GraknTxType.WRITE);
assertThat(tx.admin().getMetaRelationType().instances().collect(toSet()), Matchers.hasItem(rel1));
assertThat(tx.admin().getMetaRelationType().instances().collect(toSet()), Matchers.hasItem(rel2));
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class RelationshipTest method whenAddingRelationshipWithNoRolePlayers_Throw.
@Test
public void whenAddingRelationshipWithNoRolePlayers_Throw() {
Role role1 = tx.putRole("r1");
Role role2 = tx.putRole("r2");
RelationshipType relationshipType = tx.putRelationshipType("A thing of sorts").relates(role1).relates(role2);
Relationship relationship = relationshipType.addRelationship();
expectedException.expect(InvalidKBException.class);
expectedException.expectMessage(containsString(ErrorMessage.VALIDATION_RELATIONSHIP_WITH_NO_ROLE_PLAYERS.getMessage(relationship.getId(), relationship.type().getLabel())));
tx.commit();
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class PostProcessingTest method whenMergingDuplicateResourceEdges_EnsureNoDuplicatesRemain.
@Test
public void whenMergingDuplicateResourceEdges_EnsureNoDuplicatesRemain() {
AttributeTypeImpl<String> resourceType = (AttributeTypeImpl<String>) tx.putAttributeType("My Sad Attribute", AttributeType.DataType.STRING);
EntityType entityType = tx.putEntityType("My Happy EntityType").attribute(resourceType);
RelationshipType relationshipType = tx.putRelationshipType("My Miserable RelationshipType").attribute(resourceType);
Entity entity = entityType.addEntity();
Relationship relationship = relationshipType.addRelationship();
AttributeImpl<?> r1dup1 = createFakeResource(resourceType, "1");
AttributeImpl<?> r1dup2 = createFakeResource(resourceType, "1");
AttributeImpl<?> r1dup3 = createFakeResource(resourceType, "1");
AttributeImpl<?> r2dup1 = createFakeResource(resourceType, "2");
AttributeImpl<?> r2dup2 = createFakeResource(resourceType, "2");
AttributeImpl<?> r2dup3 = createFakeResource(resourceType, "2");
entity.attribute(r1dup1);
entity.attribute(r1dup2);
entity.attribute(r1dup3);
relationship.attribute(r1dup1);
relationship.attribute(r1dup2);
relationship.attribute(r1dup3);
entity.attribute(r2dup1);
// Check everything is broken
// Entities Too Many Resources
assertEquals(4, entity.attributes().count());
assertEquals(3, relationship.attributes().count());
// There are too many resources
assertEquals(6, tx.admin().getMetaAttributeType().instances().count());
// Now fix everything for resource 1
tx.fixDuplicateResources(r1dup1.getIndex(), new HashSet<>(Arrays.asList(r1dup1.getId(), r1dup2.getId(), r1dup3.getId())));
// Check resource one has been sorted out
assertEquals(2, entity.attributes().count());
assertEquals(2, entity.attributes().count());
// 4 because we still have 2 dups on r2
assertEquals(4, tx.admin().getMetaAttributeType().instances().count());
// Now fix everything for resource 2
tx.fixDuplicateResources(r2dup1.getIndex(), new HashSet<>(Arrays.asList(r2dup1.getId(), r2dup2.getId(), r2dup3.getId())));
// Check resource one has been sorted out
assertEquals(2, entity.attributes().count());
assertEquals(2, entity.attributes().count());
// 4 because we still have 2 dups on r2
assertEquals(2, tx.admin().getMetaAttributeType().instances().count());
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class RelationshipTest method checkRolePlayerEdgesAreCreatedBetweenAllRolePlayers.
@Test
public void checkRolePlayerEdgesAreCreatedBetweenAllRolePlayers() {
// Create the Schema
Role role1 = tx.putRole("Role 1");
Role role2 = tx.putRole("Role 2");
Role role3 = tx.putRole("Role 3");
tx.putRelationshipType("Rel Type").relates(role1).relates(role2).relates(role3);
EntityType entType = tx.putEntityType("Entity Type").plays(role1).plays(role2).plays(role3);
// Data
EntityImpl entity1r1 = (EntityImpl) entType.addEntity();
EntityImpl entity2r1 = (EntityImpl) entType.addEntity();
EntityImpl entity3r2r3 = (EntityImpl) entType.addEntity();
EntityImpl entity4r3 = (EntityImpl) entType.addEntity();
EntityImpl entity5r1 = (EntityImpl) entType.addEntity();
EntityImpl entity6r1r2r3 = (EntityImpl) entType.addEntity();
// Relationship
Relationship relationship = relationshipType.addRelationship();
relationship.addRolePlayer(role1, entity1r1);
relationship.addRolePlayer(role1, entity2r1);
relationship.addRolePlayer(role1, entity5r1);
relationship.addRolePlayer(role1, entity6r1r2r3);
relationship.addRolePlayer(role2, entity3r2r3);
relationship.addRolePlayer(role2, entity6r1r2r3);
relationship.addRolePlayer(role3, entity3r2r3);
relationship.addRolePlayer(role3, entity4r3);
relationship.addRolePlayer(role3, entity6r1r2r3);
// Check the structure of the NEW role-player edges
assertThat(followRolePlayerEdgesToNeighbours(tx, entity1r1), containsInAnyOrder(entity1r1, entity2r1, entity3r2r3, entity4r3, entity5r1, entity6r1r2r3));
assertThat(followRolePlayerEdgesToNeighbours(tx, entity2r1), containsInAnyOrder(entity2r1, entity1r1, entity3r2r3, entity4r3, entity5r1, entity6r1r2r3));
assertThat(followRolePlayerEdgesToNeighbours(tx, entity3r2r3), containsInAnyOrder(entity1r1, entity2r1, entity3r2r3, entity4r3, entity5r1, entity6r1r2r3));
assertThat(followRolePlayerEdgesToNeighbours(tx, entity4r3), containsInAnyOrder(entity1r1, entity2r1, entity3r2r3, entity4r3, entity5r1, entity6r1r2r3));
assertThat(followRolePlayerEdgesToNeighbours(tx, entity5r1), containsInAnyOrder(entity1r1, entity2r1, entity3r2r3, entity4r3, entity5r1, entity6r1r2r3));
assertThat(followRolePlayerEdgesToNeighbours(tx, entity6r1r2r3), containsInAnyOrder(entity1r1, entity2r1, entity3r2r3, entity4r3, entity5r1, entity6r1r2r3));
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class RelationshipTest method whenCreatingAnInferredRelationship_EnsureMarkedAsInferred.
@Test
public void whenCreatingAnInferredRelationship_EnsureMarkedAsInferred() {
RelationshipTypeImpl rt = RelationshipTypeImpl.from(tx.putRelationshipType("rt"));
Relationship relationship = rt.addRelationship();
Relationship relationshipInferred = rt.addRelationshipInferred();
assertFalse(relationship.isInferred());
assertTrue(relationshipInferred.isInferred());
}
Aggregations