dups = DuplicateStatementFinder.findDuplicateStatements(sc);
53 | assertEquals(2, dups.size());
54 | //...
55 |
56 | sc.rollback();
57 | sc.close();
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/deduplication-sail/src/test/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO, R
2 |
3 | log4j.appender.R=org.apache.log4j.ConsoleAppender
4 | log4j.appender.R.layout=org.apache.log4j.PatternLayout
5 | log4j.appender.R.layout.ConversionPattern=%-5p - %d{dd/MM/yyyy HH:mm:ss} - %m%n
6 |
7 | log4j.logger.org.openrdf=INFO
8 | log4j.logger.net.fortytwo.sesametools=INFO
9 |
--------------------------------------------------------------------------------
/linked-data-server/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | .classpath
3 | .project
4 | .settings
5 | *.iml
6 | *~
7 |
--------------------------------------------------------------------------------
/linked-data-server/src/main/java/net/fortytwo/sesametools/ldserver/GraphResource.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.ldserver;
2 |
3 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
4 | import org.eclipse.rdf4j.model.Namespace;
5 | import org.eclipse.rdf4j.model.Statement;
6 | import org.eclipse.rdf4j.model.IRI;
7 | import org.eclipse.rdf4j.rio.RDFFormat;
8 | import org.eclipse.rdf4j.sail.Sail;
9 | import org.eclipse.rdf4j.sail.SailConnection;
10 | import org.eclipse.rdf4j.sail.SailException;
11 | import org.restlet.data.MediaType;
12 | import org.restlet.representation.Representation;
13 | import org.restlet.representation.Variant;
14 | import org.restlet.resource.Get;
15 | import org.restlet.resource.ServerResource;
16 |
17 | import java.util.Collection;
18 | import java.util.LinkedList;
19 | import java.util.logging.Level;
20 | import java.util.logging.Logger;
21 |
22 | /**
23 | * Graph resources are information resources which (in the present schema) do not use suffixes identifying the RDF
24 | * format (e.g. .rdf or .ttl). Instead, they use content negotiation to serve an appropriate representation against
25 | * the IRI of the graph, without redirection.
26 | *
27 | * This conforms to the common expectation that RDF documents and corresponding named graphs have the same IRI.
28 | *
29 | * @author Joshua Shinavier (http://fortytwo.net)
30 | */
31 | public class GraphResource extends ServerResource {
32 | private static final Logger logger = Logger.getLogger(GraphResource.class.getName());
33 |
34 | protected String selfIRI;
35 |
36 | protected Sail sail;
37 |
38 | public GraphResource() {
39 | super();
40 |
41 | getVariants().addAll(RDFMediaTypes.getRDFVariants());
42 |
43 | sail = LinkedDataServer.getInstance().getSail();
44 | }
45 |
46 | @Override
47 | @Get
48 | public Representation get(final Variant entity) {
49 | MediaType type = entity.getMediaType();
50 | RDFFormat format = RDFMediaTypes.findRdfFormat(type);
51 | selfIRI = this.getRequest().getResourceRef().toString();
52 |
53 | try {
54 | IRI subject = sail.getValueFactory().createIRI(selfIRI);
55 | return getRDFRepresentation(subject, format);
56 | } catch (Throwable t) {
57 | t.printStackTrace();
58 | return null;
59 | }
60 | }
61 |
62 | private void addStatementsInGraph(final org.eclipse.rdf4j.model.Resource graph,
63 | final Collection statements,
64 | final SailConnection c) throws SailException {
65 | try (CloseableIteration extends Statement, SailException> stIter
66 | = c.getStatements(null, null, null, false, graph)) {
67 | while (stIter.hasNext()) {
68 | statements.add(stIter.next());
69 | }
70 | }
71 | }
72 |
73 | private Representation getRDFRepresentation(final IRI graph,
74 | final RDFFormat format) {
75 | try {
76 | Collection namespaces = new LinkedList<>();
77 | Collection statements = new LinkedList<>();
78 |
79 | SailConnection c = sail.getConnection();
80 | try {
81 | // Note: do NOT add graph or document metadata, as this document is to contain only those statements
82 | // asserted in the graph in question.
83 |
84 | // Add statements in this graph, preserving the graph component of the statements.
85 | addStatementsInGraph(graph, statements, c);
86 |
87 | // Select namespaces, for human-friendliness
88 | try (CloseableIteration extends Namespace, SailException> ns = c.getNamespaces()) {
89 | while (ns.hasNext()) {
90 | namespaces.add(ns.next());
91 | }
92 | }
93 | } finally {
94 | c.close();
95 | }
96 | return new RDFRepresentation(statements, namespaces, format);
97 |
98 | } catch (Throwable t) {
99 | logger.log(Level.WARNING, "failed to create RDF representation", t);
100 | t.printStackTrace(System.err);
101 |
102 | return null;
103 | }
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/linked-data-server/src/main/java/net/fortytwo/sesametools/ldserver/LinkedDataServer.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.ldserver;
2 |
3 | import net.fortytwo.sesametools.mappingsail.MappingSail;
4 | import net.fortytwo.sesametools.mappingsail.MappingSchema;
5 | import net.fortytwo.sesametools.mappingsail.RewriteRule;
6 | import org.eclipse.rdf4j.model.IRI;
7 | import org.eclipse.rdf4j.model.ValueFactory;
8 | import org.eclipse.rdf4j.sail.Sail;
9 | import org.restlet.Application;
10 |
11 | /**
12 | * A RESTful web service which publishes the contents of a Sail data store as Linked Data.
13 | *
14 | * @author Joshua Shinavier (http://fortytwo.net)
15 | */
16 | public class LinkedDataServer extends Application {
17 |
18 | private final Sail sail;
19 | private final IRI datasetURI;
20 |
21 | private static LinkedDataServer SINGLETON = null;
22 |
23 | /**
24 | * @param baseSail the data store published by this server
25 | * @param internalBaseURI the base URI of resources within the data store
26 | * @param externalBaseURI the base URI of resources as they are to be seen in the Linked Data
27 | */
28 | public LinkedDataServer(final Sail baseSail,
29 | final String internalBaseURI,
30 | final String externalBaseURI) {
31 | this(baseSail, internalBaseURI, externalBaseURI, null);
32 | }
33 |
34 | /**
35 | * @param baseSail the data store published by this server
36 | * @param internalBaseURI the base URI of resources within the data store
37 | * @param externalBaseURI the base URI of resources as they are to be seen in the Linked Data
38 | * @param dataset the URI of the data set to be published.
39 | * This allows resource descriptions to be associated
40 | * with metadata about the data set which contains them.
41 | */
42 | public LinkedDataServer(final Sail baseSail,
43 | final String internalBaseURI,
44 | final String externalBaseURI,
45 | final String dataset) {
46 | if (null != SINGLETON) {
47 | throw new IllegalStateException("only one LinkedDataServer may be instantiated per JVM");
48 | }
49 |
50 | SINGLETON = this;
51 |
52 | final ValueFactory vf = baseSail.getValueFactory();
53 |
54 | if (!internalBaseURI.equals(externalBaseURI)) {
55 | RewriteRule outboundRewriter = new RewriteRule() {
56 | public IRI rewrite(final IRI original) {
57 |
58 | if (null == original) {
59 | return null;
60 | } else {
61 | String s = original.stringValue();
62 | return s.startsWith(internalBaseURI)
63 | ? vf.createIRI(s.replace(internalBaseURI, externalBaseURI))
64 | : original;
65 | }
66 | }
67 | };
68 |
69 | RewriteRule inboundRewriter = new RewriteRule() {
70 | public IRI rewrite(final IRI original) {
71 | if (null == original) {
72 | return null;
73 | } else {
74 | String s = original.stringValue();
75 | return s.startsWith(externalBaseURI)
76 | ? vf.createIRI(s.replace(externalBaseURI, internalBaseURI))
77 | : original;
78 | }
79 | }
80 | };
81 |
82 | MappingSchema schema = new MappingSchema();
83 | schema.setRewriter(MappingSchema.Direction.INBOUND, inboundRewriter);
84 | schema.setRewriter(MappingSchema.Direction.OUTBOUND, outboundRewriter);
85 | this.sail = new MappingSail(baseSail, schema);
86 |
87 | datasetURI = null == dataset
88 | ? null
89 | : outboundRewriter.rewrite(vf.createIRI(dataset));
90 | } else {
91 | this.sail = baseSail;
92 | datasetURI = null == dataset
93 | ? null
94 | : vf.createIRI(dataset);
95 | }
96 | }
97 |
98 | /**
99 | * @return the data store published by this server
100 | */
101 | public Sail getSail() {
102 | return sail;
103 | }
104 |
105 | /**
106 | * @return the internal URI for the data set published by this server
107 | */
108 | public IRI getDatasetURI() {
109 | return datasetURI;
110 | }
111 |
112 | /**
113 | * @return the single Linked Data server
114 | */
115 | public static LinkedDataServer getInstance() {
116 | return SINGLETON;
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/linked-data-server/src/main/java/net/fortytwo/sesametools/ldserver/RDFMediaTypes.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.ldserver;
2 |
3 | import org.eclipse.rdf4j.rio.RDFFormat;
4 | import org.eclipse.rdf4j.rio.RDFParserRegistry;
5 | import org.restlet.data.MediaType;
6 | import org.restlet.representation.Variant;
7 |
8 | import java.util.HashMap;
9 | import java.util.LinkedHashSet;
10 | import java.util.LinkedList;
11 | import java.util.List;
12 | import java.util.Map;
13 |
14 | /**
15 | * @author Joshua Shinavier (http://fortytwo.net)
16 | */
17 | public class RDFMediaTypes {
18 |
19 | private static final List RDF_VARIANTS;
20 | private static final Map MEDATYPE_BY_RDFFORMAT;
21 | private static final Map RDFFORMAT_BY_MEDIATYPE;
22 |
23 | static {
24 | RDF_VARIANTS = new LinkedList<>();
25 | MEDATYPE_BY_RDFFORMAT = new HashMap<>();
26 | RDFFORMAT_BY_MEDIATYPE = new HashMap<>();
27 |
28 | LinkedHashSet toAdd = new LinkedHashSet<>();
29 | // add RDF/XML first, as the default format
30 | toAdd.add(RDFFormat.RDFXML);
31 | // now add the others
32 | toAdd.addAll(RDFParserRegistry.getInstance().getKeys());
33 |
34 | for (RDFFormat f : toAdd) {
35 | MediaType mt = new MediaType(f.getDefaultMIMEType());
36 | Variant v = new Variant(mt);
37 | MEDATYPE_BY_RDFFORMAT.put(f, mt);
38 | RDFFORMAT_BY_MEDIATYPE.put(mt, f);
39 | RDF_VARIANTS.add(v);
40 | }
41 | }
42 |
43 | private RDFMediaTypes() {
44 |
45 | }
46 |
47 | public static List getRDFVariants() {
48 | return RDF_VARIANTS;
49 | }
50 |
51 | public static RDFFormat findRdfFormat(final MediaType mediaType) {
52 | return RDFFORMAT_BY_MEDIATYPE.get(mediaType);
53 | }
54 |
55 | public static MediaType findMediaType(final RDFFormat format) {
56 | return MEDATYPE_BY_RDFFORMAT.get(format);
57 | }
58 |
59 | public static Variant findVariant(final RDFFormat format) {
60 | MediaType mt = findMediaType(format);
61 | return null == mt ? null : new Variant(MEDATYPE_BY_RDFFORMAT.get(format));
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/linked-data-server/src/main/java/net/fortytwo/sesametools/ldserver/RDFRepresentation.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.ldserver;
2 |
3 | import net.fortytwo.sesametools.SesameTools;
4 | import org.eclipse.rdf4j.model.Namespace;
5 | import org.eclipse.rdf4j.model.Statement;
6 | import org.eclipse.rdf4j.rio.RDFFormat;
7 | import org.eclipse.rdf4j.rio.RDFWriter;
8 | import org.eclipse.rdf4j.rio.Rio;
9 | import org.restlet.representation.OutputRepresentation;
10 |
11 | import java.io.IOException;
12 | import java.io.OutputStream;
13 | import java.util.Collection;
14 | import java.util.logging.Level;
15 | import java.util.logging.Logger;
16 |
17 | /**
18 | * An RDF document as an HTTP entity.
19 | *
20 | * @author Joshua Shinavier (http://fortytwo.net)
21 | */
22 | public class RDFRepresentation extends OutputRepresentation {
23 | private static final Logger logger
24 | = Logger.getLogger(RDFRepresentation.class.getName());
25 |
26 | private final Collection statements;
27 | private final Collection namespaces;
28 | private final RDFFormat format;
29 |
30 | public RDFRepresentation(final Collection statements,
31 | final Collection namespaces,
32 | final RDFFormat format) {
33 | super(RDFMediaTypes.findMediaType(format));
34 |
35 | this.statements = statements;
36 | this.namespaces = namespaces;
37 | this.format = format;
38 | }
39 |
40 | @Override
41 | public void write(final OutputStream os) throws IOException {
42 | try {
43 | RDFWriter writer = Rio.createWriter(format, os);
44 | writer.startRDF();
45 | try {
46 | for (Namespace ns : namespaces) {
47 | writer.handleNamespace(ns.getPrefix(), ns.getName());
48 | }
49 | for (Statement st : statements) {
50 | writer.handleStatement(st);
51 | }
52 | writer.handleComment("created by LinkedDataServer "
53 | + SesameTools.getProperties().getProperty(SesameTools.VERSION_PROP)
54 | + " using the Sesame 2 RDF framework");
55 | } finally {
56 | writer.endRDF();
57 | }
58 | } catch (Throwable t) {
59 | if (t instanceof IOException) {
60 | throw (IOException) t;
61 | } else {
62 | logger.log(Level.WARNING, "failed to write RDF representation", t);
63 | throw new IOException(t.getMessage());
64 | }
65 | }
66 | }
67 | }
68 |
69 |
--------------------------------------------------------------------------------
/linked-data-server/src/main/java/net/fortytwo/sesametools/ldserver/ServerException.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.ldserver;
2 |
3 | /**
4 | * @author Joshua Shinavier (http://fortytwo.net)
5 | */
6 | public class ServerException extends Exception {
7 | public ServerException(final Throwable cause) {
8 | super(cause);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/linked-data-server/src/main/java/net/fortytwo/sesametools/ldserver/query/QueryException.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.ldserver.query;
2 |
3 | /**
4 | * @author Joshua Shinavier (http://fortytwo.net)
5 | */
6 | public class QueryException extends Exception {
7 | public QueryException(final Throwable cause) {
8 | super(cause);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/linked-data-server/src/main/java/net/fortytwo/sesametools/ldserver/query/QueryResource.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.ldserver.query;
2 |
3 | import net.fortytwo.sesametools.ldserver.LinkedDataServer;
4 | import org.eclipse.rdf4j.sail.Sail;
5 | import org.restlet.Request;
6 | import org.restlet.Restlet;
7 | import org.restlet.resource.ResourceException;
8 |
9 | import java.io.UnsupportedEncodingException;
10 | import java.net.URLDecoder;
11 | import java.util.HashMap;
12 | import java.util.Map;
13 | import java.util.logging.Logger;
14 |
15 | /**
16 | * @author Joshua Shinavier (http://fortytwo.net)
17 | */
18 | public abstract class QueryResource extends Restlet {
19 | private static final Logger logger = Logger.getLogger(QueryResource.class.getName());
20 |
21 | private static final String UTF_8 = "UTF-8";
22 |
23 | private static final int
24 | MAX_LIMIT = 500,
25 | DEFAULT_LIMIT = 300;
26 |
27 | private static final String LIMIT_PARAM = "limit";
28 |
29 | protected String selfURI;
30 |
31 | protected Sail sail;
32 | //private final String query;
33 |
34 | protected Map getArguments(final Request request) throws ResourceException {
35 | Map arguments;
36 | selfURI = request.getResourceRef().toString();
37 |
38 | sail = LinkedDataServer.getInstance().getSail();
39 |
40 | //getVariants().add(new Variant(MediaType.APPLICATION_JSON));
41 |
42 | int i = selfURI.lastIndexOf("?");
43 | arguments = new HashMap<>();
44 | if (0 < i) {
45 | String args = selfURI.substring(i + 1);
46 | if (0 < args.length()) {
47 | try {
48 | arguments = parseParams(args);
49 | } catch (UnsupportedEncodingException e) {
50 | throw new ResourceException(e);
51 | }
52 | }
53 | }
54 |
55 | return arguments;
56 | }
57 |
58 | protected Map parseParams(final String s) throws UnsupportedEncodingException {
59 | Map map = new HashMap<>();
60 |
61 | String[] a = s.split("&");
62 | for (String p : a) {
63 | String[] b = p.split("=");
64 | map.put(urlDecode(b[0]), urlDecode(b[1]));
65 | }
66 |
67 | return map;
68 | }
69 |
70 | protected int readLimit(final Map arguments) {
71 | String l = arguments.get(LIMIT_PARAM);
72 | int limit;
73 | if (null == l) {
74 | limit = DEFAULT_LIMIT;
75 | } else {
76 | try {
77 | limit = Integer.valueOf(l);
78 |
79 | if (limit > MAX_LIMIT) {
80 | limit = MAX_LIMIT;
81 | } else if (limit < 1) {
82 | limit = DEFAULT_LIMIT;
83 | }
84 | } catch (NumberFormatException e) {
85 | logger.warning("bad limit value: " + l);
86 | limit = DEFAULT_LIMIT;
87 | }
88 | }
89 |
90 | return limit;
91 | }
92 |
93 | protected String urlDecode(final String encoded) throws UnsupportedEncodingException {
94 | return URLDecoder.decode(encoded, UTF_8);
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/linked-data-server/src/main/java/net/fortytwo/sesametools/ldserver/query/SparqlQueryRepresentation.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.ldserver.query;
2 |
3 | import org.eclipse.rdf4j.sail.Sail;
4 | import org.eclipse.rdf4j.sail.SailConnection;
5 | import org.restlet.data.MediaType;
6 | import org.restlet.representation.OutputRepresentation;
7 |
8 | import java.io.ByteArrayOutputStream;
9 | import java.io.IOException;
10 | import java.io.OutputStream;
11 |
12 | /**
13 | * A SPARQL query result as an HTTP entity.
14 | *
15 | * @author Joshua Shinavier (http://fortytwo.net)
16 | */
17 | public class SparqlQueryRepresentation extends OutputRepresentation {
18 |
19 | private final ByteArrayOutputStream data;
20 |
21 | public SparqlQueryRepresentation(final String query,
22 | final Sail sail,
23 | final int limit,
24 | final MediaType mediaType) throws Exception {
25 | super(mediaType);
26 |
27 | try {
28 | data = new ByteArrayOutputStream();
29 | SailConnection sc = sail.getConnection();
30 | try {
31 | SparqlTools.SparqlResultFormat format
32 | = SparqlTools.SparqlResultFormat.lookup(this.getMediaType());
33 |
34 | SparqlTools.executeQuery(query, sc, data, limit, format);
35 | } finally {
36 | sc.close();
37 | }
38 | } catch (Throwable e) {
39 | // TODO: use logging instead
40 | e.printStackTrace(System.err);
41 | throw new Exception(e);
42 | }
43 | }
44 |
45 | public void write(final OutputStream out) throws IOException {
46 | data.writeTo(out);
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/linked-data-server/src/main/java/net/fortytwo/sesametools/ldserver/query/SparqlResource.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.ldserver.query;
2 |
3 | import org.restlet.Request;
4 | import org.restlet.Response;
5 | import org.restlet.data.MediaType;
6 | import org.restlet.data.Method;
7 | import org.restlet.data.Status;
8 | import org.restlet.resource.ResourceException;
9 |
10 | import java.util.Map;
11 |
12 | /**
13 | * A RESTful resource serving as a SPARQL endpoint.
14 | *
15 | * @author Joshua Shinavier (http://fortytwo.net)
16 | */
17 | public class SparqlResource extends QueryResource {
18 |
19 | @Override
20 | public void handle(final Request request,
21 | final Response response) {
22 | MediaType mt;
23 | String query = null;
24 | Map arguments;
25 |
26 | try {
27 | arguments = getArguments(request);
28 |
29 | if (request.getMethod() == Method.POST) {
30 | String type = request.getEntity().getMediaType().toString();
31 | String ent = request.getEntity().getText();
32 |
33 | switch (type) {
34 | case "application/x-www-form-urlencoded":
35 | arguments = parseParams(ent);
36 | break;
37 | case "application/sparql-query":
38 | query = ent;
39 | break;
40 | default:
41 | throw new IllegalArgumentException("POST entity has unsupported media type for SPARQL");
42 | }
43 | }
44 |
45 | if (null == query) {
46 | query = arguments.get("query");
47 | }
48 |
49 | if (null == query) {
50 | throw new IllegalArgumentException("no query argument specified");
51 | }
52 |
53 | String output = arguments.get("output");
54 |
55 | // If an "output" argument is provided, use it.
56 | if (null != output) {
57 | switch (output) {
58 | case "json":
59 | mt = SparqlTools.SparqlResultFormat.JSON.getMediaType();
60 | break;
61 | case "xml":
62 | mt = SparqlTools.SparqlResultFormat.XML.getMediaType();
63 | break;
64 | default:
65 | throw new IllegalArgumentException("bad value for 'output' parameter: " + output);
66 | }
67 | } else {
68 | mt = SparqlTools.SparqlResultFormat.getVariants().get(0).getMediaType();
69 | }
70 | } catch (Throwable t) {
71 | t.printStackTrace(System.err);
72 | throw new ResourceException(t);
73 | }
74 |
75 | try {
76 | response.setEntity(new SparqlQueryRepresentation(query, sail, readLimit(arguments), mt));
77 | } catch (QueryException e) {
78 | throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e);
79 | } catch (Throwable e) {
80 | // TODO: use logging instead
81 | e.printStackTrace(System.err);
82 | System.err.flush();
83 | throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
84 | }
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/linked-data-server/src/main/resources/META-INF/services/org.openrdf.rio.RDFWriterFactory:
--------------------------------------------------------------------------------
1 | org.openrdf.rio.n3.N3WriterFactory
2 | org.openrdf.rio.ntriples.NTriplesWriterFactory
3 | org.openrdf.rio.rdfxml.RDFXMLWriterFactory
4 | org.openrdf.rio.trig.TriGWriterFactory
5 | org.openrdf.rio.trix.TriXWriterFactory
6 | org.openrdf.rio.turtle.TurtleWriterFactory
7 | org.openrdf.rio.nquads.NQuadsWriterFactory
8 | org.openrdf.rio.rdfjson.RDFJSONWriterFactory
9 |
--------------------------------------------------------------------------------
/linked-data-server/src/test/java/net/fortytwo/sesametools/ldserver/DemoApp.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.ldserver;
2 |
3 | import net.fortytwo.sesametools.ldserver.query.SparqlResource;
4 | import org.eclipse.rdf4j.repository.Repository;
5 | import org.eclipse.rdf4j.repository.RepositoryConnection;
6 | import org.eclipse.rdf4j.repository.sail.SailRepository;
7 | import org.eclipse.rdf4j.rio.RDFFormat;
8 | import org.eclipse.rdf4j.sail.Sail;
9 | import org.eclipse.rdf4j.sail.memory.MemoryStore;
10 | import org.restlet.Component;
11 | import org.restlet.data.Protocol;
12 |
13 | /**
14 | * @author Joshua Shinavier (http://fortytwo.net)
15 | */
16 | public class DemoApp {
17 | public static void main(final String[] args) throws Exception {
18 | Sail sail = new MemoryStore();
19 | sail.initialize();
20 |
21 | Repository repo = new SailRepository(sail);
22 | try (RepositoryConnection rc = repo.getConnection()) {
23 | rc.add(DemoApp.class.getResourceAsStream("demoApp.trig"), "", RDFFormat.TRIG);
24 | }
25 |
26 | LinkedDataServer server = new LinkedDataServer(
27 | sail,
28 | "http://example.org",
29 | "http://localhost:8001");
30 |
31 | Component component = new Component();
32 | component.getServers().add(Protocol.HTTP, 8001);
33 | component.getDefaultHost().attach("/person", WebResource.class);
34 | component.getDefaultHost().attach("/graph", GraphResource.class);
35 | component.getDefaultHost().attach("/sparql", new SparqlResource());
36 | server.setInboundRoot(component);
37 | server.start();
38 |
39 | /* Now try:
40 | wget http://localhost:8001/person/arthur
41 | wget --header="Accept: application/x-trig" http://localhost:8001/person/arthur
42 | wget --header="Accept: application/x-trig" http://localhost:8001/graph/demoGraph
43 |
44 | wget "http://localhost:8001/sparql?query=SELECT%20%3Fs%20%3Fp%20%3Fo%20WHERE%20%7B%20%3Fs%20%3Fp%20%3Fo%20%7D%20LIMIT%2010"
45 | curl --data-urlencode query@/tmp/myquery.rq http://localhost:8001/sparql
46 | */
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/linked-data-server/src/test/java/net/fortytwo/sesametools/ldserver/SparqlAskTest.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.ldserver;
2 |
3 | import java.net.URI;
4 | import java.util.logging.Level;
5 |
6 | import net.fortytwo.sesametools.ldserver.query.SparqlResource;
7 |
8 | import org.junit.AfterClass;
9 | import org.junit.Assert;
10 | import org.junit.BeforeClass;
11 | import org.junit.Test;
12 | import org.eclipse.rdf4j.query.QueryLanguage;
13 | import org.eclipse.rdf4j.repository.Repository;
14 | import org.eclipse.rdf4j.repository.RepositoryConnection;
15 | import org.eclipse.rdf4j.repository.sail.SailRepository;
16 | import org.eclipse.rdf4j.repository.sparql.SPARQLRepository;
17 | import org.eclipse.rdf4j.rio.RDFFormat;
18 | import org.eclipse.rdf4j.sail.Sail;
19 | import org.eclipse.rdf4j.sail.memory.MemoryStore;
20 | import org.restlet.Component;
21 | import org.restlet.data.Protocol;
22 | import org.restlet.engine.Engine;
23 |
24 | public class SparqlAskTest {
25 |
26 | private static final URI ENDPOINT_URL = URI.create("http://localhost:8001/sparql");
27 | private static final String DATA_FILE = "demoApp.trig";
28 |
29 | private static final Sail sail;
30 | private static final LinkedDataServer server;
31 |
32 | static {
33 | sail = new MemoryStore();
34 | sail.initialize();
35 | server = new LinkedDataServer(sail, "", "");
36 | }
37 |
38 | @BeforeClass
39 | public static void setUp() throws Exception {
40 | // add test data
41 | final Repository repo = new SailRepository(sail);
42 | try (RepositoryConnection con = repo.getConnection()) {
43 | con.add(SparqlAskTest.class.getResourceAsStream(DATA_FILE), "", RDFFormat.TRIG);
44 | }
45 |
46 | // turn off verbose logging in Restlet engine
47 | Engine.setLogLevel(Level.WARNING);
48 |
49 | // configure SPARQL endpoint
50 | final Component component = new Component();
51 | component.getServers().add(Protocol.HTTP, ENDPOINT_URL.getPort());
52 | component.getDefaultHost().attach(ENDPOINT_URL.getPath(), new SparqlResource());
53 | server.setInboundRoot(component);
54 | server.start();
55 | }
56 |
57 | @AfterClass
58 | public static void tearDown() throws Exception {
59 | server.stop();
60 | sail.shutDown();
61 | }
62 |
63 | @Test
64 | public void testSatisfiableAskQuery() throws Exception {
65 | Assert.assertTrue(executeAskQuery(ENDPOINT_URL, "ASK { [] a ?type }"));
66 | }
67 |
68 | @Test
69 | public void testUnsatisfiableAskQuery() throws Exception {
70 | Assert.assertFalse(executeAskQuery(ENDPOINT_URL, "ASK { [] ?p }"));
71 | }
72 |
73 | private Boolean executeAskQuery(final URI endpoint, final String query) {
74 |
75 | final SPARQLRepository repo = new SPARQLRepository(endpoint.toString());
76 | try {
77 | repo.initialize();
78 |
79 | try (RepositoryConnection con = repo.getConnection()) {
80 | return con.prepareBooleanQuery(QueryLanguage.SPARQL, query).evaluate();
81 | } catch (Exception e) {
82 | Assert.fail(e.getMessage());
83 | }
84 |
85 | } catch (Exception e) {
86 | Assert.fail(e.getMessage());
87 | } finally {
88 | repo.shutDown();
89 | }
90 | return null;
91 | }
92 |
93 | }
94 |
--------------------------------------------------------------------------------
/linked-data-server/src/test/resources/META-INF/services/org.openrdf.rio.RDFParserFactory:
--------------------------------------------------------------------------------
1 | org.openrdf.rio.trig.TriGParserFactory
2 |
--------------------------------------------------------------------------------
/linked-data-server/src/test/resources/net/fortytwo/sesametools/ldserver/demoApp.trig:
--------------------------------------------------------------------------------
1 | @prefix foaf: .
2 | @prefix rdfs: .
3 | @prefix xsd: .
4 | @prefix p: .
5 | @prefix g: .
6 |
7 | {
8 | p:arthur a foaf:Person;
9 | rdfs:comment "he's a jerk".
10 | }
11 |
12 | g:demoGraph
13 | {
14 | p:ford a foaf:Person;
15 | foaf:knows p:arthur, p:zaphod;
16 | rdfs:comment "he really knows where his towel is".
17 |
18 | p:zaphod a foaf:Person;
19 | foaf:knows p:ford;
20 | rdfs:comment "so cool you could keep a side of meat in him for a month".
21 | }
22 |
--------------------------------------------------------------------------------
/mapping-sail/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | .classpath
3 | .project
4 | .settings
5 | *.iml
6 | *~
7 |
--------------------------------------------------------------------------------
/mapping-sail/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 4.0.0
5 | mapping-sail
6 | jar
7 | MappingSail
8 | A Sail which translates between two URI spaces
9 |
10 |
11 | net.fortytwo.sesametools
12 | sesametools-all
13 | 2.0-SNAPSHOT
14 | ../pom.xml
15 |
16 |
17 |
18 |
19 | net.fortytwo.sesametools
20 | common
21 |
22 |
23 | org.eclipse.rdf4j
24 | rdf4j-sail-api
25 |
26 |
27 |
28 | junit
29 | junit
30 | test
31 |
32 |
33 |
34 |
35 |
36 |
37 | org.apache.maven.plugins
38 | maven-compiler-plugin
39 |
40 |
41 | org.apache.maven.plugins
42 | maven-source-plugin
43 |
44 |
45 | org.apache.maven.plugins
46 | maven-jar-plugin
47 |
48 |
49 | org.apache.maven.plugins
50 | maven-javadoc-plugin
51 |
52 |
53 | org.apache.maven.plugins
54 | maven-surefire-plugin
55 |
56 |
57 | org.eluder.coveralls
58 | coveralls-maven-plugin
59 |
60 |
61 | org.jacoco
62 | jacoco-maven-plugin
63 |
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/mapping-sail/src/main/java/net/fortytwo/sesametools/mappingsail/MappingSail.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.mappingsail;
2 |
3 | import org.eclipse.rdf4j.sail.Sail;
4 | import org.eclipse.rdf4j.sail.SailConnection;
5 | import org.eclipse.rdf4j.sail.SailException;
6 | import org.eclipse.rdf4j.sail.helpers.SailWrapper;
7 |
8 | /**
9 | * A Sail
which maps between the internal URI space of a lower-level data store,
10 | * and an externally visible URI space
11 | * (for example, published Linked Data).
12 | *
13 | * @author Joshua Shinavier (http://fortytwo.net)
14 | */
15 | public class MappingSail extends SailWrapper {
16 | private final MappingSchema schema;
17 |
18 | /**
19 | * @param baseSail the internal data store
20 | * @param schema a set of rules for URI rewriting
21 | */
22 | public MappingSail(final Sail baseSail,
23 | final MappingSchema schema) {
24 | this.setBaseSail(baseSail);
25 | this.schema = schema;
26 | }
27 |
28 | @Override
29 | public SailConnection getConnection() throws SailException {
30 | return new MappingSailConnection(this.getBaseSail().getConnection(), schema, this.getValueFactory());
31 | }
32 |
33 | @Override
34 | public boolean isWritable() throws SailException {
35 | // TODO: handle rewriting for write operations
36 | return false;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/mapping-sail/src/main/java/net/fortytwo/sesametools/mappingsail/MappingSailConnection.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.mappingsail;
2 |
3 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
4 | import org.eclipse.rdf4j.model.IRI;
5 | import org.eclipse.rdf4j.model.Resource;
6 | import org.eclipse.rdf4j.model.Statement;
7 | import org.eclipse.rdf4j.model.Value;
8 | import org.eclipse.rdf4j.model.ValueFactory;
9 | import org.eclipse.rdf4j.sail.SailConnection;
10 | import org.eclipse.rdf4j.sail.SailException;
11 | import org.eclipse.rdf4j.sail.helpers.SailConnectionWrapper;
12 |
13 | /**
14 | * @author Joshua Shinavier (http://fortytwo.net)
15 | */
16 | class MappingSailConnection extends SailConnectionWrapper {
17 | private final MappingSchema rewriters;
18 | private final ValueFactory valueFactory;
19 |
20 | public MappingSailConnection(final SailConnection baseConnection,
21 | final MappingSchema rewriters,
22 | final ValueFactory valueFactory) {
23 | super(baseConnection);
24 | this.rewriters = rewriters;
25 | this.valueFactory = valueFactory;
26 | }
27 |
28 | @Override
29 | public CloseableIteration extends Statement, SailException> getStatements(
30 | Resource subj, IRI pred, Value obj, final boolean includeInferred, Resource... contexts)
31 | throws SailException {
32 |
33 | if (subj instanceof IRI) {
34 | subj = rewriters.getRewriter(
35 | MappingSchema.PartOfSpeech.SUBJECT, MappingSchema.Direction.INBOUND).rewrite((IRI) subj);
36 | }
37 | pred = rewriters.getRewriter(
38 | MappingSchema.PartOfSpeech.PREDICATE, MappingSchema.Direction.INBOUND).rewrite(pred);
39 | if (obj instanceof IRI) {
40 | obj = rewriters.getRewriter(
41 | MappingSchema.PartOfSpeech.OBJECT, MappingSchema.Direction.INBOUND).rewrite((IRI) obj);
42 | }
43 | for (int i = 0; i < contexts.length; i++) {
44 | if (contexts[i] instanceof IRI) {
45 | contexts[i] = rewriters.getRewriter(
46 | MappingSchema.PartOfSpeech.CONTEXT, MappingSchema.Direction.INBOUND).rewrite((IRI) contexts[i]);
47 | }
48 | }
49 |
50 | return new RewritingStatementIteration(
51 | this.getWrappedConnection().getStatements(subj, pred, obj, includeInferred, contexts));
52 | }
53 |
54 | private class RewritingStatementIteration implements CloseableIteration {
55 | private final CloseableIteration extends Statement, SailException> baseIteration;
56 |
57 | public RewritingStatementIteration(final CloseableIteration extends Statement, SailException> baseIteration) {
58 | this.baseIteration = baseIteration;
59 | }
60 |
61 | public void close() throws SailException {
62 | baseIteration.close();
63 | }
64 |
65 | public boolean hasNext() throws SailException {
66 | return baseIteration.hasNext();
67 | }
68 |
69 | public Statement next() throws SailException {
70 | Statement st = baseIteration.next();
71 |
72 | Resource subject = st.getSubject();
73 | IRI predicate = st.getPredicate();
74 | Value object = st.getObject();
75 | Resource context = st.getContext();
76 |
77 | if (subject instanceof IRI) {
78 | subject = rewriters.getRewriter(
79 | MappingSchema.PartOfSpeech.SUBJECT, MappingSchema.Direction.OUTBOUND)
80 | .rewrite((IRI) subject);
81 | }
82 | predicate = rewriters.getRewriter(
83 | MappingSchema.PartOfSpeech.PREDICATE, MappingSchema.Direction.OUTBOUND)
84 | .rewrite(predicate);
85 | if (object instanceof IRI) {
86 | object = rewriters.getRewriter(
87 | MappingSchema.PartOfSpeech.OBJECT, MappingSchema.Direction.OUTBOUND)
88 | .rewrite((IRI) object);
89 | }
90 | if (null != context && context instanceof IRI) {
91 | context = rewriters.getRewriter(
92 | MappingSchema.PartOfSpeech.CONTEXT, MappingSchema.Direction.OUTBOUND)
93 | .rewrite((IRI) context);
94 | }
95 |
96 | return valueFactory.createStatement(subject, predicate, object, context);
97 | }
98 |
99 | public void remove() throws SailException {
100 | baseIteration.remove();
101 | }
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/mapping-sail/src/main/java/net/fortytwo/sesametools/mappingsail/MappingSchema.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.mappingsail;
2 |
3 | import org.eclipse.rdf4j.model.IRI;
4 |
5 | import java.util.HashMap;
6 | import java.util.Map;
7 |
8 | /**
9 | * A set of rules for rewriting URIs based on direction (to or from the data store)
10 | * and part of speech (the position in an RDF statement in which a URI appears)
11 | *
12 | * @author Joshua Shinavier (http://fortytwo.net)
13 | */
14 | public class MappingSchema {
15 | /**
16 | * The position in an RDF statement in which a URI appears
17 | */
18 | public enum PartOfSpeech {
19 | SUBJECT, PREDICATE, OBJECT, CONTEXT
20 | }
21 |
22 | /**
23 | * The direction of a URI rewriting rule: inbound rules map externally visible URIs
24 | * to the URIs found in the data store, while outbound rules do the opposite.
25 | */
26 | public enum Direction {
27 | INBOUND, OUTBOUND
28 | }
29 |
30 | private final RewriteRule defaultRewriter = new RewriteRule() {
31 | public IRI rewrite(final IRI original) {
32 | return original;
33 | }
34 | };
35 |
36 | private final Map rewriters
37 | = new HashMap<>();
38 |
39 | /**
40 | * @param partOfSpeech the position in an RDF statement (subject, predicate, object or context)
41 | * in which the URI appears
42 | * @param direction whether this is a rule to map externally visible URIs to internal URIs (inbound)
43 | * or the reverse (outbound)
44 | * @return the matching rewriting rule.
45 | * If no such rule has been explicitly defined, the default rule (the identity mapping) is returned.
46 | */
47 | public RewriteRule getRewriter(final PartOfSpeech partOfSpeech,
48 | final Direction direction) {
49 | RewriteRule r = rewriters.get("" + partOfSpeech + direction);
50 | return null == r
51 | ? defaultRewriter
52 | : r;
53 | }
54 |
55 | /**
56 | * Defines an inbound or outbound URI rewriter.
57 | *
58 | * @param direction whether this is a rule to map externally visible URIs to internal URIs (inbound)
59 | * or the reverse (outbound)
60 | * @param rule the rewriting rule
61 | */
62 | public void setRewriter(final Direction direction,
63 | final RewriteRule rule) {
64 | for (PartOfSpeech p : PartOfSpeech.values()) {
65 | setRewriter(direction, p, rule);
66 | }
67 | }
68 |
69 | /**
70 | * Defines an inbound or outbound URI rewriter for a specific part of speech
71 | * (subject, predicate, object, or context).
72 | *
73 | * @param partOfSpeech the part of speech to which this rewriter applies
74 | * @param direction whether this is a rule to map externally visible URIs to internal URIs (inbound)
75 | * or the reverse (outbound)
76 | * @param rule the rewriting rule
77 | */
78 | public void setRewriter(final Direction direction,
79 | final PartOfSpeech partOfSpeech,
80 | final RewriteRule rule) {
81 | rewriters.put("" + partOfSpeech + direction, rule);
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/mapping-sail/src/main/java/net/fortytwo/sesametools/mappingsail/RewriteRule.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.mappingsail;
2 |
3 | import org.eclipse.rdf4j.model.IRI;
4 |
5 | /**
6 | * Represents a rule to map an original URI to a new URI.
7 | * Rules are considered to be complete and self contained: MappingSail does not impose its own rewriting logic.
8 | *
9 | * @author Joshua Shinavier (http://fortytwo.net)
10 | */
11 | public interface RewriteRule {
12 | /**
13 | * @param original an complete URI (i.e. not only a URI prefix) to be rewritten
14 | * @return the resulting URI
15 | */
16 | IRI rewrite(IRI original);
17 | }
18 |
--------------------------------------------------------------------------------
/rdf-transaction-sail/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | .classpath
3 | .project
4 | .settings
5 | *.iml
6 | *~
7 |
--------------------------------------------------------------------------------
/rdf-transaction-sail/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 4.0.0
5 | rdf-transaction-sail
6 | jar
7 | RDFTransactionSail
8 | A write-only Sail which generates RDF transactions instead of performing per-call updates
9 |
10 |
11 | net.fortytwo.sesametools
12 | sesametools-all
13 | 2.0-SNAPSHOT
14 | ../pom.xml
15 |
16 |
17 |
18 |
19 | net.fortytwo.sesametools
20 | common
21 |
22 |
23 | org.eclipse.rdf4j
24 | rdf4j-sail-api
25 |
26 |
27 | org.eclipse.rdf4j
28 | rdf4j-http-protocol
29 |
30 |
31 |
32 |
33 | junit
34 | junit
35 | test
36 |
37 |
38 |
39 |
40 |
41 |
42 | org.apache.maven.plugins
43 | maven-compiler-plugin
44 |
45 |
46 | org.apache.maven.plugins
47 | maven-source-plugin
48 |
49 |
50 | org.apache.maven.plugins
51 | maven-jar-plugin
52 |
53 |
54 | org.apache.maven.plugins
55 | maven-javadoc-plugin
56 |
57 |
58 | org.apache.maven.plugins
59 | maven-surefire-plugin
60 |
61 |
62 | org.eluder.coveralls
63 | coveralls-maven-plugin
64 |
65 |
66 | org.jacoco
67 | jacoco-maven-plugin
68 |
69 |
70 |
71 |
72 |
73 |
--------------------------------------------------------------------------------
/rdf-transaction-sail/src/main/java/net/fortytwo/sesametools/rdftransaction/RDFTransactionSail.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.rdftransaction;
2 |
3 | import org.eclipse.rdf4j.http.protocol.transaction.TransactionWriter;
4 | import org.eclipse.rdf4j.http.protocol.transaction.operations.TransactionOperation;
5 | import org.eclipse.rdf4j.sail.Sail;
6 | import org.eclipse.rdf4j.sail.SailConnection;
7 | import org.eclipse.rdf4j.sail.SailException;
8 | import org.eclipse.rdf4j.sail.helpers.SailWrapper;
9 |
10 | import java.io.ByteArrayOutputStream;
11 | import java.io.IOException;
12 | import java.util.List;
13 |
14 | /**
15 | * A Sail
which uploads committed transactions in the application/x-rdftransaction format
16 | *
17 | * @author Joshua Shinavier (http://fortytwo.net).
18 | */
19 | public abstract class RDFTransactionSail extends SailWrapper {
20 | private final int commitsPerTransaction;
21 | private final TransactionWriter writer = new TransactionWriter();
22 |
23 | public RDFTransactionSail(final Sail baseSail,
24 | final int commitsPerTransaction) {
25 | super(baseSail);
26 | this.commitsPerTransaction = commitsPerTransaction;
27 | }
28 |
29 | public RDFTransactionSail(final Sail baseSail) {
30 | this(baseSail, 1);
31 | }
32 |
33 | @Override
34 | public SailConnection getConnection() throws SailException {
35 | SailConnection b = this.getBaseSail().getConnection();
36 |
37 | return new RDFTransactionSailConnection(b, this, commitsPerTransaction);
38 | }
39 |
40 | protected byte[] createTransactionEntity(final List operations) throws SailException {
41 | try {
42 | try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
43 | writer.serialize(operations, bos);
44 | return bos.toByteArray();
45 | }
46 | } catch (IOException e) {
47 | throw new SailException(e);
48 | }
49 | }
50 |
51 | public abstract void handleTransaction(final List operations) throws SailException;
52 | }
53 |
--------------------------------------------------------------------------------
/rdf-transaction-sail/src/main/java/net/fortytwo/sesametools/rdftransaction/RDFTransactionSailConnection.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.rdftransaction;
2 |
3 | import org.eclipse.rdf4j.http.protocol.transaction.operations.AddStatementOperation;
4 | import org.eclipse.rdf4j.http.protocol.transaction.operations.ClearNamespacesOperation;
5 | import org.eclipse.rdf4j.http.protocol.transaction.operations.ClearOperation;
6 | import org.eclipse.rdf4j.http.protocol.transaction.operations.RemoveNamespaceOperation;
7 | import org.eclipse.rdf4j.http.protocol.transaction.operations.RemoveStatementsOperation;
8 | import org.eclipse.rdf4j.http.protocol.transaction.operations.SetNamespaceOperation;
9 | import org.eclipse.rdf4j.http.protocol.transaction.operations.TransactionOperation;
10 | import org.eclipse.rdf4j.model.IRI;
11 | import org.eclipse.rdf4j.model.Resource;
12 | import org.eclipse.rdf4j.model.Value;
13 | import org.eclipse.rdf4j.sail.SailConnection;
14 | import org.eclipse.rdf4j.sail.SailException;
15 | import org.eclipse.rdf4j.sail.helpers.SailConnectionWrapper;
16 |
17 | import java.util.LinkedList;
18 | import java.util.List;
19 |
20 | /**
21 | * @author Joshua Shinavier (http://fortytwo.net).
22 | */
23 | public class RDFTransactionSailConnection extends SailConnectionWrapper {
24 | private final List operations;
25 | private final List buffer;
26 | private final RDFTransactionSail sail;
27 | private final int commitsPerUpload;
28 | private int commits = 0;
29 |
30 | /**
31 | * @param c the wrapped connection
32 | * @param sail the owner Sail
33 | * @param commitsPerUpload if transactions should be grouped together, rather than uploaded
34 | * individually. Note: when the SailConnection is closed, any leftover
35 | * transactions (committed but not uploaded) will be uploaded.
36 | */
37 | public RDFTransactionSailConnection(final SailConnection c,
38 | final RDFTransactionSail sail,
39 | final int commitsPerUpload) {
40 | super(c);
41 | this.sail = sail;
42 | this.operations = new LinkedList<>();
43 | this.buffer = new LinkedList<>();
44 | this.commitsPerUpload = commitsPerUpload;
45 | }
46 |
47 | @Override
48 | public void commit() throws SailException {
49 |
50 | this.getWrappedConnection().commit();
51 |
52 | buffer.addAll(operations);
53 | operations.clear();
54 |
55 | commits++;
56 | if (commits == commitsPerUpload) {
57 | commitAll();
58 | }
59 | }
60 |
61 | private void commitAll() throws SailException {
62 | if (!buffer.isEmpty()) {
63 | sail.handleTransaction(buffer);
64 | }
65 |
66 | buffer.clear();
67 | commits = 0;
68 | }
69 |
70 | @Override
71 | public void rollback() throws SailException {
72 | this.getWrappedConnection().rollback();
73 |
74 | operations.clear();
75 | }
76 |
77 | @Override
78 | public void addStatement(Resource subject, IRI predicate, Value object, Resource... contexts)
79 | throws SailException {
80 |
81 | this.getWrappedConnection().addStatement(subject, predicate, object, contexts);
82 |
83 | operations.add(new AddStatementOperation(subject, predicate, object, contexts));
84 | }
85 |
86 | @Override
87 | public void removeStatements(Resource subject, IRI predicate, Value object, Resource... contexts)
88 | throws SailException {
89 |
90 | this.getWrappedConnection().removeStatements(subject, predicate, object, contexts);
91 |
92 | operations.add(new RemoveStatementsOperation(subject, predicate, object, contexts));
93 | }
94 |
95 | @Override
96 | public void clear(Resource... contexts) throws SailException {
97 | this.getWrappedConnection().clear(contexts);
98 |
99 | operations.add(new ClearOperation(contexts));
100 | }
101 |
102 | @Override
103 | public void setNamespace(String prefix, String uri) throws SailException {
104 | this.getWrappedConnection().setNamespace(prefix, uri);
105 |
106 | operations.add(new SetNamespaceOperation(prefix, uri));
107 | }
108 |
109 | @Override
110 | public void removeNamespace(String prefix) throws SailException {
111 | this.getWrappedConnection().removeNamespace(prefix);
112 |
113 | operations.add(new RemoveNamespaceOperation(prefix));
114 | }
115 |
116 | @Override
117 | public void clearNamespaces() throws SailException {
118 | this.getWrappedConnection().clearNamespaces();
119 |
120 | operations.add(new ClearNamespacesOperation());
121 | }
122 |
123 | @Override
124 | public void close() throws SailException {
125 | commitAll();
126 | super.close();
127 | }
128 | }
129 |
--------------------------------------------------------------------------------
/readonly-sail/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | .classpath
3 | .project
4 | .settings
5 | *.iml
6 | *~
7 |
--------------------------------------------------------------------------------
/readonly-sail/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 4.0.0
5 | readonly-sail
6 | jar
7 | ReadOnlySail
8 | A read-only Sail implementation
9 |
10 |
11 | net.fortytwo.sesametools
12 | sesametools-all
13 | 2.0-SNAPSHOT
14 | ../pom.xml
15 |
16 |
17 |
18 |
19 | net.fortytwo.sesametools
20 | common
21 |
22 |
23 | org.eclipse.rdf4j
24 | rdf4j-sail-api
25 |
26 |
27 |
28 |
29 | junit
30 | junit
31 | test
32 |
33 |
34 |
35 |
36 |
37 |
38 | org.apache.maven.plugins
39 | maven-compiler-plugin
40 |
41 |
42 | org.apache.maven.plugins
43 | maven-source-plugin
44 |
45 |
46 | org.apache.maven.plugins
47 | maven-jar-plugin
48 |
49 |
50 | org.apache.maven.plugins
51 | maven-javadoc-plugin
52 |
53 |
54 | org.apache.maven.plugins
55 | maven-surefire-plugin
56 |
57 |
58 | org.eluder.coveralls
59 | coveralls-maven-plugin
60 |
61 |
62 | org.jacoco
63 | jacoco-maven-plugin
64 |
65 |
66 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/readonly-sail/src/main/java/net/fortytwo/sesametools/readonly/ReadOnlySail.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.readonly;
2 |
3 | import org.eclipse.rdf4j.model.ValueFactory;
4 | import org.eclipse.rdf4j.sail.Sail;
5 | import org.eclipse.rdf4j.sail.SailConnection;
6 | import org.eclipse.rdf4j.sail.SailException;
7 | import org.eclipse.rdf4j.sail.StackableSail;
8 | import org.eclipse.rdf4j.sail.helpers.AbstractSail;
9 |
10 | import java.io.File;
11 |
12 | /**
13 | * A Sail implementation which protects a base Sail from write operations.
14 | * This Sail does not throw exceptions when write operations are attempted;
15 | * it simply ignores them so that they have no effect.
16 | *
17 | * @author Joshua Shinavier (http://fortytwo.net).
18 | */
19 | public class ReadOnlySail extends AbstractSail implements StackableSail {
20 | private Sail baseSail;
21 |
22 | public ReadOnlySail(final Sail baseSail) {
23 | this.baseSail = baseSail;
24 | }
25 |
26 | @Override
27 | public void setDataDir(final File dir) {
28 | baseSail.setDataDir(dir);
29 | }
30 |
31 | @Override
32 | public File getDataDir() {
33 | return baseSail.getDataDir();
34 | }
35 |
36 | protected void initializeInternal() throws SailException {
37 | // Do nothing.
38 | }
39 |
40 | protected void shutDownInternal() throws SailException {
41 | // Do nothing.
42 | }
43 |
44 | @Override
45 | public boolean isWritable() throws SailException {
46 | return false;
47 | }
48 |
49 | protected SailConnection getConnectionInternal() throws SailException {
50 | return new ReadOnlySailConnection(this, baseSail);
51 | }
52 |
53 | public ValueFactory getValueFactory() {
54 | return baseSail.getValueFactory();
55 | }
56 |
57 | public void setBaseSail(final Sail sail) {
58 | this.baseSail = sail;
59 | }
60 |
61 | public Sail getBaseSail() {
62 | return baseSail;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/readonly-sail/src/main/java/net/fortytwo/sesametools/readonly/ReadOnlySailConnection.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.readonly;
2 |
3 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
4 | import org.eclipse.rdf4j.model.IRI;
5 | import org.eclipse.rdf4j.model.Namespace;
6 | import org.eclipse.rdf4j.model.Resource;
7 | import org.eclipse.rdf4j.model.Statement;
8 | import org.eclipse.rdf4j.model.Value;
9 | import org.eclipse.rdf4j.query.BindingSet;
10 | import org.eclipse.rdf4j.query.Dataset;
11 | import org.eclipse.rdf4j.query.QueryEvaluationException;
12 | import org.eclipse.rdf4j.query.algebra.TupleExpr;
13 | import org.eclipse.rdf4j.sail.Sail;
14 | import org.eclipse.rdf4j.sail.SailConnection;
15 | import org.eclipse.rdf4j.sail.SailException;
16 | import org.eclipse.rdf4j.sail.helpers.AbstractSail;
17 | import org.eclipse.rdf4j.sail.helpers.AbstractSailConnection;
18 |
19 | /**
20 | * @author Joshua Shinavier (http://fortytwo.net).
21 | */
22 | public class ReadOnlySailConnection extends AbstractSailConnection {
23 | private final SailConnection baseSailConnection;
24 |
25 | public ReadOnlySailConnection(final AbstractSail sail,
26 | final Sail baseSail) throws SailException {
27 | super(sail);
28 | baseSailConnection = baseSail.getConnection();
29 | }
30 |
31 | protected void closeInternal() throws SailException {
32 | baseSailConnection.close();
33 | }
34 |
35 | protected CloseableIteration extends BindingSet, QueryEvaluationException> evaluateInternal(
36 | final TupleExpr tupleExpr, final Dataset dataset, final BindingSet bindings, final boolean includeInferred)
37 | throws SailException {
38 |
39 | return baseSailConnection.evaluate(tupleExpr, dataset, bindings, includeInferred);
40 | }
41 |
42 | protected CloseableIteration extends Resource, SailException> getContextIDsInternal() throws SailException {
43 | return baseSailConnection.getContextIDs();
44 | }
45 |
46 | protected CloseableIteration extends Statement, SailException> getStatementsInternal(
47 | final Resource subject, final IRI predicate, final Value object, final boolean includeInferred,
48 | final Resource... contexts) throws SailException {
49 |
50 | return baseSailConnection.getStatements(subject, predicate, object, includeInferred, contexts);
51 | }
52 |
53 | protected long sizeInternal(final Resource... contexts) throws SailException {
54 | return baseSailConnection.size(contexts);
55 | }
56 |
57 | protected void commitInternal() throws SailException {
58 | // Do nothing.
59 | }
60 |
61 | protected void rollbackInternal() throws SailException {
62 | // Do nothing.
63 | }
64 |
65 | protected void addStatementInternal(final Resource subject,
66 | final IRI predicate,
67 | final Value object,
68 | final Resource... contexts) throws SailException {
69 | // Do nothing.
70 | }
71 |
72 | protected void removeStatementsInternal(final Resource subject,
73 | final IRI predicate,
74 | final Value object,
75 | final Resource... contexts) throws SailException {
76 | // Do nothing.
77 | }
78 |
79 | protected void clearInternal(final Resource... contexts) throws SailException {
80 | // Do nothing.
81 | }
82 |
83 | protected CloseableIteration extends Namespace, SailException> getNamespacesInternal() throws SailException {
84 | return baseSailConnection.getNamespaces();
85 | }
86 |
87 | protected String getNamespaceInternal(final String prefix) throws SailException {
88 | return baseSailConnection.getNamespace(prefix);
89 | }
90 |
91 | protected void setNamespaceInternal(final String prefix,
92 | final String uri) throws SailException {
93 | // Do nothing.
94 | }
95 |
96 | protected void removeNamespaceInternal(final String prefix) throws SailException {
97 | // Do nothing.
98 | }
99 |
100 | protected void clearNamespacesInternal() throws SailException {
101 | // Do nothing.
102 | }
103 |
104 | protected void startTransactionInternal() throws SailException {
105 | baseSailConnection.begin();
106 | }
107 |
108 | @Override
109 | public boolean pendingRemovals() {
110 | return false;
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/replay-sail/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | .classpath
3 | .project
4 | .settings
5 | *.iml
6 | *~
7 |
--------------------------------------------------------------------------------
/replay-sail/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 4.0.0
5 | replay-sail
6 | jar
7 | ReplaySail
8 | A pair of Sail implementations which allow Sail operations to be first recorded to a log file, then
9 | reproduced from the log file
10 |
11 |
12 |
13 | net.fortytwo.sesametools
14 | sesametools-all
15 | 2.0-SNAPSHOT
16 | ../pom.xml
17 |
18 |
19 |
20 |
21 | net.fortytwo.sesametools
22 | common
23 |
24 |
25 | org.eclipse.rdf4j
26 | rdf4j-sail-api
27 |
28 |
29 | org.eclipse.rdf4j
30 | rdf4j-rio-ntriples
31 |
32 |
33 |
34 |
35 | junit
36 | junit
37 | test
38 |
39 |
40 |
41 |
42 |
43 |
44 | org.apache.maven.plugins
45 | maven-compiler-plugin
46 |
47 |
48 | org.apache.maven.plugins
49 | maven-source-plugin
50 |
51 |
52 | org.apache.maven.plugins
53 | maven-jar-plugin
54 |
55 |
56 | org.apache.maven.plugins
57 | maven-javadoc-plugin
58 |
59 |
60 | org.apache.maven.plugins
61 | maven-surefire-plugin
62 |
63 |
64 | org.eluder.coveralls
65 | coveralls-maven-plugin
66 |
67 |
68 | org.jacoco
69 | jacoco-maven-plugin
70 |
71 |
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/Handler.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.replay;
2 |
3 | /**
4 | * @author Joshua Shinavier (http://fortytwo.net).
5 | */
6 | @FunctionalInterface
7 | public interface Handler {
8 | void handle(T t) throws E;
9 | }
10 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/RecorderIteration.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.replay;
2 |
3 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
4 | import net.fortytwo.sesametools.replay.calls.HasNextCall;
5 | import net.fortytwo.sesametools.replay.calls.NextCall;
6 | import net.fortytwo.sesametools.replay.calls.RemoveCall;
7 | import net.fortytwo.sesametools.replay.calls.CloseIterationCall;
8 |
9 | /**
10 | * @author Joshua Shinavier (http://fortytwo.net).
11 | */
12 | public class RecorderIteration implements CloseableIteration {
13 | private final String id;
14 | private final CloseableIteration baseIteration;
15 | private final Handler queryHandler;
16 |
17 | public RecorderIteration(final CloseableIteration baseIteration,
18 | final String id,
19 | final Handler queryHandler) {
20 | this.baseIteration = baseIteration;
21 | this.id = id;
22 | this.queryHandler = queryHandler;
23 | }
24 |
25 | public void close() throws E {
26 | queryHandler.handle(new CloseIterationCall(id));
27 | baseIteration.close();
28 | }
29 |
30 | public boolean hasNext() throws E {
31 | queryHandler.handle(new HasNextCall(id));
32 | return baseIteration.hasNext();
33 | }
34 |
35 | public T next() throws E {
36 | queryHandler.handle(new NextCall(id));
37 | return baseIteration.next();
38 | }
39 |
40 | public void remove() throws E {
41 | queryHandler.handle(new RemoveCall(id));
42 | baseIteration.remove();
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/RecorderSail.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay;
3 |
4 | import org.eclipse.rdf4j.model.ValueFactory;
5 | import org.eclipse.rdf4j.sail.Sail;
6 | import org.eclipse.rdf4j.sail.SailConnection;
7 | import org.eclipse.rdf4j.sail.SailException;
8 | import org.eclipse.rdf4j.sail.StackableSail;
9 | import org.eclipse.rdf4j.sail.helpers.AbstractSail;
10 |
11 | import java.io.File;
12 | import java.io.OutputStream;
13 | import java.io.PrintStream;
14 |
15 | /**
16 | * A Sail which creates an ongoing log of operations as they are executed.
17 | * The log can later be used to recreate the operations in order.
18 | *
19 | * @author Joshua Shinavier (http://fortytwo.net).
20 | */
21 | public class RecorderSail extends AbstractSail implements StackableSail {
22 | private final Sail baseSail;
23 | private final ReplayConfiguration config;
24 | private final Handler queryHandler;
25 |
26 | public RecorderSail(final Sail baseSail,
27 | final Handler queryHandler) {
28 | this.baseSail = baseSail;
29 | config = new ReplayConfiguration();
30 |
31 | this.queryHandler = queryHandler;
32 | }
33 |
34 | public RecorderSail(final Sail baseSail, final OutputStream out) {
35 | this(baseSail, createDefaultHandler(out));
36 | }
37 |
38 | private static Handler createDefaultHandler(final OutputStream out) {
39 | final PrintStream ps = (out instanceof PrintStream)
40 | ? (PrintStream) out
41 | : new PrintStream(out);
42 |
43 | return new Handler() {
44 | public void handle(final SailConnectionCall sailQuery) throws SailException {
45 | ps.println(sailQuery.toString());
46 | }
47 | };
48 | }
49 |
50 | @Override
51 | public void setDataDir(final File file) {
52 | baseSail.setDataDir(file);
53 | }
54 |
55 | @Override
56 | public File getDataDir() {
57 | return baseSail.getDataDir();
58 | }
59 |
60 | protected void initializeInternal() throws SailException {
61 | baseSail.initialize();
62 | }
63 |
64 | protected void shutDownInternal() throws SailException {
65 | baseSail.shutDown();
66 | }
67 |
68 | @Override
69 | public boolean isWritable() throws SailException {
70 | return baseSail.isWritable();
71 | }
72 |
73 | protected SailConnection getConnectionInternal() throws SailException {
74 | return new RecorderSailConnection(this, baseSail, config, queryHandler);
75 | }
76 |
77 | public ValueFactory getValueFactory() {
78 | return baseSail.getValueFactory();
79 | }
80 |
81 | public void setBaseSail(final Sail sail) {
82 | // Do nothing -- base Sail is final
83 | }
84 |
85 | public Sail getBaseSail() {
86 | return baseSail;
87 | }
88 |
89 | public ReplayConfiguration getConfiguration() {
90 | return config;
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/ReplayConfiguration.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.replay;
2 |
3 | /**
4 | * @author Joshua Shinavier (http://fortytwo.net).
5 | */
6 | public class ReplayConfiguration {
7 | public static final boolean LOG_TRANSACTIONS = true;
8 | public static final boolean LOG_READ_OPERATIONS = true;
9 | public static final boolean LOG_WRITE_OPERATIONS = true;
10 | }
11 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/Source.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.replay;
2 |
3 | /**
4 | * @author Joshua Shinavier (http://fortytwo.net).
5 | */
6 | public interface Source {
7 | void writeTo(Handler handler) throws E;
8 | }
9 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/AddStatementCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.model.IRI;
6 | import org.eclipse.rdf4j.model.Resource;
7 | import org.eclipse.rdf4j.model.Value;
8 | import org.eclipse.rdf4j.sail.SailConnection;
9 | import org.eclipse.rdf4j.sail.SailException;
10 |
11 | import java.util.StringTokenizer;
12 |
13 | /**
14 | * @author Joshua Shinavier (http://fortytwo.net).
15 | */
16 | public class AddStatementCall extends SailConnectionCall {
17 |
18 | private final Resource subject;
19 | private final IRI predicate;
20 | private final Value object;
21 | private final Resource[] contexts;
22 |
23 | public AddStatementCall(final String id,
24 | final Resource subj,
25 | final IRI pred,
26 | final Value obj,
27 | final Resource... contexts) {
28 | super(id, Type.ADD_STATEMENT);
29 | this.subject = subj;
30 | this.predicate = pred;
31 | this.object = obj;
32 | this.contexts = contexts;
33 | }
34 |
35 | public String toString() {
36 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type)
37 | .append(DELIM).append(toString(subject))
38 | .append(DELIM).append(toString(predicate))
39 | .append(DELIM).append(toString(object))
40 | .append(DELIM).append(toString(contexts));
41 |
42 | return sb.toString();
43 | }
44 |
45 | public AddStatementCall(final String id,
46 | final Type type,
47 | final StringTokenizer tok) {
48 | super(id, type);
49 | this.subject = parseResource(tok.nextToken());
50 | this.predicate = parseIRI(tok.nextToken());
51 | this.object = parseValue(tok.nextToken());
52 | this.contexts = parseContexts(tok.nextToken());
53 | }
54 |
55 | public Object execute(final SailConnection sc) throws SailException {
56 | sc.addStatement(subject, predicate, object, contexts);
57 | return null;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/BeginCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.sail.SailConnection;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import java.util.StringTokenizer;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class BeginCall extends SailConnectionCall {
14 | public BeginCall(final String id) {
15 | super(id, Type.BEGIN);
16 | }
17 |
18 | public String toString() {
19 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type);
20 |
21 | return sb.toString();
22 | }
23 |
24 | public BeginCall(final String id,
25 | final Type type,
26 | final StringTokenizer tok) {
27 | super(id, type);
28 | }
29 |
30 | public Object execute(final SailConnection sc) throws SailException {
31 | sc.begin();
32 | return null;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/ClearCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.model.Resource;
6 | import org.eclipse.rdf4j.sail.SailConnection;
7 | import org.eclipse.rdf4j.sail.SailException;
8 |
9 | import java.util.StringTokenizer;
10 |
11 | /**
12 | * @author Joshua Shinavier (http://fortytwo.net).
13 | */
14 | public class ClearCall extends SailConnectionCall {
15 |
16 | private final Resource[] contexts;
17 |
18 | public ClearCall(final String id,
19 | final Resource... contexts) {
20 | super(id, Type.CLEAR);
21 | this.contexts = contexts;
22 | }
23 |
24 | public String toString() {
25 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type)
26 | .append(DELIM).append(toString(contexts));
27 |
28 | return sb.toString();
29 | }
30 |
31 | public ClearCall(final String id,
32 | final Type type,
33 | final StringTokenizer tok) {
34 | super(id, type);
35 | this.contexts = parseContexts(tok.nextToken());
36 | }
37 |
38 | public Object execute(final SailConnection sc) throws SailException {
39 | sc.clear(contexts);
40 | return null;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/ClearNamespacesCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.sail.SailConnection;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import java.util.StringTokenizer;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class ClearNamespacesCall extends SailConnectionCall {
14 |
15 | public ClearNamespacesCall(final String id) {
16 | super(id, Type.CLEAR_NAMESPACES);
17 | }
18 |
19 | public String toString() {
20 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type);
21 |
22 | return sb.toString();
23 | }
24 |
25 | public ClearNamespacesCall(final String id,
26 | final Type type,
27 | final StringTokenizer tok) {
28 | super(id, type);
29 | }
30 |
31 | public Object execute(final SailConnection sc) throws SailException {
32 | sc.clearNamespaces();
33 | return null;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/CloseConnectionCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.sail.SailConnection;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import java.util.StringTokenizer;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class CloseConnectionCall extends SailConnectionCall {
14 | public CloseConnectionCall(final String id) {
15 | super(id, Type.CLOSE_CONNECTION);
16 | }
17 |
18 | public String toString() {
19 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type);
20 |
21 | return sb.toString();
22 | }
23 |
24 | public CloseConnectionCall(final String id,
25 | final Type type,
26 | final StringTokenizer tok) {
27 | super(id, type);
28 | }
29 |
30 | public Object execute(final SailConnection sc) throws SailException {
31 | sc.close();
32 | return null;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/CloseIterationCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import java.util.StringTokenizer;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class CloseIterationCall extends SailConnectionCall, Object> {
14 | public CloseIterationCall(final String id) {
15 | super(id, Type.CLOSE_ITERATION);
16 | }
17 |
18 | public String toString() {
19 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type);
20 |
21 | return sb.toString();
22 | }
23 |
24 | public CloseIterationCall(final String id,
25 | final Type type,
26 | final StringTokenizer tok) {
27 | super(id, type);
28 | }
29 |
30 | public Object execute(final CloseableIteration, SailException> t) throws SailException {
31 | t.close();
32 | return null;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/CommitCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.sail.SailConnection;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import java.util.StringTokenizer;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class CommitCall extends SailConnectionCall {
14 | public CommitCall(final String id) {
15 | super(id, Type.COMMIT);
16 | }
17 |
18 | public String toString() {
19 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type);
20 |
21 | return sb.toString();
22 | }
23 |
24 | public CommitCall(final String id,
25 | final Type type,
26 | final StringTokenizer tok) {
27 | super(id, type);
28 | }
29 |
30 | public Object execute(final SailConnection sc) throws SailException {
31 | sc.commit();
32 | return null;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/ConstructorCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.sail.SailConnection;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import java.util.StringTokenizer;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class ConstructorCall extends SailConnectionCall {
14 | public ConstructorCall(final String id) {
15 | super(id, Type.CONSTRUCT);
16 | }
17 |
18 | public String toString() {
19 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type);
20 |
21 | return sb.toString();
22 | }
23 |
24 | public ConstructorCall(final String id,
25 | final Type type,
26 | final StringTokenizer tok) {
27 | super(id, type);
28 | }
29 |
30 | public Object execute(final SailConnection sc) throws SailException {
31 | // Do nothing: connection has already been constructed
32 | return null;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/EvaluateCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
5 | import net.fortytwo.sesametools.EmptyCloseableIteration;
6 | import net.fortytwo.sesametools.replay.SailConnectionCall;
7 | import org.eclipse.rdf4j.sail.SailConnection;
8 | import org.eclipse.rdf4j.sail.SailException;
9 |
10 | import java.util.StringTokenizer;
11 |
12 | /**
13 | * @author Joshua Shinavier (http://fortytwo.net).
14 | */
15 | public class EvaluateCall extends SailConnectionCall {
16 |
17 | private final boolean includeInferred;
18 |
19 | public EvaluateCall(final String id,
20 | final boolean includeInferred) {
21 | super(id, Type.EVALUATE);
22 | this.includeInferred = includeInferred;
23 | }
24 |
25 | public String toString() {
26 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type)
27 | .append(DELIM).append(toString(includeInferred));
28 |
29 | return sb.toString();
30 | }
31 |
32 | public EvaluateCall(final String id,
33 | final Type type,
34 | final StringTokenizer tok) {
35 | super(id, type);
36 | this.includeInferred = parseBoolean(tok.nextToken());
37 | }
38 |
39 | public CloseableIteration execute(final SailConnection sc) throws SailException {
40 | // not enough information to reconstruct an evaluate call
41 | return new EmptyCloseableIteration();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/GetContextIDsCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
6 | import org.eclipse.rdf4j.sail.SailConnection;
7 | import org.eclipse.rdf4j.sail.SailException;
8 |
9 | import java.util.StringTokenizer;
10 |
11 | /**
12 | * @author Joshua Shinavier (http://fortytwo.net).
13 | */
14 | public class GetContextIDsCall extends SailConnectionCall {
15 | public GetContextIDsCall(final String id) {
16 | super(id, Type.GET_CONTEXT_IDS);
17 | }
18 |
19 | public String toString() {
20 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type);
21 |
22 | return sb.toString();
23 | }
24 |
25 | public GetContextIDsCall(final String id,
26 | final Type type,
27 | final StringTokenizer tok) {
28 | super(id, type);
29 | }
30 |
31 | public CloseableIteration execute(final SailConnection sc) throws SailException {
32 | return sc.getContextIDs();
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/GetNamespaceCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.sail.SailConnection;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import java.util.StringTokenizer;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class GetNamespaceCall extends SailConnectionCall {
14 | private final String prefix;
15 |
16 | public GetNamespaceCall(final String id,
17 | final String prefix) {
18 | super(id, Type.GET_NAMESPACE);
19 | this.prefix = prefix;
20 | }
21 |
22 | public String toString() {
23 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type)
24 | .append(DELIM).append(toString(prefix));
25 |
26 | return sb.toString();
27 | }
28 |
29 | public GetNamespaceCall(final String id,
30 | final Type type,
31 | final StringTokenizer tok) {
32 | super(id, type);
33 | this.prefix = parseString(tok.nextToken());
34 | }
35 |
36 | public String execute(final SailConnection sc) throws SailException {
37 | return sc.getNamespace(prefix);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/GetNamespacesCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
6 | import org.eclipse.rdf4j.sail.SailConnection;
7 | import org.eclipse.rdf4j.sail.SailException;
8 |
9 | import java.util.StringTokenizer;
10 |
11 | /**
12 | * @author Joshua Shinavier (http://fortytwo.net).
13 | */
14 | public class GetNamespacesCall extends SailConnectionCall {
15 |
16 | public GetNamespacesCall(final String id) {
17 | super(id, Type.GET_NAMESPACES);
18 | }
19 |
20 | public String toString() {
21 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type);
22 |
23 | return sb.toString();
24 | }
25 |
26 | public GetNamespacesCall(final String id,
27 | final Type type,
28 | final StringTokenizer tok) {
29 | super(id, type);
30 | }
31 |
32 | public CloseableIteration execute(final SailConnection sc) throws SailException {
33 | return sc.getNamespaces();
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/GetStatementsCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
6 | import org.eclipse.rdf4j.model.Resource;
7 | import org.eclipse.rdf4j.model.IRI;
8 | import org.eclipse.rdf4j.model.Value;
9 | import org.eclipse.rdf4j.sail.SailConnection;
10 | import org.eclipse.rdf4j.sail.SailException;
11 |
12 | import java.util.StringTokenizer;
13 |
14 | /**
15 | * @author Joshua Shinavier (http://fortytwo.net).
16 | */
17 | public class GetStatementsCall extends SailConnectionCall {
18 |
19 | private final Resource subject;
20 | private final IRI predicate;
21 | private final Value object;
22 | private final boolean includeInferred;
23 | private final Resource[] contexts;
24 |
25 | public GetStatementsCall(final String id,
26 | final Resource subj,
27 | final IRI pred,
28 | final Value obj,
29 | final boolean includeInferred,
30 | final Resource... contexts) {
31 | super(id, Type.GET_STATEMENTS);
32 | this.subject = subj;
33 | this.predicate = pred;
34 | this.object = obj;
35 | this.includeInferred = includeInferred;
36 | this.contexts = contexts;
37 | }
38 |
39 | public String toString() {
40 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type)
41 | .append(DELIM).append(toString(subject))
42 | .append(DELIM).append(toString(predicate))
43 | .append(DELIM).append(toString(object))
44 | .append(DELIM).append(toString(includeInferred))
45 | .append(DELIM).append(toString(contexts));
46 |
47 | return sb.toString();
48 | }
49 |
50 | public GetStatementsCall(final String id,
51 | final Type type,
52 | final StringTokenizer tok) {
53 | super(id, type);
54 | this.subject = parseResource(tok.nextToken());
55 | this.predicate = parseIRI(tok.nextToken());
56 | this.object = parseValue(tok.nextToken());
57 | this.includeInferred = parseBoolean(tok.nextToken());
58 | this.contexts = parseContexts(tok.nextToken());
59 | }
60 |
61 | public CloseableIteration execute(final SailConnection sc) throws SailException {
62 | return sc.getStatements(subject, predicate, object, includeInferred, contexts);
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/HasNextCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import java.util.StringTokenizer;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class HasNextCall extends SailConnectionCall, Object> {
14 | public HasNextCall(final String id) {
15 | super(id, Type.HAS_NEXT);
16 | }
17 |
18 | public String toString() {
19 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type);
20 |
21 | return sb.toString();
22 | }
23 |
24 | public HasNextCall(final String id,
25 | final Type type,
26 | final StringTokenizer tok) {
27 | super(id, type);
28 | }
29 |
30 | public Object execute(final CloseableIteration, SailException> t) throws SailException {
31 | t.hasNext();
32 | return null;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/NextCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import java.util.StringTokenizer;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class NextCall extends SailConnectionCall, Object> {
14 | public NextCall(final String id) {
15 | super(id, Type.NEXT);
16 | }
17 |
18 | public String toString() {
19 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type);
20 |
21 | return sb.toString();
22 | }
23 |
24 | public NextCall(final String id,
25 | final Type type,
26 | final StringTokenizer tok) {
27 | super(id, type);
28 | }
29 |
30 | public Object execute(final CloseableIteration, SailException> t) throws SailException {
31 | t.next();
32 | return null;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/RemoveCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import java.util.StringTokenizer;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class RemoveCall extends SailConnectionCall, Object> {
14 | public RemoveCall(final String id) {
15 | super(id, Type.REMOVE);
16 | }
17 |
18 | public String toString() {
19 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type);
20 |
21 | return sb.toString();
22 | }
23 |
24 | public RemoveCall(final String id,
25 | final Type type,
26 | final StringTokenizer tok) {
27 | super(id, type);
28 | }
29 |
30 | public Object execute(final CloseableIteration, SailException> t) throws SailException {
31 | t.remove();
32 | return null;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/RemoveNamespaceCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.sail.SailConnection;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import java.util.StringTokenizer;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class RemoveNamespaceCall extends SailConnectionCall {
14 | private final String prefix;
15 |
16 | public RemoveNamespaceCall(final String id,
17 | final String prefix) {
18 | super(id, Type.REMOVE_NAMESPACE);
19 | this.prefix = prefix;
20 | }
21 |
22 | public String toString() {
23 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type)
24 | .append(DELIM).append(toString(prefix));
25 |
26 | return sb.toString();
27 | }
28 |
29 | public RemoveNamespaceCall(final String id,
30 | final Type type,
31 | final StringTokenizer tok) {
32 | super(id, type);
33 | this.prefix = parseString(tok.nextToken());
34 | }
35 |
36 | public Object execute(final SailConnection sc) throws SailException {
37 | sc.removeNamespace(prefix);
38 | return null;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/RemoveStatementsCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.model.IRI;
6 | import org.eclipse.rdf4j.model.Resource;
7 | import org.eclipse.rdf4j.model.Value;
8 | import org.eclipse.rdf4j.sail.SailConnection;
9 | import org.eclipse.rdf4j.sail.SailException;
10 |
11 | import java.util.StringTokenizer;
12 |
13 | /**
14 | * @author Joshua Shinavier (http://fortytwo.net).
15 | */
16 | public class RemoveStatementsCall extends SailConnectionCall {
17 |
18 | private final Resource subject;
19 | private final IRI predicate;
20 | private final Value object;
21 | private final Resource[] contexts;
22 |
23 | public RemoveStatementsCall(final String id,
24 | final Resource subj,
25 | final IRI pred,
26 | final Value obj,
27 | final Resource... contexts) {
28 | super(id, Type.REMOVE_STATEMENTS);
29 | this.subject = subj;
30 | this.predicate = pred;
31 | this.object = obj;
32 | this.contexts = contexts;
33 | }
34 |
35 | public String toString() {
36 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type)
37 | .append(DELIM).append(toString(subject))
38 | .append(DELIM).append(toString(predicate))
39 | .append(DELIM).append(toString(object))
40 | .append(DELIM).append(toString(contexts));
41 |
42 | return sb.toString();
43 | }
44 |
45 | public RemoveStatementsCall(final String id,
46 | final Type type,
47 | final StringTokenizer tok) {
48 | super(id, type);
49 | this.subject = parseResource(tok.nextToken());
50 | this.predicate = parseIRI(tok.nextToken());
51 | this.object = parseValue(tok.nextToken());
52 | this.contexts = parseContexts(tok.nextToken());
53 | }
54 |
55 | public Object execute(final SailConnection sc) throws SailException {
56 | sc.removeStatements(subject, predicate, object, contexts);
57 | return null;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/RollbackCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.sail.SailConnection;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import java.util.StringTokenizer;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class RollbackCall extends SailConnectionCall {
14 | public RollbackCall(final String id) {
15 | super(id, Type.ROLLBACK);
16 | }
17 |
18 | public String toString() {
19 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type);
20 |
21 | return sb.toString();
22 | }
23 |
24 | public RollbackCall(final String id,
25 | final Type type,
26 | final StringTokenizer tok) {
27 | super(id, type);
28 | }
29 |
30 | public Object execute(final SailConnection sc) throws SailException {
31 | sc.rollback();
32 | return null;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/SetNamespaceCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.sail.SailConnection;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import java.util.StringTokenizer;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class SetNamespaceCall extends SailConnectionCall {
14 | private final String prefix;
15 | private final String uri;
16 |
17 | public SetNamespaceCall(final String id,
18 | final String prefix,
19 | final String uri) {
20 | super(id, Type.SET_NAMESPACE);
21 | this.prefix = prefix;
22 | this.uri = uri;
23 | }
24 |
25 | public String toString() {
26 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type)
27 | .append(DELIM).append(toString(prefix))
28 | .append(DELIM).append(toString(uri));
29 |
30 | return sb.toString();
31 | }
32 |
33 | public SetNamespaceCall(final String id,
34 | final Type type,
35 | final StringTokenizer tok) {
36 | super(id, type);
37 | this.prefix = parseString(tok.nextToken());
38 | this.uri = parseString(tok.nextToken());
39 | }
40 |
41 | public Object execute(final SailConnection sc) throws SailException {
42 | sc.setNamespace(prefix, uri);
43 | return null;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/replay-sail/src/main/java/net/fortytwo/sesametools/replay/calls/SizeCall.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.replay.calls;
3 |
4 | import net.fortytwo.sesametools.replay.SailConnectionCall;
5 | import org.eclipse.rdf4j.model.Resource;
6 | import org.eclipse.rdf4j.sail.SailConnection;
7 | import org.eclipse.rdf4j.sail.SailException;
8 |
9 | import java.util.StringTokenizer;
10 |
11 | /**
12 | * @author Joshua Shinavier (http://fortytwo.net).
13 | */
14 | public class SizeCall extends SailConnectionCall {
15 |
16 | private final Resource[] contexts;
17 |
18 | public SizeCall(final String id,
19 | final Resource... contexts) {
20 | super(id, Type.SIZE);
21 | this.contexts = contexts;
22 | }
23 |
24 | public String toString() {
25 | StringBuilder sb = new StringBuilder(id).append(DELIM).append(type)
26 | .append(DELIM).append(toString(contexts));
27 |
28 | return sb.toString();
29 | }
30 |
31 | public SizeCall(final String id,
32 | final Type type,
33 | final StringTokenizer tok) {
34 | super(id, type);
35 | this.contexts = parseContexts(tok.nextToken());
36 | }
37 |
38 | public Long execute(final SailConnection sc) throws SailException {
39 | return sc.size(contexts);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/repository-sail/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | .classpath
3 | .project
4 | .settings
5 | *.iml
6 | *~
7 |
--------------------------------------------------------------------------------
/repository-sail/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 4.0.0
5 | repository-sail
6 | jar
7 | RepositorySail
8 | A Sail implementation which wraps a Repository object
9 |
10 |
11 | net.fortytwo.sesametools
12 | sesametools-all
13 | 2.0-SNAPSHOT
14 | ../pom.xml
15 |
16 |
17 |
18 |
19 | net.fortytwo.sesametools
20 | common
21 |
22 |
23 | org.eclipse.rdf4j
24 | rdf4j-repository-api
25 |
26 |
27 | org.eclipse.rdf4j
28 | rdf4j-sail-api
29 |
30 |
31 |
32 |
33 | junit
34 | junit
35 | test
36 |
37 |
38 |
39 |
40 |
41 |
42 | org.apache.maven.plugins
43 | maven-compiler-plugin
44 |
45 |
46 | org.apache.maven.plugins
47 | maven-source-plugin
48 |
49 |
50 | org.apache.maven.plugins
51 | maven-jar-plugin
52 |
53 |
54 | org.apache.maven.plugins
55 | maven-javadoc-plugin
56 |
57 |
58 | org.apache.maven.plugins
59 | maven-surefire-plugin
60 |
61 |
62 | org.eluder.coveralls
63 | coveralls-maven-plugin
64 |
65 |
66 | org.jacoco
67 | jacoco-maven-plugin
68 |
69 |
70 |
71 |
72 |
73 |
--------------------------------------------------------------------------------
/repository-sail/src/main/java/net/fortytwo/sesametools/reposail/RepositoryNamespaceIteration.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.reposail;
3 |
4 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
5 | import org.eclipse.rdf4j.model.Namespace;
6 | import org.eclipse.rdf4j.repository.RepositoryException;
7 | import org.eclipse.rdf4j.sail.SailException;
8 |
9 | /**
10 | * @author Joshua Shinavier (http://fortytwo.net).
11 | */
12 | public class RepositoryNamespaceIteration implements CloseableIteration {
13 | private CloseableIteration extends Namespace, RepositoryException> innerIter;
14 |
15 | public RepositoryNamespaceIteration(CloseableIteration extends Namespace, RepositoryException> innerIter) {
16 | this.innerIter = innerIter;
17 | }
18 |
19 | public void close() throws SailException {
20 | try {
21 | innerIter.close();
22 | } catch (RepositoryException e) {
23 | throw new SailException(e);
24 | }
25 | }
26 |
27 | public boolean hasNext() throws SailException {
28 | try {
29 | return innerIter.hasNext();
30 | } catch (RepositoryException e) {
31 | throw new SailException(e);
32 | }
33 | }
34 |
35 | public Namespace next() throws SailException {
36 | try {
37 | return innerIter.next();
38 | } catch (RepositoryException e) {
39 | throw new SailException(e);
40 | }
41 | }
42 |
43 | public void remove() throws SailException {
44 | try {
45 | innerIter.remove();
46 | } catch (RepositoryException e) {
47 | throw new SailException(e);
48 | }
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/repository-sail/src/main/java/net/fortytwo/sesametools/reposail/RepositoryResourceIteration.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.reposail;
3 |
4 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
5 | import org.eclipse.rdf4j.model.Resource;
6 | import org.eclipse.rdf4j.repository.RepositoryException;
7 | import org.eclipse.rdf4j.sail.SailException;
8 |
9 | /**
10 | * @author Joshua Shinavier (http://fortytwo.net).
11 | */
12 | public class RepositoryResourceIteration implements CloseableIteration {
13 | private CloseableIteration extends Resource, RepositoryException> innerIter;
14 |
15 | public RepositoryResourceIteration(CloseableIteration extends Resource, RepositoryException> innerIter) {
16 | this.innerIter = innerIter;
17 | }
18 |
19 | public void close() throws SailException {
20 | try {
21 | innerIter.close();
22 | } catch (RepositoryException e) {
23 | throw new SailException(e);
24 | }
25 | }
26 |
27 | public boolean hasNext() throws SailException {
28 | try {
29 | return innerIter.hasNext();
30 | } catch (RepositoryException e) {
31 | throw new SailException(e);
32 | }
33 | }
34 |
35 | public Resource next() throws SailException {
36 | try {
37 | return innerIter.next();
38 | } catch (RepositoryException e) {
39 | throw new SailException(e);
40 | }
41 | }
42 |
43 | public void remove() throws SailException {
44 | try {
45 | innerIter.remove();
46 | } catch (RepositoryException e) {
47 | throw new SailException(e);
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/repository-sail/src/main/java/net/fortytwo/sesametools/reposail/RepositorySail.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.reposail;
2 |
3 | import org.eclipse.rdf4j.model.ValueFactory;
4 | import org.eclipse.rdf4j.repository.Repository;
5 | import org.eclipse.rdf4j.repository.RepositoryConnection;
6 | import org.eclipse.rdf4j.repository.RepositoryException;
7 | import org.eclipse.rdf4j.sail.SailConnection;
8 | import org.eclipse.rdf4j.sail.SailException;
9 | import org.eclipse.rdf4j.sail.helpers.AbstractSail;
10 |
11 | /**
12 | * A Sail
which wraps a Repository
13 | *
14 | * @author Joshua Shinavier (http://fortytwo.net).
15 | */
16 | public class RepositorySail extends AbstractSail {
17 |
18 | private Repository repository;
19 | private boolean inferenceDisabled = false;
20 |
21 | public RepositorySail(final Repository repo) {
22 | this.repository = repo;
23 | }
24 |
25 | public Repository getRepository() {
26 | return repository;
27 | }
28 |
29 | public void disableInference() {
30 | inferenceDisabled = true;
31 | }
32 |
33 | protected SailConnection getConnectionInternal() throws SailException {
34 | RepositoryConnection rc;
35 |
36 | try {
37 | rc = repository.getConnection();
38 | } catch (RepositoryException e) {
39 | throw new SailException(e);
40 | }
41 |
42 | return new RepositorySailConnection(this, rc, inferenceDisabled, this.getValueFactory());
43 | }
44 |
45 | public ValueFactory getValueFactory() {
46 | return repository.getValueFactory();
47 | }
48 |
49 | protected void initializeInternal() throws SailException {
50 | try {
51 | repository.initialize();
52 | } catch (RepositoryException e) {
53 | throw new SailException(e);
54 | }
55 | }
56 |
57 | public boolean isWritable() throws SailException {
58 | try {
59 | return repository.isWritable();
60 | } catch (RepositoryException e) {
61 | throw new SailException(e);
62 | }
63 | }
64 |
65 | protected void shutDownInternal() throws SailException {
66 | try {
67 | repository.shutDown();
68 | } catch (RepositoryException e) {
69 | throw new SailException(e);
70 | }
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/repository-sail/src/main/java/net/fortytwo/sesametools/reposail/RepositoryStatementIteration.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.reposail;
3 |
4 | import org.eclipse.rdf4j.model.Statement;
5 | import org.eclipse.rdf4j.repository.RepositoryException;
6 | import org.eclipse.rdf4j.sail.SailException;
7 |
8 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
9 |
10 | /**
11 | * @author Joshua Shinavier (http://fortytwo.net).
12 | */
13 | public class RepositoryStatementIteration implements CloseableIteration {
14 | private CloseableIteration extends Statement, RepositoryException> innerIter;
15 |
16 | public RepositoryStatementIteration(CloseableIteration extends Statement, RepositoryException> innerIter) {
17 | this.innerIter = innerIter;
18 | }
19 |
20 | public void close() throws SailException {
21 | try {
22 | innerIter.close();
23 | } catch (RepositoryException e) {
24 | throw new SailException(e);
25 | }
26 | }
27 |
28 | public boolean hasNext() throws SailException {
29 | try {
30 | return innerIter.hasNext();
31 | } catch (RepositoryException e) {
32 | throw new SailException(e);
33 | }
34 | }
35 |
36 | public Statement next() throws SailException {
37 | try {
38 | return innerIter.next();
39 | } catch (RepositoryException e) {
40 | throw new SailException(e);
41 | }
42 | }
43 |
44 | public void remove() throws SailException {
45 | try {
46 | innerIter.remove();
47 | } catch (RepositoryException e) {
48 | throw new SailException(e);
49 | }
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/sesamize/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | .classpath
3 | .project
4 | .settings
5 | *.iml
6 | *~
7 |
--------------------------------------------------------------------------------
/sesamize/sesamize.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # Find Java
4 | if [ "$JAVA_HOME" = "" ] ; then
5 | JAVA="java"
6 | else
7 | JAVA="$JAVA_HOME/bin/java"
8 | fi
9 |
10 | # Set Java options
11 | if [ "$JAVA_OPTIONS" = "" ] ; then
12 | JAVA_OPTIONS="-Xms32M -Xmx512M"
13 | fi
14 |
15 | DIR=`dirname $0`
16 |
17 | # Launch the application
18 | $JAVA $JAVA_OPTIONS -cp $DIR/target/classes:$DIR/"target/dependency/*" net.fortytwo.sesametools.sesamize.Sesamize $*
19 |
20 | # Return the program's exit code
21 | exit $?
22 |
--------------------------------------------------------------------------------
/sesamize/src/main/java/net/fortytwo/sesametools/sesamize/Command.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.sesamize;
2 |
3 | import com.google.common.base.Preconditions;
4 |
5 | import java.util.HashMap;
6 | import java.util.LinkedList;
7 | import java.util.List;
8 | import java.util.Map;
9 |
10 | import static net.fortytwo.sesametools.sesamize.Sesamize.DEFAULT_BASEURI;
11 |
12 | public abstract class Command {
13 | private final String name;
14 | private List anonymousParameters = new LinkedList<>();
15 | private Map namedParameters = new HashMap<>();
16 |
17 | public Command(final String name) {
18 | Preconditions.checkNotNull(name);
19 | this.name = name;
20 | }
21 |
22 | public abstract void execute(SesamizeArgs args) throws Exception;
23 |
24 | protected void addParameter(final Parameter param) {
25 | if (null == param.getName()) {
26 | anonymousParameters.add(param);
27 | } else {
28 | if (getNamedParameters().keySet().contains(param.getName())) {
29 | throw new ExceptionInInitializerError("duplicate parameter '" + param.getName() + "'");
30 | }
31 |
32 | getNamedParameters().put(param.getName(), param);
33 | }
34 | }
35 |
36 | public String getName() {
37 | return name;
38 | }
39 |
40 | public Map getNamedParameters() {
41 | return namedParameters;
42 | }
43 |
44 | public List getAnonymousParameters() {
45 | return anonymousParameters;
46 | }
47 |
48 | protected static String getBaseURI(final SesamizeArgs args) {
49 | return args.getOption(DEFAULT_BASEURI, "b", "baseuri");
50 | }
51 |
52 | public static class Parameter {
53 | private final String name;
54 | private final String shortName;
55 | private final boolean required;
56 | private final Class valueClass;
57 | private final T defaultValue;
58 | private final String description;
59 |
60 | public Parameter(String name,
61 | String shortName,
62 | boolean required,
63 | Class valueClass,
64 | T defaultValue,
65 | String description) {
66 | Preconditions.checkNotNull(valueClass);
67 | Preconditions.checkArgument(null == shortName || null != name);
68 |
69 | this.name = name;
70 | this.description = description;
71 | this.shortName = shortName;
72 | this.required = required;
73 | this.valueClass = valueClass;
74 | this.defaultValue = defaultValue;
75 | }
76 |
77 | public String getShortestName() {
78 | return null != getShortName() ? getShortName() : getName();
79 | }
80 |
81 | public String getName() {
82 | return name;
83 | }
84 |
85 | public String getShortName() {
86 | return shortName;
87 | }
88 |
89 | public boolean isRequired() {
90 | return required;
91 | }
92 |
93 | public boolean isRequiredAndHasNoDefaultValue() {
94 | return required && null == defaultValue;
95 | }
96 |
97 | public Class getValueClass() {
98 | return valueClass;
99 | }
100 |
101 | public T getDefaultValue() {
102 | return defaultValue;
103 | }
104 |
105 | public String getDescription() {
106 | return description;
107 | }
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/sesamize/src/main/java/net/fortytwo/sesametools/sesamize/SparqlResultFormat.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.sesamize;
2 |
3 | /**
4 | * @author Joshua Shinavier (http://fortytwo.net).
5 | */
6 | public enum SparqlResultFormat {
7 | // Note: the XML format is defined first, so that it is the default format.
8 | XML("application/sparql-results+xml", "xml"),
9 | CSV("text/csv", "csv"),
10 | JSON("application/sparql-results+json", "json"),
11 | TAB("text/tab-delimited-values", "tab", "tsv");
12 |
13 | private final String mediaType;
14 | private final String[] nicknames;
15 |
16 | SparqlResultFormat(final String mimeType,
17 | final String... nicknames) {
18 | mediaType = mimeType;
19 | this.nicknames = nicknames;
20 | }
21 |
22 | public String getMediaType() {
23 | return mediaType;
24 | }
25 |
26 | public static SparqlResultFormat lookup(final String mediaType) {
27 | for (SparqlResultFormat f : SparqlResultFormat.values()) {
28 | if (f.mediaType.equals(mediaType)) {
29 | return f;
30 | }
31 | }
32 |
33 | return null;
34 | }
35 |
36 | public static SparqlResultFormat lookupByNickname(final String name) {
37 | for (SparqlResultFormat f : SparqlResultFormat.values()) {
38 | for (String s : f.nicknames) {
39 | if (s.equals(name)) {
40 | return f;
41 | }
42 | }
43 | }
44 |
45 | return null;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/sesamize/src/main/java/net/fortytwo/sesametools/sesamize/commands/Construct.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.sesamize.commands;
2 |
3 | import net.fortytwo.sesametools.sesamize.SesamizeArgs;
4 | import net.fortytwo.sesametools.sesamize.Command;
5 | import org.apache.commons.io.IOUtils;
6 | import org.eclipse.rdf4j.query.MalformedQueryException;
7 | import org.eclipse.rdf4j.query.QueryEvaluationException;
8 | import org.eclipse.rdf4j.query.QueryLanguage;
9 | import org.eclipse.rdf4j.repository.Repository;
10 | import org.eclipse.rdf4j.repository.RepositoryConnection;
11 | import org.eclipse.rdf4j.repository.RepositoryException;
12 | import org.eclipse.rdf4j.repository.sail.SailRepository;
13 | import org.eclipse.rdf4j.rio.RDFFormat;
14 | import org.eclipse.rdf4j.rio.RDFHandlerException;
15 | import org.eclipse.rdf4j.rio.RDFParseException;
16 | import org.eclipse.rdf4j.rio.RDFWriter;
17 | import org.eclipse.rdf4j.rio.Rio;
18 | import org.eclipse.rdf4j.sail.Sail;
19 | import org.eclipse.rdf4j.sail.SailException;
20 | import org.eclipse.rdf4j.sail.memory.MemoryStore;
21 |
22 | import java.io.File;
23 | import java.io.FileInputStream;
24 | import java.io.IOException;
25 | import java.io.InputStream;
26 | import java.io.OutputStream;
27 |
28 | public class Construct extends Command {
29 |
30 | public Construct() {
31 | super("construct");
32 |
33 | addParameter(new Parameter<>(
34 | "query", null, true, File.class, null,
35 | "file with SPARQL CONSTRUCT query"));
36 | addParameter(new Parameter<>(
37 | "inputFormat", "i", true, RDFFormat.class, RDFFormat.RDFXML,
38 | "input RDF format (e.g. 'Turtle')"));
39 | addParameter(new Parameter<>(
40 | "outputFormat", "o", true, RDFFormat.class, RDFFormat.RDFXML,
41 | "output RDF format (e.g. 'N-Triples')"));
42 | }
43 |
44 | @Override
45 | public void execute(SesamizeArgs args) throws IOException {
46 | File inputFile = new File(args.nonOptions.get(1));
47 |
48 | RDFFormat inputFormat = args.getRDFFormat(inputFile, RDFFormat.RDFXML, "i", "inputFormat");
49 | RDFFormat outputFormat = args.getRDFFormat(RDFFormat.RDFXML, "o", "outputFormat");
50 |
51 | String qFile = args.getOption(null, "query");
52 |
53 | try (InputStream fileInput = new FileInputStream(qFile)) {
54 |
55 | String query = IOUtils.toString(fileInput, "UTF-8");
56 |
57 | translateRDFDocumentUseingConstructQuery(
58 | query, inputFile, System.out, inputFormat, outputFormat, getBaseURI(args));
59 | }
60 | }
61 |
62 | private static void translateRDFDocumentUseingConstructQuery(final String query,
63 | final File inputFile,
64 | final OutputStream out,
65 | final RDFFormat inFormat,
66 | final RDFFormat outFormat,
67 | final String baseURI)
68 | throws SailException, IOException, RDFHandlerException, RDFParseException, RepositoryException,
69 | MalformedQueryException, QueryEvaluationException {
70 |
71 | Sail sail = new MemoryStore();
72 | sail.initialize();
73 |
74 | try {
75 | Repository repo = new SailRepository(sail);
76 | try (RepositoryConnection rc = repo.getConnection()) {
77 | rc.add(inputFile, baseURI, inFormat);
78 | rc.commit();
79 |
80 | RDFWriter w = Rio.createWriter(outFormat, out);
81 |
82 | rc.prepareGraphQuery(QueryLanguage.SPARQL, query).evaluate(w);
83 | }
84 | } finally {
85 | sail.shutDown();
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/sesamize/src/main/java/net/fortytwo/sesametools/sesamize/commands/Dump.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.sesamize.commands;
2 |
3 | import net.fortytwo.sesametools.sesamize.SesamizeArgs;
4 | import net.fortytwo.sesametools.sesamize.Command;
5 | import org.eclipse.rdf4j.model.Resource;
6 | import org.eclipse.rdf4j.repository.Repository;
7 | import org.eclipse.rdf4j.repository.RepositoryConnection;
8 | import org.eclipse.rdf4j.repository.RepositoryException;
9 | import org.eclipse.rdf4j.repository.sail.SailRepository;
10 | import org.eclipse.rdf4j.rio.RDFFormat;
11 | import org.eclipse.rdf4j.rio.RDFHandler;
12 | import org.eclipse.rdf4j.rio.RDFHandlerException;
13 | import org.eclipse.rdf4j.rio.Rio;
14 | import org.eclipse.rdf4j.sail.Sail;
15 | import org.eclipse.rdf4j.sail.SailException;
16 | import org.eclipse.rdf4j.sail.nativerdf.NativeStore;
17 | import org.slf4j.Logger;
18 | import org.slf4j.LoggerFactory;
19 |
20 | import java.io.File;
21 | import java.io.FileOutputStream;
22 | import java.io.IOException;
23 | import java.io.OutputStream;
24 |
25 | public class Dump extends Command {
26 | private final static Logger logger = LoggerFactory.getLogger(Dump.class);
27 |
28 | public Dump() {
29 | super("dump");
30 |
31 | addParameter(new Parameter<>(null, null, true, File.class, null,
32 | "NativeStore directory"));
33 | addParameter(new Parameter<>(null, null, true, File.class, null,
34 | "output location (e.g. 'dump.nt')"));
35 | addParameter(new Parameter<>(
36 | "outputFormat", "o", true, RDFFormat.class, RDFFormat.RDFXML,
37 | "output RDF format (e.g. 'N-Triples')"));
38 | }
39 |
40 | @Override
41 | public void execute(SesamizeArgs args) throws Exception {
42 | File dir = new File(args.nonOptions.get(1));
43 | File file = new File(args.nonOptions.get(2));
44 |
45 | RDFFormat outputFormat = args.getRDFFormat(RDFFormat.RDFXML, "o", "outputFormat");
46 |
47 | dumpNativeStoreToRDFDocument(dir, file, outputFormat);
48 | }
49 |
50 | private void dumpNativeStoreToRDFDocument(final File nativeStoreDirectory,
51 | final File dumpFile,
52 | final RDFFormat format,
53 | final Resource... contexts)
54 | throws SailException, RepositoryException, IOException, RDFHandlerException {
55 |
56 | logger.info("dumping store at " + nativeStoreDirectory + " to file " + dumpFile);
57 |
58 | Sail sail = new NativeStore(nativeStoreDirectory);
59 | sail.initialize();
60 |
61 | try {
62 | Repository repo = new SailRepository(sail);
63 |
64 | try (RepositoryConnection rc = repo.getConnection()) {
65 | try (OutputStream out = new FileOutputStream(dumpFile)) {
66 | RDFHandler h = Rio.createWriter(format, out);
67 | rc.export(h, contexts);
68 | }
69 | }
70 | } finally {
71 | sail.shutDown();
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/sesamize/src/main/java/net/fortytwo/sesametools/sesamize/commands/Import.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.sesamize.commands;
2 |
3 | import net.fortytwo.sesametools.sesamize.SesamizeArgs;
4 | import net.fortytwo.sesametools.sesamize.Command;
5 | import org.eclipse.rdf4j.model.Resource;
6 | import org.eclipse.rdf4j.repository.Repository;
7 | import org.eclipse.rdf4j.repository.RepositoryConnection;
8 | import org.eclipse.rdf4j.repository.RepositoryException;
9 | import org.eclipse.rdf4j.repository.sail.SailRepository;
10 | import org.eclipse.rdf4j.rio.RDFFormat;
11 | import org.eclipse.rdf4j.rio.RDFParseException;
12 | import org.eclipse.rdf4j.sail.Sail;
13 | import org.eclipse.rdf4j.sail.SailException;
14 | import org.eclipse.rdf4j.sail.nativerdf.NativeStore;
15 | import org.slf4j.Logger;
16 | import org.slf4j.LoggerFactory;
17 |
18 | import java.io.File;
19 | import java.io.IOException;
20 |
21 | import static net.fortytwo.sesametools.sesamize.Sesamize.DEFAULT_BASEURI;
22 |
23 | public class Import extends Command {
24 | private final static Logger logger = LoggerFactory.getLogger(Import.class);
25 |
26 | public Import() {
27 | super("import");
28 |
29 | addParameter(new Parameter<>(null, null, true, File.class, null,
30 | "NativeStore directory"));
31 | addParameter(new Parameter<>(null, null, true, File.class, null,
32 | "input location (e.g. 'dump.nt')"));
33 | addParameter(new Parameter<>(
34 | "inputFormat", "i", true, RDFFormat.class, RDFFormat.RDFXML,
35 | "input RDF format (e.g. 'N-Triples')"));
36 | }
37 |
38 | @Override
39 | public void execute(SesamizeArgs args) throws Exception {
40 | File dir = new File(args.nonOptions.get(1));
41 | File file = new File(args.nonOptions.get(2));
42 |
43 | RDFFormat inputFormat = args.getRDFFormat(file, RDFFormat.RDFXML, "i", "inputFormat");
44 |
45 | importRDFDocumentIntoNativeStore(dir, file, inputFormat);
46 | }
47 |
48 | private void importRDFDocumentIntoNativeStore(final File nativeStoreDirectory,
49 | final File dumpFile,
50 | final RDFFormat format,
51 | final Resource... contexts)
52 | throws SailException, RepositoryException, IOException, RDFParseException {
53 |
54 | logger.info("importing file " + dumpFile + " into store at " + nativeStoreDirectory);
55 | Sail sail = new NativeStore(nativeStoreDirectory);
56 | sail.initialize();
57 |
58 | try {
59 | Repository repo = new SailRepository(sail);
60 |
61 | try (RepositoryConnection rc = repo.getConnection()) {
62 | rc.add(dumpFile, DEFAULT_BASEURI, format, contexts);
63 | rc.commit();
64 | }
65 | } finally {
66 | sail.shutDown();
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/sesamize/src/main/java/net/fortytwo/sesametools/sesamize/commands/Random.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.sesamize.commands;
2 |
3 | import net.fortytwo.sesametools.RandomValueFactory;
4 | import net.fortytwo.sesametools.sesamize.SesamizeArgs;
5 | import net.fortytwo.sesametools.sesamize.Command;
6 | import org.eclipse.rdf4j.model.Statement;
7 | import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
8 | import org.eclipse.rdf4j.rio.RDFFormat;
9 | import org.eclipse.rdf4j.rio.RDFWriter;
10 | import org.eclipse.rdf4j.rio.Rio;
11 |
12 | import java.io.File;
13 | import java.io.OutputStream;
14 |
15 | public class Random extends Command {
16 |
17 | public Random() {
18 | super("random");
19 |
20 | addParameter(new Parameter<>(null, null, true, Long.class, null,
21 | "total triples"));
22 | addParameter(new Parameter<>(
23 | "outputFormat", "o", true, RDFFormat.class, RDFFormat.RDFXML,
24 | "output RDF format (e.g. 'N-Triples')"));
25 | }
26 |
27 | @Override
28 | public void execute(SesamizeArgs args) throws Exception {
29 | Long totalTriples = Long.valueOf(args.nonOptions.get(1));
30 | RDFFormat outputFormat = args.getRDFFormat(RDFFormat.RDFXML, "o", "outputFormat");
31 |
32 | OutputStream os = System.out;
33 |
34 | RandomValueFactory rvf = new RandomValueFactory(
35 | SimpleValueFactory.getInstance());
36 |
37 | RDFWriter writer = Rio.createWriter(outputFormat, os);
38 | writer.startRDF();
39 | for (long l = 0L; l < totalTriples; l++) {
40 | Statement st = rvf.randomStatement();
41 | writer.handleStatement(st);
42 | }
43 | writer.endRDF();
44 |
45 | os.close();
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/sesamize/src/main/java/net/fortytwo/sesametools/sesamize/commands/Translate.java:
--------------------------------------------------------------------------------
1 | package net.fortytwo.sesametools.sesamize.commands;
2 |
3 | import net.fortytwo.sesametools.sesamize.SesamizeArgs;
4 | import net.fortytwo.sesametools.sesamize.Command;
5 | import net.fortytwo.sesametools.sesamize.SparqlResultFormat;
6 | import org.eclipse.rdf4j.rio.RDFFormat;
7 | import org.eclipse.rdf4j.rio.RDFHandlerException;
8 | import org.eclipse.rdf4j.rio.RDFParseException;
9 | import org.eclipse.rdf4j.rio.RDFParser;
10 | import org.eclipse.rdf4j.rio.RDFWriter;
11 | import org.eclipse.rdf4j.rio.Rio;
12 | import org.eclipse.rdf4j.sail.SailException;
13 |
14 | import java.io.File;
15 | import java.io.FileInputStream;
16 | import java.io.IOException;
17 | import java.io.InputStream;
18 | import java.io.OutputStream;
19 |
20 | public class Translate extends Command {
21 |
22 | public Translate() {
23 | super("translate");
24 |
25 | addParameter(new Parameter<>(null, null, true, File.class, null,
26 | "input file"));
27 | addParameter(new Parameter<>(
28 | "inputFormat", "i", true, RDFFormat.class, RDFFormat.RDFXML,
29 | "input RDF format (e.g. 'N-Triples')"));
30 | addParameter(new Parameter<>(
31 | "outputFormat", "o", true, SparqlResultFormat.class, SparqlResultFormat.XML,
32 | "output SPARQL format (e.g. 'XML')"));
33 | }
34 |
35 | @Override
36 | public void execute(SesamizeArgs args) throws IOException {
37 | File inputFile = new File(args.nonOptions.get(1));
38 |
39 | RDFFormat inputFormat = args.getRDFFormat(inputFile, RDFFormat.RDFXML, "i", "inputFormat");
40 | RDFFormat outputFormat = args.getRDFFormat(RDFFormat.RDFXML, "o", "outputFormat");
41 |
42 | translateRDFDocument(inputFile, System.out, inputFormat, outputFormat, getBaseURI(args));
43 | }
44 |
45 | private void translateRDFDocument(final File inputFile,
46 | final OutputStream out,
47 | final RDFFormat inFormat,
48 | final RDFFormat outFormat,
49 | final String baseURI)
50 | throws SailException, IOException, RDFHandlerException, RDFParseException {
51 |
52 | try (InputStream in = new FileInputStream(inputFile)) {
53 | translateRDFDocument(in, out, inFormat, outFormat, baseURI);
54 | }
55 | }
56 |
57 | private void translateRDFDocument(final InputStream in,
58 | final OutputStream out,
59 | final RDFFormat inFormat,
60 | final RDFFormat outFormat,
61 | final String baseURI)
62 | throws SailException, IOException, RDFHandlerException, RDFParseException {
63 |
64 | RDFParser p = Rio.createParser(inFormat);
65 | RDFWriter w = Rio.createWriter(outFormat, out);
66 |
67 | p.setRDFHandler(w);
68 |
69 | p.parse(in, baseURI);
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/sesamize/src/main/resources/META-INF/services/org.openrdf.query.parser.QueryParserFactory:
--------------------------------------------------------------------------------
1 | org.openrdf.query.parser.sparql.SPARQLParserFactory
2 |
3 |
--------------------------------------------------------------------------------
/sesamize/src/main/resources/META-INF/services/org.openrdf.rio.RDFParserFactory:
--------------------------------------------------------------------------------
1 | org.openrdf.rio.n3.N3ParserFactory
2 | org.openrdf.rio.nquads.NQuadsParserFactory
3 | org.openrdf.rio.ntriples.NTriplesParserFactory
4 | org.openrdf.rio.rdfjson.RDFJSONParserFactory
5 | org.openrdf.rio.rdfxml.RDFXMLParserFactory
6 | org.openrdf.rio.trig.TriGParserFactory
7 | org.openrdf.rio.trix.TriXParserFactory
8 | org.openrdf.rio.turtle.TurtleParserFactory
9 |
--------------------------------------------------------------------------------
/sesamize/src/main/resources/META-INF/services/org.openrdf.rio.RDFWriterFactory:
--------------------------------------------------------------------------------
1 | org.openrdf.rio.n3.N3WriterFactory
2 | org.openrdf.rio.nquads.NQuadsWriterFactory
3 | org.openrdf.rio.ntriples.NTriplesWriterFactory
4 | org.openrdf.rio.rdfjson.RDFJSONWriterFactory
5 | org.openrdf.rio.rdfxml.RDFXMLWriterFactory
6 | org.openrdf.rio.trig.TriGWriterFactory
7 | org.openrdf.rio.trix.TriXWriterFactory
8 | org.openrdf.rio.turtle.TurtleWriterFactory
9 |
--------------------------------------------------------------------------------
/sesamize/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=DEBUG, A1, A2
2 |
3 | log4j.appender.A1=org.apache.log4j.ConsoleAppender
4 | log4j.appender.A1.layout=org.apache.log4j.PatternLayout
5 | log4j.appender.A1.layout.ConversionPattern=%-5p %c %x - %m%n
6 | log4j.appender.A1.Target = System.err
7 |
8 | log4j.appender.A2=org.apache.log4j.FileAppender
9 | log4j.appender.A2.File=sesamize.log
10 | log4j.appender.A2.layout=org.apache.log4j.PatternLayout
11 | log4j.appender.A2.layout.ConversionPattern=%-5p %c %x - %m%n
12 |
13 | log4j.logger.io.milton=TRACE
--------------------------------------------------------------------------------
/uri-translator/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | .classpath
3 | .project
4 | .settings
5 | .idea
6 | *.iml
7 | *~
8 |
--------------------------------------------------------------------------------
/uri-translator/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 4.0.0
5 | uri-translator
6 | URI Translator
7 | http://fortytwo.net/projects/sesametools
8 |
9 |
10 | net.fortytwo.sesametools
11 | sesametools-all
12 | 2.0-SNAPSHOT
13 | ../pom.xml
14 |
15 |
16 |
17 |
18 | org.eclipse.rdf4j
19 | rdf4j-model
20 |
21 |
22 | org.eclipse.rdf4j
23 | rdf4j-sail-api
24 |
25 |
26 | org.eclipse.rdf4j
27 | rdf4j-util
28 |
29 |
30 | org.eclipse.rdf4j
31 | rdf4j-queryalgebra-evaluation
32 |
33 |
34 | org.slf4j
35 | slf4j-api
36 |
37 |
38 |
39 |
40 | org.eclipse.rdf4j
41 | rdf4j-repository-sail
42 | test
43 |
44 |
45 | org.eclipse.rdf4j
46 | rdf4j-sail-memory
47 | test
48 |
49 |
50 | org.eclipse.rdf4j
51 | rdf4j-queryparser-serql
52 |
53 |
54 |
55 |
56 | org.eclipse.rdf4j
57 | rdf4j-queryparser-sparql
58 | runtime
59 |
60 |
61 | junit
62 | junit
63 | test
64 |
65 |
66 | log4j
67 | log4j
68 | test
69 |
70 |
71 | org.slf4j
72 | slf4j-log4j12
73 | test
74 |
75 |
76 |
77 |
78 |
79 |
80 | org.apache.maven.plugins
81 | maven-compiler-plugin
82 |
83 |
84 | org.apache.maven.plugins
85 | maven-source-plugin
86 |
87 |
88 | org.apache.maven.plugins
89 | maven-jar-plugin
90 |
91 |
92 | org.apache.maven.plugins
93 | maven-javadoc-plugin
94 |
95 |
96 | org.apache.maven.plugins
97 | maven-surefire-plugin
98 |
99 |
100 | org.eluder.coveralls
101 | coveralls-maven-plugin
102 |
103 |
104 | org.jacoco
105 | jacoco-maven-plugin
106 |
107 |
108 |
109 |
110 |
--------------------------------------------------------------------------------
/uri-translator/src/test/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO, R
2 |
3 | log4j.appender.R=org.apache.log4j.ConsoleAppender
4 | log4j.appender.R.layout=org.apache.log4j.PatternLayout
5 | log4j.appender.R.layout.ConversionPattern=%-5p - %d{dd/MM/yyyy HH:mm:ss} - %m%n
6 |
7 | log4j.logger.org.openrdf=INFO
8 | log4j.logger.net.fortytwo.sesametools=INFO
9 |
--------------------------------------------------------------------------------
/writeonly-sail/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | .classpath
3 | .project
4 | .settings
5 | *.iml
6 | *~
7 |
--------------------------------------------------------------------------------
/writeonly-sail/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 4.0.0
5 | writeonly-sail
6 | jar
7 | WriteOnlySail
8 | A write-only Sail implementation
9 |
10 |
11 | net.fortytwo.sesametools
12 | sesametools-all
13 | 2.0-SNAPSHOT
14 | ../pom.xml
15 |
16 |
17 |
18 |
19 | net.fortytwo.sesametools
20 | common
21 |
22 |
23 | org.eclipse.rdf4j
24 | rdf4j-sail-api
25 |
26 |
27 |
28 |
29 | junit
30 | junit
31 | test
32 |
33 |
34 |
35 |
36 |
37 |
38 | org.apache.maven.plugins
39 | maven-compiler-plugin
40 |
41 |
42 | org.apache.maven.plugins
43 | maven-source-plugin
44 |
45 |
46 | org.apache.maven.plugins
47 | maven-jar-plugin
48 |
49 |
50 | org.apache.maven.plugins
51 | maven-javadoc-plugin
52 |
53 |
54 | org.apache.maven.plugins
55 | maven-surefire-plugin
56 |
57 |
58 | org.eluder.coveralls
59 | coveralls-maven-plugin
60 |
61 |
62 | org.jacoco
63 | jacoco-maven-plugin
64 |
65 |
66 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/writeonly-sail/src/main/java/net/fortytwo/sesametools/writeonly/WriteOnlySail.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.writeonly;
3 |
4 | import org.eclipse.rdf4j.model.ValueFactory;
5 | import org.eclipse.rdf4j.rio.RDFHandler;
6 | import org.eclipse.rdf4j.sail.SailConnection;
7 | import org.eclipse.rdf4j.sail.SailException;
8 | import org.eclipse.rdf4j.sail.helpers.AbstractSail;
9 |
10 | /**
11 | * A Sail
which can be written to, but not read from.
12 | * Read operations are simply ignored or return no results.
13 | *
14 | * @author Joshua Shinavier (http://fortytwo.net).
15 | */
16 | public class WriteOnlySail extends AbstractSail {
17 | private RDFHandler handler;
18 | private ValueFactory valueFactory;
19 |
20 | public WriteOnlySail(final RDFHandler handler,
21 | final ValueFactory valueFactory) {
22 | this.handler = handler;
23 | this.valueFactory = valueFactory;
24 | }
25 |
26 | protected SailConnection getConnectionInternal() throws SailException {
27 | return new WriteOnlySailConnection(this, handler, valueFactory);
28 | }
29 |
30 | public ValueFactory getValueFactory() {
31 | return valueFactory;
32 | }
33 |
34 | protected void initializeInternal() throws SailException {
35 | // Does nothing
36 | }
37 |
38 | public boolean isWritable() throws SailException {
39 | return true;
40 | }
41 |
42 | protected void shutDownInternal() throws SailException {
43 | // Does nothing
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/writeonly-sail/src/main/java/net/fortytwo/sesametools/writeonly/WriteOnlySailConnection.java:
--------------------------------------------------------------------------------
1 |
2 | package net.fortytwo.sesametools.writeonly;
3 |
4 | import org.eclipse.rdf4j.common.iteration.CloseableIteration;
5 | import net.fortytwo.sesametools.EmptyCloseableIteration;
6 | import org.eclipse.rdf4j.model.IRI;
7 | import org.eclipse.rdf4j.model.Namespace;
8 | import org.eclipse.rdf4j.model.Resource;
9 | import org.eclipse.rdf4j.model.Statement;
10 | import org.eclipse.rdf4j.model.Value;
11 | import org.eclipse.rdf4j.model.ValueFactory;
12 | import org.eclipse.rdf4j.query.BindingSet;
13 | import org.eclipse.rdf4j.query.Dataset;
14 | import org.eclipse.rdf4j.query.QueryEvaluationException;
15 | import org.eclipse.rdf4j.query.algebra.TupleExpr;
16 | import org.eclipse.rdf4j.rio.RDFHandler;
17 | import org.eclipse.rdf4j.rio.RDFHandlerException;
18 | import org.eclipse.rdf4j.sail.SailException;
19 | import org.eclipse.rdf4j.sail.helpers.AbstractSail;
20 | import org.eclipse.rdf4j.sail.helpers.AbstractSailConnection;
21 |
22 | /**
23 | * @author Joshua Shinavier (http://fortytwo.net).
24 | */
25 | public class WriteOnlySailConnection extends AbstractSailConnection {
26 | private RDFHandler handler;
27 | private ValueFactory valueFactory;
28 |
29 | public WriteOnlySailConnection(final AbstractSail sail,
30 | final RDFHandler handler,
31 | final ValueFactory valueFactory) {
32 | super(sail);
33 | this.handler = handler;
34 | this.valueFactory = valueFactory;
35 | }
36 |
37 | protected void addStatementInternal(
38 | final Resource subj, final IRI pred, final Value obj, final Resource... contexts) throws SailException {
39 |
40 | if (null == contexts || 0 == contexts.length) {
41 | Statement st = valueFactory.createStatement(subj, pred, obj);
42 | try {
43 | handler.handleStatement(st);
44 | } catch (RDFHandlerException e) {
45 | throw new SailException(e);
46 | }
47 | } else {
48 | for (Resource ctx : contexts) {
49 | Statement st = valueFactory.createStatement(subj, pred, obj, ctx);
50 | try {
51 | handler.handleStatement(st);
52 | } catch (RDFHandlerException e) {
53 | throw new SailException(e);
54 | }
55 | }
56 | }
57 | }
58 |
59 | protected void clearInternal(Resource... arg0) throws SailException {
60 | // Does nothing.
61 | }
62 |
63 | protected void clearNamespacesInternal() throws SailException {
64 | // Does nothing.
65 | }
66 |
67 | protected void closeInternal() throws SailException {
68 | }
69 |
70 | protected void commitInternal() throws SailException {
71 | }
72 |
73 | protected CloseableIteration extends BindingSet, QueryEvaluationException> evaluateInternal(
74 | TupleExpr arg0, Dataset arg1, BindingSet arg2, boolean arg3)
75 | throws SailException {
76 | return new EmptyCloseableIteration<>();
77 | }
78 |
79 | protected CloseableIteration extends Resource, SailException> getContextIDsInternal()
80 | throws SailException {
81 | return new EmptyCloseableIteration<>();
82 | }
83 |
84 | protected String getNamespaceInternal(final String prefix) throws SailException {
85 | return null;
86 | }
87 |
88 | protected CloseableIteration extends Namespace, SailException> getNamespacesInternal()
89 | throws SailException {
90 | return new EmptyCloseableIteration<>();
91 | }
92 |
93 | protected CloseableIteration extends Statement, SailException> getStatementsInternal(
94 | Resource arg0, IRI arg1, Value arg2, boolean arg3, Resource... arg4)
95 | throws SailException {
96 | return new EmptyCloseableIteration<>();
97 | }
98 |
99 | protected void removeNamespaceInternal(String arg0) throws SailException {
100 | // Does nothing.
101 | }
102 |
103 | protected void removeStatementsInternal(Resource arg0, IRI arg1, Value arg2,
104 | Resource... arg3) throws SailException {
105 | // Does nothing.
106 | }
107 |
108 | protected void rollbackInternal() throws SailException {
109 | }
110 |
111 | protected void setNamespaceInternal(String arg0, String arg1) throws SailException {
112 | // Does nothing.
113 | }
114 |
115 | protected long sizeInternal(final Resource... contexts) throws SailException {
116 | return 0;
117 | }
118 |
119 | protected void startTransactionInternal() throws SailException {
120 | // Does nothing.
121 | }
122 |
123 | @Override
124 | public boolean pendingRemovals() {
125 | return false;
126 | }
127 | }
128 |
--------------------------------------------------------------------------------