use of ai.grakn.concept.ConceptId in project grakn by graknlabs.
the class TransitivityChainKB method buildExtensionalDB.
private void buildExtensionalDB(GraknTx graph, int n) {
Role qfrom = graph.getRole("Q-from");
Role qto = graph.getRole("Q-to");
EntityType aEntity = graph.getEntityType("a-entity");
RelationshipType q = graph.getRelationshipType("Q");
Thing aInst = putEntityWithResource(graph, "a", graph.getEntityType("entity2"), key);
ConceptId[] aInstanceIds = new ConceptId[n];
for (int i = 0; i < n; i++) {
aInstanceIds[i] = putEntityWithResource(graph, "a" + i, aEntity, key).getId();
}
q.addRelationship().addRolePlayer(qfrom, aInst).addRolePlayer(qto, graph.getConcept(aInstanceIds[0]));
for (int i = 0; i < n - 1; i++) {
q.addRelationship().addRolePlayer(qfrom, graph.getConcept(aInstanceIds[i])).addRolePlayer(qto, graph.getConcept(aInstanceIds[i + 1]));
}
}
use of ai.grakn.concept.ConceptId in project grakn by graknlabs.
the class EntityTest method whenDeletingInstanceInRelationShip_TheInstanceAndCastingsAreDeletedAndTheRelationRemains.
@Test
public void whenDeletingInstanceInRelationShip_TheInstanceAndCastingsAreDeletedAndTheRelationRemains() throws GraknTxOperationException {
// Schema
EntityType type = tx.putEntityType("Concept Type");
RelationshipType relationshipType = tx.putRelationshipType("relationTypes");
Role role1 = tx.putRole("role1");
Role role2 = tx.putRole("role2");
Role role3 = tx.putRole("role3");
// Data
ThingImpl<?, ?> rolePlayer1 = (ThingImpl) type.addEntity();
ThingImpl<?, ?> rolePlayer2 = (ThingImpl) type.addEntity();
ThingImpl<?, ?> rolePlayer3 = (ThingImpl) type.addEntity();
relationshipType.relates(role1);
relationshipType.relates(role2);
relationshipType.relates(role3);
// Check Structure is in order
RelationshipImpl relation = (RelationshipImpl) relationshipType.addRelationship().addRolePlayer(role1, rolePlayer1).addRolePlayer(role2, rolePlayer2).addRolePlayer(role3, rolePlayer3);
Casting rp1 = rolePlayer1.castingsInstance().findAny().get();
Casting rp2 = rolePlayer2.castingsInstance().findAny().get();
Casting rp3 = rolePlayer3.castingsInstance().findAny().get();
assertThat(relation.reified().get().castingsRelation().collect(toSet()), containsInAnyOrder(rp1, rp2, rp3));
// Delete And Check Again
ConceptId idOfDeleted = rolePlayer1.getId();
rolePlayer1.delete();
assertNull(tx.getConcept(idOfDeleted));
assertThat(relation.reified().get().castingsRelation().collect(toSet()), containsInAnyOrder(rp2, rp3));
}
use of ai.grakn.concept.ConceptId in project grakn by graknlabs.
the class PostProcessingTest method whenMergingDuplicateResourcesWithRelations_EnsureSingleResourceRemainsAndNoDuplicateRelationsAreCreated.
@Test
public void whenMergingDuplicateResourcesWithRelations_EnsureSingleResourceRemainsAndNoDuplicateRelationsAreCreated() {
Role roleEntity = tx.putRole("Entity Role");
Role roleResource = tx.putRole("Attribute Role");
RelationshipType relationshipType = tx.putRelationshipType("Relationship Type").relates(roleEntity).relates(roleResource);
AttributeTypeImpl<String> resourceType = (AttributeTypeImpl<String>) tx.putAttributeType("Attribute Type", AttributeType.DataType.STRING).plays(roleResource);
EntityType entityType = tx.putEntityType("Entity Type").plays(roleEntity).attribute(resourceType);
Entity e1 = entityType.addEntity();
Entity e2 = entityType.addEntity();
Entity e3 = entityType.addEntity();
// Create fake resources
Set<ConceptId> resourceIds = new HashSet<>();
AttributeImpl<?> r1 = createFakeResource(resourceType, "1");
AttributeImpl<?> r11 = createFakeResource(resourceType, "1");
AttributeImpl<?> r111 = createFakeResource(resourceType, "1");
resourceIds.add(r1.getId());
resourceIds.add(r11.getId());
resourceIds.add(r111.getId());
// Give resources some relationships
addReifiedRelation(roleEntity, roleResource, relationshipType, e1, r1);
// When merging this relation should not be absorbed
addReifiedRelation(roleEntity, roleResource, relationshipType, e1, r11);
// Absorb
addReifiedRelation(roleEntity, roleResource, relationshipType, e2, r11);
// Don't Absorb
addEdgeRelation(e2, r111);
// Absorb
addEdgeRelation(e3, r111);
// Check everything is broken
assertEquals(3, resourceType.instances().count());
assertEquals(1, r1.relationships().count());
assertEquals(2, r11.relationships().count());
assertEquals(1, r1.relationships().count());
assertEquals(4, tx.getTinkerTraversal().V().hasLabel(Schema.BaseType.RELATIONSHIP.name()).toList().size());
assertEquals(2, tx.getTinkerTraversal().E().hasLabel(Schema.EdgeLabel.ATTRIBUTE.getLabel()).toList().size());
r1.relationships().forEach(rel -> assertTrue(rel.rolePlayers().anyMatch(thing -> thing.equals(e1))));
// Now fix everything
tx.fixDuplicateResources(r1.getIndex(), resourceIds);
// Check everything is in order
assertEquals(1, resourceType.instances().count());
// Get back the surviving resource
Attribute<String> foundR1 = null;
for (Attribute<String> attribute : resourceType.instances().collect(toSet())) {
if (attribute.getValue().equals("1")) {
foundR1 = attribute;
break;
}
}
assertNotNull(foundR1);
assertThat(foundR1.ownerInstances().collect(toSet()), containsInAnyOrder(e1, e2, e3));
assertEquals(6, tx.admin().getMetaRelationType().instances().count());
}
use of ai.grakn.concept.ConceptId in project grakn by graknlabs.
the class DualLinearTransitivityMatrixKB method buildExtensionalDB.
private void buildExtensionalDB(GraknTx graph, int n, int m) {
Role R1from = graph.getRole("R1-from");
Role R1to = graph.getRole("R1-to");
Role R2from = graph.getRole("R2-from");
Role R2to = graph.getRole("R2-to");
EntityType aEntity = graph.getEntityType("a-entity");
EntityType bEntity = graph.getEntityType("b-entity");
RelationshipType R1 = graph.getRelationshipType("R1");
RelationshipType R2 = graph.getRelationshipType("R2");
ConceptId[] aInstancesIds = new ConceptId[m + 1];
ConceptId[][] bInstancesIds = new ConceptId[m][n + 1];
aInstancesIds[0] = putEntityWithResource(graph, "a0", graph.getEntityType("start"), key).getId();
aInstancesIds[m] = putEntityWithResource(graph, "a" + m, graph.getEntityType("end"), key).getId();
for (int i = 1; i < m; i++) {
aInstancesIds[i] = putEntityWithResource(graph, "a" + i, aEntity, key).getId();
}
for (int i = 1; i < m; i++) {
for (int j = 1; j <= n; j++) {
bInstancesIds[i][j] = putEntityWithResource(graph, "b" + i + j, bEntity, key).getId();
}
}
for (int i = 0; i < m; i++) {
R1.addRelationship().addRolePlayer(R1from, graph.getConcept(aInstancesIds[i])).addRolePlayer(R1to, graph.getConcept(aInstancesIds[i + 1]));
}
for (int j = 1; j <= n; j++) {
R2.addRelationship().addRolePlayer(R2from, graph.getConcept(aInstancesIds[0])).addRolePlayer(R2to, graph.getConcept(bInstancesIds[1][j]));
R2.addRelationship().addRolePlayer(R2from, graph.getConcept(bInstancesIds[m - 1][j])).addRolePlayer(R2to, graph.getConcept(aInstancesIds[m]));
for (int i = 1; i < m - 1; i++) {
R2.addRelationship().addRolePlayer(R2from, graph.getConcept(bInstancesIds[i][j])).addRolePlayer(R2to, graph.getConcept(bInstancesIds[i + 1][j]));
}
}
}
use of ai.grakn.concept.ConceptId in project grakn by graknlabs.
the class RelationshipTest method whenDeletingFinalInstanceOfRelation_RelationIsDeleted.
@Test
public void whenDeletingFinalInstanceOfRelation_RelationIsDeleted() {
Role roleA = tx.putRole("RoleA");
Role roleB = tx.putRole("RoleB");
Role roleC = tx.putRole("RoleC");
RelationshipType relation = tx.putRelationshipType("relation type").relates(roleA).relates(roleB).relates(roleC);
EntityType type = tx.putEntityType("concept type").plays(roleA).plays(roleB).plays(roleC);
Entity a = type.addEntity();
Entity b = type.addEntity();
Entity c = type.addEntity();
ConceptId relationId = relation.addRelationship().addRolePlayer(roleA, a).addRolePlayer(roleB, b).addRolePlayer(roleC, c).getId();
a.delete();
assertNotNull(tx.getConcept(relationId));
b.delete();
assertNotNull(tx.getConcept(relationId));
c.delete();
assertNull(tx.getConcept(relationId));
}
Aggregations