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

N3 parser: do not create formulas if the Turtle mode is activated #1142

Merged
merged 3 commits into from
Sep 17, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions rdflib/plugins/parsers/notation3.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def __init__(
else:
self._genPrefix = uniqueURI()

if openFormula is None:
if openFormula is None and not turtle:
if self._thisDoc:
self._formula = store.newFormula(thisDoc + "#_formula")
else:
Expand Down Expand Up @@ -1726,6 +1726,7 @@ def close(self):
class RDFSink(object):
def __init__(self, graph):
self.rootFormula = None
self.uuid = uuid4().hex
self.counter = 0
self.graph = graph

Expand All @@ -1745,7 +1746,7 @@ def newBlankNode(self, arg=None, uri=None, why=None):
return arg.newBlankNode(uri)
elif isinstance(arg, Graph) or arg is None:
self.counter += 1
bn = BNode("n" + str(self.counter))
bn = BNode("n%sb%s" % (self.uuid, self.counter))
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm going to have to look into this a little deeper. I don't know if adding a UUID to the RDFSink is the best way to approach this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

UUID usage is what is currently done internally by the formula blank node generator

else:
bn = BNode(str(arg[0]).split("#").pop().replace("_", "b"))
return bn
Expand Down
27 changes: 27 additions & 0 deletions test/test_issue1141.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
from io import BytesIO

from rdflib import Graph
from rdflib.plugins.memory import IOMemory
from rdflib.plugins.stores.auditable import AuditableStore


class TestIssue1141(unittest.TestCase):
"""
Tests is Turtle and TriG parsing works with a store with or without formula support
"""
def test_issue_1141(self):
file = b"@prefix : <http://example.com/> . :s :p :o ."

for format in ("turtle", "trig"):
# with formula
graph = Graph()
self.assertTrue(graph.store.formula_aware)
graph.load(BytesIO(file), format=format)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just FYI, in RDFLib 5.1.0 the graph.load() method will be deprecated as part of the Graph API cleanup process, and in 6.0.0 load() will be removed.
You will need to use graph.parse() instead. In this case where file is a string with the RDF data, use graph.parse(data=file).

Copy link
Contributor Author

@Tpt Tpt Aug 17, 2020

Choose a reason for hiding this comment

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

Ok! Thank you! I'll fix it if the global PR design is accepted

self.assertEqual(len(graph), 1)

# without
graph = Graph(store=AuditableStore(IOMemory()))
self.assertFalse(graph.store.formula_aware)
graph.load(BytesIO(file), format=format)
self.assertEqual(len(graph), 1)