use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class SchemaConceptTest method whenSpecifyingTheResourceTypeOfAnEntityType_EnsureTheImplicitStructureIsCreated.
@Test
public void whenSpecifyingTheResourceTypeOfAnEntityType_EnsureTheImplicitStructureIsCreated() {
Label resourceLabel = Label.of("Attribute Type");
EntityType entityType = tx.putEntityType("Entity1");
AttributeType attributeType = tx.putAttributeType("Attribute Type", AttributeType.DataType.STRING);
// Implicit Names
Label hasResourceOwnerLabel = Schema.ImplicitType.HAS_OWNER.getLabel(resourceLabel);
Label hasResourceValueLabel = Schema.ImplicitType.HAS_VALUE.getLabel(resourceLabel);
Label hasResourceLabel = Schema.ImplicitType.HAS.getLabel(resourceLabel);
entityType.attribute(attributeType);
RelationshipType relationshipType = tx.getRelationshipType(hasResourceLabel.getValue());
Assert.assertEquals(hasResourceLabel, relationshipType.getLabel());
Set<Label> roleLabels = relationshipType.relates().map(SchemaConcept::getLabel).collect(toSet());
assertThat(roleLabels, containsInAnyOrder(hasResourceOwnerLabel, hasResourceValueLabel));
assertThat(entityType.plays().collect(toSet()), containsInAnyOrder(tx.getRole(hasResourceOwnerLabel.getValue())));
assertThat(attributeType.plays().collect(toSet()), containsInAnyOrder(tx.getRole(hasResourceValueLabel.getValue())));
// Check everything is implicit
assertTrue(relationshipType.isImplicit());
relationshipType.relates().forEach(role -> assertTrue(role.isImplicit()));
// Check that resource is not required
EdgeElement entityPlays = ((EntityTypeImpl) entityType).vertex().getEdgesOfType(Direction.OUT, Schema.EdgeLabel.PLAYS).iterator().next();
assertFalse(entityPlays.propertyBoolean(Schema.EdgeProperty.REQUIRED));
EdgeElement resourcePlays = ((AttributeTypeImpl<?>) attributeType).vertex().getEdgesOfType(Direction.OUT, Schema.EdgeLabel.PLAYS).iterator().next();
assertFalse(resourcePlays.propertyBoolean(Schema.EdgeProperty.REQUIRED));
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class ValidatorTest method checkRoleTypeValidSuperOfSelfTypeWhenLinkedToRelationsWhichAreSubsOfEachOther.
@Test
public void checkRoleTypeValidSuperOfSelfTypeWhenLinkedToRelationsWhichAreSubsOfEachOther() throws InvalidKBException {
Role insurer = tx.putRole("insurer");
Role monoline = tx.putRole("monoline").sup(insurer);
Role insured = tx.putRole("insured");
RelationshipType insure = tx.putRelationshipType("insure").relates(insurer).relates(insured);
tx.putRelationshipType("monoline-insure").relates(monoline).relates(insured).sup(insure);
tx.commit();
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class AttributeTest method whenAttachingResourcesToInstances_EnsureInstancesAreReturnedAsOwners.
@Test
public void whenAttachingResourcesToInstances_EnsureInstancesAreReturnedAsOwners() throws Exception {
EntityType randomThing = tx.putEntityType("A Thing");
AttributeType<String> attributeType = tx.putAttributeType("A Attribute Thing", AttributeType.DataType.STRING);
RelationshipType hasResource = tx.putRelationshipType("Has Attribute");
Role resourceRole = tx.putRole("Attribute Role");
Role actorRole = tx.putRole("Actor");
Thing pacino = randomThing.addEntity();
Thing jennifer = randomThing.addEntity();
Thing bob = randomThing.addEntity();
Thing alice = randomThing.addEntity();
Attribute<String> birthDate = attributeType.putAttribute("10/10/10");
hasResource.relates(resourceRole).relates(actorRole);
assertThat(birthDate.ownerInstances().collect(toSet()), empty());
hasResource.addRelationship().addRolePlayer(resourceRole, birthDate).addRolePlayer(actorRole, pacino);
hasResource.addRelationship().addRolePlayer(resourceRole, birthDate).addRolePlayer(actorRole, jennifer);
hasResource.addRelationship().addRolePlayer(resourceRole, birthDate).addRolePlayer(actorRole, bob);
hasResource.addRelationship().addRolePlayer(resourceRole, birthDate).addRolePlayer(actorRole, alice);
assertThat(birthDate.ownerInstances().collect(toSet()), containsInAnyOrder(pacino, jennifer, bob, alice));
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class TransitivityMatrixKB method buildExtensionalDB.
private void buildExtensionalDB(GraknTx graph, int n, int m) {
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][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
aInstanceIds[i][j] = putEntityWithResource(graph, "a" + i + "," + j, aEntity, key).getId();
}
}
q.addRelationship().addRolePlayer(qfrom, aInst).addRolePlayer(qto, graph.getConcept(aInstanceIds[0][0]));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i < n - 1) {
q.addRelationship().addRolePlayer(qfrom, graph.getConcept(aInstanceIds[i][j])).addRolePlayer(qto, graph.getConcept(aInstanceIds[i + 1][j]));
}
if (j < m - 1) {
q.addRelationship().addRolePlayer(qfrom, graph.getConcept(aInstanceIds[i][j])).addRolePlayer(qto, graph.getConcept(aInstanceIds[i][j + 1]));
}
}
}
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class EntityTypeTest method whenAddingResourcesWithSubTypesToEntityTypes_EnsureImplicitStructureFollowsSubTypes.
@Test
public void whenAddingResourcesWithSubTypesToEntityTypes_EnsureImplicitStructureFollowsSubTypes() {
EntityType entityType1 = tx.putEntityType("Entity Type 1");
EntityType entityType2 = tx.putEntityType("Entity Type 2");
Label superLabel = Label.of("Super Attribute Type");
Label label = Label.of("Attribute Type");
AttributeType rtSuper = tx.putAttributeType(superLabel, AttributeType.DataType.STRING);
AttributeType rt = tx.putAttributeType(label, AttributeType.DataType.STRING).sup(rtSuper);
entityType1.attribute(rtSuper);
entityType2.attribute(rt);
// Check role types are only built explicitly
assertThat(entityType1.plays().collect(toSet()), containsInAnyOrder(tx.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(superLabel).getValue())));
assertThat(entityType2.plays().collect(toSet()), containsInAnyOrder(tx.getRole(Schema.ImplicitType.HAS_OWNER.getLabel(label).getValue())));
// Check Implicit Types Follow SUB Structure
RelationshipType rtSuperRelation = tx.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(rtSuper.getLabel()));
Role rtSuperRoleOwner = tx.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(rtSuper.getLabel()));
Role rtSuperRoleValue = tx.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(rtSuper.getLabel()));
RelationshipType rtRelation = tx.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(rt.getLabel()));
Role reRoleOwner = tx.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(rt.getLabel()));
Role reRoleValue = tx.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(rt.getLabel()));
assertEquals(rtSuperRoleOwner, reRoleOwner.sup());
assertEquals(rtSuperRoleValue, reRoleValue.sup());
assertEquals(rtSuperRelation, rtRelation.sup());
}
Aggregations