Skip to content

Commit

Permalink
serialize bnode graph ids in trig correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
drewp committed Apr 1, 2015
1 parent aff5b25 commit f167ecf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
9 changes: 6 additions & 3 deletions rdflib/plugins/serializers/trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ def serialize(self, stream, base=None, encoding=None,
if self.default_context and store.identifier==self.default_context:
self.write(self.indent() + '\n{')
else:
iri = self.getQName(store.identifier)
if iri is None:
iri = '<%s>' % store.identifier
if isinstance(store.identifier, BNode):
iri = store.identifier.n3()
else:
iri = self.getQName(store.identifier)
if iri is None:
iri = store.identifier.n3()
self.write(self.indent() + '\n%s {' % iri)

self.depth += 1
Expand Down
33 changes: 17 additions & 16 deletions test/test_trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

from rdflib.py3compat import b

TRIPLE = (rdflib.URIRef("http://example.com/s"),
rdflib.RDFS.label,
rdflib.Literal("example 1"))

class TestTrig(unittest.TestCase):

def testEmpty(self):
Expand Down Expand Up @@ -49,35 +53,32 @@ def testSameSubject(self):

def testRememberNamespace(self):
g = rdflib.ConjunctiveGraph()
g.add((rdflib.URIRef("http://example.com/s"),
rdflib.RDFS.label,
rdflib.Literal("example 1"),
rdflib.URIRef("http://example.com/graph1")))
g.add(TRIPLE + (rdflib.URIRef("http://example.com/graph1"),))
# In 4.2.0 the first serialization would fail to include the
# prefix for the graph but later serialize() calls would work.
first_out = g.serialize(format='trig')
second_out = g.serialize(format='trig')
self.assertIn(b'@prefix ns1: <http://example.com/> .', second_out)
self.assertIn(b'@prefix ns1: <http://example.com/> .', first_out)

print first_out

def testGraphQnameSyntax(self):
g = rdflib.ConjunctiveGraph()
g.add((rdflib.URIRef("http://example.com/s"),
rdflib.RDFS.label,
rdflib.Literal("example 1"),
rdflib.URIRef("http://example.com/graph1")))
g.add(TRIPLE + (rdflib.URIRef("http://example.com/graph1"),))
out = g.serialize(format='trig')
self.assertIn(b'ns1:graph1 {', out)

def testGraphUriSyntax(self):
g = rdflib.ConjunctiveGraph()
g.add((rdflib.URIRef("http://example.com/s"),
rdflib.RDFS.label,
rdflib.Literal("example 1"),
# getQName will not abbreviate this, so it should come
# out as a '<...>' term.
rdflib.URIRef("http://example.com/foo.")))
# getQName will not abbreviate this, so it should serialize as
# a '<...>' term.
g.add(TRIPLE + (rdflib.URIRef("http://example.com/foo."),))
out = g.serialize(format='trig')
self.assertIn(b'<http://example.com/foo.> {', out)

def testBlankGraphIdentifier(self):
g = rdflib.ConjunctiveGraph()
g.add(TRIPLE + (rdflib.BNode(),))
out = g.serialize(format='trig')
graph_label_line = out.splitlines()[-4]
self.assert_(re.match(br'^_:[a-zA-Z0-9]+ \{', graph_label_line))

0 comments on commit f167ecf

Please sign in to comment.