Skip to content

Commit

Permalink
added support for nquads
Browse files Browse the repository at this point in the history
  • Loading branch information
nikokaoja committed Jul 29, 2024
1 parent 1bdfeeb commit 54e7d44
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
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
5 changes: 5 additions & 0 deletions oxrdflib/_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,22 @@ 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}")


def ox_to_rdflib_type(ox_format: str) -> str:
"""Convert an Oxigraph format to a rdflib parser format."""
print(ox_format)
if ox_format in ("ox-turtle", "ox-ttl"):
return "turtle"
if ox_format in ("ox-nt", "ox-ntriples"):
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}")
13 changes: 7 additions & 6 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

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 @@ -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> .
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)

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()

0 comments on commit 54e7d44

Please sign in to comment.