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:
Georgy Litvinov 2023-06-06 14:07:19 +02:00 committed by GitHub
parent f19d0a08c2
commit a5a7e640d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 34 deletions

View file

@ -10,7 +10,7 @@ public class BulkGraphMem extends GraphMem {
performAdd(t); performAdd(t);
} }
public final void deleteWithoutNotify(Triple t) { public void deleteWithoutNotify(Triple t) {
checkOpen(); checkOpen();
performDelete(t); performDelete(t);
} }

View file

@ -25,10 +25,7 @@ final public class GraphUtils {
public static Graph unwrapUnionGraphs(Graph graph) { public static Graph unwrapUnionGraphs(Graph graph) {
if (graph != null && graph instanceof MultiUnion) { if (graph != null && graph instanceof MultiUnion) {
List<Graph> subGraphs = ((MultiUnion) graph).getSubGraphs(); return unwrapUnionGraphs(((MultiUnion)graph).getBaseGraph());
if (subGraphs == null || subGraphs.isEmpty()) {
return ((MultiUnion) graph).getBaseGraph();
}
} }
return graph; return graph;
} }

View file

@ -13,10 +13,11 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.apache.jena.graph.Triple;
import org.apache.jena.graph.impl.GraphWithPerform; import org.apache.jena.graph.impl.GraphWithPerform;
import org.apache.jena.mem.GraphMem; import org.apache.jena.mem.GraphMem;
import org.apache.jena.ontology.OntModel; 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.listeners.StatementListener;
import org.apache.jena.rdf.model.Literal; import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model; 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.Resource;
import org.apache.jena.rdf.model.ResourceFactory; import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdf.model.Statement; 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.AbstractTestClass;
import edu.cornell.mannlib.vitro.testing.RecordingProxy; 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. * presumably the bulk updaters will be removed completely.
*/ */
public class VitroModelFactoryTest extends AbstractTestClass { 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( private static final Statement SINGLE_STATEMENT = stmt(
resource("http://subject"), property("http://add"), resource("http://subject"), property("http://add"),
literal("object")); literal("object"));
@ -463,6 +469,52 @@ public class VitroModelFactoryTest extends AbstractTestClass {
.test(); .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 // OntModel of Union of Models
// //

View file

@ -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> .