com.blazegraph.gremlin.structure.BlazeVertex Maven / Gradle / Ivy
/**
Copyright (C) SYSTAP, LLC DBA Blazegraph 2006-2016. All rights reserved.
Contact:
SYSTAP, LLC DBA Blazegraph
2501 Calvert ST NW #106
Washington, DC 20008
[email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.blazegraph.gremlin.structure;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.apache.tinkerpop.gremlin.structure.VertexProperty.Cardinality;
import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
import org.openrdf.model.Literal;
import com.bigdata.rdf.model.BigdataURI;
import com.blazegraph.gremlin.util.CloseableIterator;
/**
* Concrete vertex implementation for BlazeGraph.
*
* Vertex existence is represented as one triples as follows:
*
*
* :vertexId rdf:type :label .
*
*
* Vertex properties are represented as follows:
*
*
* :vertexId :key "val" .
*
*
* @author mikepersonick
*/
public class BlazeVertex extends AbstractBlazeElement implements Vertex, BlazeElement {
/**
* Construct an instance.
*/
BlazeVertex(final BlazeGraph graph, final BigdataURI uri,
final BigdataURI label) {
super(graph, uri, label);
}
/**
* Strengthen return type. Vertex RDF id is its URI.
*/
@Override
public BigdataURI rdfId() {
return uri;
}
/**
* Pass through to {@link StringFactory#vertexString(Vertex)}
*/
@Override
public String toString() {
return StringFactory.vertexString(this);
}
/**
* Strengthen return type to {@link BlazeVertexProperty}.
*
* @see Vertex#property(String)
* @see BlazeVertex#properties(String...)
*/
@Override
public BlazeVertexProperty property(final String key) {
try (CloseableIterator> it = this.properties(key)) {
if (it.hasNext()) {
final VertexProperty property = it.next();
if (it.hasNext())
throw Vertex.Exceptions.multiplePropertiesExistForProvidedKey(key);
else
return (BlazeVertexProperty) property;
} else {
return EmptyBlazeVertexProperty.instance();
}
}
}
/**
* Strengthen return type to {@link BlazeVertexProperty} and use
* Cardinality.single by default.
*/
@Override
public BlazeVertexProperty property(final String key, final V val) {
return property(Cardinality.single, key, val);
}
/**
* Strengthen return type to {@link BlazeVertexProperty}.
*
* @see BlazeGraph#vertexProperty(BlazeVertex, Cardinality, String, Object, Object...)
*/
@Override
public BlazeVertexProperty property(final Cardinality cardinality,
final String key, final V val, final Object... kvs) {
ElementHelper.validateProperty(key, val);
if (ElementHelper.getIdValue(kvs).isPresent())
throw Vertex.Exceptions.userSuppliedIdsNotSupported();
return graph.vertexProperty(this, cardinality, key, val, kvs);
}
/**
* Strength return type to {@link CloseableIterator}. You MUST close this
* iterator when finished.
*/
@Override
public CloseableIterator> properties(String... keys) {
return graph.properties(this, keys);
}
/**
* @see Vertex#addEdge(String, Vertex, Object...)
* @see BlazeGraph#addEdge(BlazeVertex, BlazeVertex, String, Object...)
*/
@Override
public BlazeEdge addEdge(final String label, final Vertex to, final Object... kvs) {
if (to == null) throw Graph.Exceptions.argumentCanNotBeNull("vertex");
return graph.addEdge(this, (BlazeVertex) to, label, kvs);
}
/**
* Strength return type to {@link CloseableIterator}. You MUST close this
* iterator when finished.
*
* @see Vertex#edges(Direction, String...)
* @see BlazeGraph#edgesFromVertex(BlazeVertex, Direction, String...)
*/
@Override
public CloseableIterator edges(final Direction direction,
final String... edgeLabels) {
return graph.edgesFromVertex(this, direction, edgeLabels);
}
/**
* Strength return type to {@link CloseableIterator}. You MUST close this
* iterator when finished.
*
* @see Vertex#vertices(Direction, String...)
*/
@Override
public CloseableIterator vertices(final Direction direction,
final String... edgeLabels) {
return CloseableIterator.of(edges(direction, edgeLabels)
.stream()
.map(e -> {
final BlazeEdge be = (BlazeEdge) e;
return be.outVertex().equals(this) ? be.inVertex() : be.outVertex();
}));
}
/**
* @see Vertex#remove()
* @see BlazeGraph#remove(BlazeVertex)
*/
@Override
public void remove() {
graph.remove(this);
}
}