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

Add support for parsing N-Quads #47

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## unreleased

### Added
- Oxigraph native parsers for N-Triples, Turtle and RDF/XML.
- Oxigraph native parsers for N-Triples, Turtle, RDF/XML and N-Quads.

### Improved
- Restructured a code based to account for further additions to the codebase.
Expand Down
4 changes: 4 additions & 0 deletions oxrdflib/_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ def rdflib_to_mime_type(rdflib_type: str) -> str:
return "application/rdf+xml"
if rdflib_type == "trig":
return "application/trig"
if rdflib_type == "nquads":
return "application/n-quads"
if rdflib_type == "trix":
return "application/trix"
raise ValueError(f"Unsupported rdflib type: {rdflib_type}")
Expand All @@ -135,4 +137,6 @@ def ox_to_rdflib_type(ox_format: str) -> str:
return "nt"
if ox_format == "ox-xml":
return "xml"
if ox_format in ("ox-nquads", "ox-nq"):
return "nquads"
raise ValueError(f"Unsupported Oxigraph type: {ox_format}")
15 changes: 8 additions & 7 deletions oxrdflib/parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import warnings
from typing import Any, Optional

from rdflib import ConjunctiveGraph, Graph
from rdflib import Graph
from rdflib.exceptions import ParserError
from rdflib.parser import (
FileInputSource,
Expand Down Expand Up @@ -47,6 +47,7 @@ def parse(

else:
base_iri = sink.absolutize(source.getPublicId() or source.getSystemId() or "")
graph_identifier = to_ox(sink.identifier) if format not in ("ox-nq", "ox-nquads") else None
Copy link
Contributor

Choose a reason for hiding this comment

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

We should map the default graph for dataset formats too.

Suggested change
graph_identifier = to_ox(sink.identifier) if format not in ("ox-nq", "ox-nquads") else None
graph_identifier = to_ox(sink.identifier)


if isinstance(source, FileInputSource):
input = source.file
Expand All @@ -60,14 +61,14 @@ def parse(
input,
rdflib_to_mime_type(ox_to_rdflib_type(format)),
base_iri=base_iri,
to_graph=to_ox(sink.identifier),
to_graph=graph_identifier,
)
else:
sink.store._inner.bulk_load(
input,
rdflib_to_mime_type(ox_to_rdflib_type(format)),
base_iri=base_iri,
to_graph=to_ox(sink.identifier),
to_graph=graph_identifier,
)


Expand Down Expand Up @@ -98,7 +99,7 @@ def parse(
class OxigraphRdfXmlParser(OxigraphParser):
def parse(
self,
source: FileInputSource,
source: InputSource,
sink: Graph,
format: str = "ox-xml",
encoding: Optional[str] = None,
Expand All @@ -111,12 +112,12 @@ class OxigraphNQuadsParser(OxigraphParser):
def parse(
self,
source: InputSource,
sink: ConjunctiveGraph,
format: str,
sink: Graph,
format: str = "ox-nquads",
encoding: Optional[str] = None,
**kwargs: Any,
) -> None:
raise NotImplementedError("N-Quads is not supported yet")
super().parse(source, sink, format, encoding, **kwargs)


class OxigraphTriGParser(OxigraphParser):
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ ox-ttl = "oxrdflib.parser:OxigraphTurtleParser"
ox-ntriples = "oxrdflib.parser:OxigraphNTriplesParser"
ox-nt = "oxrdflib.parser:OxigraphNTriplesParser"
ox-xml = "oxrdflib.parser:OxigraphRdfXmlParser"
ox-nquads = "oxrdflib.parser:OxigraphNQuadsParser"
ox-nq = "oxrdflib.parser:OxigraphNQuadsParser"


[project.urls]
Expand Down
6 changes: 6 additions & 0 deletions tests/data/test.nq
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<http://example.org/stuff/1.0/document> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Document> <http://example.com/graph1> .
<http://example.org/stuff/1.0/document> <http://xmlns.com/foaf/0.1/title> "Example Document" <http://example.com/graph1> .
<http://example.org/stuff/1.0/document> <http://xmlns.com/foaf/0.1/creator> <http://example.org/stuff/1.0/creator> <http://example.com/graph1> .
<http://example.org/stuff/1.0/creator> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> <http://example.com/graph1> .
<http://example.org/stuff/1.0/creator> <http://xmlns.com/foaf/0.1/name> "John Doe" <http://example.com/graph1> .
<http://example.org/stuff/1.0/creator> <http://xmlns.com/foaf/0.1/mbox> <mailto:[email protected]> <http://example.com/graph1> .
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be great to have a few quads in the default graph to check that they are properly loaded into the default graph

18 changes: 18 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

_TEST_DIR = Path(__file__).resolve().parent

_NAMEDGRAPH_QUERY = """SELECT DISTINCT ?g WHERE {
GRAPH ?g {
?s ?p ?o .
}
}"""


class TestGraphParsing(unittest.TestCase):
def test_parsing_ox_turtle_bulk_load(self):
Expand Down Expand Up @@ -127,6 +133,18 @@ def test_parsing_ox_rdfxml_fallback(self):
)
self.assertEqual(len(graph), 6)

def test_parsing_ox_nquads_bulk_load(self):
graph = rdflib.Dataset(store="Oxigraph")
graph.parse(_TEST_DIR / "data/test.nq", format="ox-nquads", transactional=False)
self.assertEqual(len(graph), 6)
self.assertEqual(len(graph.query(_NAMEDGRAPH_QUERY)), 1)
nikokaoja marked this conversation as resolved.
Show resolved Hide resolved

def test_parsing_ox_nquads_load(self):
graph = rdflib.Dataset(store="Oxigraph")
graph.parse(_TEST_DIR / "data/test.nq", format="ox-nquads", transactional=True)
self.assertEqual(len(graph), 6)
self.assertEqual(len(graph.query(_NAMEDGRAPH_QUERY)), 1)


if __name__ == "__main__":
unittest.main()