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));
}
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);
}
}
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);
}
}
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();
}
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();
}
Aggregations