Search in sources :

Example 16 with Relationship

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

the class RemoteConceptsTest method whenCallingRelationshipsWithRoles_GetTheExpectedResult.

@Test
public void whenCallingRelationshipsWithRoles_GetTheExpectedResult() {
    Role foo = RemoteConcepts.createRole(tx, ConceptId.of("foo"));
    Role bar = RemoteConcepts.createRole(tx, ConceptId.of("bar"));
    Role baz = RemoteConcepts.createRole(tx, ConceptId.of("baz"));
    Relationship a = RemoteConcepts.createRelationship(tx, A);
    Relationship b = RemoteConcepts.createRelationship(tx, B);
    Relationship c = RemoteConcepts.createRelationship(tx, C);
    mockConceptMethod(ConceptMethods.getRelationshipsByRoles(foo, bar, baz), Stream.of(a, b, c));
    assertThat(thing.relationships(foo, bar, baz).collect(toSet()), containsInAnyOrder(a, b, c));
}
Also used : Role(ai.grakn.concept.Role) Relationship(ai.grakn.concept.Relationship) Test(org.junit.Test)

Example 17 with Relationship

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

the class RemoteConceptsTest method whenCallingAddAttributeRelationshipOnThing_ExecuteAConceptMethod.

@Test
public void whenCallingAddAttributeRelationshipOnThing_ExecuteAConceptMethod() {
    Attribute<Long> attribute = RemoteConcepts.createAttribute(tx, A);
    Relationship relationship = RemoteConcepts.createRelationship(tx, C);
    mockConceptMethod(ConceptMethods.setAttribute(attribute), relationship);
    assertEquals(relationship, thing.attributeRelationship(attribute));
}
Also used : Relationship(ai.grakn.concept.Relationship) Test(org.junit.Test)

Example 18 with Relationship

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

the class GrpcServerIT method whenDefiningASchema_TheSchemaIsDefined.

@Test
public void whenDefiningASchema_TheSchemaIsDefined() {
    try (GraknTx tx = remoteSession.open(GraknTxType.WRITE)) {
        EntityType animal = tx.putEntityType("animal");
        EntityType dog = tx.putEntityType("dog").sup(animal);
        EntityType cat = tx.putEntityType("cat");
        animal.sub(cat);
        cat.setLabel(Label.of("feline"));
        dog.setAbstract(true).setAbstract(false);
        cat.setAbstract(true);
        RelationshipType chases = tx.putRelationshipType("chases");
        Role chased = tx.putRole("chased");
        Role chaser = tx.putRole("chaser");
        chases.relates(chased).relates(chaser);
        Role pointlessRole = tx.putRole("pointless-role");
        tx.putRelationshipType("pointless").relates(pointlessRole);
        chases.relates(pointlessRole).deleteRelates(pointlessRole);
        dog.plays(chaser);
        cat.plays(chased);
        AttributeType<String> name = tx.putAttributeType("name", DataType.STRING);
        AttributeType<String> id = tx.putAttributeType("id", DataType.STRING).setRegex("(good|bad)-dog");
        AttributeType<Long> age = tx.putAttributeType("age", DataType.LONG);
        animal.attribute(name);
        animal.key(id);
        dog.attribute(age).deleteAttribute(age);
        cat.key(age).deleteKey(age);
        cat.plays(chaser).deletePlays(chaser);
        Entity dunstan = dog.addEntity();
        Attribute<String> dunstanId = id.putAttribute("good-dog");
        assertNotNull(dunstan.attributeRelationship(dunstanId));
        Attribute<String> dunstanName = name.putAttribute("Dunstan");
        dunstan.attribute(dunstanName).deleteAttribute(dunstanName);
        chases.addRelationship().addRolePlayer(chaser, dunstan);
        tx.commit();
    }
    try (GraknTx tx = localSession.open(GraknTxType.READ)) {
        EntityType animal = tx.getEntityType("animal");
        EntityType dog = tx.getEntityType("dog");
        EntityType cat = tx.getEntityType("feline");
        RelationshipType chases = tx.getRelationshipType("chases");
        Role chased = tx.getRole("chased");
        Role chaser = tx.getRole("chaser");
        AttributeType<String> name = tx.getAttributeType("name");
        AttributeType<String> id = tx.getAttributeType("id");
        Entity dunstan = Iterators.getOnlyElement(dog.instances().iterator());
        Relationship aChase = Iterators.getOnlyElement(chases.instances().iterator());
        assertEquals(animal, dog.sup());
        assertEquals(animal, cat.sup());
        assertEquals(ImmutableSet.of(chased, chaser), chases.relates().collect(toSet()));
        assertEquals(ImmutableSet.of(chaser), dog.plays().filter(role -> !role.isImplicit()).collect(toSet()));
        assertEquals(ImmutableSet.of(chased), cat.plays().filter(role -> !role.isImplicit()).collect(toSet()));
        assertEquals(ImmutableSet.of(name, id), animal.attributes().collect(toSet()));
        assertEquals(ImmutableSet.of(id), animal.keys().collect(toSet()));
        assertEquals(ImmutableSet.of(name, id), dog.attributes().collect(toSet()));
        assertEquals(ImmutableSet.of(id), dog.keys().collect(toSet()));
        assertEquals(ImmutableSet.of(name, id), cat.attributes().collect(toSet()));
        assertEquals(ImmutableSet.of(id), cat.keys().collect(toSet()));
        assertEquals("good-dog", Iterables.getOnlyElement(dunstan.keys(id).collect(toSet())).getValue());
        ImmutableMap<Role, ImmutableSet<?>> expectedRolePlayers = ImmutableMap.of(chaser, ImmutableSet.of(dunstan), chased, ImmutableSet.of());
        assertEquals(expectedRolePlayers, aChase.allRolePlayers());
        assertEquals("(good|bad)-dog", id.getRegex());
        assertFalse(dog.isAbstract());
        assertTrue(cat.isAbstract());
    }
}
Also used : Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) ImmutableSet(com.google.common.collect.ImmutableSet) Relationship(ai.grakn.concept.Relationship) Test(org.junit.Test)

Example 19 with Relationship

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

the class GrpcServerIT method whenGettingARelationship_TheInformationOnTheRelationshipIsCorrect.

@Test
public void whenGettingARelationship_TheInformationOnTheRelationshipIsCorrect() {
    try (GraknTx remoteTx = remoteSession.open(GraknTxType.READ);
        GraknTx localTx = localSession.open(GraknTxType.READ)) {
        GetQuery query = remoteTx.graql().match(var("x").isa("has-cast")).get();
        Relationship remoteConcept = query.stream().findAny().get().get("x").asRelationship();
        Relationship localConcept = localTx.getConcept(remoteConcept.getId()).asRelationship();
        assertEqualConcepts(localConcept, remoteConcept, Relationship::rolePlayers);
        ImmutableMultimap.Builder<ConceptId, ConceptId> localRolePlayers = ImmutableMultimap.builder();
        localConcept.allRolePlayers().forEach((role, players) -> {
            for (Thing player : players) {
                localRolePlayers.put(role.getId(), player.getId());
            }
        });
        ImmutableMultimap.Builder<ConceptId, ConceptId> remoteRolePlayers = ImmutableMultimap.builder();
        remoteConcept.allRolePlayers().forEach((role, players) -> {
            for (Thing player : players) {
                remoteRolePlayers.put(role.getId(), player.getId());
            }
        });
        assertEquals(localRolePlayers.build(), remoteRolePlayers.build());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) GetQuery(ai.grakn.graql.GetQuery) Relationship(ai.grakn.concept.Relationship) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Thing(ai.grakn.concept.Thing) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 20 with Relationship

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

the class ValidateGlobalRules method validatePlaysAndRelatesStructure.

/**
 * This method checks if the plays edge has been added between the roleplayer's {@link Type} and
 * the {@link Role} being played.
 *
 * It also checks if the {@link Role} of the {@link Casting} has been linked to the {@link RelationshipType} of the
 * {@link Relationship} which the {@link Casting} connects to.
 *
 * @return Specific errors if any are found
 */
static Set<String> validatePlaysAndRelatesStructure(Casting casting) {
    Set<String> errors = new HashSet<>();
    // Gets here to make sure we traverse/read only once
    Thing thing = casting.getRolePlayer();
    Role role = casting.getRole();
    Relationship relationship = casting.getRelationship();
    // Actual checks
    roleNotAllowedToBePlayed(role, thing).ifPresent(errors::add);
    roleNotLinkedToRelationShip(role, relationship.type(), relationship).ifPresent(errors::add);
    return errors;
}
Also used : Role(ai.grakn.concept.Role) Relationship(ai.grakn.concept.Relationship) Thing(ai.grakn.concept.Thing) HashSet(java.util.HashSet)

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