From f167ecf880ea48490c0ecc3e61103e784c42438a Mon Sep 17 00:00:00 2001 From: Drew Perttula Date: Tue, 31 Mar 2015 22:19:30 -0700 Subject: serialize bnode graph ids in trig correctly --- rdflib/plugins/serializers/trig.py | 9 ++++++--- test/test_trig.py | 33 +++++++++++++++++---------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/rdflib/plugins/serializers/trig.py b/rdflib/plugins/serializers/trig.py index b30d10c8..f8bd789d 100644 --- a/rdflib/plugins/serializers/trig.py +++ b/rdflib/plugins/serializers/trig.py @@ -68,9 +68,12 @@ class TrigSerializer(TurtleSerializer): 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 diff --git a/test/test_trig.py b/test/test_trig.py index 742e3281..c985e002 100644 --- a/test/test_trig.py +++ b/test/test_trig.py @@ -4,6 +4,10 @@ import re 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): @@ -49,10 +53,7 @@ class TestTrig(unittest.TestCase): 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') @@ -60,24 +61,24 @@ class TestTrig(unittest.TestCase): self.assertIn(b'@prefix ns1: .', second_out) self.assertIn(b'@prefix ns1: .', 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' {', 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)) + -- cgit v1.2.1