Search in sources :

Example 1 with Relationship

use of ai.grakn.concept.Relationship in project grakn by graknlabs.

the class MatchTest method whenQueryingForAnImplicitRelationById_TheRelationIsReturned.

@Test
public void whenQueryingForAnImplicitRelationById_TheRelationIsReturned() {
    Match match = qb.match(var("x").isa(label(Schema.ImplicitType.HAS.getLabel("name"))));
    Relationship relationship = match.get("x").findAny().get().asRelationship();
    Match queryById = qb.match(var("x").id(relationship.getId()));
    assertThat(queryById, variable(x, contains(MatchableConcept.of(relationship))));
}
Also used : Relationship(ai.grakn.concept.Relationship) Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 2 with Relationship

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

Example 3 with Relationship

use of ai.grakn.concept.Relationship in project grakn by graknlabs.

the class MigratorTestUtils method getResources.

public static Stream<Attribute> getResources(GraknTx graph, Thing thing, Label label) {
    Role roleOwner = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(label));
    Role roleOther = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(label));
    Stream<Relationship> relations = thing.relationships(roleOwner);
    return relations.flatMap(r -> r.rolePlayers(roleOther)).map(Concept::asAttribute);
}
Also used : Role(ai.grakn.concept.Role) InvalidKBException(ai.grakn.exception.InvalidKBException) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) Relationship(ai.grakn.concept.Relationship) GraknTxType(ai.grakn.GraknTxType) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) Schema(ai.grakn.util.Schema) TestCase.assertEquals(junit.framework.TestCase.assertEquals) Concept(ai.grakn.concept.Concept) Relationship(ai.grakn.concept.Relationship)

Example 4 with Relationship

use of ai.grakn.concept.Relationship in project grakn by graknlabs.

the class EntityTest method whenAddingResourceToAnEntity_EnsureTheImplicitStructureIsCreated.

@Test
public void whenAddingResourceToAnEntity_EnsureTheImplicitStructureIsCreated() {
    Label resourceLabel = Label.of("A Attribute Thing");
    EntityType entityType = tx.putEntityType("A Thing");
    AttributeType<String> attributeType = tx.putAttributeType(resourceLabel, AttributeType.DataType.STRING);
    entityType.attribute(attributeType);
    Entity entity = entityType.addEntity();
    Attribute attribute = attributeType.putAttribute("A attribute thing");
    entity.attribute(attribute);
    Relationship relationship = entity.relationships().iterator().next();
    checkImplicitStructure(attributeType, relationship, entity, Schema.ImplicitType.HAS, Schema.ImplicitType.HAS_OWNER, Schema.ImplicitType.HAS_VALUE);
}
Also used : EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) Relationship(ai.grakn.concept.Relationship) Label(ai.grakn.concept.Label) Test(org.junit.Test)

Example 5 with Relationship

use of ai.grakn.concept.Relationship in project grakn by graknlabs.

the class RelationshipTest method whenAddingRolePlayerToRelation_RelationIsExpanded.

@Test
public void whenAddingRolePlayerToRelation_RelationIsExpanded() {
    Relationship relationship = relationshipType.addRelationship();
    Role role = tx.putRole("A role");
    Entity entity1 = type.addEntity();
    relationship.addRolePlayer(role, entity1);
    assertThat(relationship.allRolePlayers().keySet(), containsInAnyOrder(role1, role2, role3, role));
    assertThat(relationship.allRolePlayers().get(role), containsInAnyOrder(entity1));
}
Also used : Role(ai.grakn.concept.Role) Entity(ai.grakn.concept.Entity) Relationship(ai.grakn.concept.Relationship) Test(org.junit.Test)

Aggregations

Relationship (ai.grakn.concept.Relationship)50 Test (org.junit.Test)36 Role (ai.grakn.concept.Role)27 EntityType (ai.grakn.concept.EntityType)22 RelationshipType (ai.grakn.concept.RelationshipType)22 Entity (ai.grakn.concept.Entity)15 Thing (ai.grakn.concept.Thing)11 Label (ai.grakn.concept.Label)9 HashSet (java.util.HashSet)9 Attribute (ai.grakn.concept.Attribute)8 ConceptId (ai.grakn.concept.ConceptId)8 GraknTx (ai.grakn.GraknTx)7 Set (java.util.Set)7 Concept (ai.grakn.concept.Concept)6 Stream (java.util.stream.Stream)6 Type (ai.grakn.concept.Type)5 Answer (ai.grakn.graql.admin.Answer)5 Schema (ai.grakn.util.Schema)5 Property (com.pholser.junit.quickcheck.Property)5 Collectors (java.util.stream.Collectors)5