Search code examples
javasparqlrdfrdf4j

Is it possible to retrieve the update statements of an RDF4J transaction?


I am trying to support "dry run" functionality on SPARQL update queries using RDF4J. I want to inform the user of the statements that will be inserted/deleted, ideally by getting the statements from the current transaction. I'm thinking something like:

conn.begin();
Update query = conn.prepareUpdate(queryString);
query.execute();
// Print statements in the transaction
System.out.println("Dry run completed.");
conn.rollback();
System.out.println("Dry run rolled back.");

Is there a way to do this with RDF4J?


Solution

  • (copied from https://github.com/eclipse/rdf4j/discussions/3163 )

    You can do something along those lines with a SailConnectionListener. The way to access this is a little bit clunky though. Here's an example:

    Repository rep = new SailRepository(new MemoryStore());
    try (SailRepositoryConnection conn = (SailRepositoryConnection) rep.getConnection()) {
        NotifyingSailConnection sailConn = (NotifyingSailConnection) conn.getSailConnection();
        sailConn.addConnectionListener(new SailConnectionListener() {
    
            @Override
            public void statementRemoved(Statement removed) {
                System.out.println("removed: " + removed);
            }
    
            @Override
            public void statementAdded(Statement added) {
                System.out.println("added: " + added);
            }
        });
    
        conn.begin();
        conn.add(FOAF.PERSON, RDF.TYPE, RDFS.CLASS);
        String update = "DELETE { ?p a rdfs:Class } INSERT { ?p rdfs:label \"Person\" } WHERE { ?p a rdfs:Class }";
        conn.prepareUpdate(update).execute();
        System.out.println("executed");
        conn.rollback();
        System.out.println("transaction aborted");
    }
    

    As you can see we need to cast the RepositoryConnection to a specific type to retrieve the underlying SailConnection, and then we further need to cast that SailConnection to a NotifyingSailConnection to be able to register a SailConnectionListener on it. This listener will receive pre-commit added and removed events for individual statements. Running the above code would produce the following console output:

    added: (http://xmlns.com/foaf/0.1/Person, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2000/01/rdf-schema#Class)
    removed: (http://xmlns.com/foaf/0.1/Person, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2000/01/rdf-schema#Class) [null]
    added: (http://xmlns.com/foaf/0.1/Person, http://www.w3.org/2000/01/rdf-schema#label, "Person")
    executed
    transaction aborted