Skip to content

Commit

Permalink
feat: Don't generate prefixes for unknown URIs
Browse files Browse the repository at this point in the history
When serializing RDF graphs, URIs with unknown prefixes were assigned a
namespace like `ns1:`. While the result would be smaller files, it does
result in output that is not as readable.

This change removes this automatic assignment of namespace prefixes.

This is somewhat of an aesthetic choice, eventually we should have more
flexibility in this regard so that users can exercise more control over
how URIs in unknown namespaces are handled.

With this change, users can still manually create namespace prefixes for
URIs in unknown namespaces, but before it there was no way to avoid the
undesired behaviour, so this seems like the better default.
  • Loading branch information
mgberg committed Jul 31, 2023
1 parent 2cfe595 commit bd797ac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
6 changes: 4 additions & 2 deletions rdflib/plugins/serializers/trig.py
Expand Up @@ -40,7 +40,8 @@ def preprocess(self) -> None:
if len(context) == 0:
continue
self.store = context
self.getQName(context.identifier)
# Don't generate a new prefix for a graph URI if one already exists
self.getQName(context.identifier, False)
self._subjects = {}

for triple in context:
Expand Down Expand Up @@ -97,7 +98,8 @@ def serialize(
if isinstance(store.identifier, BNode):
iri = store.identifier.n3()
else:
iri = self.getQName(store.identifier)
# Show the full graph URI if a prefix for it doesn't already exist
iri = self.getQName(store.identifier, False)
if iri is None:
# type error: "IdentifiedNode" has no attribute "n3"
iri = store.identifier.n3() # type: ignore[attr-defined]
Expand Down
10 changes: 5 additions & 5 deletions test/test_trig.py
Expand Up @@ -60,15 +60,15 @@ def test_remember_namespace():
# prefix for the graph but later serialize() calls would work.
first_out = g.serialize(format="trig", encoding="latin-1")
second_out = g.serialize(format="trig", encoding="latin-1")
assert b"@prefix ns1: <http://example.com/> ." in second_out
assert b"@prefix ns1: <http://example.com/> ." in first_out
assert b"@prefix ns1: <http://example.com/> ." not in second_out
assert b"@prefix ns1: <http://example.com/> ." not in first_out


def test_graph_qname_syntax():
g = rdflib.ConjunctiveGraph()
g.add(TRIPLE + (rdflib.URIRef("http://example.com/graph1"),))
out = g.serialize(format="trig", encoding="latin-1")
assert b"ns1:graph1 {" in out
assert b"ns1:graph1 {" not in out


def test_graph_uri_syntax():
Expand Down Expand Up @@ -178,9 +178,9 @@ def test_prefixes():
cg.parse(data=data, format="trig")
data = cg.serialize(format="trig", encoding="latin-1")

assert "ns2: <http://ex.org/docs/".encode("latin-1") in data, data
assert "ns2: <http://ex.org/docs/".encode("latin-1") not in data, data
assert "<ns2:document1>".encode("latin-1") not in data, data
assert "ns2:document1".encode("latin-1") in data, data
assert "ns2:document1".encode("latin-1") not in data, data


def test_issue_2154():
Expand Down

0 comments on commit bd797ac

Please sign in to comment.