Skip to content

Commit

Permalink
Oxigraph stores are now graph aware
Browse files Browse the repository at this point in the history
  • Loading branch information
Tpt committed Jan 7, 2021
1 parent bfe18bd commit ba6b0ef
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 3 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[flake8]
max-line-length = 120
ignore = E203
17 changes: 14 additions & 3 deletions oxrdflib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pyoxigraph as ox
from rdflib import Graph
from rdflib.graph import DATASET_DEFAULT_GRAPH_ID
from rdflib.query import Result
from rdflib.store import VALID_STORE, Store
from rdflib.term import BNode, Literal, Node, URIRef, Variable
Expand All @@ -13,7 +14,7 @@ class _BaseOxStore(Store, ABC):
context_aware = True
formula_aware = False
transaction_aware = False
graph_aware = False
graph_aware = True

@property
@abstractmethod
Expand Down Expand Up @@ -45,8 +46,10 @@ def __len__(self, context=None):
return sum(1 for _ in self._inner.quads_for_pattern(None, None, None, _to_ox(context)))

def contexts(self, triple=None):
iter = self._inner if triple is None else self._inner.quads_for_pattern(*_to_ox_quad_pattern(triple))
return (_from_ox(q[3]) for q in iter)
if triple is None:
return (_from_ox(g) for g in self._inner.named_graphs())
else:
return (_from_ox(q[3]) for q in self._inner.quads_for_pattern(*_to_ox_quad_pattern(triple)))

def query(self, query, initNs, initBindings, queryGraph, **kwargs):
if initNs:
Expand Down Expand Up @@ -88,6 +91,12 @@ def rollback(self):
# TODO: implement
pass

def add_graph(self, graph):
self._inner.add_graph(_to_ox(graph))

def remove_graph(self, graph):
self._inner.remove_graph(_to_ox(graph))


class MemoryOxStore(_BaseOxStore):
def __init__(self, configuration=None, identifier=None):
Expand Down Expand Up @@ -124,6 +133,8 @@ def _inner(self):
def _to_ox(term, context=None):
if term is None:
return None
elif term == DATASET_DEFAULT_GRAPH_ID:
return ox.DefaultGraph()
elif isinstance(term, URIRef):
return ox.NamedNode(term)
elif isinstance(term, BNode):
Expand Down
145 changes: 145 additions & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
"""
Dataset test.
Code from https://github.com/RDFLib/rdflib/blob/91037207580838e41c07eb457bd65d7cc6d6ed85/test/test_dataset.py
Copyright (c) 2002-2017, RDFLib Team
See CONTRIBUTORS and urn:github.com/RDFLib/rdflib
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Daniel Krech nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

import unittest

from rdflib import Dataset, URIRef
from rdflib.graph import DATASET_DEFAULT_GRAPH_ID


class DatasetTestCase(unittest.TestCase):
def setUp(self):
self.graph = Dataset(store="OxMemory")
self.michel = URIRef("urn:michel")
self.tarek = URIRef("urn:tarek")
self.bob = URIRef("urn:bob")
self.likes = URIRef("urn:likes")
self.hates = URIRef("urn:hates")
self.pizza = URIRef("urn:pizza")
self.cheese = URIRef("urn:cheese")

# Use regular URIs because SPARQL endpoints like Fuseki alter short names
self.c1 = URIRef("urn:context-1")
self.c2 = URIRef("urn:context-2")

# delete the graph for each test!
self.graph.remove((None, None, None))
for c in self.graph.contexts():
c.remove((None, None, None))
self.assertEqual(len(c), 0)
self.graph.remove_graph(c)

def tearDown(self):
self.graph.close()

def testGraphAware(self):

if not self.graph.store.graph_aware:
return

g = self.graph
g1 = g.graph(self.c1)

# added graph exists
self.assertEqual(
set(x.identifier for x in self.graph.contexts()),
{self.c1, DATASET_DEFAULT_GRAPH_ID},
)

# added graph is empty
self.assertEqual(len(g1), 0)

g1.add((self.tarek, self.likes, self.pizza))

# added graph still exists
self.assertEqual(
set(x.identifier for x in self.graph.contexts()),
{self.c1, DATASET_DEFAULT_GRAPH_ID},
)

# added graph contains one triple
self.assertEqual(len(g1), 1)

g1.remove((self.tarek, self.likes, self.pizza))

# added graph is empty
self.assertEqual(len(g1), 0)

# graph still exists, although empty
self.assertEqual(
set(x.identifier for x in self.graph.contexts()),
{self.c1, DATASET_DEFAULT_GRAPH_ID},
)

g.remove_graph(self.c1)

# graph is gone
self.assertEqual(
set(x.identifier for x in self.graph.contexts()),
{DATASET_DEFAULT_GRAPH_ID},
)

def testDefaultGraph(self):
self.graph.add((self.tarek, self.likes, self.pizza))
self.assertEqual(len(self.graph), 1)
# only default exists
self.assertEqual(
set(x.identifier for x in self.graph.contexts()),
{DATASET_DEFAULT_GRAPH_ID},
)

# removing default graph removes triples but not actual graph
self.graph.remove_graph(DATASET_DEFAULT_GRAPH_ID)

self.assertEqual(len(self.graph), 0)
# default still exists
self.assertEqual(
set(x.identifier for x in self.graph.contexts()),
{DATASET_DEFAULT_GRAPH_ID},
)

def testNotUnion(self):
g1 = self.graph.graph(self.c1)
g1.add((self.tarek, self.likes, self.pizza))

self.assertEqual(list(self.graph.objects(self.tarek, None)), [])
self.assertEqual(list(g1.objects(self.tarek, None)), [self.pizza])


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

0 comments on commit ba6b0ef

Please sign in to comment.