Search in sources :

Example 1 with EdgeElement

use of ai.grakn.kb.internal.structure.EdgeElement in project grakn by graknlabs.

the class RelationshipReified method putRolePlayerEdge.

/**
 * If the edge does not exist then it adds a {@link Schema.EdgeLabel#ROLE_PLAYER} edge from
 * this {@link Relationship} to a target {@link Thing} which is playing some {@link Role}.
 *
 * If the edge does exist nothing is done.
 *
 * @param role The {@link Role} being played by the {@link Thing} in this {@link Relationship}
 * @param toThing The {@link Thing} playing a {@link Role} in this {@link Relationship}
 */
public void putRolePlayerEdge(Role role, Thing toThing) {
    // Checking if the edge exists
    GraphTraversal<Vertex, Edge> traversal = vertex().tx().getTinkerTraversal().V().has(Schema.VertexProperty.ID.name(), this.getId().getValue()).outE(Schema.EdgeLabel.ROLE_PLAYER.getLabel()).has(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID.name(), this.type().getLabelId().getValue()).has(Schema.EdgeProperty.ROLE_LABEL_ID.name(), role.getLabelId().getValue()).as("edge").inV().has(Schema.VertexProperty.ID.name(), toThing.getId()).select("edge");
    if (traversal.hasNext()) {
        return;
    }
    // Role player edge does not exist create a new one
    EdgeElement edge = this.addEdge(ConceptVertex.from(toThing), Schema.EdgeLabel.ROLE_PLAYER);
    edge.property(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID, this.type().getLabelId().getValue());
    edge.property(Schema.EdgeProperty.ROLE_LABEL_ID, role.getLabelId().getValue());
    Casting casting = Casting.create(edge, owner, role, toThing);
    vertex().tx().txCache().trackForValidation(casting);
}
Also used : EdgeElement(ai.grakn.kb.internal.structure.EdgeElement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Casting(ai.grakn.kb.internal.structure.Casting) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 2 with EdgeElement

use of ai.grakn.kb.internal.structure.EdgeElement in project grakn by graknlabs.

the class SchemaConceptTest method whenSpecifyingTheResourceTypeOfAnEntityType_EnsureTheImplicitStructureIsCreated.

@Test
public void whenSpecifyingTheResourceTypeOfAnEntityType_EnsureTheImplicitStructureIsCreated() {
    Label resourceLabel = Label.of("Attribute Type");
    EntityType entityType = tx.putEntityType("Entity1");
    AttributeType attributeType = tx.putAttributeType("Attribute Type", AttributeType.DataType.STRING);
    // Implicit Names
    Label hasResourceOwnerLabel = Schema.ImplicitType.HAS_OWNER.getLabel(resourceLabel);
    Label hasResourceValueLabel = Schema.ImplicitType.HAS_VALUE.getLabel(resourceLabel);
    Label hasResourceLabel = Schema.ImplicitType.HAS.getLabel(resourceLabel);
    entityType.attribute(attributeType);
    RelationshipType relationshipType = tx.getRelationshipType(hasResourceLabel.getValue());
    Assert.assertEquals(hasResourceLabel, relationshipType.getLabel());
    Set<Label> roleLabels = relationshipType.relates().map(SchemaConcept::getLabel).collect(toSet());
    assertThat(roleLabels, containsInAnyOrder(hasResourceOwnerLabel, hasResourceValueLabel));
    assertThat(entityType.plays().collect(toSet()), containsInAnyOrder(tx.getRole(hasResourceOwnerLabel.getValue())));
    assertThat(attributeType.plays().collect(toSet()), containsInAnyOrder(tx.getRole(hasResourceValueLabel.getValue())));
    // Check everything is implicit
    assertTrue(relationshipType.isImplicit());
    relationshipType.relates().forEach(role -> assertTrue(role.isImplicit()));
    // Check that resource is not required
    EdgeElement entityPlays = ((EntityTypeImpl) entityType).vertex().getEdgesOfType(Direction.OUT, Schema.EdgeLabel.PLAYS).iterator().next();
    assertFalse(entityPlays.propertyBoolean(Schema.EdgeProperty.REQUIRED));
    EdgeElement resourcePlays = ((AttributeTypeImpl<?>) attributeType).vertex().getEdgesOfType(Direction.OUT, Schema.EdgeLabel.PLAYS).iterator().next();
    assertFalse(resourcePlays.propertyBoolean(Schema.EdgeProperty.REQUIRED));
}
Also used : EntityType(ai.grakn.concept.EntityType) EdgeElement(ai.grakn.kb.internal.structure.EdgeElement) AttributeType(ai.grakn.concept.AttributeType) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 3 with EdgeElement

use of ai.grakn.kb.internal.structure.EdgeElement in project grakn by graknlabs.

the class ConceptTest method whenGettingEdgesFromAConcept_EdgesFilteredByLabelAreReturned.

@Test
public void whenGettingEdgesFromAConcept_EdgesFilteredByLabelAreReturned() {
    EntityType entityType1 = tx.putEntityType("entity type");
    EntityTypeImpl entityType2 = (EntityTypeImpl) tx.putEntityType("entity type 1").sup(entityType1);
    EntityType entityType3 = tx.putEntityType("entity type 2").sup(entityType2);
    Set<EdgeElement> superType = entityType2.vertex().getEdgesOfType(Direction.OUT, Schema.EdgeLabel.SUB).collect(Collectors.toSet());
    Set<EdgeElement> subs = entityType2.vertex().getEdgesOfType(Direction.IN, Schema.EdgeLabel.SUB).collect(Collectors.toSet());
    assertThat(superType, is(not(empty())));
    assertThat(subs, is(not(empty())));
    superType.forEach(edge -> assertEquals(entityType1, tx.factory().buildConcept(edge.target())));
    subs.forEach(edge -> assertEquals(entityType3, tx.factory().buildConcept(edge.source())));
}
Also used : EntityType(ai.grakn.concept.EntityType) EdgeElement(ai.grakn.kb.internal.structure.EdgeElement) Test(org.junit.Test)

Example 4 with EdgeElement

use of ai.grakn.kb.internal.structure.EdgeElement 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 5 with EdgeElement

use of ai.grakn.kb.internal.structure.EdgeElement in project grakn by graknlabs.

the class TypeImpl method plays.

public T plays(Role role, boolean required) {
    checkSchemaMutationAllowed();
    // Update the internal cache of role types played
    cachedDirectPlays.ifPresent(map -> map.put(role, required));
    // Update the cache of types played by the role
    ((RoleImpl) role).addCachedDirectPlaysByType(this);
    EdgeElement edge = putEdge(ConceptVertex.from(role), Schema.EdgeLabel.PLAYS);
    if (required) {
        edge.property(Schema.EdgeProperty.REQUIRED, true);
    }
    return getThis();
}
Also used : EdgeElement(ai.grakn.kb.internal.structure.EdgeElement)

Aggregations

EdgeElement (ai.grakn.kb.internal.structure.EdgeElement)6 AttributeType (ai.grakn.concept.AttributeType)2 EntityType (ai.grakn.concept.EntityType)2 Label (ai.grakn.concept.Label)2 RelationshipType (ai.grakn.concept.RelationshipType)2 Casting (ai.grakn.kb.internal.structure.Casting)2 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)2 Test (org.junit.Test)2 Attribute (ai.grakn.concept.Attribute)1 Concept (ai.grakn.concept.Concept)1 ConceptId (ai.grakn.concept.ConceptId)1 LabelId (ai.grakn.concept.LabelId)1 Relationship (ai.grakn.concept.Relationship)1 Role (ai.grakn.concept.Role)1 Thing (ai.grakn.concept.Thing)1 Type (ai.grakn.concept.Type)1 GraknTxOperationException (ai.grakn.exception.GraknTxOperationException)1 Cache (ai.grakn.kb.internal.cache.Cache)1 Cacheable (ai.grakn.kb.internal.cache.Cacheable)1 ConceptVertex (ai.grakn.kb.internal.concept.ConceptVertex)1