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 0f1c224 commit 049c24e
Show file tree
Hide file tree
Showing 26 changed files with 310 additions and 305 deletions.
54 changes: 32 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@
<maven.compiler.target>1.7</maven.compiler.target>
<!-- 1.6 does not work because of java.nio.file dependency -->
<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>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -48,39 +58,39 @@
<version>1.7.2</version>
</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-rdfxml</artifactId>
<version>${sesame.version}</version>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-rdfxml</artifactId>
<version>${rdf4j.version}</version>
</dependency>
<dependency>
<groupId>org.openrdf.sesame</groupId>
<artifactId>sesame-rio-turtle</artifactId>
<version>${sesame.version}</version>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-turtle</artifactId>
<version>${rdf4j.version}</version>
</dependency>
<dependency>
<groupId>org.openrdf.sesame</groupId>
<artifactId>sesame-rio-ntriples</artifactId>
<version>${sesame.version}</version>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-ntriples</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>
</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>
</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>org.apache.commons</groupId>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/trustyuri/AbstractTrustyUriModule.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.trustyuri;

import org.openrdf.model.URI;
import org.eclipse.rdf4j.model.IRI;

/**
* A partial implementation of a trusty URI module.
Expand All @@ -18,7 +18,7 @@ public AbstractTrustyUriModule() {
pattern = ".*[^A-Za-z0-9\\-_]" + getModuleId() + "[A-Za-z0-9\\-_]{" + getDataPartLength() + "}";
}

public boolean matches(URI uri) {
public boolean matches(IRI uri) {
return uri.stringValue().matches(pattern);
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/trustyuri/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.IOException;
import java.util.Arrays;

import org.openrdf.OpenRDFException;
import org.eclipse.rdf4j.RDF4JException;

import net.trustyuri.file.ProcessFile;
import net.trustyuri.rdf.CheckLargeRdf;
Expand All @@ -27,7 +27,7 @@ private Run() {} // no instances allowed
*
* @param args the command
*/
public static void main(String[] args) throws IOException, OpenRDFException, TrustyUriException {
public static void main(String[] args) throws IOException, RDF4JException, TrustyUriException {
run(args);
}

Expand All @@ -36,7 +36,7 @@ public static void main(String[] args) throws IOException, OpenRDFException, Tru
*
* @param command the command, as a String array
*/
public static void run(String[] command) throws IOException, OpenRDFException, TrustyUriException {
public static void run(String[] command) throws IOException, RDF4JException, TrustyUriException {
String cmd = command[0];
String[] cmdArgs = Arrays.copyOfRange(command, 1, command.length);
if (cmd.equals("CheckFile")) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/trustyuri/RunBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import java.nio.charset.Charset;
import java.nio.file.Files;

import org.openrdf.OpenRDFException;
import org.eclipse.rdf4j.RDF4JException;

public class RunBatch {

private RunBatch() {} // no instances allowed

public static void main(String[] args) throws IOException, OpenRDFException, TrustyUriException {
public static void main(String[] args) throws IOException, RDF4JException, TrustyUriException {
String batchFile = args[0];

BufferedReader reader = new BufferedReader(new FileReader(batchFile));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/trustyuri/TrustyUriModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.File;
import java.io.IOException;

import org.openrdf.model.URI;
import org.eclipse.rdf4j.model.IRI;

/**
* A trusty URI module handles a particular type of content (such as RDF graphs or the byte content
Expand Down Expand Up @@ -51,6 +51,6 @@ public interface TrustyUriModule {
* @param uri the URI
* @return true if the URI matches the format of this module
*/
public boolean matches(URI uri);
public boolean matches(IRI uri);

}
16 changes: 10 additions & 6 deletions src/main/java/net/trustyuri/TrustyUriResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.Optional;
import java.util.zip.GZIPInputStream;

import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.Rio;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.Rio;

public class TrustyUriResource {

Expand Down Expand Up @@ -106,11 +107,14 @@ public String getModuleId() {
}

public RDFFormat getFormat(RDFFormat defaultFormat) {
RDFFormat format = Rio.getParserFormatForMIMEType(getMimetype());
if (format == null) {
format = Rio.getParserFormatForFileName(getFilename(), defaultFormat);
Optional<RDFFormat> format = Rio.getParserFormatForMIMEType(getMimetype());
if (!format.isPresent()) {
format = Rio.getParserFormatForFileName(getFilename());
}
return format;
if (format.isPresent()) {
return format.get();
}
return null;
}

}
29 changes: 13 additions & 16 deletions src/main/java/net/trustyuri/rdf/CheckLargeRdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@
import java.util.Comparator;
import java.util.List;

import net.trustyuri.TrustyUriException;
import net.trustyuri.TrustyUriResource;

import org.openrdf.OpenRDFException;
import org.openrdf.model.Statement;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParser;
import org.openrdf.rio.Rio;
import org.openrdf.rio.helpers.RDFHandlerBase;
import org.openrdf.rio.helpers.RDFaParserSettings;
import org.eclipse.rdf4j.RDF4JException;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFHandlerException;
import org.eclipse.rdf4j.rio.RDFParser;
import org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler;

import com.google.code.externalsorting.ExternalSort;

import net.trustyuri.TrustyUriException;
import net.trustyuri.TrustyUriResource;

public class CheckLargeRdf {

public static void main(String[] args) throws IOException, TrustyUriException {
Expand Down Expand Up @@ -52,12 +50,11 @@ public boolean check() throws IOException, TrustyUriException {
md = RdfHasher.getDigest();
RDFFormat format = r.getFormat(RDFFormat.TURTLE);

RDFParser p = Rio.createParser(format);
p.getParserConfig().set(RDFaParserSettings.FAIL_ON_RDFA_UNDEFINED_PREFIXES, true);
RDFParser p = RdfUtils.getParser(format);
File sortInFile = new File(dir, fileName + ".temp.sort-in");
final FileOutputStream preOut = new FileOutputStream(sortInFile);
p.setRDFHandler(new RdfPreprocessor(new RDFHandlerBase() {
p.setRDFHandler(new RdfPreprocessor(new AbstractRDFHandler() {

@Override
public void handleStatement(Statement st) throws RDFHandlerException {
String s = SerStatementComparator.toString(st) + "\n";
Expand All @@ -72,7 +69,7 @@ public void handleStatement(Statement st) throws RDFHandlerException {
BufferedReader reader = new BufferedReader(r.getInputStreamReader(), 64*1024);
try {
p.parse(reader, "");
} catch (OpenRDFException ex) {
} catch (RDF4JException ex) {
throw new TrustyUriException(ex);
} finally {
reader.close();
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/net/trustyuri/rdf/CheckRdfGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;

import net.trustyuri.TrustyUriException;
import net.trustyuri.TrustyUriResource;
import net.trustyuri.TrustyUriUtils;

import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;

public class CheckRdfGraph {

public static void main(String[] args) throws IOException, TrustyUriException {
Expand All @@ -30,7 +30,7 @@ public static void main(String[] args) throws IOException, TrustyUriException {
c = new CheckRdfGraph(new File(fileName));
}
for (int i = 1 ; i < args.length ; i++) {
URI graphUri = new URIImpl(args[i]);
IRI graphUri = SimpleValueFactory.getInstance().createIRI(args[i]);
boolean valid = c.check(graphUri);
if (valid) {
System.out.println("Correct hash: " + getArtifactCode(graphUri));
Expand All @@ -57,7 +57,7 @@ private void init() throws IOException, TrustyUriException {
content = RdfUtils.load(r);
}

public boolean check(URI graphUri) throws TrustyUriException {
public boolean check(IRI graphUri) throws TrustyUriException {
String artifactCode = getArtifactCode(graphUri);
if (artifactCode == null) {
throw new TrustyUriException("Not a trusty URI: " + graphUri);
Expand All @@ -74,7 +74,7 @@ public boolean check(URI graphUri) throws TrustyUriException {
return artifactCode.equals(ac);
}

private static String getArtifactCode(URI graphUri) {
private static String getArtifactCode(IRI graphUri) {
return TrustyUriUtils.getArtifactCode(graphUri.stringValue());
}

Expand Down
23 changes: 10 additions & 13 deletions src/main/java/net/trustyuri/rdf/CheckSortedRdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
import java.io.IOException;
import java.security.MessageDigest;

import org.eclipse.rdf4j.RDF4JException;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFHandlerException;
import org.eclipse.rdf4j.rio.RDFParser;
import org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler;

import net.trustyuri.TrustyUriException;
import net.trustyuri.TrustyUriResource;

import org.openrdf.OpenRDFException;
import org.openrdf.model.Statement;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParser;
import org.openrdf.rio.Rio;
import org.openrdf.rio.helpers.RDFHandlerBase;
import org.openrdf.rio.helpers.RDFaParserSettings;

public class CheckSortedRdf {

public static void main(String[] args) throws IOException, TrustyUriException {
Expand Down Expand Up @@ -54,10 +52,9 @@ public boolean check() throws IOException, TrustyUriException {
}
RDFFormat format = r.getFormat(RDFFormat.TURTLE);

RDFParser p = Rio.createParser(format);
RDFParser p = RdfUtils.getParser(format);
previous = null;
p.getParserConfig().set(RDFaParserSettings.FAIL_ON_RDFA_UNDEFINED_PREFIXES, true);
p.setRDFHandler(new RdfPreprocessor(new RDFHandlerBase() {
p.setRDFHandler(new RdfPreprocessor(new AbstractRDFHandler() {

@Override
public void handleStatement(Statement st) throws RDFHandlerException {
Expand All @@ -74,7 +71,7 @@ public void handleStatement(Statement st) throws RDFHandlerException {
BufferedReader reader = new BufferedReader(r.getInputStreamReader(), 64*1024);
try {
p.parse(reader, "");
} catch (OpenRDFException ex) {
} catch (RDF4JException ex) {
throw new TrustyUriException(ex);
} finally {
reader.close();
Expand Down
Loading

0 comments on commit 049c24e

Please sign in to comment.