Search in sources :

Example 36 with Relationship

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();
}
Also used : Role(ai.grakn.concept.Role) Arrays(java.util.Arrays) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Type(ai.grakn.concept.Type) Cacheable(ai.grakn.kb.internal.cache.Cacheable) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) LabelId(ai.grakn.concept.LabelId) RelationshipType(ai.grakn.concept.RelationshipType) Cache(ai.grakn.kb.internal.cache.Cache) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) EdgeElement(ai.grakn.kb.internal.structure.EdgeElement) P(org.apache.tinkerpop.gremlin.process.traversal.P) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) Set(java.util.Set) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) VertexElement(ai.grakn.kb.internal.structure.VertexElement) Direction(org.apache.tinkerpop.gremlin.structure.Direction) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) Casting(ai.grakn.kb.internal.structure.Casting) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Concept(ai.grakn.concept.Concept) Relationship(ai.grakn.concept.Relationship) Stream(java.util.stream.Stream)

Example 37 with Relationship

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);
}
Also used : Role(ai.grakn.concept.Role) Arrays(java.util.Arrays) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Type(ai.grakn.concept.Type) Cacheable(ai.grakn.kb.internal.cache.Cacheable) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) LabelId(ai.grakn.concept.LabelId) RelationshipType(ai.grakn.concept.RelationshipType) Cache(ai.grakn.kb.internal.cache.Cache) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) EdgeElement(ai.grakn.kb.internal.structure.EdgeElement) P(org.apache.tinkerpop.gremlin.process.traversal.P) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) Set(java.util.Set) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) VertexElement(ai.grakn.kb.internal.structure.VertexElement) Direction(org.apache.tinkerpop.gremlin.structure.Direction) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) Casting(ai.grakn.kb.internal.structure.Casting) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) EdgeElement(ai.grakn.kb.internal.structure.EdgeElement) Schema(ai.grakn.util.Schema) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType)

Example 38 with Relationship

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"));
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) ConceptId(ai.grakn.concept.ConceptId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 39 with Relationship

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();
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 40 with Relationship

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();
}
Also used : Role(ai.grakn.concept.Role) Relationship(ai.grakn.concept.Relationship) Label(ai.grakn.concept.Label) Map(java.util.Map) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) TypeImpl(ai.grakn.kb.internal.concept.TypeImpl)

Aggregations

Relationship (ai.grakn.concept.Relationship)50 Test (org.junit.Test)36 Role (ai.grakn.concept.Role)27 EntityType (ai.grakn.concept.EntityType)22 RelationshipType (ai.grakn.concept.RelationshipType)22 Entity (ai.grakn.concept.Entity)15 Thing (ai.grakn.concept.Thing)11 Label (ai.grakn.concept.Label)9 HashSet (java.util.HashSet)9 Attribute (ai.grakn.concept.Attribute)8 ConceptId (ai.grakn.concept.ConceptId)8 GraknTx (ai.grakn.GraknTx)7 Set (java.util.Set)7 Concept (ai.grakn.concept.Concept)6 Stream (java.util.stream.Stream)6 Type (ai.grakn.concept.Type)5 Answer (ai.grakn.graql.admin.Answer)5 Schema (ai.grakn.util.Schema)5 Property (com.pholser.junit.quickcheck.Property)5 Collectors (java.util.stream.Collectors)5