Skip to content

Commit

Permalink
attempt: replace ConjunctiveGraph with Dataset
Browse files Browse the repository at this point in the history
this won't work for now
  • Loading branch information
aMahanna committed Jan 9, 2025
1 parent 674474e commit 0703108
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
63 changes: 31 additions & 32 deletions arango_rdf/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from arango.graph import Graph as ADBGraph
from isodate import Duration
from rdflib import RDF, RDFS, XSD, BNode
from rdflib import ConjunctiveGraph as RDFConjunctiveGraph
from rdflib import Dataset as RDFDataset
from rdflib import Graph as RDFGraph
from rdflib import Literal, URIRef
Expand Down Expand Up @@ -111,10 +110,10 @@ def __init__(
self.__adb_key_statements = RDFGraph()
self.__adb_ns = "http://www.arangodb.com/"

# An RDF Conjunctive Graph representing the
# An RDF Conjunctive Graph (i.e Dataset) representing the
# Ontology files found under the `arango_rdf/meta/` directory.
# Essential for fully contextualizing an RDF Graph in ArangoDB.
self.__meta_graph = RDFConjunctiveGraph()
self.__meta_graph = RDFDataset()
for ns in os.listdir(f"{PROJECT_DIR}/meta"):
self.__meta_graph.parse(f"{PROJECT_DIR}/meta/{ns}", format="trig")

Expand Down Expand Up @@ -237,7 +236,7 @@ def arangodb_to_rdf(
raise ValueError(msg)

self.__rdf_graph = rdf_graph
self.__graph_supports_quads = isinstance(self.__rdf_graph, RDFConjunctiveGraph)
self.__graph_supports_quads = isinstance(self.__rdf_graph, RDFDataset)

self.__graph_ns = f"{self.db._conn._url_prefixes[0]}/{name}"
self.__rdf_graph.bind("adb", self.__adb_ns)
Expand Down Expand Up @@ -589,14 +588,14 @@ def rdf_to_arangodb_by_rpt(
:return: The ArangoDB Graph API wrapper.
:rtype: arango.graph.Graph
"""
if isinstance(rdf_graph, RDFDataset): # pragma: no cover
m = """
Invalid type for **rdf_graph**: ArangoRDF does not yet
support RDF Graphs of type rdflib.graph.Dataset. Consider
using rdflib.graph.ConjunctiveGraph if using quads instead
of triples is required.
"""
raise TypeError(m)
# if isinstance(rdf_graph, RDFDataset): # pragma: no cover
# m = """
# Invalid type for **rdf_graph**: ArangoRDF does not yet
# support RDF Graphs of type rdflib.graph.Dataset. Consider
# using rdflib.graph.ConjunctiveGraph if using quads instead
# of triples is required.
# """
# raise TypeError(m)

self.__rdf_graph = rdf_graph
self.__adb_key_statements = self.extract_adb_key_statements(rdf_graph)
Expand Down Expand Up @@ -668,7 +667,7 @@ def rdf_to_arangodb_by_rpt(

statements = (
self.__rdf_graph.quads
if isinstance(rdf_graph, RDFConjunctiveGraph)
if isinstance(rdf_graph, RDFDataset)
else self.__rdf_graph.triples
)

Expand Down Expand Up @@ -775,14 +774,14 @@ def rdf_to_arangodb_by_pgt(
:return: The ArangoDB Graph API wrapper.
:rtype: arango.graph.Graph
"""
if isinstance(rdf_graph, RDFDataset): # pragma: no cover
m = """
Invalid type for **rdf_graph**: ArangoRDF does not yet
support RDF Graphs of type rdflib.graph.Dataset. Consider
using rdflib.graph.ConjunctiveGraph if using quads instead
of triples is required.
"""
raise TypeError(m)
# if isinstance(rdf_graph, RDFDataset): # pragma: no cover
# m = """
# Invalid type for **rdf_graph**: ArangoRDF does not yet
# support RDF Graphs of type rdflib.graph.Dataset. Consider
# using rdflib.graph.ConjunctiveGraph if using quads instead
# of triples is required.
# """
# raise TypeError(m)

self.__rdf_graph = rdf_graph
self.__adb_key_statements = self.extract_adb_key_statements(rdf_graph)
Expand Down Expand Up @@ -895,7 +894,7 @@ def rdf_to_arangodb_by_pgt(

statements = (
self.__rdf_graph.quads
if isinstance(self.__rdf_graph, RDFConjunctiveGraph)
if isinstance(self.__rdf_graph, RDFDataset)
else self.__rdf_graph.triples
)

Expand Down Expand Up @@ -1876,7 +1875,7 @@ def __pgt_parse_literal_statements(

statements = (
self.__rdf_graph.quads
if isinstance(self.__rdf_graph, RDFConjunctiveGraph)
if isinstance(self.__rdf_graph, RDFDataset)
else self.__rdf_graph.triples
)

Expand Down Expand Up @@ -2545,28 +2544,28 @@ def __pgt_create_adb_graph(self, name: str) -> ADBGraph:
# Private: RDF -> ArangoDB (RPT & PGT) #
########################################

def __load_meta_ontology(self, rdf_graph: RDFGraph) -> RDFConjunctiveGraph:
def __load_meta_ontology(self, rdf_graph: RDFGraph) -> RDFDataset:
"""RDF -> ArangoDB: Load the RDF, RDFS, and OWL
Ontologies into **rdf_graph** as 3 sub-graphs. This method returns
an RDF Graph of type rdflib.graph.ConjunctiveGraph in order to support
an RDF Graph of type rdflib.graph.Dataset in order to support
sub-graph functionality.
Essential for Graph Contextualization.
NOTE: If **rdf_graph** is already of type rdflib.graph.ConjunctiveGraph,
NOTE: If **rdf_graph** is already of type rdflib.graph.Dataset,
then the **same** graph is returned (pass by reference).
:param rdf_graph: The RDF Graph, soon to be converted into an ArangoDB Graph.
:type rdf_graph: rdflib.graph.Graph
:return: A ConjunctiveGraph equivalent of **rdf_graph** containing 3
:return: A Dataset equivalent of **rdf_graph** containing 3
additional subgraphs (RDF, RDFS, OWL)
:rtype: rdflib.graph.ConjunctiveGraph
:rtype: rdflib.graph.Dataset
"""

graph: RDFConjunctiveGraph = (
graph: RDFDataset = (
rdf_graph
if isinstance(rdf_graph, RDFConjunctiveGraph)
else RDFConjunctiveGraph() + rdf_graph
if isinstance(rdf_graph, RDFDataset)
else RDFDataset() + rdf_graph
)

for ns in os.listdir(f"{PROJECT_DIR}/meta"):
Expand Down Expand Up @@ -2596,7 +2595,7 @@ def __flatten_reified_triples(
is disabled.
:type contextualize_statement_func: Callable[..., None]
"""
graph_supports_quads = isinstance(self.__rdf_graph, RDFConjunctiveGraph)
graph_supports_quads = isinstance(self.__rdf_graph, RDFDataset)

# Recursion is used to process nested reified triples
# Things can get really wild here...
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Topic :: Utilities",
"Typing :: Typed",
]
Expand Down
14 changes: 7 additions & 7 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from arango_datasets import Datasets
from rdflib import RDF, RDFS, BNode
from rdflib import ConjunctiveGraph as RDFConjunctiveGraph
from rdflib import Dataset as RDFDataset
from rdflib import Graph as RDFGraph
from rdflib import Literal, URIRef

Expand Down Expand Up @@ -1638,7 +1638,7 @@ def test_rpt_case_15_3(name: str, rdf_graph: RDFGraph) -> None:
def test_rpt_case_15_4(name: str, rdf_graph: RDFGraph) -> None:
# Reified Triple Simplification modifies the source graph,
# so we must make a copy of the graph to test against
rdf_graph_copy = RDFConjunctiveGraph()
rdf_graph_copy = RDFDataset()
for quad in rdf_graph.quads((None, None, None, None)):
rdf_graph_copy.add(quad)

Expand Down Expand Up @@ -1908,7 +1908,7 @@ def test_pgt_case_1(name: str, rdf_graph: RDFGraph) -> None:

rdf_graph_2 = adbrdf.arangodb_graph_to_rdf(
name,
RDFConjunctiveGraph(),
RDFDataset(),
include_adb_v_col_statements=True,
)

Expand Down Expand Up @@ -4112,7 +4112,7 @@ def test_pgt_case_15_3(name: str, rdf_graph: RDFGraph) -> None:
def test_pgt_case_15_4(name: str, rdf_graph: RDFGraph) -> None:
# Reified Triple Simplification modifies the source graph,
# so we must make a copy of the graph to test against
rdf_graph_copy = RDFConjunctiveGraph()
rdf_graph_copy = RDFDataset()
for quad in rdf_graph.quads((None, None, None, None)):
rdf_graph_copy.add(quad)

Expand Down Expand Up @@ -4235,7 +4235,7 @@ def test_pgt_case_15_4(name: str, rdf_graph: RDFGraph) -> None:
assert graph1 in rdf_graph_contexts and graph1 in rdf_graph_2_contexts
assert graph2 in rdf_graph_contexts and graph2 in rdf_graph_2_contexts

rdf_graph_2_copy = RDFConjunctiveGraph()
rdf_graph_2_copy = RDFDataset()
for quad in rdf_graph_2.quads((None, None, None, None)):
rdf_graph_2_copy.add(quad)

Expand Down Expand Up @@ -4275,7 +4275,7 @@ def test_pgt_case_15_4(name: str, rdf_graph: RDFGraph) -> None:
assert len(subtract_graphs(rdf_graph_3, rdf_graph_2)) == 0
assert len(subtract_graphs(rdf_graph_2, rdf_graph_3)) == 0

rdf_graph_3_copy = RDFConjunctiveGraph()
rdf_graph_3_copy = RDFDataset()
for quad in rdf_graph_3.quads((None, None, None, None)):
rdf_graph_3_copy.add(quad)

Expand Down Expand Up @@ -4309,7 +4309,7 @@ def test_pgt_case_15_4(name: str, rdf_graph: RDFGraph) -> None:
"name, rdf_graph",
[("Meta_PGT", get_meta_graph())],
)
def test_pgt_meta(name: str, rdf_graph: RDFConjunctiveGraph) -> None:
def test_pgt_meta(name: str, rdf_graph: RDFDataset) -> None:
META_GRAPH_CONTEXTS = {
"http://www.arangodb.com/",
"http://www.w3.org/2002/07/owl#",
Expand Down

0 comments on commit 0703108

Please sign in to comment.