Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

rfe11493: Support writing N-Quads from an AGModel

<release-note>
rfe11493: Support writing N-Quads from an AGModel

With this change, the Jena adapter now supports writing to
N-Quads via AGModel.write(out, "N-QUADS").
</release-note>

Added prepush tests for writing to N-Quads.
make prepush passes.
make javadoc runs clean.

Change-Id: Ice8d74501ddb88b45dd61aebe21412f1fcfe17d7
Reviewed-on: https://gerrit.franz.com:9080/2124
Reviewed-by: John O'Rourke <jor@franz.com>
Reviewed-by: Ahmon Dancy <dancy@franz.com>
Tested-by: Kevin Layer <layer@franz.com>
  • Loading branch information...
commit d86937a2b7c22b42c88a739db063dc4f2c46d9b4 1 parent e69ff5d
Bill Millar authored dklayer committed
View
57 src/com/franz/agraph/jena/AGModel.java
@@ -10,16 +10,23 @@
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import org.openrdf.model.BNode;
import org.openrdf.repository.RepositoryException;
import org.openrdf.rio.RDFFormat;
+import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParseException;
+import org.openrdf.rio.RDFWriter;
+import org.openrdf.rio.ntriples.NTriplesWriter;
+import org.openrdf.rio.rdfxml.RDFXMLWriter;
+import org.openrdf.rio.turtle.TurtleWriter;
import com.franz.agraph.http.AGHttpRepoClient;
import com.franz.agraph.repository.AGRDFFormat;
import com.franz.agraph.repository.AGRepositoryConnection;
import com.franz.agraph.repository.AGValueFactory;
+import com.franz.openrdf.rio.nquads.NQuadsWriter;
import com.franz.util.Closeable;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.AnonId;
@@ -77,6 +84,56 @@ public AGModel read(InputStream reader, String base, String lang) {
return this;
}
+ /**
+ * <p>Write a serialized representation of this model in a specified language.
+ * </p>
+ * <p>The language in which to write the model is specified by the
+ * <code>lang</code> argument. Predefined values are "RDF/XML",
+ * "RDF/XML-ABBREV", "N-TRIPLE", "N-QUADS", "TURTLE", (and "TTL") and "N3". The default value,
+ * represented by <code>null</code>, is "RDF/XML".</p>
+ * @param out The output stream to which the RDF is written
+ * @param lang The output language
+ * @return This model
+ */
+ @Override
+ public Model write(OutputStream out, String lang)
+ {
+ return write(out,lang, "");
+ }
+
+ /**
+ * <p>Write a serialized representation of a model in a specified language.
+ * </p>
+ * <p>The language in which to write the model is specified by the
+ * <code>lang</code> argument. Predefined values are "RDF/XML",
+ * "RDF/XML-ABBREV", "N-TRIPLE", "N-QUADS", "TURTLE", (and "TTL") and "N3". The default value,
+ * represented by <code>null</code>, is "RDF/XML".</p>
+ * @param out The output stream to which the RDF is written
+ * @param base The base uri to use when writing relative URI's. <code>null</code>
+ * means use only absolute URI's. This is used for relative
+ * URIs that would be resolved against the document retrieval URL.
+ * For some values of <code>lang</code>, this value may be included in the output.
+ * @param lang The language in which the RDF should be written
+ * @return This model
+ */
+ @Override
+ public Model write(OutputStream out, String lang, String base) {
+ RDFWriter writer;
+ if (lang.contains("QUADS")) {
+ writer = new NQuadsWriter(out);
+ } else {
+ return super.write(out, lang, base);
+ }
+ try {
+ getGraph().getConnection().exportStatements(null, null, null, false, writer, getGraph().getGraphContexts());
+ } catch (RDFHandlerException e) {
+ throw new RuntimeException(e);
+ } catch (RepositoryException e) {
+ throw new RuntimeException(e);
+ }
+ return this;
+ }
+
/**
* Returns a new blank node with an AG-allocated id.
*
View
2  src/com/franz/openrdf/rio/nquads/NQuadsWriter.java
@@ -135,8 +135,8 @@ else if (obj instanceof Literal) {
// CONTEXT
if (context!=null) {
- writeResource(context);
writer.write(" ");
+ writeResource(context);
}
writer.write(" .");
View
7 src/test/NQuadsTests.java
@@ -10,6 +10,7 @@
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileOutputStream;
import junit.framework.Assert;
@@ -21,6 +22,7 @@
import com.franz.agraph.jena.AGGraphMaker;
import com.franz.agraph.jena.AGModel;
import com.franz.agraph.repository.AGRDFFormat;
+import com.franz.openrdf.rio.nquads.NQuadsWriter;
public class NQuadsTests extends AGAbstractTest {
@@ -43,6 +45,11 @@ public void nquads_jena_rfe10201() throws Exception {
AGModel model = closeLater( new AGModel(graph) );
model.read(new FileInputStream("src/test/example.nq"), null, "NQUADS");
Assert.assertEquals("expected size 10", 10, model.size());
+ model.write(new FileOutputStream("target/exampleModelWrite.nq"),"NQUADS");
+ model.removeAll();
+ Assert.assertEquals("expected size 0", 0, model.size());
+ model.read(new FileInputStream("target/exampleModelWrite.nq"), null, "NQUADS");
+ Assert.assertEquals("expected size 10", 10, model.size());
}
@Test
Please sign in to comment.
Something went wrong with that request. Please try again.