VIVO-823 Create some tests for the VitroModelFactory
Make changes as determined by the tests, to BulkUpdatingOntModel as well. Add debug statements to ModelSynchronizer.
This commit is contained in:
parent
04f763109e
commit
63ed82cef9
5 changed files with 982 additions and 59 deletions
|
@ -3,79 +3,115 @@
|
|||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
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.rdf.model.StmtIterator;
|
||||
import com.hp.hpl.jena.rdf.model.impl.StmtIteratorImpl;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.event.CloseEvent;
|
||||
|
||||
/**
|
||||
* Simple change listener to keep a model (the 'synchronizee') in synch with the model with which it is registered.
|
||||
* Simple change listener to keep a model (the 'synchronizee') in synch with the
|
||||
* model with which it is registered.
|
||||
*
|
||||
* @author bjl23
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class ModelSynchronizer implements ModelChangedListener {
|
||||
private static final Log log = LogFactory.getLog(ModelSynchronizer.class);
|
||||
|
||||
private Model m;
|
||||
|
||||
public ModelSynchronizer (Model synchronizee) {
|
||||
private String hash;
|
||||
|
||||
public ModelSynchronizer(Model synchronizee, String name) {
|
||||
this.m = synchronizee;
|
||||
}
|
||||
|
||||
public void addedStatement(Statement arg0) {
|
||||
m.add(arg0);
|
||||
this.hash = Integer.toHexString(this.hashCode());
|
||||
log.debug(String.format("create: %s, wraps %s(%s) as %s", hash, this.m
|
||||
.getClass().getName(), Integer.toHexString(this.m.hashCode()),
|
||||
name));
|
||||
}
|
||||
|
||||
public void addedStatements(Statement[] arg0) {
|
||||
m.add(arg0);
|
||||
@Override
|
||||
public void addedStatement(Statement s) {
|
||||
log.debug(hash + " addedStatement" + s);
|
||||
m.add(s);
|
||||
}
|
||||
|
||||
|
||||
public void addedStatements(List arg0) {
|
||||
m.add(arg0);
|
||||
@Override
|
||||
public void addedStatements(Statement[] statements) {
|
||||
log.debug(hash + " addedStatements: " + statements.length);
|
||||
m.add(statements);
|
||||
}
|
||||
|
||||
|
||||
public void addedStatements(StmtIterator arg0) {
|
||||
m.add(arg0);
|
||||
@Override
|
||||
public void addedStatements(List<Statement> statements) {
|
||||
log.debug(hash + " addedStatements: " + statements.size());
|
||||
m.add(statements);
|
||||
}
|
||||
|
||||
|
||||
public void addedStatements(Model arg0) {
|
||||
m.add(arg0);
|
||||
@Override
|
||||
public void addedStatements(StmtIterator statements) {
|
||||
if (log.isDebugEnabled()) {
|
||||
Set<Statement> set = statements.toSet();
|
||||
log.debug(hash + " addedStatements: " + set.size());
|
||||
m.add(new StmtIteratorImpl(set.iterator()));
|
||||
} else {
|
||||
m.add(new StmtIteratorImpl(statements));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void notifyEvent(Model arg0, Object arg1) {
|
||||
if ( arg1 instanceof CloseEvent ) {
|
||||
@Override
|
||||
public void addedStatements(Model model) {
|
||||
log.debug(hash + " addedStatements: " + model.size());
|
||||
m.add(model);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyEvent(Model model, Object event) {
|
||||
if (event instanceof CloseEvent) {
|
||||
m.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void removedStatement(Statement arg0) {
|
||||
m.remove(arg0);
|
||||
@Override
|
||||
public void removedStatement(Statement s) {
|
||||
log.debug(hash + " removedStatement" + s);
|
||||
m.remove(s);
|
||||
}
|
||||
|
||||
|
||||
public void removedStatements(Statement[] arg0) {
|
||||
m.remove(arg0);
|
||||
}
|
||||
|
||||
public void removedStatements(List arg0) {
|
||||
m.remove(arg0);
|
||||
@Override
|
||||
public void removedStatements(Statement[] statements) {
|
||||
log.debug(hash + " removedStatements: " + statements.length);
|
||||
m.remove(statements);
|
||||
}
|
||||
|
||||
|
||||
public void removedStatements(StmtIterator arg0) {
|
||||
m.remove(arg0);
|
||||
@Override
|
||||
public void removedStatements(List<Statement> statements) {
|
||||
log.debug(hash + " removedStatements: " + statements.size());
|
||||
m.remove(statements);
|
||||
}
|
||||
|
||||
|
||||
public void removedStatements(Model arg0) {
|
||||
m.remove(arg0);
|
||||
@Override
|
||||
public void removedStatements(StmtIterator statements) {
|
||||
if (log.isDebugEnabled()) {
|
||||
Set<Statement> set = statements.toSet();
|
||||
log.debug(hash + " removedStatements: " + set.size());
|
||||
m.remove(new StmtIteratorImpl(set.iterator()));
|
||||
} else {
|
||||
m.remove(new StmtIteratorImpl(statements));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void removedStatements(Model model) {
|
||||
log.debug(hash + " removedStatements: " + model.size());
|
||||
m.remove(model);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,8 +9,14 @@ import java.net.URL;
|
|||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import com.hp.hpl.jena.graph.BulkUpdateHandler;
|
||||
import com.hp.hpl.jena.graph.Graph;
|
||||
import com.hp.hpl.jena.graph.Triple;
|
||||
import com.hp.hpl.jena.graph.impl.GraphWithPerform;
|
||||
import com.hp.hpl.jena.graph.impl.WrappedBulkUpdateHandler;
|
||||
import com.hp.hpl.jena.ontology.OntModel;
|
||||
import com.hp.hpl.jena.rdf.model.Model;
|
||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||
|
@ -27,13 +33,31 @@ import com.hp.hpl.jena.util.iterator.Map1;
|
|||
* BulkUpdateHandler.
|
||||
*/
|
||||
public class BulkUpdatingOntModel extends AbstractOntModelDecorator {
|
||||
private static final Log log = LogFactory
|
||||
.getLog(BulkUpdatingOntModel.class);
|
||||
|
||||
private static final RDFReaderF readerFactory = new RDFReaderFImpl();
|
||||
|
||||
private final BulkUpdateHandler buh;
|
||||
|
||||
public BulkUpdatingOntModel(OntModel inner, BulkUpdateHandler buh) {
|
||||
public BulkUpdatingOntModel(OntModel inner) {
|
||||
super(inner);
|
||||
this.buh = buh;
|
||||
this.buh = inner.getGraph().getBulkUpdateHandler();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private static BulkUpdateHandler getWrappedBulkUpdateHandler(Graph graph) {
|
||||
if (graph instanceof GraphWithPerform) {
|
||||
return new WrappedBulkUpdateHandler((GraphWithPerform) graph,
|
||||
graph.getBulkUpdateHandler());
|
||||
} else {
|
||||
try {
|
||||
throw new IllegalStateException();
|
||||
} catch (IllegalStateException e) {
|
||||
log.warn("Graph is not an instance of GraphWithPerform", e);
|
||||
}
|
||||
return graph.getBulkUpdateHandler();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
|
|
|
@ -4,16 +4,25 @@ package edu.cornell.mannlib.vitro.webapp.rdfservice.adapters;
|
|||
|
||||
import static com.hp.hpl.jena.ontology.OntModelSpec.OWL_MEM;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import com.hp.hpl.jena.graph.BulkUpdateHandler;
|
||||
import com.hp.hpl.jena.graph.Graph;
|
||||
import com.hp.hpl.jena.graph.compose.Union;
|
||||
import com.hp.hpl.jena.graph.impl.WrappedBulkUpdateHandler;
|
||||
import com.hp.hpl.jena.ontology.OntModel;
|
||||
import com.hp.hpl.jena.ontology.impl.OntModelImpl;
|
||||
import com.hp.hpl.jena.rdf.model.Model;
|
||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||
import com.hp.hpl.jena.rdf.model.impl.ModelCom;
|
||||
|
||||
/**
|
||||
* Make models that will do proper bulk updates.
|
||||
*/
|
||||
public class VitroModelFactory {
|
||||
private static final Log log = LogFactory.getLog(VitroModelFactory.class);
|
||||
|
||||
public static Model createModel() {
|
||||
return ModelFactory.createDefaultModel();
|
||||
}
|
||||
|
@ -21,38 +30,54 @@ public class VitroModelFactory {
|
|||
public static OntModel createOntologyModel() {
|
||||
return ModelFactory.createOntologyModel(OWL_MEM);
|
||||
}
|
||||
|
||||
public static OntModel createOntologyModel(Model model) {
|
||||
@SuppressWarnings("deprecation")
|
||||
BulkUpdateHandler buh = model.getGraph().getBulkUpdateHandler();
|
||||
|
||||
OntModel ontModel = ModelFactory.createOntologyModel(OWL_MEM, model);
|
||||
return new BulkUpdatingOntModel(ontModel, buh);
|
||||
public static OntModel createOntologyModel(Model model) {
|
||||
Graph graph = model.getGraph();
|
||||
Model bareModel = new ModelCom(graph);
|
||||
OntModel ontModel = new OntModelImpl(OWL_MEM, bareModel);
|
||||
return new BulkUpdatingOntModel(ontModel);
|
||||
}
|
||||
|
||||
public static Model createUnion(Model baseModel, Model otherModel) {
|
||||
@SuppressWarnings("deprecation")
|
||||
BulkUpdateHandler buh = baseModel.getGraph().getBulkUpdateHandler();
|
||||
public static Model createUnion(Model baseModel, Model plusModel) {
|
||||
Graph baseGraph = baseModel.getGraph();
|
||||
Graph plusGraph = plusModel.getGraph();
|
||||
BulkUpdatingUnion unionGraph = new BulkUpdatingUnion(baseGraph,
|
||||
plusGraph);
|
||||
|
||||
Model unionModel = ModelFactory.createUnion(baseModel, otherModel);
|
||||
BulkUpdateHandler buh = getBulkUpdateHandler(unionGraph);
|
||||
Model unionModel = ModelFactory.createModelForGraph(unionGraph);
|
||||
return new BulkUpdatingModel(unionModel, buh);
|
||||
}
|
||||
|
||||
public static OntModel createUnion(OntModel baseModel, OntModel otherModel) {
|
||||
@SuppressWarnings("deprecation")
|
||||
BulkUpdateHandler buh = baseModel.getGraph().getBulkUpdateHandler();
|
||||
public static OntModel createUnion(OntModel baseModel, OntModel plusModel) {
|
||||
Graph baseGraph = baseModel.getGraph();
|
||||
Graph plusGraph = plusModel.getGraph();
|
||||
BulkUpdatingUnion unionGraph = new BulkUpdatingUnion(baseGraph,
|
||||
plusGraph);
|
||||
|
||||
Model unionModel = createUnion((Model) baseModel, (Model) otherModel);
|
||||
Model unionModel = ModelFactory.createModelForGraph(unionGraph);
|
||||
OntModel unionOntModel = ModelFactory.createOntologyModel(OWL_MEM,
|
||||
unionModel);
|
||||
return new BulkUpdatingOntModel(unionOntModel, buh);
|
||||
return new BulkUpdatingOntModel(unionOntModel);
|
||||
}
|
||||
|
||||
public static Model createModelForGraph(Graph g) {
|
||||
@SuppressWarnings("deprecation")
|
||||
BulkUpdateHandler buh = g.getBulkUpdateHandler();
|
||||
|
||||
BulkUpdateHandler buh = getBulkUpdateHandler(g);
|
||||
return new BulkUpdatingModel(ModelFactory.createModelForGraph(g), buh);
|
||||
}
|
||||
|
||||
private static class BulkUpdatingUnion extends Union {
|
||||
@SuppressWarnings("deprecation")
|
||||
public BulkUpdatingUnion(Graph L, Graph R) {
|
||||
super(L, R);
|
||||
this.bulkHandler = new WrappedBulkUpdateHandler(this,
|
||||
L.getBulkUpdateHandler());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private static BulkUpdateHandler getBulkUpdateHandler(Graph graph) {
|
||||
return graph.getBulkUpdateHandler();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue