Search in sources :

Example 56 with RelationshipType

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

the class MatchTest method whenQueryingForHas_AllowReferringToTheImplicitRelation.

@Test
public void whenQueryingForHas_AllowReferringToTheImplicitRelation() {
    Label title = Label.of("title");
    RelationshipType hasTitle = movieKB.tx().getType(HAS.getLabel(title));
    Role titleOwner = movieKB.tx().getSchemaConcept(HAS_OWNER.getLabel(title));
    Role titleValue = movieKB.tx().getSchemaConcept(HAS_VALUE.getLabel(title));
    Relationship implicitRelation = hasTitle.instances().iterator().next();
    ConceptId owner = implicitRelation.rolePlayers(titleOwner).iterator().next().getId();
    ConceptId value = implicitRelation.rolePlayers(titleValue).iterator().next().getId();
    Match query = qb.match(x.id(owner).has(title, y.id(value), r));
    assertThat(query, variable(r, contains(MatchableConcept.of(implicitRelation))));
}
Also used : Role(ai.grakn.concept.Role) Relationship(ai.grakn.concept.Relationship) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId) Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 57 with RelationshipType

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

the class RelationshipTypePropertyTest method whenRelatingARole_TheDirectSubTypeRelatedRolesAreUnchanged.

@Property
public void whenRelatingARole_TheDirectSubTypeRelatedRolesAreUnchanged(@NonMeta RelationshipType subType, @FromTx Role role) {
    RelationshipType superType = subType.sup();
    assumeFalse(isMetaLabel(superType.getLabel()));
    Set<Role> previousHasRoles = subType.relates().collect(toSet());
    superType.relates(role);
    Set<Role> newHasRoles = subType.relates().collect(toSet());
    assertEquals(previousHasRoles, newHasRoles);
}
Also used : Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Property(com.pholser.junit.quickcheck.Property)

Example 58 with RelationshipType

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

the class RelationshipTypePropertyTest method whenDeletingARelatedRole_TheDirectSubTypeRelatedRolesAreUnchanged.

@Property
public void whenDeletingARelatedRole_TheDirectSubTypeRelatedRolesAreUnchanged(@NonMeta RelationshipType subType, @FromTx Role role) {
    RelationshipType superType = subType.sup();
    assumeFalse(isMetaLabel(superType.getLabel()));
    Set<Role> previousHasRoles = subType.relates().collect(toSet());
    superType.deleteRelates(role);
    Set<Role> newHasRoles = subType.relates().collect(toSet());
    assertEquals(previousHasRoles, newHasRoles);
}
Also used : Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Property(com.pholser.junit.quickcheck.Property)

Example 59 with RelationshipType

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

the class RelationshipTypePropertyTest method whenRelatingARole_TheDirectSuperTypeRelatedRolesAreUnchanged.

@Property
public void whenRelatingARole_TheDirectSuperTypeRelatedRolesAreUnchanged(@NonMeta RelationshipType subType, @FromTx Role role) {
    RelationshipType superType = subType.sup();
    Set<Role> previousHasRoles = superType.relates().collect(toSet());
    subType.relates(role);
    Set<Role> newHasRoles = superType.relates().collect(toSet());
    assertEquals(previousHasRoles, newHasRoles);
}
Also used : Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Property(com.pholser.junit.quickcheck.Property)

Example 60 with RelationshipType

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

the class ShortestPathTest method testMultipleIndependentShortestPaths.

@Test
public void testMultipleIndependentShortestPaths() throws InvalidKBException {
    Set<List<ConceptId>> validPaths = new HashSet<>();
    ConceptId startId;
    ConceptId endId;
    int numberOfPaths = 3;
    try (GraknTx graph = session.open(GraknTxType.WRITE)) {
        EntityType entityType = graph.putEntityType(thing);
        Role role1 = graph.putRole("role1");
        Role role2 = graph.putRole("role2");
        entityType.plays(role1).plays(role2);
        RelationshipType relationshipType = graph.putRelationshipType(related).relates(role1).relates(role2);
        Entity start = entityType.addEntity();
        Entity end = entityType.addEntity();
        startId = start.getId();
        endId = end.getId();
        // create N identical length paths
        for (int i = 0; i < numberOfPaths; i++) {
            List<ConceptId> validPath = new ArrayList<>();
            validPath.add(startId);
            Entity middle = entityType.addEntity();
            ConceptId middleId = middle.getId();
            ConceptId assertion1 = relationshipType.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
            validPath.add(assertion1);
            validPath.add(middleId);
            ConceptId assertion2 = relationshipType.addRelationship().addRolePlayer(role1, middle).addRolePlayer(role2, end).getId();
            validPath.add(assertion2);
            validPath.add(endId);
            validPaths.add(validPath);
        }
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        List<List<Concept>> allPaths = graph.graql().compute().paths().from(startId).to(endId).execute();
        assertEquals(numberOfPaths, allPaths.size());
        Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
        assertEquals(validPaths, computedPaths);
    }
}
Also used : InvalidKBException(ai.grakn.exception.InvalidKBException) GraknTestUtil(ai.grakn.util.GraknTestUtil) Role(ai.grakn.concept.Role) Assume.assumeFalse(org.junit.Assume.assumeFalse) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Graql(ai.grakn.graql.Graql) EntityType(ai.grakn.concept.EntityType) ArrayList(java.util.ArrayList) Attribute(ai.grakn.concept.Attribute) SessionContext(ai.grakn.test.rule.SessionContext) HashSet(java.util.HashSet) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) Matchers.empty(org.hamcrest.Matchers.empty) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Utility.getResourceEdgeId(ai.grakn.graql.internal.analytics.Utility.getResourceEdgeId) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) ArrayList(java.util.ArrayList) ConceptId(ai.grakn.concept.ConceptId) EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) 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