Skip to content

Commit

Permalink
Migrate from Sesame to RDF4J
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuhn committed Feb 8, 2019
1 parent 066ae13 commit cd430d1
Show file tree
Hide file tree
Showing 42 changed files with 563 additions and 564 deletions.
34 changes: 17 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sesame.version>2.9.0</sesame.version>
<rdf4j.version>2.4.4</rdf4j.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -32,32 +32,32 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrdf.sesame</groupId>
<artifactId>sesame-rio-trig</artifactId>
<version>${sesame.version}</version>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-trig</artifactId>
<version>${rdf4j.version}</version>
</dependency>
<dependency>
<groupId>org.openrdf.sesame</groupId>
<artifactId>sesame-rio-trix</artifactId>
<version>${sesame.version}</version>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-trix</artifactId>
<version>${rdf4j.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.openrdf.sesame</groupId>
<artifactId>sesame-rio-nquads</artifactId>
<version>${sesame.version}</version>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-nquads</artifactId>
<version>${rdf4j.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.openrdf.sesame</groupId>
<artifactId>sesame-rio-jsonld</artifactId>
<version>${sesame.version}</version>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-jsonld</artifactId>
<version>${rdf4j.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.openrdf.sesame</groupId>
<artifactId>sesame-repository-sparql</artifactId>
<version>${sesame.version}</version>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-repository-sparql</artifactId>
<version>${rdf4j.version}</version>
</dependency>
<dependency>
<groupId>com.google.collections</groupId>
Expand Down Expand Up @@ -102,7 +102,7 @@
<dependency>
<groupId>net.trustyuri</groupId>
<artifactId>trustyuri</artifactId>
<version>1.11</version>
<version>1.12-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/nanopub/CheckNanopub.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.rdf4j.RDF4JException;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.repository.sparql.SPARQLRepository;
import org.nanopub.MultiNanopubRdfHandler.NanopubHandler;
import org.nanopub.extra.security.LegacySignatureUtils;
import org.nanopub.extra.security.MalformedCryptoElementException;
import org.nanopub.extra.security.NanopubSignatureElement;
import org.nanopub.extra.security.SignatureUtils;
import org.nanopub.trusty.TrustyNanopubUtils;
import org.openrdf.OpenRDFException;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sparql.SPARQLRepository;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
Expand Down Expand Up @@ -84,7 +84,7 @@ public Report check() throws IOException {
sparqlRepo = new SPARQLRepository(sparqlEndpointUrl);
sparqlRepo.initialize();
}
Nanopub np = new NanopubImpl(sparqlRepo, new URIImpl(s));
Nanopub np = new NanopubImpl(sparqlRepo, SimpleValueFactory.getInstance().createIRI(s));
check(np);
} else {
if (verbose) {
Expand All @@ -105,7 +105,7 @@ public void handleNanopub(Nanopub np) {
report.countError();
}
}
} catch (OpenRDFException ex) {
} catch (RDF4JException ex) {
log("RDF ERROR: " + s + "\n");
if (logOut != null) ex.printStackTrace(logOut);
report.countError();
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/nanopub/CustomTrigWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import java.util.Set;

import org.apache.commons.io.output.NullOutputStream;
import org.openrdf.model.URI;
import org.openrdf.rio.trig.TriGWriter;
import org.openrdf.rio.turtle.TurtleUtil;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.rio.trig.TriGWriter;
import org.eclipse.rdf4j.rio.turtle.TurtleUtil;

/**
* @author Tobias Kuhn
Expand Down Expand Up @@ -41,7 +41,7 @@ public CustomTrigWriter(Set<String> usedPrefixes) {
}

@Override
protected void writeURI(URI uri) throws IOException {
protected void writeURI(IRI uri) throws IOException {
String uriString = uri.toString();

String prefix = namespaceTable.get(uriString);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/nanopub/CustomTrigWriterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.io.OutputStream;
import java.io.Writer;

import org.openrdf.rio.RDFWriter;
import org.openrdf.rio.trig.TriGWriterFactory;
import org.eclipse.rdf4j.rio.RDFWriter;
import org.eclipse.rdf4j.rio.trig.TriGWriterFactory;

/**
* @author Tobias Kuhn
Expand Down
44 changes: 22 additions & 22 deletions src/main/java/org/nanopub/HtmlWriter.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package org.nanopub;

import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Set;

import org.openrdf.model.BNode;
import org.openrdf.model.Literal;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.datatypes.XMLDatatypeUtil;
import org.openrdf.model.util.Literals;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.model.vocabulary.XMLSchema;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.helpers.BasicWriterSettings;
import org.openrdf.rio.turtle.TurtleUtil;
import org.openrdf.rio.turtle.TurtleWriter;

import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;
import org.eclipse.rdf4j.model.BNode;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.datatypes.XMLDatatypeUtil;
import org.eclipse.rdf4j.model.util.Literals;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.XMLSchema;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFHandlerException;
import org.eclipse.rdf4j.rio.helpers.BasicWriterSettings;
import org.eclipse.rdf4j.rio.turtle.TurtleUtil;
import org.eclipse.rdf4j.rio.turtle.TurtleWriter;

// Contains copied code from TurtleWriter, TrigWriter, and CustomTrigWriter

Expand Down Expand Up @@ -139,7 +139,7 @@ public void handleStatement(Statement st)
}

Resource subj = st.getSubject();
URI pred = st.getPredicate();
IRI pred = st.getPredicate();
Value obj = st.getObject();

try {
Expand Down Expand Up @@ -232,7 +232,7 @@ protected void closeActiveContext()
}
}

protected void writePredicate(URI predicate)
protected void writePredicate(IRI predicate)
throws IOException
{
if (predicate.equals(RDF.TYPE)) {
Expand All @@ -244,7 +244,7 @@ protected void writePredicate(URI predicate)
}
}

protected void writeURI(URI uri)
protected void writeURI(IRI uri)
throws IOException
{
String uriString = uri.toString();
Expand Down Expand Up @@ -345,7 +345,7 @@ protected void writeLiteral(Literal lit)
throws IOException
{
String label = lit.getLabel();
URI datatype = lit.getDatatype();
IRI datatype = lit.getDatatype();

if (getWriterConfig().get(BasicWriterSettings.PRETTY_PRINT)) {
if (XMLSchema.INTEGER.equals(datatype) || XMLSchema.DECIMAL.equals(datatype)
Expand Down Expand Up @@ -378,7 +378,7 @@ protected void writeLiteral(Literal lit)
if (Literals.isLanguageLiteral(lit)) {
// Append the literal's language
writer.write("@");
writer.write(lit.getLanguage());
writer.write(lit.getLanguage().get());
}
else if (!XMLSchema.STRING.equals(datatype) || !xsdStringToPlainLiteral()) {
// Append the literal's datatype (possibly written as an abbreviated
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/org/nanopub/MultiNanopubRdfHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@
import java.util.Set;
import java.util.zip.GZIPInputStream;

import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.RDFParser;
import org.openrdf.rio.Rio;
import org.openrdf.rio.helpers.RDFHandlerBase;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFHandlerException;
import org.eclipse.rdf4j.rio.RDFParseException;
import org.eclipse.rdf4j.rio.RDFParser;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler;

/**
* Handles files or streams with a sequence of nanopubs.
*
* @author Tobias Kuhn
*/
public class MultiNanopubRdfHandler extends RDFHandlerBase {
public class MultiNanopubRdfHandler extends AbstractRDFHandler {

public static void process(RDFFormat format, InputStream in, NanopubHandler npHandler)
throws IOException, RDFParseException, RDFHandlerException, MalformedNanopubException {
Expand All @@ -55,7 +55,7 @@ public static void process(RDFFormat format, File file, NanopubHandler npHandler

public static void process(File file, NanopubHandler npHandler)
throws IOException, RDFParseException, RDFHandlerException, MalformedNanopubException {
RDFFormat format = Rio.getParserFormatForFileName(file.getName(), RDFFormat.TRIG);
RDFFormat format = Rio.getParserFormatForFileName(file.getName()).orElse(null);
process(format, file, npHandler);
}

Expand All @@ -79,11 +79,11 @@ private static void process(RDFFormat format, InputStream in, File file, Nanopub

private NanopubHandler npHandler;

private URI headUri = null;
private URI nanopubUri = null;
private IRI headUri = null;
private IRI nanopubUri = null;
private boolean headComplete = false;
private Map<URI,Boolean> graphs = new HashMap<>();
private Map<URI,Map<URI,Boolean>> members = new HashMap<>();
private Map<IRI,Boolean> graphs = new HashMap<>();
private Map<IRI,Map<IRI,Boolean>> members = new HashMap<>();
private Set<Statement> statements = new LinkedHashSet<>();
private List<String> nsPrefixes = new ArrayList<>();
private Map<String,String> ns = new HashMap<>();
Expand All @@ -99,20 +99,20 @@ public MultiNanopubRdfHandler(NanopubHandler npHandler) {
public void handleStatement(Statement st) throws RDFHandlerException {
if (!headComplete) {
if (headUri == null) {
headUri = (URI) st.getContext();
headUri = (IRI) st.getContext();
if (headUri == null) {
throwMalformed("Triple without context found: " +
st.getSubject() + " " + st.getPredicate() + " " + st.getObject());
}
graphs.put(headUri, true);
}
if (headUri.equals(st.getContext())) {
URI p = st.getPredicate();
IRI p = st.getPredicate();
if (p.equals(RDF.TYPE) && st.getObject().equals(NANOPUB_TYPE_URI)) {
nanopubUri = (URI) st.getSubject();
nanopubUri = (IRI) st.getSubject();
}
if (p.equals(HAS_ASSERTION_URI) || p.equals(HAS_PROVENANCE_URI) || p.equals(HAS_PUBINFO_URI)) {
graphs.put((URI) st.getObject(), true);
graphs.put((IRI) st.getObject(), true);
}
} else {
if (nanopubUri == null) {
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/org/nanopub/Nanopub.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,52 @@
import java.util.Calendar;
import java.util.Set;

import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;

/**
* @author Tobias Kuhn
*/
public interface Nanopub {

// URIs in the nanopub namespace:
public static final URI NANOPUB_TYPE_URI = new URIImpl("http://www.nanopub.org/nschema#Nanopublication");
public static final URI HAS_ASSERTION_URI = new URIImpl("http://www.nanopub.org/nschema#hasAssertion");
public static final URI HAS_PROVENANCE_URI = new URIImpl("http://www.nanopub.org/nschema#hasProvenance");
public static final URI HAS_PUBINFO_URI = new URIImpl("http://www.nanopub.org/nschema#hasPublicationInfo");
public static final IRI NANOPUB_TYPE_URI = SimpleValueFactory.getInstance().createIRI("http://www.nanopub.org/nschema#Nanopublication");
public static final IRI HAS_ASSERTION_URI = SimpleValueFactory.getInstance().createIRI("http://www.nanopub.org/nschema#hasAssertion");
public static final IRI HAS_PROVENANCE_URI = SimpleValueFactory.getInstance().createIRI("http://www.nanopub.org/nschema#hasProvenance");
public static final IRI HAS_PUBINFO_URI = SimpleValueFactory.getInstance().createIRI("http://www.nanopub.org/nschema#hasPublicationInfo");

// URIs that link nanopublications:
public static final URI SUPERSEDES = new URIImpl("http://purl.org/nanopub/x/supersedes");
public static final IRI SUPERSEDES = SimpleValueFactory.getInstance().createIRI("http://purl.org/nanopub/x/supersedes");

public URI getUri();
public IRI getUri();

public URI getHeadUri();
public IRI getHeadUri();

public Set<Statement> getHead();

public URI getAssertionUri();
public IRI getAssertionUri();

public Set<Statement> getAssertion();

public URI getProvenanceUri();
public IRI getProvenanceUri();

public Set<Statement> getProvenance();

public URI getPubinfoUri();
public IRI getPubinfoUri();

public Set<Statement> getPubinfo();

public Set<URI> getGraphUris();
public Set<IRI> getGraphUris();

// TODO: Now that we have SimpleCreatorPattern and SimpleTimestampPattern,
// we might not need the following three methods anymore...

public Calendar getCreationTime();

public Set<URI> getAuthors();
public Set<IRI> getAuthors();

public Set<URI> getCreators();
public Set<IRI> getCreators();

public int getTripleCount();

Expand Down
Loading

0 comments on commit cd430d1

Please sign in to comment.