Search in sources :

Example 51 with RelationshipType

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

the class RelationshipTypeTest method whenMutatingRolesOfRelationType_EnsureRelationTypeRolesAreAlwaysUpdated.

@Test
public void whenMutatingRolesOfRelationType_EnsureRelationTypeRolesAreAlwaysUpdated() {
    RelationshipType relationshipType = tx.putRelationshipType("c1");
    Role role1 = tx.putRole("c2");
    Role role2 = tx.putRole("c3");
    assertThat(relationshipType.relates().collect(toSet()), empty());
    relationshipType.relates(role1).relates(role2);
    assertThat(relationshipType.relates().collect(toSet()), containsInAnyOrder(role1, role2));
    relationshipType.deleteRelates(role1);
    assertThat(relationshipType.relates().collect(toSet()), containsInAnyOrder(role2));
}
Also used : Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 52 with RelationshipType

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

the class RelationshipTypeTest method whenGettingTheRolesOfRelationTypes_AllTheRolesAreReturned.

@Test
public void whenGettingTheRolesOfRelationTypes_AllTheRolesAreReturned() throws Exception {
    RelationshipType relationshipType = tx.putRelationshipType("relationTypes");
    Role role1 = tx.putRole("role1");
    Role role2 = tx.putRole("role2");
    Role role3 = tx.putRole("role3");
    relationshipType.relates(role1).relates(role2).relates(role3);
    assertThat(relationshipType.relates().collect(toSet()), containsInAnyOrder(role1, role2, role3));
}
Also used : Role(ai.grakn.concept.Role) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 53 with RelationshipType

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

the class ShortestPathTest method testMultiplePathsSharing1Instance.

@Test
public void testMultiplePathsSharing1Instance() throws InvalidKBException {
    ConceptId startId;
    ConceptId endId;
    Set<List<ConceptId>> correctPaths = new HashSet<>();
    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 relationshipType1 = graph.putRelationshipType(related).relates(role1).relates(role2);
        Role role3 = graph.putRole("role3");
        Role role4 = graph.putRole("role4");
        entityType.plays(role3).plays(role4);
        RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4);
        Entity start = entityType.addEntity();
        Entity end = entityType.addEntity();
        Entity middle = entityType.addEntity();
        startId = start.getId();
        endId = end.getId();
        ConceptId middleId = middle.getId();
        ConceptId assertion11 = relationshipType1.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
        ConceptId assertion12 = relationshipType1.addRelationship().addRolePlayer(role1, middle).addRolePlayer(role2, end).getId();
        ConceptId assertion21 = relationshipType2.addRelationship().addRolePlayer(role3, start).addRolePlayer(role4, middle).getId();
        ConceptId assertion22 = relationshipType2.addRelationship().addRolePlayer(role3, middle).addRolePlayer(role4, end).getId();
        correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion12, endId));
        correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion22, endId));
        correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion12, endId));
        correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion22, endId));
        graph.commit();
    }
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        List<List<Concept>> allPaths = graph.graql().compute().paths().from(startId).to(endId).execute();
        assertEquals(correctPaths.size(), allPaths.size());
        Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
        assertEquals(correctPaths, computedPaths);
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) 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) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) ArrayList(java.util.ArrayList) List(java.util.List) ConceptId(ai.grakn.concept.ConceptId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 54 with RelationshipType

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

the class ValidateGlobalRules method validateRelationTypesToRolesSchema.

/**
 * @param relationshipType the {@link RelationshipType} to be validated
 * @return Error messages if the role type sub structure does not match the {@link RelationshipType} sub structure
 */
static Set<String> validateRelationTypesToRolesSchema(RelationshipType relationshipType) {
    RelationshipTypeImpl superRelationType = (RelationshipTypeImpl) relationshipType.sup();
    if (Schema.MetaSchema.isMetaLabel(superRelationType.getLabel()) || superRelationType.isAbstract()) {
        // If super type is a meta type no validation needed
        return Collections.emptySet();
    }
    Set<String> errorMessages = new HashSet<>();
    Collection<Role> superRelates = superRelationType.relates().collect(Collectors.toSet());
    Collection<Role> relates = relationshipType.relates().collect(Collectors.toSet());
    Set<Label> relatesLabels = relates.stream().map(SchemaConcept::getLabel).collect(Collectors.toSet());
    // Check 1) Every role of relationTypes is the sub of a role which is in the relates of it's supers
    if (!superRelationType.isAbstract()) {
        Set<Label> allSuperRolesPlayed = new HashSet<>();
        superRelationType.sups().forEach(rel -> rel.relates().forEach(roleType -> allSuperRolesPlayed.add(roleType.getLabel())));
        for (Role relate : relates) {
            boolean validRoleTypeFound = SchemaConceptImpl.from(relate).sups().anyMatch(superRole -> allSuperRolesPlayed.contains(superRole.getLabel()));
            if (!validRoleTypeFound) {
                errorMessages.add(VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(relate.getLabel(), relationshipType.getLabel(), "super", "super", superRelationType.getLabel()));
            }
        }
    }
    // Check 2) Every role of superRelationType has a sub role which is in the relates of relationTypes
    for (Role superRelate : superRelates) {
        boolean subRoleNotFoundInRelates = superRelate.subs().noneMatch(sub -> relatesLabels.contains(sub.getLabel()));
        if (subRoleNotFoundInRelates) {
            errorMessages.add(VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(superRelate.getLabel(), superRelationType.getLabel(), "sub", "sub", relationshipType.getLabel()));
        }
    }
    return errorMessages;
}
Also used : Role(ai.grakn.concept.Role) RuleImpl(ai.grakn.kb.internal.concept.RuleImpl) Iterables(com.google.common.collect.Iterables) VALIDATION_CASTING(ai.grakn.util.ErrorMessage.VALIDATION_CASTING) VALIDATION_REQUIRED_RELATION(ai.grakn.util.ErrorMessage.VALIDATION_REQUIRED_RELATION) Role(ai.grakn.concept.Role) SchemaConcept(ai.grakn.concept.SchemaConcept) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Type(ai.grakn.concept.Type) Rule(ai.grakn.concept.Rule) Attribute(ai.grakn.concept.Attribute) TypeImpl(ai.grakn.kb.internal.concept.TypeImpl) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) CommonUtil(ai.grakn.util.CommonUtil) Map(java.util.Map) Relationship(ai.grakn.concept.Relationship) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) ErrorMessage(ai.grakn.util.ErrorMessage) VALIDATION_RELATION_TYPES_ROLES_SCHEMA(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_TYPES_ROLES_SCHEMA) SchemaConceptImpl(ai.grakn.kb.internal.concept.SchemaConceptImpl) Conjunction(ai.grakn.graql.admin.Conjunction) VALIDATION_RELATION_TYPE(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_TYPE) Collection(java.util.Collection) Set(java.util.Set) VALIDATION_ROLE_TYPE_MISSING_RELATION_TYPE(ai.grakn.util.ErrorMessage.VALIDATION_ROLE_TYPE_MISSING_RELATION_TYPE) Collectors(java.util.stream.Collectors) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) VALIDATION_RELATION_CASTING_LOOP_FAIL(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_CASTING_LOOP_FAIL) Casting(ai.grakn.kb.internal.structure.Casting) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Collections(java.util.Collections) VALIDATION_NOT_EXACTLY_ONE_KEY(ai.grakn.util.ErrorMessage.VALIDATION_NOT_EXACTLY_ONE_KEY) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Label(ai.grakn.concept.Label) HashSet(java.util.HashSet)

Example 55 with RelationshipType

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

the class TypeInferenceQueryTest method typedAnswers.

private List<Answer> typedAnswers(List<RelationshipType> possibleTypes, String pattern, EmbeddedGraknTx<?> graph) {
    List<Answer> answers = new ArrayList<>();
    ReasonerAtomicQuery query = ReasonerQueries.atomic(conjunction(pattern, graph), graph);
    for (Type type : possibleTypes) {
        GetQuery typedQuery = graph.graql().match(ReasonerQueries.atomic(query.getAtom().addType(type)).getPattern()).get();
        typedQuery.stream().filter(ans -> !answers.contains(ans)).forEach(answers::add);
    }
    return answers;
}
Also used : Iterables(com.google.common.collect.Iterables) Concept(ai.grakn.concept.Concept) Type(ai.grakn.concept.Type) Answer(ai.grakn.graql.admin.Answer) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) CollectionUtils(org.apache.commons.collections.CollectionUtils) ConceptId(ai.grakn.concept.ConceptId) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) GraqlTestUtil(ai.grakn.util.GraqlTestUtil) ExpectedException(org.junit.rules.ExpectedException) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Collectors.toSet(java.util.stream.Collectors.toSet) QueryBuilder(ai.grakn.graql.QueryBuilder) Patterns(ai.grakn.graql.internal.pattern.Patterns) Conjunction(ai.grakn.graql.admin.Conjunction) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) List(java.util.List) Rule(org.junit.Rule) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) SampleKBContext(ai.grakn.test.rule.SampleKBContext) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Schema(ai.grakn.util.Schema) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Type(ai.grakn.concept.Type) RelationshipType(ai.grakn.concept.RelationshipType) GetQuery(ai.grakn.graql.GetQuery) ArrayList(java.util.ArrayList) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)

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