Fix for VIVO-3871 (#393)
* Fallback to default graph in bulk ontology model update * test added --------- Co-authored-by: Georgy Litvinov <georgy.litvinov@tib.eu>
This commit is contained in:
parent
f19d0a08c2
commit
a5a7e640d2
5 changed files with 88 additions and 34 deletions
|
@ -10,7 +10,7 @@ public class BulkGraphMem extends GraphMem {
|
|||
performAdd(t);
|
||||
}
|
||||
|
||||
public final void deleteWithoutNotify(Triple t) {
|
||||
public void deleteWithoutNotify(Triple t) {
|
||||
checkOpen();
|
||||
performDelete(t);
|
||||
}
|
||||
|
|
|
@ -7,33 +7,33 @@ import org.apache.jena.rdf.model.Model;
|
|||
|
||||
public class BulkOntModelImpl extends OntModelImpl {
|
||||
|
||||
public BulkOntModelImpl(OntModelSpec spec) {
|
||||
super(spec);
|
||||
}
|
||||
|
||||
public BulkOntModelImpl(OntModelSpec owlMem, Model bareModel) {
|
||||
super(owlMem, bareModel);
|
||||
}
|
||||
public BulkOntModelImpl(OntModelSpec spec) {
|
||||
super(spec);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Model remove(Model m) {
|
||||
Graph unwrappedGraph = GraphUtils.unwrapUnionGraphs(graph);
|
||||
if (unwrappedGraph instanceof BulkGraphMem) {
|
||||
GraphUtils.deleteFrom((BulkGraphMem) unwrappedGraph, m.getGraph());
|
||||
} else {
|
||||
super.remove(m);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public BulkOntModelImpl(OntModelSpec owlMem, Model bareModel) {
|
||||
super(owlMem, bareModel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Model add(Model m) {
|
||||
Graph unwrappedGraph = GraphUtils.unwrapUnionGraphs(graph);
|
||||
if (unwrappedGraph instanceof BulkGraphMem) {
|
||||
GraphUtils.addInto((BulkGraphMem) unwrappedGraph, m.getGraph());
|
||||
} else {
|
||||
super.add(m);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Model remove(Model m) {
|
||||
Graph unwrappedGraph = GraphUtils.unwrapUnionGraphs(graph);
|
||||
if (unwrappedGraph instanceof BulkGraphMem) {
|
||||
GraphUtils.deleteFrom((BulkGraphMem) unwrappedGraph, m.getGraph());
|
||||
} else {
|
||||
super.remove(m);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Model add(Model m) {
|
||||
Graph unwrappedGraph = GraphUtils.unwrapUnionGraphs(graph);
|
||||
if (unwrappedGraph instanceof BulkGraphMem) {
|
||||
GraphUtils.addInto((BulkGraphMem) unwrappedGraph, m.getGraph());
|
||||
} else {
|
||||
super.add(m);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,10 +25,7 @@ final public class GraphUtils {
|
|||
|
||||
public static Graph unwrapUnionGraphs(Graph graph) {
|
||||
if (graph != null && graph instanceof MultiUnion) {
|
||||
List<Graph> subGraphs = ((MultiUnion) graph).getSubGraphs();
|
||||
if (subGraphs == null || subGraphs.isEmpty()) {
|
||||
return ((MultiUnion) graph).getBaseGraph();
|
||||
}
|
||||
return unwrapUnionGraphs(((MultiUnion)graph).getBaseGraph());
|
||||
}
|
||||
return graph;
|
||||
}
|
||||
|
|
|
@ -13,10 +13,11 @@ import java.util.Arrays;
|
|||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.apache.jena.graph.Triple;
|
||||
import org.apache.jena.graph.impl.GraphWithPerform;
|
||||
import org.apache.jena.mem.GraphMem;
|
||||
import org.apache.jena.ontology.OntModel;
|
||||
import org.apache.jena.ontology.impl.OntModelImpl;
|
||||
import org.apache.jena.rdf.listeners.StatementListener;
|
||||
import org.apache.jena.rdf.model.Literal;
|
||||
import org.apache.jena.rdf.model.Model;
|
||||
|
@ -27,6 +28,8 @@ import org.apache.jena.rdf.model.RDFNode;
|
|||
import org.apache.jena.rdf.model.Resource;
|
||||
import org.apache.jena.rdf.model.ResourceFactory;
|
||||
import org.apache.jena.rdf.model.Statement;
|
||||
import org.apache.jena.rdf.model.impl.ModelCom;
|
||||
import org.apache.jena.shared.Lock;
|
||||
|
||||
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||
import edu.cornell.mannlib.vitro.testing.RecordingProxy;
|
||||
|
@ -65,6 +68,9 @@ import edu.cornell.mannlib.vitro.testing.RecordingProxy.MethodCallRecorder;
|
|||
* presumably the bulk updaters will be removed completely.
|
||||
*/
|
||||
public class VitroModelFactoryTest extends AbstractTestClass {
|
||||
|
||||
private String testData = "src/test/resources/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/vitro_model_factory_test_data.n3";
|
||||
|
||||
private static final Statement SINGLE_STATEMENT = stmt(
|
||||
resource("http://subject"), property("http://add"),
|
||||
literal("object"));
|
||||
|
@ -463,7 +469,53 @@ public class VitroModelFactoryTest extends AbstractTestClass {
|
|||
.test();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
@Test
|
||||
public void testCreateNewBulkOntModel() {
|
||||
TestBulkGraphMem spyGraph = new TestBulkGraphMem();
|
||||
Model baseModel = new ModelCom(spyGraph);
|
||||
baseModel = wrap(wrap(wrap(baseModel)));
|
||||
OntModel ontModel = VitroModelFactory.createOntologyModel(baseModel);
|
||||
Model inputModel = ModelFactory.createDefaultModel();
|
||||
try {
|
||||
inputModel.enterCriticalSection(Lock.WRITE);
|
||||
inputModel.read(testData);
|
||||
} finally {
|
||||
inputModel.leaveCriticalSection();
|
||||
}
|
||||
assertEquals(0, spyGraph.countAddWithoutNotify);
|
||||
assertEquals(0, spyGraph.countDeleteWithoutNotify);
|
||||
ontModel.add(inputModel);
|
||||
assertEquals(5, spyGraph.countAddWithoutNotify);
|
||||
assertEquals(0, spyGraph.countDeleteWithoutNotify);
|
||||
|
||||
ontModel.remove(inputModel);
|
||||
assertEquals(5, spyGraph.countAddWithoutNotify);
|
||||
assertEquals(5, spyGraph.countDeleteWithoutNotify);
|
||||
}
|
||||
|
||||
private OntModelImpl wrap(Model baseModel) {
|
||||
return new OntModelImpl(OWL_MEM, baseModel);
|
||||
}
|
||||
|
||||
private static class TestBulkGraphMem extends BulkGraphMem {
|
||||
int countDeleteWithoutNotify = 0;
|
||||
int countAddWithoutNotify = 0;
|
||||
|
||||
@Override
|
||||
public void addWithoutNotify(Triple t) {
|
||||
checkOpen();
|
||||
performAdd(t);
|
||||
countAddWithoutNotify++;
|
||||
}
|
||||
@Override
|
||||
public final void deleteWithoutNotify(Triple t) {
|
||||
checkOpen();
|
||||
performDelete(t);
|
||||
countDeleteWithoutNotify++;
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// OntModel of Union of Models
|
||||
//
|
||||
// This shouldn't hold any surprises, should it?
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
<test:bob> <test:has_publication> <test:bob_publication1> .
|
||||
<test:bob> <test:has_publication> <test:bob_publication2> .
|
||||
<test:bob> <test:has_publication> <test:bob_publication3> .
|
||||
<test:alice> <test:has_publication> <test:alice_publication1> .
|
||||
<test:alice> <test:has_publication> <test:alice_publication2> .
|
Loading…
Add table
Reference in a new issue