Search in sources :

Example 76 with Role

use of ai.grakn.concept.Role 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 77 with Role

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

the class GrpcUtil method convert.

public static Map<Role, Set<Thing>> convert(GrpcConceptConverter converter, RolePlayers allRolePlayers) {
    ImmutableSetMultimap.Builder<Role, Thing> map = ImmutableSetMultimap.builder();
    for (GrpcConcept.RolePlayer rolePlayer : allRolePlayers.getRolePlayerList()) {
        Role role = converter.convert(rolePlayer.getRole()).asRole();
        Thing player = converter.convert(rolePlayer.getPlayer()).asThing();
        map.put(role, player);
    }
    return Multimaps.asMap(map.build());
}
Also used : Role(ai.grakn.concept.Role) GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) Thing(ai.grakn.concept.Thing)

Example 78 with Role

use of ai.grakn.concept.Role 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 79 with Role

use of ai.grakn.concept.Role 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)

Example 80 with Role

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

the class RuleTest method initTx.

private void initTx(GraknTx graph) {
    AttributeType<Integer> res1 = graph.putAttributeType("res1", AttributeType.DataType.INTEGER);
    AttributeType<Integer> res2 = graph.putAttributeType("res2", AttributeType.DataType.INTEGER);
    Role role1 = graph.putRole("role1");
    Role role2 = graph.putRole("role2");
    Role role3 = graph.putRole("role3");
    graph.putEntityType("entity1").attribute(res1).attribute(res2).plays(role1).plays(role2);
    graph.putRelationshipType("relation1").relates(role1).relates(role2).relates(role3).plays(role1).plays(role2);
    graph.putRelationshipType("relation2").relates(role3);
}
Also used : Role(ai.grakn.concept.Role)

Aggregations

Role (ai.grakn.concept.Role)189 Test (org.junit.Test)124 RelationshipType (ai.grakn.concept.RelationshipType)114 EntityType (ai.grakn.concept.EntityType)92 Entity (ai.grakn.concept.Entity)55 GraknTx (ai.grakn.GraknTx)48 Relationship (ai.grakn.concept.Relationship)37 Set (java.util.Set)36 ConceptId (ai.grakn.concept.ConceptId)33 Label (ai.grakn.concept.Label)33 Thing (ai.grakn.concept.Thing)32 HashSet (java.util.HashSet)31 Var (ai.grakn.graql.Var)25 AttributeType (ai.grakn.concept.AttributeType)21 Concept (ai.grakn.concept.Concept)20 Schema (ai.grakn.util.Schema)17 Collectors (java.util.stream.Collectors)17 Attribute (ai.grakn.concept.Attribute)16 SchemaConcept (ai.grakn.concept.SchemaConcept)16 Type (ai.grakn.concept.Type)16