Skip to content

Commit

Permalink
Fix for VIVO-3871 (#393)
Browse files Browse the repository at this point in the history
* Fallback to default graph in bulk ontology model update

* test added

---------

Co-authored-by: Georgy Litvinov <georgy.litvinov@tib.eu>
  • Loading branch information
litvinovg and litvinovg committed Jun 6, 2023
1 parent f19d0a0 commit a5a7e64
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void addWithoutNotify(Triple t) {
performAdd(t);
}

public final void deleteWithoutNotify(Triple t) {
public void deleteWithoutNotify(Triple t) {
checkOpen();
performDelete(t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
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;
Expand All @@ -27,6 +28,8 @@
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;
Expand Down Expand Up @@ -65,6 +68,9 @@
* 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"));
Expand Down Expand Up @@ -463,7 +469,53 @@ public void addMultipleToVitroOntUnion() {
.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?
Expand Down
Original file line number Diff line number Diff line change
@@ -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> .

0 comments on commit a5a7e64

Please sign in to comment.