Search in sources :

Example 56 with Role

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

the class RemoteConceptsTest method whenCallingPlays_GetTheExpectedResult.

@Test
public void whenCallingPlays_GetTheExpectedResult() {
    Role a = RemoteConcepts.createRole(tx, A);
    Role b = RemoteConcepts.createRole(tx, B);
    Role c = RemoteConcepts.createRole(tx, C);
    mockConceptMethod(ConceptMethods.GET_ROLES_PLAYED_BY_TYPE, Stream.of(a, b, c));
    assertThat(type.plays().collect(toSet()), containsInAnyOrder(a, b, c));
}
Also used : Role(ai.grakn.concept.Role) Test(org.junit.Test)

Example 57 with Role

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

the class RelatesProperty method define.

@Override
public Collection<PropertyExecutor> define(Var var) throws GraqlQueryException {
    Var roleVar = role().var();
    PropertyExecutor.Method relatesMethod = executor -> {
        Role role = executor.get(roleVar).asRole();
        executor.get(var).asRelationshipType().relates(role);
    };
    PropertyExecutor relatesExecutor = PropertyExecutor.builder(relatesMethod).requires(var, roleVar).build();
    // This allows users to skip stating `$roleVar sub role` when they say `$var relates $roleVar`
    PropertyExecutor.Method isRoleMethod = executor -> executor.builder(roleVar).isRole();
    PropertyExecutor isRoleExecutor = PropertyExecutor.builder(isRoleMethod).produces(roleVar).build();
    VarPatternAdmin superRoleVarPattern = superRole();
    if (superRoleVarPattern != null) {
        Var superRoleVar = superRoleVarPattern.var();
        PropertyExecutor.Method subMethod = executor -> {
            Role superRole = executor.get(superRoleVar).asRole();
            executor.builder(roleVar).sub(superRole);
        };
        PropertyExecutor subExecutor = PropertyExecutor.builder(subMethod).requires(superRoleVar).produces(roleVar).build();
        return ImmutableSet.of(relatesExecutor, isRoleExecutor, subExecutor);
    } else {
        return ImmutableSet.of(relatesExecutor, isRoleExecutor);
    }
}
Also used : GraqlQueryException(ai.grakn.exception.GraqlQueryException) ImmutableSet(com.google.common.collect.ImmutableSet) Role(ai.grakn.concept.Role) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) EquivalentFragmentSets.sub(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets.sub) EquivalentFragmentSets.relates(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets.relates) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) RelationshipType(ai.grakn.concept.RelationshipType) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) RelatesAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelatesAtom) ReasonerUtils.getIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getIdPredicate) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) EquivalentFragmentSet(ai.grakn.graql.internal.gremlin.EquivalentFragmentSet) Nullable(javax.annotation.Nullable) Role(ai.grakn.concept.Role) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var)

Example 58 with Role

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

the class GrpcServerTest method whenRemovingRolePlayer_RolePlayerIsRemoved.

@Test
public void whenRemovingRolePlayer_RolePlayerIsRemoved() throws InterruptedException {
    ConceptId conceptId = ConceptId.of("V123456");
    ConceptId roleId = ConceptId.of("ROLE");
    ConceptId playerId = ConceptId.of("PLAYER");
    Concept concept = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(tx.getConcept(conceptId)).thenReturn(concept);
    when(concept.isRelationship()).thenReturn(true);
    Role role = mock(Role.class, RETURNS_DEEP_STUBS);
    when(tx.getConcept(roleId)).thenReturn(role);
    when(role.isRole()).thenReturn(true);
    when(role.asRole()).thenReturn(role);
    when(role.getId()).thenReturn(roleId);
    Entity player = mock(Entity.class, RETURNS_DEEP_STUBS);
    when(tx.getConcept(playerId)).thenReturn(player);
    when(player.isEntity()).thenReturn(true);
    when(player.asEntity()).thenReturn(player);
    when(player.isThing()).thenReturn(true);
    when(player.asThing()).thenReturn(player);
    when(player.getId()).thenReturn(playerId);
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.READ));
        tx.receive().ok();
        ConceptMethod<Void> conceptMethod = ConceptMethods.removeRolePlayer(RolePlayer.create(role, player));
        tx.send(GrpcUtil.runConceptMethodRequest(conceptId, conceptMethod));
        tx.receive().ok();
        verify(concept.asRelationship()).removeRolePlayer(role, player);
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) Role(ai.grakn.concept.Role) Entity(ai.grakn.concept.Entity) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 59 with Role

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

the class TypeImpl method deleteAttribute.

/**
 * Helper method to delete a {@link AttributeType} which is possible linked to this {@link Type}.
 * The link to {@link AttributeType} is removed if <code>attributeToRemove</code> is in the candidate list
 * <code>attributeTypes</code>
 *
 * @param implicitType the {@link Schema.ImplicitType} which specifies which implicit {@link Role} should be removed
 * @param attributeTypes The list of candidate which potentially contains the {@link AttributeType} to remove
 * @param attributeToRemove the {@link AttributeType} to remove
 * @return the {@link Type} itself
 */
private T deleteAttribute(Schema.ImplicitType implicitType, Stream<AttributeType> attributeTypes, AttributeType attributeToRemove) {
    if (attributeTypes.anyMatch(a -> a.equals(attributeToRemove))) {
        Label label = implicitType.getLabel(attributeToRemove.getLabel());
        Role role = vertex().tx().getSchemaConcept(label);
        if (role != null)
            deletePlays(role);
    }
    return getThis();
}
Also used : Role(ai.grakn.concept.Role) Label(ai.grakn.concept.Label)

Example 60 with Role

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

the class TypeImpl method has.

/**
 * Creates a relation type which allows this type and a {@link ai.grakn.concept.Attribute} type to be linked.
 * @param attributeType The {@link AttributeType} which instances of this type should be allowed to play.
 * @param has the implicit relation type to build
 * @param hasValue the implicit role type to build for the {@link AttributeType}
 * @param hasOwner the implicit role type to build for the type
 * @param required Indicates if the {@link ai.grakn.concept.Attribute} is required on the entity
 * @return The {@link Type} itself
 */
private T has(AttributeType attributeType, Schema.ImplicitType has, Schema.ImplicitType hasValue, Schema.ImplicitType hasOwner, boolean required) {
    // Check if this is a met type
    checkSchemaMutationAllowed();
    // Check if attribute type is the meta
    if (Schema.MetaSchema.ATTRIBUTE.getLabel().equals(attributeType.getLabel())) {
        throw GraknTxOperationException.metaTypeImmutable(attributeType.getLabel());
    }
    Label attributeLabel = attributeType.getLabel();
    Role ownerRole = vertex().tx().putRoleTypeImplicit(hasOwner.getLabel(attributeLabel));
    Role valueRole = vertex().tx().putRoleTypeImplicit(hasValue.getLabel(attributeLabel));
    RelationshipType relationshipType = vertex().tx().putRelationTypeImplicit(has.getLabel(attributeLabel)).relates(ownerRole).relates(valueRole);
    // Linking with ako structure if present
    AttributeType attributeTypeSuper = attributeType.sup();
    Label superLabel = attributeTypeSuper.getLabel();
    if (!Schema.MetaSchema.ATTRIBUTE.getLabel().equals(superLabel)) {
        // Check to make sure we dont add plays edges to meta types accidentally
        Role ownerRoleSuper = vertex().tx().putRoleTypeImplicit(hasOwner.getLabel(superLabel));
        Role valueRoleSuper = vertex().tx().putRoleTypeImplicit(hasValue.getLabel(superLabel));
        RelationshipType relationshipTypeSuper = vertex().tx().putRelationTypeImplicit(has.getLabel(superLabel)).relates(ownerRoleSuper).relates(valueRoleSuper);
        // Create the super type edges from sub role/relations to super roles/relation
        ownerRole.sup(ownerRoleSuper);
        valueRole.sup(valueRoleSuper);
        relationshipType.sup(relationshipTypeSuper);
        // Make sure the supertype attribute is linked with the role as well
        ((AttributeTypeImpl) attributeTypeSuper).plays(valueRoleSuper);
    }
    this.plays(ownerRole, required);
    // TODO: Use explicit cardinality of 0-1 rather than just false
    ((AttributeTypeImpl) attributeType).plays(valueRole, false);
    return getThis();
}
Also used : Role(ai.grakn.concept.Role) AttributeType(ai.grakn.concept.AttributeType) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType)

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