Search in sources :

Example 81 with RelationshipType

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));
}
Also used : EntityType(ai.grakn.concept.EntityType) EdgeElement(ai.grakn.kb.internal.structure.EdgeElement) AttributeType(ai.grakn.concept.AttributeType) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 82 with RelationshipType

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();
}
Also used : Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 83 with RelationshipType

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));
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 84 with RelationshipType

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]));
            }
        }
    }
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) ConceptId(ai.grakn.concept.ConceptId)

Example 85 with RelationshipType

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());
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) AttributeType(ai.grakn.concept.AttributeType) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Aggregations

RelationshipType (ai.grakn.concept.RelationshipType)127 Role (ai.grakn.concept.Role)105 Test (org.junit.Test)91 EntityType (ai.grakn.concept.EntityType)80 Entity (ai.grakn.concept.Entity)52 GraknTx (ai.grakn.GraknTx)39 Relationship (ai.grakn.concept.Relationship)25 ConceptId (ai.grakn.concept.ConceptId)23 Label (ai.grakn.concept.Label)21 HashSet (java.util.HashSet)20 Set (java.util.Set)20 AttributeType (ai.grakn.concept.AttributeType)17 Thing (ai.grakn.concept.Thing)17 Attribute (ai.grakn.concept.Attribute)16 Schema (ai.grakn.util.Schema)12 Collectors (java.util.stream.Collectors)12 List (java.util.List)11 GraknSession (ai.grakn.GraknSession)10 GraknTxType (ai.grakn.GraknTxType)10 Concept (ai.grakn.concept.Concept)10