From: <mrp...@us...> - 2014-05-13 19:32:16
|
Revision: 8298 http://sourceforge.net/p/bigdata/code/8298 Author: mrpersonick Date: 2014-05-13 19:32:10 +0000 (Tue, 13 May 2014) Log Message: ----------- full blueprints integration commit Modified Paths: -------------- branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/test/com/bigdata/blueprints/TestBigdataGraphEmbedded.java branches/BIGDATA_RELEASE_1_3_0/bigdata-sails/src/java/com/bigdata/rdf/sail/webapp/RESTServlet.java Added Paths: ----------- branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataEdge.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataElement.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraph.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraphBulkLoad.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraphClient.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraphEmbedded.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraphFactory.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraphQuery.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataPredicate.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataRDFFactory.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataVertex.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BlueprintsRDFFactory.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/test/com/bigdata/blueprints/AbstractTestBigdataGraph.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/test/com/bigdata/blueprints/TestAll.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/test/com/bigdata/blueprints/TestBigdataGraphClient.java branches/BIGDATA_RELEASE_1_3_0/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataSailFactory.java branches/BIGDATA_RELEASE_1_3_0/bigdata-sails/src/java/com/bigdata/rdf/sail/webapp/BlueprintsServlet.java branches/BIGDATA_RELEASE_1_3_0/bigdata-sails/src/java/com/bigdata/rdf/sail/webapp/client/BigdataSailNSSWrapper.java branches/BIGDATA_RELEASE_1_3_0/build.xml Removed Paths: ------------- branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataEdge.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataElement.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraph.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraphBulkLoad.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraphClient.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraphEmbedded.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraphFactory.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataRDFFactory.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataVertex.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BlueprintsRDFFactory.java branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/edge.rq branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/edges.rq branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/edgesByProperty.rq branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/vertex.rq branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/test/com/bigdata/blueprints/TestBigdataGraphClient.java branches/BIGDATA_RELEASE_1_3_0/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataSailFactory.java branches/BIGDATA_RELEASE_1_3_0/build.xml Deleted: branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataEdge.java =================================================================== --- branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataEdge.java 2014-05-13 18:15:26 UTC (rev 8297) +++ branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataEdge.java 2014-05-13 19:32:10 UTC (rev 8298) @@ -1,107 +0,0 @@ -/** -Copyright (C) SYSTAP, LLC 2006-2014. All rights reserved. - -Contact: - SYSTAP, LLC - 4501 Tower Road - Greensboro, NC 27410 - lic...@bi... - -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.bigdata.blueprints; - -import java.util.Arrays; -import java.util.List; - -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.vocabulary.RDFS; - -import com.tinkerpop.blueprints.Direction; -import com.tinkerpop.blueprints.Edge; -import com.tinkerpop.blueprints.Vertex; - -/** - * Edge implementation that wraps an Edge statement and points to a - * {@link BigdataGraph} instance. - * - * @author mikepersonick - * - */ -public class BigdataEdge extends BigdataElement implements Edge { - - private static final List<String> blacklist = Arrays.asList(new String[] { - "id", "", "label" - }); - - protected final Statement stmt; - - public BigdataEdge(final Statement stmt, final BigdataGraph graph) { - super(stmt.getPredicate(), graph); - - this.stmt = stmt; - } - - @Override - public Object getId() { - return graph.factory.fromEdgeURI(uri); - } - - @Override - public void remove() { - graph.removeEdge(this); - } - - @Override - public String getLabel() { - return (String) graph.getProperty(uri, RDFS.LABEL); - } - - @Override - public Vertex getVertex(final Direction dir) throws IllegalArgumentException { - - if (dir == Direction.BOTH) { - throw new IllegalArgumentException(); - } - - final URI uri = (URI) - (dir == Direction.OUT ? stmt.getSubject() : stmt.getObject()); - - final String id = graph.factory.fromVertexURI(uri); - - return graph.getVertex(id); - - } - - @Override - public void setProperty(final String property, final Object val) { - - if (property == null || blacklist.contains(property)) { - throw new IllegalArgumentException(); - } - - super.setProperty(property, val); - - } - - @Override - public String toString() { - final URI s = (URI) stmt.getSubject(); - final URI p = (URI) stmt.getPredicate(); - final URI o = (URI) stmt.getObject(); - return "e["+p.getLocalName()+"]["+s.getLocalName()+"->"+o.getLocalName()+"]"; - } - -} Added: branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataEdge.java =================================================================== --- branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataEdge.java (rev 0) +++ branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataEdge.java 2014-05-13 19:32:10 UTC (rev 8298) @@ -0,0 +1,115 @@ +/** +Copyright (C) SYSTAP, LLC 2006-2014. All rights reserved. + +Contact: + SYSTAP, LLC + 4501 Tower Road + Greensboro, NC 27410 + lic...@bi... + +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.bigdata.blueprints; + +import java.util.Arrays; +import java.util.List; + +import org.openrdf.model.Statement; +import org.openrdf.model.URI; +import org.openrdf.model.vocabulary.RDFS; + +import com.tinkerpop.blueprints.Direction; +import com.tinkerpop.blueprints.Edge; +import com.tinkerpop.blueprints.Vertex; + +/** + * Edge implementation that wraps an Edge statement and points to a + * {@link BigdataGraph} instance. + * + * @author mikepersonick + * + */ +public class BigdataEdge extends BigdataElement implements Edge { + + private static final List<String> blacklist = Arrays.asList(new String[] { + "id", "", "label" + }); + + protected final Statement stmt; + + public BigdataEdge(final Statement stmt, final BigdataGraph graph) { + super(stmt.getPredicate(), graph); + + this.stmt = stmt; + } + + @Override + public Object getId() { + + return graph.factory.fromEdgeURI(uri); + + } + + @Override + public void remove() { + + graph.removeEdge(this); + + } + + @Override + public String getLabel() { + + return (String) graph.getProperty(uri, RDFS.LABEL); + + } + + @Override + public Vertex getVertex(final Direction dir) throws IllegalArgumentException { + + if (dir == Direction.BOTH) { + throw new IllegalArgumentException(); + } + + final URI uri = (URI) + (dir == Direction.OUT ? stmt.getSubject() : stmt.getObject()); + + final String id = graph.factory.fromVertexURI(uri); + + return graph.getVertex(id); + + } + + @Override + public void setProperty(final String prop, final Object val) { + + if (prop == null || blacklist.contains(prop)) { + throw new IllegalArgumentException(); + } + + super.setProperty(prop, val); + + } + + @Override + public String toString() { + + final URI s = (URI) stmt.getSubject(); + final URI p = (URI) stmt.getPredicate(); + final URI o = (URI) stmt.getObject(); + return "e["+p.getLocalName()+"]["+s.getLocalName()+"->"+o.getLocalName()+"]"; + + } + +} Property changes on: branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataEdge.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Deleted: branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataElement.java =================================================================== --- branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataElement.java 2014-05-13 18:15:26 UTC (rev 8297) +++ branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataElement.java 2014-05-13 19:32:10 UTC (rev 8298) @@ -1,134 +0,0 @@ -/** -Copyright (C) SYSTAP, LLC 2006-2014. All rights reserved. - -Contact: - SYSTAP, LLC - 4501 Tower Road - Greensboro, NC 27410 - lic...@bi... - -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.bigdata.blueprints; - -import java.util.Arrays; -import java.util.List; -import java.util.Set; - -import org.openrdf.model.Literal; -import org.openrdf.model.URI; - -import com.tinkerpop.blueprints.Element; - -/** - * Base class for {@link BigdataVertex} and {@link BigdataEdge}. Handles - * property-related methods. - * - * @author mikepersonick - * - */ -public abstract class BigdataElement implements Element { - - private static final List<String> blacklist = Arrays.asList(new String[] { - "id", "" - }); - - protected final URI uri; - protected final BigdataGraph graph; - - public BigdataElement(final URI uri, final BigdataGraph graph) { - this.uri = uri; - this.graph = graph; - } - - @Override - @SuppressWarnings("unchecked") - public <T> T getProperty(final String property) { - - final URI p = graph.factory.toPropertyURI(property); - - return (T) graph.getProperty(uri, p); - - } - - @Override - public Set<String> getPropertyKeys() { - - return graph.getPropertyKeys(uri); - - } - - @Override - @SuppressWarnings("unchecked") - public <T> T removeProperty(final String property) { - - final URI p = graph.factory.toPropertyURI(property); - - return (T) graph.removeProperty(uri, p); - - } - - @Override - public void setProperty(final String property, final Object val) { - - if (property == null || blacklist.contains(property)) { - throw new IllegalArgumentException(); - } - - final URI p = graph.factory.toPropertyURI(property); - - final Literal o = graph.factory.toLiteral(val); - - graph.setProperty(uri, p, o); - - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((graph == null) ? 0 : graph.hashCode()); - result = prime * result + ((uri == null) ? 0 : uri.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - BigdataElement other = (BigdataElement) obj; - if (graph == null) { - if (other.graph != null) - return false; - } else if (!graph.equals(other.graph)) - return false; - if (uri == null) { - if (other.uri != null) - return false; - } else if (!uri.equals(other.uri)) - return false; - return true; - } - - @Override - public String toString() { - return uri.toString(); - } - - -} Added: branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataElement.java =================================================================== --- branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataElement.java (rev 0) +++ branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataElement.java 2014-05-13 19:32:10 UTC (rev 8298) @@ -0,0 +1,154 @@ +/** +Copyright (C) SYSTAP, LLC 2006-2014. All rights reserved. + +Contact: + SYSTAP, LLC + 4501 Tower Road + Greensboro, NC 27410 + lic...@bi... + +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.bigdata.blueprints; + +import java.util.Arrays; +import java.util.List; +import java.util.Set; + +import org.openrdf.model.URI; + +import com.tinkerpop.blueprints.Element; + +/** + * Base class for {@link BigdataVertex} and {@link BigdataEdge}. Handles + * property-related methods. + * + * @author mikepersonick + * + */ +public abstract class BigdataElement implements Element { + + private static final List<String> blacklist = Arrays.asList(new String[] { + "id", "" + }); + + protected final URI uri; + protected final BigdataGraph graph; + + public BigdataElement(final URI uri, final BigdataGraph graph) { + this.uri = uri; + this.graph = graph; + } + + @Override + @SuppressWarnings("unchecked") + public <T> T getProperty(final String property) { + + return (T) graph.getProperty(uri, property); + + } + + @Override + public Set<String> getPropertyKeys() { + + return graph.getPropertyKeys(uri); + + } + + @Override + @SuppressWarnings("unchecked") + public <T> T removeProperty(final String property) { + + return (T) graph.removeProperty(uri, property); + + } + + @Override + public void setProperty(final String prop, final Object val) { + + if (prop == null || blacklist.contains(prop)) { + throw new IllegalArgumentException(); + } + + graph.setProperty(uri, prop, val); + + } + + /** + * Simple extension for multi-valued properties. + */ + public void addProperty(final String prop, final Object val) { + + if (prop == null || blacklist.contains(prop)) { + throw new IllegalArgumentException(); + } + + graph.addProperty(uri, prop, val); + + } + + /** + * Simple extension for multi-valued properties. + */ + @SuppressWarnings("unchecked") + public <T> List<T> getProperties(final String property) { + + return (List<T>) graph.getProperties(uri, property); + + } + + /** + * Generated code. + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((graph == null) ? 0 : graph.hashCode()); + result = prime * result + ((uri == null) ? 0 : uri.hashCode()); + return result; + } + + /** + * Generated code. + */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BigdataElement other = (BigdataElement) obj; + if (graph == null) { + if (other.graph != null) + return false; + } else if (!graph.equals(other.graph)) + return false; + if (uri == null) { + if (other.uri != null) + return false; + } else if (!uri.equals(other.uri)) + return false; + return true; + } + + @Override + public String toString() { + return uri.toString(); + } + + +} Property changes on: branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataElement.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Deleted: branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraph.java =================================================================== --- branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraph.java 2014-05-13 18:15:26 UTC (rev 8297) +++ branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraph.java 2014-05-13 19:32:10 UTC (rev 8298) @@ -1,851 +0,0 @@ -/** -Copyright (C) SYSTAP, LLC 2006-2014. All rights reserved. - -Contact: - SYSTAP, LLC - 4501 Tower Road - Greensboro, NC 27410 - lic...@bi... - -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.bigdata.blueprints; - -import info.aduna.iteration.CloseableIteration; - -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.UUID; - -import org.apache.commons.io.IOUtils; -import org.openrdf.OpenRDFException; -import org.openrdf.model.Literal; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.Value; -import org.openrdf.model.impl.StatementImpl; -import org.openrdf.model.impl.URIImpl; -import org.openrdf.model.vocabulary.RDF; -import org.openrdf.model.vocabulary.RDFS; -import org.openrdf.query.GraphQueryResult; -import org.openrdf.query.QueryLanguage; -import org.openrdf.repository.RepositoryConnection; -import org.openrdf.repository.RepositoryResult; - -import com.bigdata.rdf.store.BD; -import com.tinkerpop.blueprints.Direction; -import com.tinkerpop.blueprints.Edge; -import com.tinkerpop.blueprints.Features; -import com.tinkerpop.blueprints.Graph; -import com.tinkerpop.blueprints.GraphQuery; -import com.tinkerpop.blueprints.Vertex; -import com.tinkerpop.blueprints.util.DefaultGraphQuery; -import com.tinkerpop.blueprints.util.io.graphml.GraphMLReader; - -/** - * A base class for a Blueprints wrapper around a bigdata back-end. - * - * @author mikepersonick - * - */ -public abstract class BigdataGraph implements Graph { - - public static final URI VERTEX = new URIImpl(BD.NAMESPACE + "Vertex"); - - public static final URI EDGE = new URIImpl(BD.NAMESPACE + "Edge"); - -// final BigdataSailRepository repo; -// -// transient BigdataSailRepositoryConnection cxn; - - final BlueprintsRDFFactory factory; - -// public BigdataGraph(final BigdataSailRepository repo) { -// this(repo, BigdataRDFFactory.INSTANCE); -// } - - public BigdataGraph(//final BigdataSailRepository repo, - final BlueprintsRDFFactory factory) { -// try { -// this.repo = repo; -// this.cxn = repo.getUnisolatedConnection(); -// this.cxn.setAutoCommit(false); - this.factory = factory; -// } catch (RepositoryException ex) { -// throw new RuntimeException(ex); -// } - } - - public String toString() { - return getClass().getSimpleName().toLowerCase(); - } - - /** - * Post a GraphML file to the remote server. (Bulk-upload operation.) - */ - public void loadGraphML(final String file) throws Exception { - GraphMLReader.inputGraph(this, file); - } - - protected abstract RepositoryConnection cxn() throws Exception; - -// public BigdataSailRepositoryConnection getConnection() { -// return this.cxn; -// } -// -// public BlueprintsRDFFactory getFactory() { -// return this.factory; -// } - -// public Value getValue(final URI s, final URI p) { -// -// try { -// -// final RepositoryResult<Statement> result = -// cxn.getStatements(s, p, null, false); -// -// if (result.hasNext()) { -// -// final Value o = result.next().getObject(); -// -// if (result.hasNext()) { -// throw new RuntimeException(s -// + ": more than one value for p: " + p -// + ", did you mean to call getValues()?"); -// } -// -// return o; -// -// } -// -// return null; -// -// } catch (Exception ex) { -// throw new RuntimeException(ex); -// } -// -// } - - public Object getProperty(final URI s, final URI p) { - - try { - - final RepositoryResult<Statement> result = - cxn().getStatements(s, p, null, false); - - if (result.hasNext()) { - - final Value value = result.next().getObject(); - - if (result.hasNext()) { - throw new RuntimeException(s - + ": more than one value for p: " + p - + ", did you mean to call getValues()?"); - } - - if (!(value instanceof Literal)) { - throw new RuntimeException("not a property: " + value); - } - - final Literal lit = (Literal) value; - - return factory.fromLiteral(lit); - - } - - return null; - - } catch (Exception ex) { - throw new RuntimeException(ex); - } - - } - -// public List<Value> getValues(final URI s, final URI p) { -// -// try { -// -// final RepositoryResult<Statement> result = -// cxn().getStatements(s, p, null, false); -// -// final List<Value> values = new LinkedList<Value>(); -// -// while (result.hasNext()) { -// -// final Value o = result.next().getObject(); -// -// values.add(o); -// -// } -// -// return values; -// -// } catch (Exception ex) { -// throw new RuntimeException(ex); -// } -// -// } - - public List<Object> getProperties(final URI s, final URI p) { - - try { - - final RepositoryResult<Statement> result = - cxn().getStatements(s, p, null, false); - - final List<Object> props = new LinkedList<Object>(); - - while (result.hasNext()) { - - final Value value = result.next().getObject(); - - if (!(value instanceof Literal)) { - throw new RuntimeException("not a property: " + value); - } - - final Literal lit = (Literal) value; - - props.add(factory.fromLiteral(lit)); - - } - - return props; - - } catch (Exception ex) { - throw new RuntimeException(ex); - } - - } - - public Set<String> getPropertyKeys(final URI s) { - - try { - - final RepositoryResult<Statement> result = - cxn().getStatements(s, null, null, false); - - final Set<String> properties = new LinkedHashSet<String>(); - - while (result.hasNext()) { - - final Statement stmt = result.next(); - - if (!(stmt.getObject() instanceof Literal)) { - continue; - } - - if (stmt.getPredicate().equals(RDFS.LABEL)) { - continue; - } - - final String p = - factory.fromPropertyURI(stmt.getPredicate()); - - properties.add(p); - - } - - return properties; - - } catch (Exception ex) { - throw new RuntimeException(ex); - } - - } - - public Object removeProperty(final URI s, final URI p) { - - try { - - final Object oldVal = getProperty(s, p); - - cxn().remove(s, p, null); - - return oldVal; - - } catch (Exception e) { - throw new RuntimeException(e); - } - - } - - public void setProperty(final URI s, final URI p, final Literal o) { - - try { - - cxn().remove(s, p, null); - - cxn().add(s, p, o); - - } catch (Exception e) { - throw new RuntimeException(e); - } - - } - - @Override - public Edge addEdge(final Object key, final Vertex from, final Vertex to, - final String label) { - - if (label == null) { - throw new IllegalArgumentException(); - } - - final String eid = key != null ? key.toString() : UUID.randomUUID().toString(); - - final URI edgeURI = factory.toEdgeURI(eid); - - if (key != null) { - - final Edge edge = getEdge(key); - - if (edge != null) { - if (!(edge.getVertex(Direction.OUT).equals(from) && - (edge.getVertex(Direction.OUT).equals(to)))) { - throw new IllegalArgumentException("edge already exists: " + key); - } - } - - } - - try { - -// if (cxn().hasStatement(edgeURI, RDF.TYPE, EDGE, false)) { -// throw new IllegalArgumentException("edge " + eid + " already exists"); -// } - - final URI fromURI = factory.toVertexURI(from.getId().toString()); - final URI toURI = factory.toVertexURI(to.getId().toString()); - - cxn().add(fromURI, edgeURI, toURI); - cxn().add(edgeURI, RDF.TYPE, EDGE); - cxn().add(edgeURI, RDFS.LABEL, factory.toLiteral(label)); - - return new BigdataEdge(new StatementImpl(fromURI, edgeURI, toURI), this); - - } catch (Exception ex) { - throw new RuntimeException(ex); - } - - } - - @Override - public Vertex addVertex(final Object key) { - - try { - - final String vid = key != null ? - key.toString() : UUID.randomUUID().toString(); - - final URI uri = factory.toVertexURI(vid); - -// if (cxn().hasStatement(vertexURI, RDF.TYPE, VERTEX, false)) { -// throw new IllegalArgumentException("vertex " + vid + " already exists"); -// } - - cxn().add(uri, RDF.TYPE, VERTEX); - - return new BigdataVertex(uri, this); - - } catch (Exception ex) { - throw new RuntimeException(ex); - } - - } - - @Override - public Edge getEdge(final Object key) { - - if (key == null) - throw new IllegalArgumentException(); - - try { - - final URI edge = factory.toEdgeURI(key.toString()); - - final RepositoryResult<Statement> result = - cxn().getStatements(null, edge, null, false); - - if (result.hasNext()) { - - final Statement stmt = result.next(); - - if (result.hasNext()) { - throw new RuntimeException( - "duplicate edge: " + key); - } - - return new BigdataEdge(stmt, this); - - } - - return null; - - } catch (Exception ex) { - throw new RuntimeException(ex); - } - - } - - @Override - public Iterable<Edge> getEdges() { - - final URI wild = null; - return getEdges(wild, wild); - - } - - public Iterable<Edge> getEdges(final URI s, final URI o, final String... labels) { - - try { - -// final RepositoryResult<Statement> result = -// cxn().getStatements(s, p, o, false); -// -// return new EdgeIterable(result); - - final StringBuilder sb = new StringBuilder(); - sb.append("construct { ?from ?edge ?to . } where {\n"); - sb.append("?edge rdf:type bd:Edge . ?from ?edge ?to .\n"); - if (labels != null && labels.length > 0) { - if (labels.length == 1) { - sb.append("?edge rdfs:label \"").append(labels[0]).append("\" .\n"); - } else { - sb.append("?edge rdfs:label ?label .\n"); - sb.append("filter(?label in ("); - for (String label : labels) { - sb.append("\""+label+"\", "); - } - sb.setLength(sb.length()-2); - sb.append(")) .\n"); - } - } - sb.append("}"); - - final String queryStr = sb.toString() - .replace("?from", s != null ? "<"+s+">" : "?from") - .replace("?to", o != null ? "<"+o+">" : "?to"); - - final org.openrdf.query.GraphQuery query = - cxn().prepareGraphQuery(QueryLanguage.SPARQL, queryStr); - - final GraphQueryResult stmts = query.evaluate(); - - return new EdgeIterable(stmts); - - } catch (Exception ex) { - throw new RuntimeException(ex); - } - - } - - public Iterable<Vertex> getVertices(final URI s, final URI o, - final String... labels) { - - if (s != null && o != null) { - throw new IllegalArgumentException(); - } - - if (s == null && o == null) { - throw new IllegalArgumentException(); - } - - try { - -// final RepositoryResult<Statement> result = -// cxn().getStatements(s, null, o, false); -// -// return new VertexIterable(result, s == null); - - final StringBuilder sb = new StringBuilder(); - sb.append("construct { ?from ?edge ?to . } where {\n"); - sb.append("?edge rdf:type bd:Edge . ?from ?edge ?to .\n"); - if (labels != null && labels.length > 0) { - if (labels.length == 1) { - sb.append("?edge rdfs:label \"").append(labels[0]).append("\" .\n"); - } else { - sb.append("?edge rdfs:label ?label .\n"); - sb.append("filter(?label in ("); - for (String label : labels) { - sb.append("\""+label+"\", "); - } - sb.setLength(sb.length()-2); - sb.append(")) .\n"); - } - } - sb.append("}"); - - final String queryStr = sb.toString() - .replace("?from", s != null ? "<"+s+">" : "?from") - .replace("?to", o != null ? "<"+o+">" : "?to"); - - final org.openrdf.query.GraphQuery query = - cxn().prepareGraphQuery(QueryLanguage.SPARQL, queryStr); - - final GraphQueryResult stmts = query.evaluate(); - - return new VertexIterable(stmts, s == null); - - } catch (Exception ex) { - throw new RuntimeException(ex); - } - - } - - public final <T> Iterable<T> fuse(final Iterable<T>... args) { - - return new FusedIterable<T>(args); - } - - - @Override - public Iterable<Edge> getEdges(final String prop, final Object val) { - - final URI p = factory.toPropertyURI(prop); - final Literal o = factory.toLiteral(val); - - try { - - final String queryStr = IOUtils.toString( - getClass().getResourceAsStream("edgesByProperty.rq")) - .replace("?prop", "<"+p+">") - .replace("?val", o.toString()); - - final org.openrdf.query.GraphQuery query = - cxn().prepareGraphQuery(QueryLanguage.SPARQL, queryStr); - - final GraphQueryResult stmts = query.evaluate(); - - return new EdgeIterable(stmts); - - } catch (Exception ex) { - throw new RuntimeException(ex); - } - - } - - @Override - public Features getFeatures() { - - return FEATURES; - - } - - @Override - public Vertex getVertex(final Object key) { - - if (key == null) - throw new IllegalArgumentException(); - - final URI uri = factory.toVertexURI(key.toString()); - try { - if (cxn().hasStatement(uri, RDF.TYPE, VERTEX, false)) { - return new BigdataVertex(uri, this); - } - return null; - } catch (Exception ex) { - throw new RuntimeException(ex); - } - - } - - @Override - public Iterable<Vertex> getVertices() { - - try { - final RepositoryResult<Statement> result = - cxn().getStatements(null, RDF.TYPE, VERTEX, false); - return new VertexIterable(result, true); - } catch (Exception ex) { - throw new RuntimeException(ex); - } - - } - - @Override - public Iterable<Vertex> getVertices(String prop, Object val) { - - final URI p = factory.toPropertyURI(prop); - final Literal o = factory.toLiteral(val); - try { - final RepositoryResult<Statement> result = - cxn().getStatements(null, p, o, false); - return new VertexIterable(result, true); - } catch (Exception ex) { - throw new RuntimeException(ex); - } - - } - - @Override - public GraphQuery query() { - return new DefaultGraphQuery(this); - } - - @Override - public void removeEdge(final Edge edge) { - try { - final URI uri = factory.toURI(edge); - if (!cxn().hasStatement(uri, RDF.TYPE, EDGE, false)) { - throw new IllegalStateException(); - } - final URI wild = null; - // remove the edge statement - cxn().remove(wild, uri, wild); - // remove its properties - cxn().remove(uri, wild, wild); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - @Override - public void removeVertex(final Vertex vertex) { - try { - final URI uri = factory.toURI(vertex); - if (!cxn().hasStatement(uri, RDF.TYPE, VERTEX, false)) { - throw new IllegalStateException(); - } - final URI wild = null; - // remove outgoing links and properties - cxn().remove(uri, wild, wild); - // remove incoming links - cxn().remove(wild, wild, uri); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - -// @Override -// public void commit() { -// try { -// cxn().commit(); -// } catch (RepositoryException e) { -// throw new RuntimeException(e); -// } -// } -// -// @Override -// public void rollback() { -// try { -// cxn().rollback(); -// cxn.close(); -// cxn = repo.getUnisolatedConnection(); -// cxn.setAutoCommit(false); -// } catch (RepositoryException e) { -// throw new RuntimeException(e); -// } -// } -// -// @Override -// public void shutdown() { -// try { -// cxn.close(); -// repo.shutDown(); -// } catch (RepositoryException e) { -// throw new RuntimeException(e); -// } -// } -// -// @Override -// @Deprecated -// public void stopTransaction(Conclusion arg0) { -// } - - public class VertexIterable implements Iterable<Vertex>, Iterator<Vertex> { - - private final CloseableIteration<Statement, ? extends OpenRDFException> stmts; - - private final boolean subject; - - private final List<Vertex> cache; - - public VertexIterable( - final CloseableIteration<Statement, ? extends OpenRDFException> stmts, - final boolean subject) { - this.stmts = stmts; - this.subject = subject; - this.cache = new LinkedList<Vertex>(); - } - - @Override - public boolean hasNext() { - try { - return stmts.hasNext(); - } catch (OpenRDFException e) { - throw new RuntimeException(e); - } - } - - @Override - public Vertex next() { - try { - final Statement stmt = stmts.next(); - final URI v = (URI) - (subject ? stmt.getSubject() : stmt.getObject()); - if (!hasNext()) { - stmts.close(); - } - final Vertex vertex = new BigdataVertex(v, BigdataGraph.this); - cache.add(vertex); - return vertex; - } catch (OpenRDFException e) { - throw new RuntimeException(e); - } - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - - @Override - public Iterator<Vertex> iterator() { - return hasNext() ? this : cache.iterator(); - } - - } - - public class EdgeIterable implements Iterable<Edge>, Iterator<Edge> { - - private final CloseableIteration<Statement, ? extends OpenRDFException> stmts; - - private final List<Edge> cache; - - public EdgeIterable( - final CloseableIteration<Statement, ? extends OpenRDFException> stmts) { - this.stmts = stmts; - this.cache = new LinkedList<Edge>(); - } - - @Override - public boolean hasNext() { - try { - return stmts.hasNext(); - } catch (OpenRDFException e) { - throw new RuntimeException(e); - } - } - - @Override - public Edge next() { - try { - final Statement stmt = stmts.next(); - if (!hasNext()) { - stmts.close(); - } - final Edge edge = new BigdataEdge(stmt, BigdataGraph.this); - cache.add(edge); - return edge; - } catch (OpenRDFException e) { - throw new RuntimeException(e); - } - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - - @Override - public Iterator<Edge> iterator() { - return hasNext() ? this : cache.iterator(); - } - - } - - public class FusedIterable<T> implements Iterable<T>, Iterator<T> { - - private final Iterable<T>[] args; - - private transient int i = 0; - - private transient Iterator<T> curr; - - public FusedIterable(final Iterable<T>... args) { - this.args = args; - this.curr = args[0].iterator(); - } - - @Override - public boolean hasNext() { - if (curr.hasNext()) { - return true; - } - while (!curr.hasNext() && i < (args.length-1)) { - curr = args[++i].iterator(); - if (curr.hasNext()) { - return true; - } - } - return false; - } - - @Override - public T next() { - return curr.next(); - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - - @Override - public Iterator<T> iterator() { - return this; - } - - } - - protected static final Features FEATURES = new Features(); - - static { - - FEATURES.supportsSerializableObjectProperty = false; - FEATURES.supportsBooleanProperty = true; - FEATURES.supportsDoubleProperty = true; - FEATURES.supportsFloatProperty = true; - FEATURES.supportsIntegerProperty = true; - FEATURES.supportsPrimitiveArrayProperty = false; - FEATURES.supportsUniformListProperty = false; - FEATURES.supportsMixedListProperty = false; - FEATURES.supportsLongProperty = true; - FEATURES.supportsMapProperty = false; - FEATURES.supportsStringProperty = true; - - FEATURES.supportsDuplicateEdges = true; - FEATURES.supportsSelfLoops = true; - FEATURES.isPersistent = true; - FEATURES.isWrapper = false; - FEATURES.supportsVertexIteration = true; - FEATURES.supportsEdgeIteration = true; - FEATURES.supportsVertexIndex = false; - FEATURES.supportsEdgeIndex = false; - FEATURES.ignoresSuppliedIds = true; - FEATURES.supportsTransactions = false; - FEATURES.supportsIndices = true; - FEATURES.supportsKeyIndices = true; - FEATURES.supportsVertexKeyIndex = true; - FEATURES.supportsEdgeKeyIndex = true; - FEATURES.supportsEdgeRetrieval = true; - FEATURES.supportsVertexProperties = true; - FEATURES.supportsEdgeProperties = true; - FEATURES.supportsThreadedTransactions = false; - } - -} Added: branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraph.java =================================================================== --- branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraph.java (rev 0) +++ branches/BIGDATA_RELEASE_1_3_0/bigdata-blueprints/src/java/com/bigdata/blueprints/BigdataGraph.java 2014-05-13 19:32:10 UTC (rev 8298) @@ -0,0 +1,1017 @@ +/** +Copyright (C) SYSTAP, LLC 2006-2014. All rights reserved. + +Contact: + SYSTAP, LLC + 4501 Tower Road + Greensboro, NC 27410 + lic...@bi... + +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.bigdata.blueprints; + +import info.aduna.iteration.CloseableIteration; + +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +import org.openrdf.OpenRDFException; +import org.openrdf.model.Literal; +import org.openrdf.model.Statement; +import org.openrdf.model.URI; +import org.openrdf.model.Value; +import org.openrdf.model.impl.StatementImpl; +import org.openrdf.model.impl.URIImpl; +import org.openrdf.model.vocabulary.RDF; +import org.openrdf.model.vocabulary.RDFS; +import org.openrdf.query.GraphQueryResult; +import org.openrdf.query.QueryLanguage; +import org.openrdf.repository.RepositoryConnection; +import org.openrdf.repository.RepositoryResult; + +import com.bigdata.rdf.store.BD; +import com.tinkerpop.blueprints.Direction; +import com.tinkerpop.blueprints.Edge; +import com.tinkerpop.blueprints.Features; +import com.tinkerpop.blueprints.Graph; +import com.tinkerpop.blueprints.GraphQuery; +import com.tinkerpop.blueprints.Vertex; +import com.tinkerpop.blueprints.util.io.graphml.GraphMLReader; + +/** + * A base class for a Blueprints wrapper around a bigdata back-end. + * + * @author mikepersonick + * + */ +public abstract class BigdataGraph implements Graph { + + /** + * URI used to represent a Vertex. + */ + public static final URI VERTEX = new URIImpl(BD.NAMESPACE + "Vertex"); + + /** + * URI used to represent a Edge. + */ + public static final URI EDGE = new URIImpl(BD.NAMESPACE + "Edge"); + + /** + * Factory for round-tripping between Blueprints data and RDF data. + */ + final BlueprintsRDFFactory factory; + + public BigdataGraph(final BlueprintsRDFFactory factory) { + + this.factory = factory; + + } + + /** + * For some reason this is part of the specification (i.e. part of the + * Blueprints test suite). + */ + public String toString() { + + return getClass().getSimpleName().toLowerCase(); + + } + + /** + * Different implementations will return different types of connections + * depending on the mode (client/server, embedded, read-only, etc.) + */ + protected abstract RepositoryConnection cxn() throws Exception; + + /** + * Return a single-valued property for an edge or vertex. + * + * @see {@link BigdataElement} + */ + public Object getProperty(final URI uri, final String prop) { + + return getProperty(uri, factory.toPropertyURI(prop)); + + } + + /** + * Return a single-valued property for an edge or vertex. + * + * @see {@link BigdataElement} + */ + public Object getProperty(final URI uri, final URI prop) { + + try { + + final RepositoryResult<Statement> result = + cxn().getStatements(uri, prop, null, false); + + if (result.hasNext()) { + + final Value value = result.next().getObject(); + + if (result.hasNext()) { + throw new RuntimeException(uri + + ": more than one value for p: " + prop + + ", did you mean to call getProperties()?"); + } + + if (!(value instanceof Literal)) { + throw new RuntimeException("not a property: " + value); + } + + final Literal lit = (Literal) value; + + return factory.fromLiteral(lit); + + } + + return null; + + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } + + /** + * Return a multi-valued property for an edge or vertex. + * + * @see {@link BigdataElement} + */ + public List<Object> getProperties(final URI uri, final String prop) { + + return getProperties(uri, factory.toPropertyURI(prop)); + + } + + + /** + * Return a multi-valued property for an edge or vertex. + * + * @see {@link BigdataElement} + */ + public List<Object> getProperties(final URI uri, final URI prop) { + + try { + + final RepositoryResult<Statement> result = + cxn().getStatements(uri, prop, null, false); + + final List<Object> props = new LinkedList<Object>(); + + while (result.hasNext()) { + + final Value value = result.next().getObject(); + + if (!(value instanceof Literal)) { + throw new RuntimeException("not a property: " + value); + } + + final Literal lit = (Literal) value; + + props.add(factory.fromLiteral(lit)); + + } + + return props; + + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } + + /** + * Return the property names for an edge or vertex. + * + * @see {@link BigdataElement} + */ + public Set<String> getPropertyKeys(final URI uri) { + + try { + + final RepositoryResult<Statement> result = + cxn().getStatements(uri, null, null, false); + + final Set<String> properties = new LinkedHashSet<String>(); + + while (result.hasNext()) { + + final Statement stmt = result.next(); + + if (!(stmt.getObject() instanceof Literal)) { + continue; + } + + if (stmt.getPredicate().equals(RDFS.LABEL)) { + continue; + } + + final String p = + factory.fromPropertyURI(stmt.getPredicate()); + + properties.add(p); + + } + + return properties; + + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } + + /** + * Remove all values for a particular property on an edge or vertex. + * + * @see {@link BigdataElement} + */ + public Object removeProperty(final URI uri, final String prop) { + + return removeProperty(uri, factory.toPropertyURI(prop)); + + } + + /** + * Remove all values for a particular property on an edge or vertex. + * + * @see {@link BigdataElement} + */ + public Object removeProperty(final URI uri, final URI prop) { + + try { + + final Object oldVal = getProperty(uri, prop); + + cxn().remove(uri, prop, null); + + return oldVal; + + } catch (Exception e) { + throw new RuntimeException(e); + } + + } + + /** + * Set a single-value property on an edge or vertex (remove the old + * value first). + * + * @see {@link BigdataElement} + */ + public void setProperty(final URI uri, final String prop, final Object val) { + + setProperty(uri, factory.toPropertyURI(prop), factory.toLiteral(val)); + + } + + /** + * Set a single-value property on an edge or vertex (remove the old + * value first). + * + * @see {@link BigdataElement} + */ + public void setProperty(final URI uri, final URI prop, final Literal val) { + + try { + + cxn().remove(uri, prop, null); + + cxn().add(uri, prop, val); + + } catch (Exception e) { + throw new RuntimeException(e); + } + + } + + /** + * Add a property on an edge or vertex (multi-value property extension). + * + * @see {@link BigdataElement} + */ + public void addProperty(final URI uri, final String prop, final Object val) { + + setProperty(uri, factory.toPropertyURI(prop), factory.toLiteral(val)); + + } + + /** + * Add a property on an edge or vertex (multi-value property extension). + * + * @see {@link BigdataElement} + */ + public void addProperty(final URI uri, final URI prop, final Literal val) { + + try { + + cxn().add(uri, prop, val); + + } catch (Exception e) { + throw new RuntimeException(e); + } + + } + + /** + * Post a GraphML file to the remote server. (Bulk-upload operation.) + */ + public void loadGraphML(final String file) throws Exception { + + GraphMLReader.inputGraph(this, file); + + } + + /** + * Add an edge. + */ + @Override + public Edge addEdge(final Object key, final Vertex from, final Vertex to, + final String label) { + + if (label == null) { + throw new IllegalArgumentException(); + } + + final String eid = key != null ? key.toString() : UUID.randomUUID().toString(); + + final URI edgeURI = factory.toEdgeURI(eid); + + if (key != null) { + + final Edge edge = getEdge(key); + + if (edge != null) { + if (!(edge.getVertex(Direction.OUT).equals(from) && + (edge.getVertex(Direction.OUT).equals(to)))) { + throw new IllegalArgumentException("edge already exists: " + key); + } + } + + } + + try { + + // do we need to check this? +// if (cxn().hasStatement(edgeURI, RDF.TYPE, EDGE, false)) { +// throw new IllegalArgumentException("edge " + eid + " already exists"); +// } + + final URI fromURI = factory.toVertexURI(from.getId().toString()); + final URI toURI = factory.toVertexURI(to.getId().toString()); + + cxn().add(fromURI, edgeURI, toURI); + cxn().add(edgeURI, RDF.TYPE, EDGE); + cxn().add(edgeURI, RDFS.LABEL, factory.toLiteral(label)); + + return new BigdataEdge(new StatementImpl(fromURI, edgeURI, toURI), this); + + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } + + /** + * Add a vertex. + */ + @Override + public Vertex addVertex(final Object key) { + + try { + + final String vid = key != null ? + key.toString() : UUID.randomUUID().toString(); + + final URI uri = factory.toVertexURI(vid); + + // do we need to check this? +// if (cxn().hasStatement(vertexURI, RDF.TYPE, VERTEX, false)) { +// throw new IllegalArgumentException("vertex " + vid + " already exists"); +// } + + cxn().add(uri, RDF.TYPE, VERTEX); + + return new BigdataVertex(uri, this); + + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } + + /** + * Lookup an edge. + */ + @Override + public Edge getEdge(final Object key) { + + if (key == null) + throw new IllegalArgumentException(); + + try { + + final URI edge = factory.toEdgeURI(key.toString()); + + final RepositoryResult<Statement> result = + cxn().getStatements(null, edge, null, false); + + if (result.hasNext()) { + + final Statement stmt = result.next(); + + if (result.hasNext()) { + throw new RuntimeException( + "duplicate edge: " + key); + } + + return new BigdataEdge(stmt, this); + + } + + return null; + + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } + + /** + * Iterate all edges. + */ + @Override + public Iterable<Edge> getEdges() { + + final URI wild = null; + return getEdges(wild, wild); + + } + + /** + * Find edges based on the from and to vertices and the edge labels, all + * optional parameters (can be null). The edge labels can be null to include + * all labels. + * <p> + * + * @param from + * the from vertex (null for wildcard) + * @param to + * the to vertex (null for wildcard) + * @param labels + * the edge labels to consider (optional) + * @return the edges matching the supplied criteria + */ + Iterable<Edge> getEdges(final URI from, final URI to, final String... labels) { + + final GraphQueryResult stmts = getElements(from, to, labels); + + return new EdgeIterable(stmts); + + } + + /** + * Translates the request to a high-performance SPARQL query: + * + * construct { + * ?from ?edge ?to . + * } where { + * ?edge rdf:type <Edge> . + * + * ?from ?edge ?to . + * + * # filter by edge label + * ?edge rdfs:label ?label . + * filter(?label in ("label1", "label2", ...)) . + * } + */ + protected GraphQueryResult getElements(final URI from, final URI to, + final String... labels) { + + final StringBuilder sb = new StringBuilder(); + sb.append("construct { ?from ?edge ?to . } where {\n"); + sb.append(" ?edge rdf:type bd:Edge .\n"); + sb.append(" ?from ?edge ?to .\n"); + if (labels != null && labels.length > 0) { + if (labels.length == 1) { + sb.append(" ?edge rdfs:label \"").append(labels[0]).append("\" .\n"); + } else { + sb.append(" ?edge rdfs:label ?label .\n"); + sb.append(" filter(?label in ("); + for (String label : labels) { + sb.append("\""+label+"\", "); + } + sb.setLength(sb.length()-2); + sb.append(")) .\n"); + } + } + sb.append("}"); + + // bind the from and/or to + final String queryStr = sb.toString() + .replace("?from", from != null ? "<"+from+">" : "?from") + .replace("?to", to != null ? "<"+to+">" : "?to"); + + try { + + final org.openrdf.query.GraphQuery query = + cxn().prepareGraphQuery(QueryLanguage.SPARQL, queryStr); + + final GraphQueryResult stmts = query.evaluate(); + + return stmts; + + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } + + /** + * Find edges based on a SPARQL construct query. The query MUST construct + * edge statements: + * <p> + * construct { ?from ?edge ?to } where { ... } + * + * @see {@link BigdataGraphQuery} + */ + Iterable<Edge> getEdges(final String queryStr) { + + try { + + final org.openrdf.query.GraphQuery query = + cxn().prepareGraphQuery(QueryLanguage.SPARQL, queryStr); + + final GraphQueryResult stmts = query.evaluate(); + + return new EdgeIterable(stmts); + + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } + + /** + * Find vertices based on the supplied from and to vertices and the edge + * labels. One or the other (from and to) must be null (wildcard), but not + * both. Use getEdges() for wildcards on both the from and to. The edge + * labels can be null to include all labels. + * + * @param from + * the from vertex (null for wildcard) + * @param to + * the to vertex (null for wildcard) + * @param labels + * the edge labels to consider (optional) + * @return + * the vertices matching the supplied criteria + */ + Iterable<Vertex> getVertices(final URI from, final URI to, + final String... labels) { + + if (from != null && to != null) { + throw new IllegalArgumentException(); + } + + if (from == null && to == null) { + throw new IllegalArgumentException(); + } + + final GraphQueryResult stmts = getElements(from, to, labels); + + return new VertexIterable(stmts, from == null); + + } + + /** + * Find vertices based on a SPARQL construct query. If the subject parameter + * is true, the vertices will be taken from the subject position of the + * constructed statements, otherwise they will be taken from the object + * position. + * + * @see {@link BigdataGraphQuery} + */ + Iterable<Vertex> getVertices(final String queryStr, final boolean subject) { + + try { + + final org.openrdf.query.GraphQuery query = + cxn().prepareGraphQuery(QueryLanguage.SPARQL, queryStr); + + final GraphQueryResult stmts = query.evaluate(); + + return new VertexIterable(stmts, subject); + + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } + + /** + * Find edges with the supplied property value. + * + * construct { + * ?from ?edge ?to . + * } + * where { + * ?edge <prop> <val> . + * ?from ?edge ?to . + * } + */ + @Override + public Iterable<Edge> getEdges(final String prop, final Object val) { + + final URI p = factory.toPropertyURI(prop); + final Literal o = factory.toLiteral(val); + + try { + + final StringBuilder sb = new StringBuilder(); + sb.append("construct { ?from ?edge ?to . } where {\n"); + sb.append(" ?edge <"+p+"> "+o+" .\n"); + sb.append(" ?from ?edge ?to .\n"); + sb.append("}"); + + final String queryStr = sb.toString(); + + return getEdges(queryStr); + + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } + + /** + * Lookup a vertex. + */ + @Override + public Vertex getVertex(final Object key) { + + if (key == null) + throw new IllegalArgumentException(); + + final URI uri = factory.toVertexURI(key.toString()); + + try { + + if (cxn().hasStatement(uri, RDF.TYPE, VERTEX, false)) { + return new BigdataVertex(uri, this); + } + + return null; + + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } + + + /** + * Iterate all vertices. + */ + @Override + public Iterable<Vertex> getVertices() { + + try { + + final RepositoryResult<Statement> result = + cxn().getStatements(null, RDF.TYPE, VERTEX, false); + + return new VertexIterable(result, true); + + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } + + /** + * Find vertices with the supplied property value. + */ + @Override + public Iterable<Vertex> getVertices(final String prop, final Object val) { + + final URI p = factory.toPropertyURI(prop); + final Literal o = factory.toLiteral(val); + + try { + + final RepositoryResult<Statement> result = + cxn().getStatements(null, p, o, false); + + return new VertexIterable(result, true); + + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + } + + /** + * Providing an override implementation for our GraphQuery to avoid the + * low-performance scan and filter paradigm. See {@link BigdataGraphQuery}. + */ + @Override + public GraphQuery query() { +// return new DefaultGraphQuery(this); + return new BigdataGraphQuery(this); + } + + /** + * Remove an edge and its properties. + */ + @Override + public void removeEdge(final Edge edge) { + + try { + + final URI uri = factory.toURI(edge); + + if (!cxn().hasStatement(uri, RDF.TYPE, EDGE, false)) { + throw new IllegalStateException(); + } + + final URI wild = null; + + // remove the edge statement + cxn().remove(wild, uri, wild); + + // remove its properties + cxn().remove(uri, wild, wild); + + } catch (Exception e) { + throw new RuntimeException(e); + ... [truncated message content] |