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

trig output fixes #480

Merged
merged 6 commits into from
Apr 1, 2015
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 5 additions & 17 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 Expand Up @@ -83,7 +68,10 @@ def serialize(self, stream, base=None, encoding=None,
if self.default_context and store.identifier==self.default_context:
self.write(self.indent() + '\n{')
else:
self.write(self.indent() + '\n<%s> {' % self.getQName(store.identifier))
iri = self.getQName(store.identifier)
if iri is None:
iri = '<%s>' % store.identifier
self.write(self.indent() + '\n%s {' % iri)
Copy link
Member

Choose a reason for hiding this comment

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

two questions here:

  • should it be possible that store.identifier isn't in getQName at all?
  • wouldn't this introduce a bug if iri is not None cause it's not wrapped in <%s> anymore?
    (so shouldn't line 73 be: iri = '%s' % store.identifier and 74: self.write(self.indent() + '\n<%s> {' % iri) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

getQName gets to return None if it has no abbreviation, which I test with the URIRef("http://example.com/foo.") case.

As I understand it, a non-None iri (e.g. a successful abbreviation like "ns1:foo") shouldn't be wrapped in <>. That seemed to be a bug before. See Example 1 in http://www.w3.org/TR/trig/#sec-triple-statements for a demo of no angle brackets.

I pushed an update to get bnode graph ids right too (like "_:a1b2c3 { ... }").


self.depth += 1
for subject in ordered_subjects:
Expand Down
35 changes: 35 additions & 0 deletions test/test_trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,38 @@ 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

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")))
out = g.serialize(format='trig')
self.assertIn('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.")))
out = g.serialize(format='trig')
self.assertIn('<http://example.com/foo.> {', out)