user3687501
user3687501

Reputation: 73

Trying to add a listener to a model (backed by a TDB2 dataset)

After a little research, org.apache.jena.sparql.core.DatasetGraphMonitor looked the way to go. To my understanding I have to crate a DatasetGraph wrapped by the DatasetGraphMonitor, use this graph to create a Model and all the modifications to the model are now notified to my DatasetChanges object. So that's what I'm doing:

//create a Dataset backed by TBD2
Dataset dataset = TDB2Factory.connectDataset(location);

//wrap the dataset with a DatasetGraphMonitor and obtain a DatasetGraph
DatasetGraph datasetGraph = new DatasetGraphMonitor(dataset.asDatasetGraph(), new DatasetChanges() {
  @Override
  public void start() {
  }
  @Override
  public void reset() {
  }
  @Override
  public void finish() {
  }
  @Override
  public void change(QuadAction qaction, Node g, Node s, Node p, Node o) {
    LOG.info("Dataset change: "+qaction);
  }
});

//create a model using the DatasetGraphMonitor as underlying graph
Model model = ModelFactory.createModelForGraph(datasetGraph.getDefaultGraph());

//run an insert sparql query to add new triples to the triplestore (this really is in a write transaction, maybe I'm oversimplifying here)
UpdateAction.parseExecute(sparqlQuery, model);

well, you guessed that already: change never gets called. Any idea about what I'm doing wrong here? Thanks.

Upvotes: 0

Views: 99

Answers (1)

AndyS
AndyS

Reputation: 16630

DatasetGraphMonitor is for monitoring actions on the dataset. Getting the default graph, making it a model, doesn't trigger that machinery. (If it did, you'd get a "not in transaction" exception). The returns graph does straight to the core database.

Instead, either:

  1. Wrap the graph from datasetGraph.getDefaultGraph() with GraphWrapper and put the monitoring code on the various add/delete methods.
  2. Do the update (in a transaction) on the datasetGraph.

Upvotes: 1

Related Questions