Skip to content

Commit

Permalink
trig serializer wasn't including a @Prefix line for the first graph's…
Browse files Browse the repository at this point in the history
… prefix
  • Loading branch information
drewp committed Mar 29, 2015
1 parent 395a401 commit 5d9a7b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
17 changes: 1 addition & 16 deletions rdflib/plugins/serializers/trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self, store):
def preprocess(self):
for context in self.contexts:
self.store = context
self.getQName(context.identifier)
self._references = defaultdict(int)
self._subjects = {}

Expand All @@ -40,22 +41,6 @@ def preprocess(self):

self._contexts[context]=(self.orderSubjects(), self._subjects, self._references)


def preprocessTriple(self, triple):
s, p, o = triple
self._references[o]+=1
self._subjects[s] = True
for i, node in enumerate(triple):
if node in self.keywords:
continue
# Don't use generated prefixes for subjects and objects
self.getQName(node, gen_prefix=(i == VERB))
if isinstance(node, Literal) and node.datatype:
self.getQName(node.datatype, gen_prefix=_GEN_QNAME_FOR_DT)
p = triple[1]
if isinstance(p, BNode):
self._references[p]+=1

def reset(self):
super(TrigSerializer, self).reset()
self._contexts = {}
Expand Down
15 changes: 15 additions & 0 deletions test/test_trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,18 @@ def testSameSubject(self):
self.assertEqual(len(re.findall(b("p2"), s)), 1)

self.assert_(b('{}') not in s) # no empty graphs!

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")))
# 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('@prefix ns1: <http://example.com/> .', second_out)
self.assertIn('@prefix ns1: <http://example.com/> .', first_out)

print first_out

0 comments on commit 5d9a7b0

Please sign in to comment.