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 improvements #699

Merged
merged 5 commits into from
Jan 24, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions rdflib/plugins/serializers/trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def serialize(self, stream, base=None, encoding=None,

firstTime = True
for store, (ordered_subjects, subjects, ref) in self._contexts.items():
if not ordered_subjects: continue

self._references = ref
self._serialized = {}
self.store = store
Expand Down
5 changes: 4 additions & 1 deletion rdflib/plugins/serializers/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def __init__(self, store):
self.reset()

def addNamespace(self, prefix, uri):
if prefix in self.namespaces and self.namespaces[prefix]!=uri:
raise Exception("Trying to override namespace prefix %s => %s, but it's already bound to %s"%(prefix, uri, self.namespaces[prefix]))
self.namespaces[prefix] = uri

def checkSubject(self, subject):
Expand Down Expand Up @@ -195,7 +197,8 @@ def addNamespace(self, prefix, namespace):
p = "p" + p
self._ns_rewrite[prefix] = p

prefix = self._ns_rewrite.get(prefix, prefix)
prefix = self._ns_rewrite.get(prefix, prefix)

super(TurtleSerializer, self).addNamespace(prefix, namespace)
return prefix

Expand Down
97 changes: 96 additions & 1 deletion test/test_trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import rdflib
import re

from nose import SkipTest
from rdflib.py3compat import b

TRIPLE = (rdflib.URIRef("http://example.com/s"),
Expand Down Expand Up @@ -80,5 +81,99 @@ def testBlankGraphIdentifier(self):
g.add(TRIPLE + (rdflib.BNode(),))
out = g.serialize(format='trig')
graph_label_line = out.splitlines()[-4]

self.assertTrue(re.match(br'^_:[a-zA-Z0-9]+ \{', graph_label_line))


def testGraphParsing(self):
# should parse into single default graph context
data = """
<http://example.com/thing#thing_a> <http://example.com/knows> <http://example.com/thing#thing_b> .
"""
g = rdflib.ConjunctiveGraph()
g.parse(data=data, format='trig')
self.assertEqual(len(list(g.contexts())), 1)

# should parse into single default graph context
data = """
<http://example.com/thing#thing_a> <http://example.com/knows> <http://example.com/thing#thing_b> .

{ <http://example.com/thing#thing_c> <http://example.com/knows> <http://example.com/thing#thing_d> . }
"""
g = rdflib.ConjunctiveGraph()
g.parse(data=data, format='trig')
self.assertEqual(len(list(g.contexts())), 1)

# should parse into 2 contexts, one default, one named
data = """
<http://example.com/thing#thing_a> <http://example.com/knows> <http://example.com/thing#thing_b> .

{ <http://example.com/thing#thing_c> <http://example.com/knows> <http://example.com/thing#thing_d> . }

<http://example.com/graph#graph_a> {
<http://example.com/thing/thing_e> <http://example.com/knows> <http://example.com/thing#thing_f> .
}
"""
g = rdflib.ConjunctiveGraph()
g.parse(data=data, format='trig')
self.assertEqual(len(list(g.contexts())), 2)

def testRoundTrips(self):

raise SkipTest('skipped until 5.0')

data = """
<http://example.com/thing#thing_a> <http://example.com/knows> <http://example.com/thing#thing_b> .

{ <http://example.com/thing#thing_c> <http://example.com/knows> <http://example.com/thing#thing_d> . }

<http://example.com/graph#graph_a> {
<http://example.com/thing/thing_e> <http://example.com/knows> <http://example.com/thing#thing_f> .
}
"""
g = rdflib.ConjunctiveGraph()
for i in range(5):
g.parse(data=data, format='trig')
data = g.serialize(format='trig')

# output should only contain 1 mention of each resource/graph name
self.assertEqual(data.count('thing_a'), 1)
self.assertEqual(data.count('thing_b'), 1)
self.assertEqual(data.count('thing_c'), 1)
self.assertEqual(data.count('thing_d'), 1)
self.assertEqual(data.count('thing_e'), 1)
self.assertEqual(data.count('thing_f'), 1)
self.assertEqual(data.count('graph_a'), 1)

def testDefaultGraphSerializesWithoutName(self):
data = """
<http://example.com/thing#thing_a> <http://example.com/knows> <http://example.com/thing#thing_b> .

{ <http://example.com/thing#thing_c> <http://example.com/knows> <http://example.com/thing#thing_d> . }
"""
g = rdflib.ConjunctiveGraph()
g.parse(data=data, format='trig')
data = g.serialize(format='trig')

self.assertTrue(b('None') not in data)

def testPrefixes(self):

data = """
@prefix ns1: <http://ex.org/schema#> .
<http://ex.org/docs/document1> = {
ns1:Person_A a ns1:Person ;
ns1:TextSpan "Simon" .
}
<http://ex.org/docs/document2> = {
ns1:Person_C a ns1:Person ;
ns1:TextSpan "Agnes" .
}
"""

cg = rdflib.ConjunctiveGraph()
cg.parse(data=data, format='trig')
data = cg.serialize(format='trig')

self.assert_(b('ns2: <http://ex.org/docs/') in data, data)
self.assert_(b('<ns2:document1>') not in data, data)
self.assert_(b('ns2:document1') in data, data)