All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tinkerpop.gremlin.giraph.structure.GiraphVertex Maven / Gradle / Ivy

package com.tinkerpop.gremlin.giraph.structure;

import com.tinkerpop.gremlin.giraph.process.graph.step.sideEffect.GiraphGraphStep;
import com.tinkerpop.gremlin.process.Step;
import com.tinkerpop.gremlin.process.T;
import com.tinkerpop.gremlin.process.computer.GraphComputer;
import com.tinkerpop.gremlin.process.graph.GraphTraversal;
import com.tinkerpop.gremlin.process.graph.step.filter.HasStep;
import com.tinkerpop.gremlin.process.graph.step.sideEffect.IdentityStep;
import com.tinkerpop.gremlin.process.graph.step.sideEffect.StartStep;
import com.tinkerpop.gremlin.process.graph.util.DefaultGraphTraversal;
import com.tinkerpop.gremlin.process.util.TraversalHelper;
import com.tinkerpop.gremlin.structure.Compare;
import com.tinkerpop.gremlin.structure.Direction;
import com.tinkerpop.gremlin.structure.Edge;
import com.tinkerpop.gremlin.structure.Element;
import com.tinkerpop.gremlin.structure.Vertex;
import com.tinkerpop.gremlin.structure.VertexProperty;
import com.tinkerpop.gremlin.structure.util.HasContainer;
import com.tinkerpop.gremlin.structure.util.wrapped.WrappedVertex;
import com.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex;
import com.tinkerpop.gremlin.tinkergraph.structure.TinkerVertexProperty;
import com.tinkerpop.gremlin.util.StreamFactory;

import java.io.Serializable;
import java.util.Iterator;

/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class GiraphVertex extends GiraphElement implements Vertex, Serializable, WrappedVertex {

    protected GiraphVertex() {
    }

    public GiraphVertex(final TinkerVertex vertex, final GiraphGraph graph) {
        super(vertex, graph);
    }

    @Override
    public  VertexProperty property(final String key) {
        final VertexProperty vertexProperty = getBaseVertex().property(key);
        return vertexProperty.isPresent() ?
                new GiraphVertexProperty<>((TinkerVertexProperty) ((Vertex) this.tinkerElement).property(key), this) :
                VertexProperty.empty();
    }

    @Override
    public  VertexProperty property(final String key, final V value) {
        throw Element.Exceptions.propertyAdditionNotSupported();
    }

    @Override
    public Edge addEdge(final String label, final Vertex inVertex, final Object... keyValues) {
        throw Vertex.Exceptions.edgeAdditionsNotSupported();
    }

    @Override
    public GraphTraversal start() {
        final GraphTraversal traversal = new DefaultGraphTraversal(this.graph) {
            @Override
            public GraphTraversal submit(final GraphComputer computer) {
                final String label = TraversalHelper.getStart(this).getLabel();
                TraversalHelper.removeStep(TraversalHelper.getStart(this), this);
                if (TraversalHelper.isLabeled(label)) {
                    final Step identityStep = new IdentityStep(this);
                    identityStep.setLabel(label);
                    TraversalHelper.insertStep(identityStep, 0, this);
                }
                TraversalHelper.insertStep(new HasStep(this, new HasContainer(T.id, Compare.EQUAL, tinkerElement.id())), 0, this);
                TraversalHelper.insertStep(new GiraphGraphStep<>(this, Vertex.class, graph), 0, this);

                return super.submit(computer);
            }
        };
        traversal.sideEffects().setGraph(this.graph);
        return (GraphTraversal) traversal.addStep(new StartStep<>(traversal, this));
    }

    @Override
    public TinkerVertex getBaseVertex() {
        return (TinkerVertex) this.tinkerElement;
    }

    @Override
    public Vertex.Iterators iterators() {
        return this.iterators;
    }

    private final Vertex.Iterators iterators = new Iterators();

    protected class Iterators implements Vertex.Iterators, Serializable {

        @Override
        public Iterator vertices(final Direction direction, final int branchFactor, final String... labels) {
            return StreamFactory.stream(getBaseVertex().iterators().vertices(direction, branchFactor, labels)).map(v -> graph.v(v.id())).iterator();
        }

        @Override
        public Iterator edges(final Direction direction, final int branchFactor, final String... labels) {
            return StreamFactory.stream(getBaseVertex().iterators().edges(direction, branchFactor, labels)).map(e -> graph.e(e.id())).iterator();
        }

        @Override
        public  Iterator> properties(final String... propertyKeys) {
            return (Iterator) StreamFactory.stream(getBaseVertex().iterators().properties(propertyKeys))
                    .map(property -> new GiraphVertexProperty<>((TinkerVertexProperty) property, GiraphVertex.this)).iterator();
        }

        @Override
        public  Iterator> hiddens(final String... propertyKeys) {
            return (Iterator) StreamFactory.stream(getBaseVertex().iterators().hiddens(propertyKeys))
                    .map(property -> new GiraphVertexProperty<>((TinkerVertexProperty) property, GiraphVertex.this)).iterator();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy