Skip to content

Commit

Permalink
[Bug #202] Ensure removeStatementsBySubjectAndPredicate removes also …
Browse files Browse the repository at this point in the history
…statements added with transaction to LocalModel.
  • Loading branch information
ledsoft committed Sep 20, 2023
1 parent cb85679 commit f88d638
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ void removeStatements(List<Statement> statements, String context) {

void removeStatementsBySubjectAndPredicate(Collection<SubjectPredicateContext> toRemove) {
removedSubjectPredicateStatements.addAll(toRemove);
toRemove.forEach(spc -> {
if (spc.getContexts().isEmpty()) {
added.getDefaultModel().remove(added.getDefaultModel()
.listStatements(spc.getSubject(), spc.getPredicate(), (RDFNode) null));
} else {
spc.getContexts().forEach(c -> added.getNamedModel(c).remove(added.getNamedModel(c)
.listStatements(spc.getSubject(), spc.getPredicate(), (RDFNode) null)));
}
});
}

Dataset getAdded() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package cz.cvut.kbss.ontodriver.jena.connector;

import cz.cvut.kbss.ontodriver.jena.environment.Generator;
import cz.cvut.kbss.ontodriver.util.Vocabulary;
import org.apache.jena.query.Dataset;
import org.apache.jena.rdf.model.Statement;
Expand All @@ -31,6 +32,9 @@
import static cz.cvut.kbss.ontodriver.jena.connector.StorageTestUtil.statement;
import static org.apache.jena.rdf.model.ResourceFactory.createProperty;
import static org.apache.jena.rdf.model.ResourceFactory.createResource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -289,4 +293,26 @@ void enhanceStatementsRemovesStatementsWhoseSubjectPredicateAndContextMatchRemov
final Collection<Statement> result = sut.enhanceStatements(Set.of(statement), createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE), null, Set.of(NAMED_GRAPH));
assertTrue(result.isEmpty());
}

@Test
void removeStatementsBySubjectAndPredicateRemovesPreviouslyAddedStatementsWithMatchingSubjectPredicate() {
final LocalModel sut = new LocalModel(true);
final Statement statement = statement(SUBJECT, Vocabulary.RDF_TYPE, TYPE_TWO);
sut.addStatements(Collections.singletonList(statement), null);
sut.removeStatementsBySubjectAndPredicate(Set.of(new SubjectPredicateContext(statement.getSubject(), statement.getPredicate(), Collections.emptySet())));

assertTrue(sut.getAdded().getDefaultModel().listStatements().toList().isEmpty());
}

@Test
void removeStatementsBySubjectAndPredicateRemovesPreviouslyAddedStatementsWithMatchingSubjectPredicateAndContext() {
final LocalModel sut = new LocalModel(true);
final Statement addedOne = statement(SUBJECT, Vocabulary.RDF_TYPE, TYPE_TWO);
final Statement addedOther = statement(SUBJECT, Generator.generateUri().toString(), Generator.generateUri().toString());
sut.addStatements(List.of(addedOne, addedOther), NAMED_GRAPH);
sut.removeStatementsBySubjectAndPredicate(Set.of(new SubjectPredicateContext(addedOne.getSubject(), addedOne.getPredicate(), Set.of(NAMED_GRAPH))));

assertThat(sut.getAdded().getNamedModel(NAMED_GRAPH).listStatements().toList(), not(hasItem(addedOne)));
assertThat(sut.getAdded().getNamedModel(NAMED_GRAPH).listStatements().toList(), hasItem(addedOther));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void removeStatements(Collection<Statement> statements) {

void removeStatementsBySubjectAndPredicate(Collection<SubjectPredicateContext> toRemove) {
removedSubjectPredicateStatements.addAll(toRemove);
addedStatements.removeIf(s -> toRemove.stream().anyMatch(spc -> spc.matches(s)));
}

Collection<Statement> getAddedStatements() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,16 @@ void enhanceStatementsRemovesStatementsWhoseSubjectPredicateAndContextMatchRemov
final List<Statement> result = sut.enhanceStatements(stream, subject, property, null, Collections.emptySet());
assertTrue(result.stream().noneMatch(s -> s.getSubject().equals(subject) && s.getPredicate().equals(property)));
}

@Test
void removeStatementsBySubjectAndPredicateRemovesPreviouslyAddedStatementsWithMatchingSubjectPredicateAndContext() {
final IRI subject = vf.createIRI(Generator.generateUri().toString());
final IRI property = vf.createIRI(Generator.generateUri().toString());
final IRI context = vf.createIRI(Generator.generateUri().toString());
sut.addStatements(Collections.singletonList(
vf.createStatement(subject, property, vf.createLiteral(117), context)));
sut.removeStatementsBySubjectAndPredicate(Set.of(new SubjectPredicateContext(subject, property, Set.of(context))));

assertTrue(sut.getAddedStatements().isEmpty());
}
}

0 comments on commit f88d638

Please sign in to comment.