Skip to content

Commit

Permalink
Replaced PlainGraph with GraphMem as a workaround for Jena plain grap…
Browse files Browse the repository at this point in the history
…h serialization bug.
  • Loading branch information
litvinovg authored and chenejac committed Apr 22, 2024
1 parent 4a9dc67 commit 35a2f3b
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -192,7 +192,7 @@ public Model remove(Statement[] statements) {
@Override
public Model remove(List<Statement> statements) {
if (updater != null && statements != null) {
Graph g = GraphFactory.createPlainGraph();
Graph g = GraphFactory.createGraphMem();
for (Statement s : statements) {
g.add(s.asTriple());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -193,7 +193,7 @@ public Model remove(Statement[] statements) {
@Override
public Model remove(List<Statement> statements) {
if (updater != null && statements != null) {
Graph g = GraphFactory.createPlainGraph();
Graph g = GraphFactory.createGraphMem();
for (Statement s : statements) {
g.add(s.asTriple());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}

}

Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 35a2f3b

Please sign in to comment.