Hi Stu,
Things seem rather half-baked round here. The code below is a way to
get the right stack of functionality.
The problem is that a normal in-memory dataset is a collection of graphs
and if you ask for a particular graph, it isn't a view of the dataset,
it is the underlying storage, no dataset on the code path any more. So
Graph.add(Triple) does nto go via DatasetGraph.add(Quad) and so does not
trigger a dataset-level event.
This is different to TDB where the storage is always a DatasetGraph and
specific graphs are views of the underlying dataset. Graph.add(Triple)
is then somethign that asdds the quad field, and calls
DatasetGraph.add(Quad), trigging the dataset-level event.
The code on the gist below works round that by adding in a "graph view"
layer.
https://gist.github.com/afs/f77fdafb10608899fd1a
(also below for the archives but that will get mangled)
To make things complicated, a graph can be in more than one dataset (I'm
not sure this is a good idea but it is possible at the moment).
A thing to consider (Jena3) is make all storage DatasetGraph-based, and
special case an isolated Graph. Graphs in multiple dataset would still
work.
Andy
/*
* Copyright 2014 Andy Seaborne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev ;
import org.apache.jena.atlas.logging.LogCtl ;
import org.apache.jena.riot.RDFDataMgr ;
import com.hp.hpl.jena.graph.Node ;
import com.hp.hpl.jena.query.Dataset ;
import com.hp.hpl.jena.query.DatasetFactory ;
import com.hp.hpl.jena.rdf.listeners.StatementListener ;
import com.hp.hpl.jena.rdf.model.Model ;
import com.hp.hpl.jena.rdf.model.ModelChangedListener ;
import com.hp.hpl.jena.rdf.model.Statement ;
import com.hp.hpl.jena.sparql.core.* ;
import com.hp.hpl.jena.sparql.util.FmtUtils ;
public class DevEvents {
static { LogCtl.setLog4j() ; }
public static void main(String... argv) throws Exception {
Dataset ds = DatasetFactory.createMem() ;
//ds = TDBFactory.createDataset() ;
DatasetGraph dsg = ds.asDatasetGraph() ;
dsg = new DatasetGraphMonitor(dsg, new ChangeLoggerDSG()) ;
//***************************
dsg = new DatasetGraphViewGraphs(dsg) ;
ds = DatasetFactory.create(dsg) ;
System.out.println("Model events") ;
Model m = ds.getDefaultModel() ;
m.register(new ChangeLoggerStmt()) ;
RDFDataMgr.read(m, "/home/afs/tmp/D.ttl") ;
m.removeAll() ;
System.out.println("No model events") ;
Model m2 = ds.getDefaultModel() ;
RDFDataMgr.read(m2, "/home/afs/tmp/D.ttl") ;
m2.removeAll() ;
//ds.getDefaultModel().read("/home/afs/tmp/D.ttl") ;
System.out.println("DONE") ;
}
public static class ChangeLoggerStmt extends StatementListener
implements ModelChangedListener {
@Override
public void addedStatement( Statement s ) {
System.out.println("ADD-stmt: "+s) ;
}
@Override
public void removedStatement( Statement s ) {
System.out.println("DEL-stmt: "+s) ;
}
}
public static class ChangeLoggerDSG implements DatasetChanges
{
@Override public void start() {}
@Override public void finish() {}
@Override
public void change(QuadAction qaction, Node g, Node s, Node p,
Node o) {
System.out.printf("%-12s %s (%s %s %s)\n",qaction+"-dsg",
FmtUtils.stringForNode(g),
FmtUtils.stringForNode(s),
FmtUtils.stringForNode(p),
FmtUtils.stringForNode(o)) ;
}
}
}