From 35a2f3bce5775ad993e5072d815e5b7e3e5ccb14 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Wed, 17 Apr 2024 09:41:22 +0200 Subject: [PATCH] Replaced PlainGraph with GraphMem as a workaround for Jena plain graph serialization bug. --- .../adapters/BulkUpdatingModel.java | 10 +-- .../adapters/BulkUpdatingOntModel.java | 10 +-- .../AbstractBulkUpdatingModelTest.java | 55 +++++++++++++++ .../adapters/BulkUpdatingModelTest.java | 64 +++++++++++++++++ .../adapters/BulkUpdatingOntModelTest.java | 70 +++++++++++++++++++ 5 files changed, 199 insertions(+), 10 deletions(-) create mode 100644 api/src/test/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/AbstractBulkUpdatingModelTest.java create mode 100644 api/src/test/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingModelTest.java create mode 100644 api/src/test/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingOntModelTest.java diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingModel.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingModel.java index 2657c6c660..ee1986e9d3 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingModel.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingModel.java @@ -51,7 +51,7 @@ public BulkUpdatingModel(Model m) { @Override public Model add(StmtIterator iter) { if (updater != null && iter != null) { - Graph g = GraphFactory.createPlainGraph(); + Graph g = GraphFactory.createGraphMem(); while (iter.hasNext()) { g.add(iter.nextStatement().asTriple()); } @@ -75,7 +75,7 @@ public Model add(Model m) { @Override public Model add(Statement[] statements) { if (updater != null && statements != null) { - Graph g = GraphFactory.createPlainGraph(); + Graph g = GraphFactory.createGraphMem(); for (Statement s : statements) { g.add(s.asTriple()); } @@ -154,7 +154,7 @@ public Model read(InputStream reader, String base, String lang) { @Override public Model remove(StmtIterator iter) { if (updater != null && iter != null) { - Graph g = GraphFactory.createPlainGraph(); + Graph g = GraphFactory.createGraphMem(); while (iter.hasNext()) { g.add(iter.nextStatement().asTriple()); } @@ -178,7 +178,7 @@ public Model remove(Model m) { @Override public Model remove(Statement[] statements) { if (updater != null && statements != null) { - Graph g = GraphFactory.createPlainGraph(); + Graph g = GraphFactory.createGraphMem(); for (Statement s : statements) { g.add(s.asTriple()); } @@ -192,7 +192,7 @@ public Model remove(Statement[] statements) { @Override public Model remove(List statements) { if (updater != null && statements != null) { - Graph g = GraphFactory.createPlainGraph(); + Graph g = GraphFactory.createGraphMem(); for (Statement s : statements) { g.add(s.asTriple()); } diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingOntModel.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingOntModel.java index 44a5582b87..6f0fd3dd90 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingOntModel.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingOntModel.java @@ -52,7 +52,7 @@ protected BulkUpdatingOntModel(OntModel m) { @Override public Model add(StmtIterator iter) { if (updater != null && iter != null) { - Graph g = GraphFactory.createPlainGraph(); + Graph g = GraphFactory.createGraphMem(); while (iter.hasNext()) { g.add(iter.nextStatement().asTriple()); } @@ -76,7 +76,7 @@ public Model add(Model m) { @Override public Model add(Statement[] statements) { if (updater != null && statements != null) { - Graph g = GraphFactory.createPlainGraph(); + Graph g = GraphFactory.createGraphMem(); for (Statement s : statements) { g.add(s.asTriple()); } @@ -155,7 +155,7 @@ public Model read(InputStream reader, String base, String lang) { @Override public Model remove(StmtIterator iter) { if (updater != null && iter != null) { - Graph g = GraphFactory.createPlainGraph(); + Graph g = GraphFactory.createGraphMem(); while (iter.hasNext()) { g.add(iter.nextStatement().asTriple()); } @@ -179,7 +179,7 @@ public Model remove(Model m) { @Override public Model remove(Statement[] statements) { if (updater != null && statements != null) { - Graph g = GraphFactory.createPlainGraph(); + Graph g = GraphFactory.createGraphMem(); for (Statement s : statements) { g.add(s.asTriple()); } @@ -193,7 +193,7 @@ public Model remove(Statement[] statements) { @Override public Model remove(List statements) { if (updater != null && statements != null) { - Graph g = GraphFactory.createPlainGraph(); + Graph g = GraphFactory.createGraphMem(); for (Statement s : statements) { g.add(s.asTriple()); } diff --git a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/AbstractBulkUpdatingModelTest.java b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/AbstractBulkUpdatingModelTest.java new file mode 100644 index 0000000000..3366b99030 --- /dev/null +++ b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/AbstractBulkUpdatingModelTest.java @@ -0,0 +1,55 @@ +package edu.cornell.mannlib.vitro.webapp.rdfservice.adapters; + +import org.apache.jena.graph.Graph; +import org.apache.jena.rdf.model.Literal; +import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.ModelFactory; +import org.apache.jena.rdf.model.ResourceFactory; +import org.apache.jena.rdf.model.impl.PropertyImpl; +import org.apache.jena.rdf.model.impl.ResourceImpl; +import org.apache.jena.rdf.model.impl.StatementImpl; + +public class AbstractBulkUpdatingModelTest { + + public AbstractBulkUpdatingModelTest() { + super(); + } + + protected StatementImpl getStatement() { + ResourceImpl subject = new ResourceImpl("test:uri1"); + PropertyImpl property = new PropertyImpl("http://www.w3.org/2000/01/rdf-schema#label"); + Literal object = ResourceFactory.createLangLiteral("Persons", "en-US"); + StatementImpl statement = new StatementImpl(subject, property, object); + return statement; + } + + static class WrappedUpdater extends AbstractBulkUpdater { + + private AbstractBulkUpdater wrappedUpdater; + + public void add(Graph g) { + performAddModel(ModelFactory.createModelForGraph(g)); + } + + public WrappedUpdater(AbstractBulkUpdater updater) { + this.wrappedUpdater = updater; + } + + @Override + protected void performAddModel(Model model) { + wrappedUpdater.performAddModel(model); + } + + @Override + protected void performRemoveModel(Model model) { + wrappedUpdater.performRemoveModel(model); + } + + @Override + protected void performRemoveAll() { + wrappedUpdater.performRemoveAll(); + } + } + +} + diff --git a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingModelTest.java b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingModelTest.java new file mode 100644 index 0000000000..74475814d7 --- /dev/null +++ b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingModelTest.java @@ -0,0 +1,64 @@ +package edu.cornell.mannlib.vitro.webapp.rdfservice.adapters; + +import java.util.Arrays; + +import edu.cornell.mannlib.vitro.webapp.dao.jena.RDFServiceGraph; +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; +import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.jena.model.RDFServiceModel; +import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.ModelFactory; +import org.apache.jena.rdf.model.Statement; +import org.junit.Test; + +public class BulkUpdatingModelTest extends AbstractBulkUpdatingModelTest { + + @Test + public void testRemoveWithIterator() { + BulkUpdatingModel wrappedModel = getWrappedModel(); + Model removeModel = VitroModelFactory.createModel(); + removeModel.add(getStatement()); + wrappedModel.remove(removeModel.listStatements()); + } + + @Test + public void testRemoveStatementArray() { + BulkUpdatingModel wrapModel = getWrappedModel(); + wrapModel.remove(new Statement[] { getStatement() }); + } + + @Test + public void testRemoveStatementList() { + BulkUpdatingModel wrapModel = getWrappedModel(); + wrapModel.remove(Arrays.asList(getStatement())); + } + + @Test + public void testAddWithIterator() { + BulkUpdatingModel wrappedModel = getWrappedModel(); + Model model = VitroModelFactory.createModel(); + model.add(getStatement()); + wrappedModel.add(model.listStatements()); + } + + @Test + public void testAddStatementArray() { + BulkUpdatingModel wrapModel = getWrappedModel(); + wrapModel.add(new Statement[] { getStatement() }); + } + + @Test + public void testAddStatementList() { + BulkUpdatingModel wrapModel = getWrappedModel(); + wrapModel.add(Arrays.asList(getStatement())); + } + + private BulkUpdatingModel getWrappedModel() { + Model m = ModelFactory.createDefaultModel(); + RDFService rdfService = new RDFServiceModel(m); + RDFServiceGraph g = new RDFServiceGraph(rdfService); + BulkUpdatingModel wrappedModel = new BulkUpdatingModel(RDFServiceGraph.createRDFServiceModel(g)); + wrappedModel.updater = new WrappedUpdater(wrappedModel.updater); + return wrappedModel; + } + +} diff --git a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingOntModelTest.java b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingOntModelTest.java new file mode 100644 index 0000000000..0e90f43f23 --- /dev/null +++ b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/rdfservice/adapters/BulkUpdatingOntModelTest.java @@ -0,0 +1,70 @@ +package edu.cornell.mannlib.vitro.webapp.rdfservice.adapters; + +import static org.apache.jena.ontology.OntModelSpec.OWL_MEM; + +import java.util.Arrays; + +import edu.cornell.mannlib.vitro.webapp.dao.jena.RDFServiceGraph; +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; +import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.jena.model.RDFServiceModel; +import org.apache.jena.ontology.OntModel; +import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.ModelFactory; +import org.apache.jena.rdf.model.Statement; +import org.junit.Test; + +public class BulkUpdatingOntModelTest extends AbstractBulkUpdatingModelTest { + + @Test + public void testRemoveWithIterator() { + OntModel wrappedModel = getWrappedModel(); + Model removeModel = VitroModelFactory.createModel(); + removeModel.add(getStatement()); + wrappedModel.remove(removeModel.listStatements()); + } + + @Test + public void testRemoveStatementArray() { + OntModel wrapModel = getWrappedModel(); + wrapModel.remove(new Statement[] { getStatement() }); + } + + @Test + public void testRemoveStatementList() { + OntModel wrapModel = getWrappedModel(); + wrapModel.remove(Arrays.asList(getStatement())); + } + + @Test + public void testAddWithIterator() { + OntModel wrappedModel = getWrappedModel(); + Model model = VitroModelFactory.createModel(); + model.add(getStatement()); + wrappedModel.add(model.listStatements()); + } + + @Test + public void testAddStatementArray() { + OntModel wrapModel = getWrappedModel(); + wrapModel.add(new Statement[] { getStatement() }); + } + + @Test + public void testAddStatementList() { + OntModel wrapModel = getWrappedModel(); + wrapModel.add(Arrays.asList(getStatement())); + } + + private OntModel getWrappedModel() { + Model m = ModelFactory.createOntologyModel(); + RDFService rdfService = new RDFServiceModel(m); + RDFServiceGraph g = new RDFServiceGraph(rdfService); + + Model bareModel = new BulkModelCom(g); + OntModel ontModel = new BulkOntModelImpl(OWL_MEM, bareModel); + BulkUpdatingOntModel wrappedModel = new BulkUpdatingOntModel(ontModel); + wrappedModel.updater = new WrappedUpdater(wrappedModel.updater); + return wrappedModel; + } + +}