Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-2749 Include Namespaces in Statement imports from NamespaceAware collections #4262

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -22,6 +22,8 @@
import org.eclipse.rdf4j.common.iteration.Iterations;
import org.eclipse.rdf4j.common.transaction.IsolationLevel;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Namespace;
import org.eclipse.rdf4j.model.NamespaceAware;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
Expand Down Expand Up @@ -366,10 +368,22 @@ 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();

try {
for (Statement st : statements) {
addWithoutCommit(st, contexts);
}

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());
}
}
}
Comment on lines +377 to +385
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!


conditionalCommit(localTransaction);
} catch (RuntimeException e) {
conditionalRollback(localTransaction);
Expand Down Expand Up @@ -407,6 +421,7 @@ public void add(Statement st, Resource... contexts) throws RepositoryException {

Objects.requireNonNull(contexts,
"contexts argument may not be null; either the value should be cast to Resource or an empty array should be supplied");

addWithoutCommit(st, contexts);

conditionalCommit(localTransaction);
Expand Down Expand Up @@ -490,6 +505,10 @@ public void clear(Resource... contexts) throws RepositoryException {
remove(null, null, null, contexts);
}

public abstract String getNamespace(String prefix) throws RepositoryException;

public abstract void setNamespace(String prefix, String name) throws RepositoryException;

protected void addWithoutCommit(Statement st, Resource... contexts) throws RepositoryException {
if (contexts.length == 0 && st.getContext() != null) {
contexts = new Resource[] { st.getContext() };
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