Skip to content

Commit

Permalink
GH-2749: Second iteration of Namespace imports
Browse files Browse the repository at this point in the history
Signed-off-by: Dzeri96 <dzeri96@proton.me>
  • Loading branch information
Dzeri96 committed Dec 13, 2022
1 parent 103b8ad commit 9b1ab6a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ public void testGetNamespace() throws Exception {
System.err.println("disabled testGetNamespace() as namespace retrieval is not supported by SPARQL");
}

@Override
@Ignore
public void testImportNamespacesFromIterable() throws Exception {
System.err
.println("disabled testImportNamespacesFromIterable() as namespace setting is not supported by SPARQL");
}

@Override
@Ignore
public void testTransactionIsolation() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,9 @@ void add(File file, String baseURI, RDFFormat dataFormat, Resource... contexts)
/**
* Adds the supplied statements to this repository, optionally to one or more named contexts.
*
* @param statements The statements that should be added.
* @param statements The statements that should be added. In case the iterable is
* {@link org.eclipse.rdf4j.model.NamespaceAware} and the target repository supports it, the
* iterable's namespaces are also added to the repository, without overwriting existing ones.
* @param contexts The contexts to add the statements to. Note that this parameter is a vararg and as such is
* optional. If no contexts are specified, each statement is added to any context specified in the
* statement, or if the statement contains no context, it is added without context. If one or more
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.rdf4j.common.iteration.Iteration;
Expand Down Expand Up @@ -370,20 +368,19 @@ public void add(Iterable<? extends Statement> statements, Resource... contexts)
"contexts argument may not be null; either the value should be cast to Resource or an empty array should be supplied");

boolean localTransaction = startLocalTransaction();
Set<Namespace> newNamespaces = new HashSet<>();

try {
for (Statement st : statements) {
if (st instanceof NamespaceAware) {
newNamespaces.addAll(((NamespaceAware) st).getNamespaces());
}
addWithoutCommit(st, contexts);
}

for (Namespace newNamespace : newNamespaces) {
String nsPrefix = newNamespace.getPrefix();
if (getNamespace(nsPrefix) == null) {
setNamespace(nsPrefix, newNamespace.getName());
if (statements instanceof NamespaceAware) {
var newNamespaces = ((NamespaceAware) statements).getNamespaces();
for (Namespace newNamespace : newNamespaces) {
String nsPrefix = newNamespace.getPrefix();
if (getNamespace(nsPrefix) == null) {
setNamespace(nsPrefix, newNamespace.getName());
}
}
}

Expand Down Expand Up @@ -427,15 +424,6 @@ public void add(Statement st, Resource... contexts) throws RepositoryException {

addWithoutCommit(st, contexts);

if (st instanceof NamespaceAware) {
for (Namespace newNamespace : ((NamespaceAware) st).getNamespaces()) {
String nsPrefix = newNamespace.getPrefix();
if (getNamespace(nsPrefix) == null) {
setNamespace(nsPrefix, newNamespace.getName());
}
}
}

conditionalCommit(localTransaction);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,32 @@ public void add(Reader reader, String baseURI, RDFFormat dataFormat, Resource...
}
}

@Override
public void add(Statement st, Resource... contexts) throws RepositoryException {
boolean localTransaction = startLocalTransaction();
addWithoutCommit(st, contexts);
try {
conditionalCommit(localTransaction);
} catch (RepositoryException e) {
conditionalRollback(localTransaction);
throw e;
}
}

@Override
public void add(Iterable<? extends Statement> statements, Resource... contexts) throws RepositoryException {
boolean localTransaction = startLocalTransaction();
for (Statement st : statements) {
addWithoutCommit(st, contexts);
}
try {
conditionalCommit(localTransaction);
} catch (RepositoryException e) {
conditionalRollback(localTransaction);
throw e;
}
}

@Override
public void clear(Resource... contexts) throws RepositoryException {
Objects.requireNonNull(contexts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,22 @@ public void testGetNamespaces() throws Exception {
assertThat(map.get(RDF_PREFIX)).isEqualTo("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
}

@Test
public void testImportNamespacesFromIterable() throws Exception {
Model nsAwareModel = new LinkedHashModel();
nsAwareModel.setNamespace(RDFS_PREFIX, RDFS_NS);
nsAwareModel.setNamespace(EXAMPLE, EXAMPLE_NS);

testCon.add(nsAwareModel);
assertThat(testCon.getNamespace(RDFS_PREFIX)).isEqualTo(RDFS_NS);
assertThat(testCon.getNamespace(EXAMPLE)).isEqualTo(EXAMPLE_NS);

// Test that existing namespaces are not overwritten
nsAwareModel.setNamespace(EXAMPLE, "http://something.else/");
testCon.add(nsAwareModel);
assertThat(testCon.getNamespace(EXAMPLE)).isEqualTo(EXAMPLE_NS);
}

private void setupNamespaces() throws IOException, RDFParseException, RepositoryException {
testCon.setNamespace(EXAMPLE, EXAMPLE_NS);
testCon.setNamespace(RDF_PREFIX, "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
Expand Down

0 comments on commit 9b1ab6a

Please sign in to comment.