use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class ThingImpl method deleteAttribute.
@Override
public T deleteAttribute(Attribute attribute) {
Role roleHasOwner = vertex().tx().getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(attribute.type().getLabel()));
Role roleKeyOwner = vertex().tx().getSchemaConcept(Schema.ImplicitType.KEY_OWNER.getLabel(attribute.type().getLabel()));
Role roleHasValue = vertex().tx().getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(attribute.type().getLabel()));
Role roleKeyValue = vertex().tx().getSchemaConcept(Schema.ImplicitType.KEY_VALUE.getLabel(attribute.type().getLabel()));
Stream<Relationship> relationships = relationships(filterNulls(roleHasOwner, roleKeyOwner));
relationships.filter(relationship -> {
Stream<Thing> rolePlayers = relationship.rolePlayers(filterNulls(roleHasValue, roleKeyValue));
return rolePlayers.anyMatch(rolePlayer -> rolePlayer.equals(attribute));
}).forEach(Concept::delete);
return getThis();
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class ThingImpl method attributeRelationship.
private Relationship attributeRelationship(Attribute attribute, boolean isInferred) {
Schema.ImplicitType has = Schema.ImplicitType.HAS;
Schema.ImplicitType hasValue = Schema.ImplicitType.HAS_VALUE;
Schema.ImplicitType hasOwner = Schema.ImplicitType.HAS_OWNER;
// Is this attribute a key to me?
if (type().keys().anyMatch(rt -> rt.equals(attribute.type()))) {
has = Schema.ImplicitType.KEY;
hasValue = Schema.ImplicitType.KEY_VALUE;
hasOwner = Schema.ImplicitType.KEY_OWNER;
}
Label label = attribute.type().getLabel();
RelationshipType hasAttribute = vertex().tx().getSchemaConcept(has.getLabel(label));
Role hasAttributeOwner = vertex().tx().getSchemaConcept(hasOwner.getLabel(label));
Role hasAttributeValue = vertex().tx().getSchemaConcept(hasValue.getLabel(label));
if (hasAttribute == null || hasAttributeOwner == null || hasAttributeValue == null || type().plays().noneMatch(play -> play.equals(hasAttributeOwner))) {
throw GraknTxOperationException.hasNotAllowed(this, attribute);
}
EdgeElement attributeEdge = addEdge(AttributeImpl.from(attribute), Schema.EdgeLabel.ATTRIBUTE);
if (isInferred)
attributeEdge.property(Schema.EdgeProperty.IS_INFERRED, true);
return vertex().tx().factory().buildRelation(attributeEdge, hasAttribute, hasAttributeOwner, hasAttributeValue);
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class ValidatorTest method whenDeletingRelations_EnsureGraphRemainsValid.
@Test
public void whenDeletingRelations_EnsureGraphRemainsValid() throws InvalidKBException {
// schema
EntityType person = tx.putEntityType("person");
EntityType movie = tx.putEntityType("movie");
RelationshipType cast = tx.putRelationshipType("cast");
Role feature = tx.putRole("feature");
Role actor = tx.putRole("actor");
cast.relates(feature).relates(actor);
person.plays(actor);
movie.plays(feature);
// add a single movie
Thing godfather = movie.addEntity();
// add many random actors
int n = 100;
for (int i = 0; i < n; i++) {
Thing newPerson = person.addEntity();
cast.addRelationship().addRolePlayer(actor, newPerson).addRolePlayer(feature, godfather);
}
tx.commit();
tx = EmbeddedGraknSession.create(tx.keyspace(), Grakn.IN_MEMORY).open(GraknTxType.WRITE);
// now try to delete all assertions and then the movie
godfather = tx.getEntityType("movie").instances().iterator().next();
Collection<Relationship> assertions = godfather.relationships().collect(Collectors.toSet());
Set<ConceptId> assertionIds = new HashSet<>();
for (Relationship a : assertions) {
assertionIds.add(a.getId());
a.delete();
}
godfather.delete();
tx.commit();
tx = EmbeddedGraknSession.create(tx.keyspace(), Grakn.IN_MEMORY).open(GraknTxType.WRITE);
assertionIds.forEach(id -> assertNull(tx.getConcept(id)));
// assert the movie is gone
assertNull(tx.getEntityType("godfather"));
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class ValidatorTest method whenARoleInARelationIsPlayedTwice_TheGraphIsValid.
@Test
public void whenARoleInARelationIsPlayedTwice_TheGraphIsValid() {
Role role1 = tx.putRole("role-1");
Role role2 = tx.putRole("role-2");
RelationshipType relationshipType = tx.putRelationshipType("my-relationship").relates(role1).relates(role2);
EntityType entityType = tx.putEntityType("my-entity").plays(role1);
Thing thing1 = entityType.addEntity();
Thing thing2 = entityType.addEntity();
Relationship relationship = relationshipType.addRelationship();
relationship.addRolePlayer(role1, thing1);
relationship.addRolePlayer(role1, thing2);
assertThat(relationship.rolePlayers(role1).collect(toSet()), hasItems(thing1, thing2));
tx.commit();
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class ValidateGlobalRules method validateInstancePlaysAllRequiredRoles.
/**
* @param thing The thing to be validated
* @return An error message if the thing does not have all the required resources
*/
static Optional<String> validateInstancePlaysAllRequiredRoles(Thing thing) {
TypeImpl<?, ?> currentConcept = (TypeImpl) thing.type();
while (currentConcept != null) {
Map<Role, Boolean> plays = currentConcept.directPlays();
for (Map.Entry<Role, Boolean> playsEntry : plays.entrySet()) {
if (playsEntry.getValue()) {
Role role = playsEntry.getKey();
// Assert there is a relationship for this type
Stream<Relationship> relationships = thing.relationships(role);
if (!CommonUtil.containsOnly(relationships, 1)) {
Label resourceTypeLabel = Schema.ImplicitType.explicitLabel(role.getLabel());
return Optional.of(VALIDATION_NOT_EXACTLY_ONE_KEY.getMessage(thing.getId(), resourceTypeLabel));
}
}
}
currentConcept = (TypeImpl) currentConcept.sup();
}
return Optional.empty();
}
Aggregations