Greenfish
Greenfish

Reputation: 378

Is it possible to create a Sail repository with sparql endpoints?

I have config.ttl file for a local GraphDB SailRepository but I also want to access it not only via direct java invocation but also via SPARQL endpoints. My config.ttl file looks like this:

# RDF4J configuration template for a GraphDB Free repository

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix rep: <http://www.openrdf.org/config/repository#>.
@prefix sr: <http://www.openrdf.org/config/repository/sail#>.
@prefix sail: <http://www.openrdf.org/config/sail#>.
@prefix owlim: <http://www.ontotext.com/trree/owlim#>.
@prefix sparql: <http://www.openrdf.org/config/repository/sparql#>.

[] a rep:Repository ;
    rep:repositoryID "test" ;
    rep:repositoryImpl [
        rep:repositoryType "graphdb:FreeSailRepository" ;
        sparql:query-endpoint <http://localhost:7200/repositories/test> ;
        sparql:update-endpoint <http://localhost:7200/repositories/test/statements> ;
        sr:sailImpl [
            sail:sailType "graphdb:FreeSail" ;
            ...
        ]
    ].

This configuration is a combination of a SPARQL and a Sail repository (https://rdf4j.org/documentation/reference/configuration/ 1.1 and 1.3). While an access via org.eclipse.rdf4j.repository.sail.SailRepository is possible I cannot access my local repository via org.eclipse.rdf4j.repository.sparql.SPARQLRepository

Here is the full code:

private static LocalRepositoryManager repositoryManager;
private static RepositoryConnection embeddedRepoCon;
private static SPARQLProtocolSession session;

@BeforeClass
public static void init() {
    try {
        //Create local repo
        File baseDir = new File("target","GraphDB");
        if (!baseDir.exists())
            baseDir.mkdirs();
        repositoryManager = new LocalRepositoryManager(baseDir);
        repositoryManager.init();
        if(new File("target/GraphDB/repositories/test").exists()) {
            repositoryManager.removeRepository("test");
            System.out.println("Repository removed.");
        }

        //Add repository config to repository manager
        InputStream config = TestRDFStarTimestampingPlugin.class.getResourceAsStream("/repo-defaults.ttl");
        Model repo_config_graph = Rio.parse(config, "", RDFFormat.TURTLE);
        Resource repositoryNode = Models.subject(repo_config_graph.filter(null, RDF.TYPE, RepositoryConfigSchema.REPOSITORY)).orElse(null);
        RepositoryConfig repositoryConfig = RepositoryConfig.create(repo_config_graph, repositoryNode);
        repositoryManager.addRepositoryConfig(repositoryConfig);

        //Initialize repo
        //Repository repo = repositoryManager.getRepository("test");
        SailRepository repo = (SailRepository) repositoryManager.getRepository("test");
        repo.init();

        //Establish connection to repo
        embeddedRepoCon = repo.getConnection();

    } catch (RDFHandlerException | RDFParseException | IOException | RepositoryConfigException | RepositoryException  e) {
        System.err.println("The GraphDB repository will not be created.");
        System.err.println(e.getMessage());
    }

}

Following test passes

   @Test
    public void repoSailConnectionTest() {
        //Test queries
        BooleanQuery query = embeddedRepoCon.prepareBooleanQuery("ask from <http://example.com/testGraph> { ?s ?p ?o }");
        boolean hasResults = query.evaluate();
        assertFalse("No triples should be in the graph yet.", hasResults);
        System.out.println("Result from ask query: " + hasResults);
        System.out.println("Read queries are executable against the embedded repository");

        // Test update statements
        String updateString;
        updateString = "clear graph <http://example.com/testGraph>";
        embeddedRepoCon.prepareUpdate(updateString).execute();
        embeddedRepoCon.commit();

        updateString = "delete data {graph <http://example.com/testGraph> " +
                "{<http://example.com/s/v1> <http://example.com/p/v2> <http://example.com/o/v3>}}";
        embeddedRepoCon.prepareUpdate(updateString).execute();
        embeddedRepoCon.commit();
        System.out.println("Write statements are executable against the embedded repository");

But the access via SPARQLRepository test does not pass because the connection gets refused:

@Test
public void repoSPARQLConnectionTest() {
    //Test queries
    SPARQLRepository repo = new SPARQLRepository("http://localhost:7200/repositories/test");
    repo.init();
    try (RepositoryConnection connection = repo.getConnection()) {
        BooleanQuery query = connection.prepareBooleanQuery("ask from <http://example.com/testGraph> { ?s ?p ?o }");
        boolean hasResults = query.evaluate();
        assertFalse("No triples should be in the graph yet.", hasResults);
        System.out.println("Result from ask query: " + hasResults);
        System.out.println("Read queries are executable against the embedded repository");
    }
    repo.shutDown();

    // Test update statements
    repo = new SPARQLRepository("http://localhost:7200/repositories/test/statements");
    repo.init();
    try (RepositoryConnection connection = repo.getConnection()) {
        String updateString;
        updateString = "clear graph <http://example.com/testGraph>";
        connection.begin();
        connection.prepareUpdate(updateString).execute();
        connection.commit();

        updateString = "delete data {graph <http://example.com/testGraph> " +
                "{<http://example.com/s/v1> <http://example.com/p/v2> <http://example.com/o/v3>}}";
        connection.prepareUpdate(updateString).execute();
        connection.commit();
        System.out.println("Write statements are executable against the embedded repository");
    }
}

Exception:

org.eclipse.rdf4j.query.QueryEvaluationException: Connect to localhost:7200 [localhost/127.0.0.1] failed: Connection refused (Connection refused)

Upvotes: 0

Views: 154

Answers (1)

Greenfish
Greenfish

Reputation: 378

The GraphDB server needs to be running on localhost:7200 first and the SailRepository should be built into GraphDB's local data directory, which is usually on ~/.graphdb/data. The SailRepository is just the "repository part", it does not provide any server. This is the beauty of SailRepositorie's that they can be mounted on any server which implements its specifications.

Upvotes: 0

Related Questions