1

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?

B. Smith
  • 1,063
  • 4
  • 14
  • 23
  • not sure what exactly you mean. What are the "statements in the transaction"? I mean,, you have to SPARQL Update command, indeed you could print this. But if you mean how the RDF graph `g1` really has changed to the new graph `g2`, this is obviously computing the *symmetric difference* of both graphs (modulo blank nodes which would need graph isomorphism stuff). Or you get lucky and there is some kind of change listener on graph level – UninformedUser Jul 09 '21 at 03:25
  • 1
    Oddly, the exact same question was asked on the RDF4J Discusion forum recently, and it received an answer there: https://github.com/eclipse/rdf4j/discussions/3163 – Jeen Broekstra Jul 10 '21 at 00:44

1 Answers1

1

(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
Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73