Search in sources :

Example 1 with VertexElement

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

the class TypeImpl method addInstance.

/**
 * Utility method used to create an instance of this type
 *
 * @param instanceBaseType The base type of the instances of this type
 * @param producer The factory method to produce the instance
 * @param checkNeeded indicates if a check is necessary before adding the instance
 * @return A new instance
 */
V addInstance(Schema.BaseType instanceBaseType, BiFunction<VertexElement, T, V> producer, boolean isInferred, boolean checkNeeded) {
    if (checkNeeded)
        preCheckForInstanceCreation();
    if (isAbstract())
        throw GraknTxOperationException.addingInstancesToAbstractType(this);
    VertexElement instanceVertex = vertex().tx().addVertexElement(instanceBaseType);
    if (!Schema.MetaSchema.isMetaLabel(getLabel())) {
        vertex().tx().txCache().addedInstance(getId());
        if (isInferred)
            instanceVertex.property(Schema.VertexProperty.IS_INFERRED, true);
    }
    V instance = producer.apply(instanceVertex, getThis());
    assert instance != null : "producer should never return null";
    return instance;
}
Also used : VertexElement(ai.grakn.kb.internal.structure.VertexElement)

Example 2 with VertexElement

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

the class RelationshipEdge method reify.

@Override
public RelationshipReified reify() {
    LOG.debug("Reifying concept [" + getId() + "]");
    // Build the Relationship Vertex
    VertexElement relationVertex = edge().tx().addVertexElement(Schema.BaseType.RELATIONSHIP, getId());
    RelationshipReified relationReified = edge().tx().factory().buildRelationReified(relationVertex, type());
    // Delete the old edge
    delete();
    return relationReified;
}
Also used : VertexElement(ai.grakn.kb.internal.structure.VertexElement)

Example 3 with VertexElement

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

the class ConceptImpl method createShard.

// ----------------------------------- Sharding Functionality
public void createShard() {
    VertexElement shardVertex = vertex().tx().addVertexElement(Schema.BaseType.SHARD);
    Shard shard = vertex().tx().factory().buildShard(this, shardVertex);
    vertex().property(Schema.VertexProperty.CURRENT_SHARD, shard.id());
    currentShard.set(shard);
    // Updated the cached shard count if needed
    if (shardCount.isPresent()) {
        shardCount.set(shardCount() + 1);
    }
}
Also used : VertexElement(ai.grakn.kb.internal.structure.VertexElement) Shard(ai.grakn.kb.internal.structure.Shard)

Example 4 with VertexElement

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

the class ElementFactory method addVertexElement.

/**
 * Creates a new {@link VertexElement} with a {@link ConceptId} which can optionally be set.
 *
 * @param baseType The {@link Schema.BaseType}
 * @param conceptIds the optional {@link ConceptId} to set as the new {@link ConceptId}
 * @return a new {@link VertexElement}
 */
public VertexElement addVertexElement(Schema.BaseType baseType, ConceptId... conceptIds) {
    Vertex vertex = tx.getTinkerPopGraph().addVertex(baseType.name());
    String newConceptId = Schema.PREFIX_VERTEX + vertex.id().toString();
    if (conceptIds.length > 1) {
        throw new IllegalArgumentException("Cannot provide more than one concept id when creating a new concept");
    } else if (conceptIds.length == 1) {
        newConceptId = conceptIds[0].getValue();
    }
    vertex.property(Schema.VertexProperty.ID.name(), newConceptId);
    tx.txCache().writeOccurred();
    return new VertexElement(tx, vertex);
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexElement(ai.grakn.kb.internal.structure.VertexElement)

Example 5 with VertexElement

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

the class EmbeddedGraknTx method putSchemaConcept.

/**
 * This is a helper method which will either find or create a {@link SchemaConcept}.
 * When a new {@link SchemaConcept} is created it is added for validation through it's own creation method for
 * example {@link ai.grakn.kb.internal.concept.RoleImpl#create(VertexElement, Role)}.
 *
 * When an existing {@link SchemaConcept} is found it is build via it's get method such as
 * {@link ai.grakn.kb.internal.concept.RoleImpl#get(VertexElement)} and skips validation.
 *
 * Once the {@link SchemaConcept} is found or created a few checks for uniqueness and correct
 * {@link ai.grakn.util.Schema.BaseType} are performed.
 *
 * @param label The {@link Label} of the {@link SchemaConcept} to find or create
 * @param baseType The {@link Schema.BaseType} of the {@link SchemaConcept} to find or create
 * @param isImplicit a flag indicating if the label we are creating is for an implicit {@link Type} or not
 * @param newConceptFactory the factory to be using when creating a new {@link SchemaConcept}
 * @param <T> The type of {@link SchemaConcept} to return
 * @return a new or existing {@link SchemaConcept}
 */
private <T extends SchemaConcept> T putSchemaConcept(Label label, Schema.BaseType baseType, boolean isImplicit, Function<VertexElement, T> newConceptFactory) {
    checkSchemaMutationAllowed();
    // Get the type if it already exists otherwise build a new one
    SchemaConceptImpl schemaConcept = getSchemaConcept(convertToId(label));
    if (schemaConcept == null) {
        if (!isImplicit && label.getValue().startsWith(Schema.ImplicitType.RESERVED.getValue())) {
            throw GraknTxOperationException.invalidLabelStart(label);
        }
        VertexElement vertexElement = addTypeVertex(getNextId(), label, baseType);
        // Mark it as implicit here so we don't have to pass it down the constructors
        if (isImplicit) {
            vertexElement.property(Schema.VertexProperty.IS_IMPLICIT, true);
        }
        schemaConcept = SchemaConceptImpl.from(buildSchemaConcept(label, () -> newConceptFactory.apply(vertexElement)));
    } else if (!baseType.equals(schemaConcept.baseType())) {
        throw labelTaken(schemaConcept);
    }
    // noinspection unchecked
    return (T) schemaConcept;
}
Also used : REST(ai.grakn.util.REST) SchemaConceptImpl(ai.grakn.kb.internal.concept.SchemaConceptImpl) VertexElement(ai.grakn.kb.internal.structure.VertexElement)

Aggregations

VertexElement (ai.grakn.kb.internal.structure.VertexElement)8 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)2 SchemaConceptImpl (ai.grakn.kb.internal.concept.SchemaConceptImpl)1 Shard (ai.grakn.kb.internal.structure.Shard)1 REST (ai.grakn.util.REST)1