Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions arango_rdf/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def arangodb_to_rdf(
include_adb_v_key_statements: bool = False,
include_adb_e_key_statements: bool = False,
namespace_collection_name: Optional[str] = None,
ignored_attributes: Optional[Set[str]] = None,
**adb_export_kwargs: Any,
) -> RDFGraph:
"""Create an RDF Graph from an ArangoDB Graph via its Metagraph.
Expand Down Expand Up @@ -228,13 +229,22 @@ def arangodb_to_rdf(
the original RDF Graph from the ArangoDB Graph. Defaults to None,
which means that the namespace prefixes will not be stored.
:type namespace_collection_name: str | None
:param ignored_attributes: The set of ArangoDB Document attributes to ignore
when transferring ArangoDB Documents into RDF. Defaults to None,
which means that all attributes will be transferred. Cannot be used
if **explicit_metagraph** is True.
:type ignored_attributes: Set[str] | None
:param adb_export_kwargs: Keyword arguments to specify AQL query options when
fetching documents from the ArangoDB instance. Full parameter list:
https://docs.python-arango.com/en/main/specs.html#arango.aql.AQL.execute
:type adb_export_kwargs: Any
:return: The RDF representation of the ArangoDB Graph.
:rtype: rdflib.graph.Graph
"""
if explicit_metagraph and ignored_attributes:
msg = "**ignored_attributes** cannot be used if **explicit_metagraph** is True" # noqa: E501
raise ValueError(msg)

list_conversion_modes = {"serialize", "collection", "container", "static"}
if list_conversion_mode not in list_conversion_modes:
msg = f"Invalid **list_conversion_mode** parameter: {list_conversion_mode}"
Expand Down Expand Up @@ -320,7 +330,12 @@ def arangodb_to_rdf(

# 1. Fetch ArangoDB vertices
v_col_cursor, v_col_size = self.__fetch_adb_docs(
v_col, False, atribs, explicit_metagraph, **adb_export_kwargs
v_col,
False,
atribs,
explicit_metagraph,
ignored_attributes,
**adb_export_kwargs,
)

# 2. Process ArangoDB vertices
Expand All @@ -346,7 +361,12 @@ def arangodb_to_rdf(

# 1. Fetch ArangoDB edges
e_col_cursor, e_col_size = self.__fetch_adb_docs(
e_col, True, atribs, explicit_metagraph, **adb_export_kwargs
e_col,
True,
atribs,
explicit_metagraph,
ignored_attributes,
**adb_export_kwargs,
)

# 2. Process ArangoDB edges
Expand Down Expand Up @@ -375,6 +395,7 @@ def arangodb_collections_to_rdf(
include_adb_v_key_statements: bool = False,
include_adb_e_key_statements: bool = False,
namespace_collection_name: Optional[str] = None,
ignored_attributes: Optional[Set[str]] = None,
**adb_export_kwargs: Any,
) -> RDFGraph:
"""Create an RDF Graph from an ArangoDB Graph via its Collection Names.
Expand Down Expand Up @@ -432,6 +453,10 @@ def arangodb_collections_to_rdf(
the original RDF Graph from the ArangoDB Graph. Defaults to None,
which means that the namespace prefixes will not be stored.
:type namespace_collection_name: str | None
:param ignored_attributes: The set of ArangoDB Document attributes to ignore
when transferring ArangoDB Documents into RDF. Defaults to None,
which means that all attributes will be transferred.
:type ignored_attributes: Set[str] | None
:param adb_export_kwargs: Keyword arguments to specify AQL query options when
fetching documents from the ArangoDB instance. Full parameter list:
https://docs.python-arango.com/en/main/specs.html#arango.aql.AQL.execute
Expand All @@ -458,6 +483,7 @@ def arangodb_collections_to_rdf(
include_adb_v_key_statements,
include_adb_e_key_statements,
namespace_collection_name,
ignored_attributes,
**adb_export_kwargs,
)

Expand All @@ -472,6 +498,7 @@ def arangodb_graph_to_rdf(
include_adb_v_key_statements: bool = False,
include_adb_e_key_statements: bool = False,
namespace_collection_name: Optional[str] = None,
ignored_attributes: Optional[Set[str]] = None,
**adb_export_kwargs: Any,
) -> RDFGraph:
"""Create an RDF Graph from an ArangoDB Graph via its Graph Name.
Expand Down Expand Up @@ -520,6 +547,15 @@ def arangodb_graph_to_rdf(
NOTE: Enabling this option will impose Triple Reification on all
ArangoDB Edges.
:type include_adb_e_key_statements: bool
:param namespace_collection_name: The name of the ArangoDB Collection
to store the namespace prefixes of **rdf_graph**. Useful for re-constructing
the original RDF Graph from the ArangoDB Graph. Defaults to None,
which means that the namespace prefixes will not be stored.
:type namespace_collection_name: str | None
:param ignored_attributes: The set of ArangoDB Document attributes to ignore
when transferring ArangoDB Documents into RDF. Defaults to None,
which means that all attributes will be transferred.
:type ignored_attributes: Set[str] | None
:param adb_export_kwargs: Keyword arguments to specify AQL query options when
fetching documents from the ArangoDB instance. Full parameter list:
https://docs.python-arango.com/en/main/specs.html#arango.aql.AQL.execute
Expand All @@ -543,6 +579,7 @@ def arangodb_graph_to_rdf(
include_adb_v_key_statements,
include_adb_e_key_statements,
namespace_collection_name,
ignored_attributes,
**adb_export_kwargs,
)

Expand Down Expand Up @@ -1421,6 +1458,7 @@ def __fetch_adb_docs(
is_edge: bool,
attributes: Set[str],
explicit_metagraph: bool,
ignored_attributes: Optional[Set[str]],
**adb_export_kwargs: Any,
) -> Tuple[Cursor, int]:
"""ArangoDB -> RDF: Fetches ArangoDB documents within a collection.
Expand All @@ -1435,18 +1473,31 @@ def __fetch_adb_docs(
specified when fetching the documents of the collection **col**.
If False, all document attributes are included.
:type explicit_metagraph: bool
:param ignored_attributes: The set of ArangoDB Document attributes to ignore
when transferring ArangoDB Documents into RDF. Defaults to None,
which means that all attributes will be transferred. Cannot be used
if **explicit_metagraph** is True.
:type ignored_attributes: Set[str] | None
:param adb_export_kwargs: Keyword arguments to specify AQL query options when
fetching documents from the ArangoDB instance.
:type adb_export_kwargs: Any
:return: The document cursor along with the total collection size.
:rtype: Tuple[arango.cursor.Cursor, int]
"""
if explicit_metagraph and ignored_attributes:
msg = "**ignored_attributes** cannot be used if **explicit_metagraph** is True" # noqa: E501
raise ValueError(msg)

aql_return_value = "doc"

if explicit_metagraph:
default_keys = ["_id", "_key"]
default_keys += ["_from", "_to"] if is_edge else []
aql_return_value = f"KEEP(doc, {list(attributes) + default_keys})"

if ignored_attributes:
aql_return_value = f"UNSET(doc, {list(ignored_attributes)})"

col_size: int = self.__db.collection(col).count()

with get_spinner_progress(f"(ADB → RDF): Export '{col}' ({col_size})") as sp:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,8 @@ def test_pgt_case_2_1(name: str, rdf_graph: RDFGraph) -> None:
name, type(rdf_graph)(), include_adb_v_col_statements=True
)

assert (None, URIRef("http://example.com/name"), None) in rdf_graph_2

adb_col_statements_1 = adbrdf.write_adb_col_statements(rdf_graph)
adb_col_statements_2 = adbrdf.extract_adb_col_statements(rdf_graph_2)
assert len(subtract_graphs(adb_col_statements_1, adb_col_statements_2)) == 0
Expand All @@ -1991,6 +1993,14 @@ def test_pgt_case_2_1(name: str, rdf_graph: RDFGraph) -> None:
assert len(subtract_graphs(rdf_graph, rdf_graph_2)) == 0
assert len(subtract_graphs(rdf_graph_2, rdf_graph)) == 0

rdf_graph_3 = adbrdf.arangodb_graph_to_rdf(
name,
type(rdf_graph)(),
ignored_attributes={"name"},
)

assert (None, URIRef("http://example.com/name"), None) not in rdf_graph_3

db.delete_graph(name, drop_collections=True)


Expand Down Expand Up @@ -4616,6 +4626,20 @@ def test_adb_doc_with_dict_property(name: str) -> None:
metagraph={"vertexCollections": {"TestDoc": {"foo"}}},
)

with pytest.raises(ValueError) as e:
adbrdf.arangodb_to_rdf(
name,
RDFGraph(),
dict_conversion_mode="static",
metagraph={"vertexCollections": {"TestDoc": {"foo"}}},
ignored_attributes={"foo"},
)

assert (
"**ignored_attributes** cannot be used if **explicit_metagraph** is True"
in str(e.value)
)

assert len(rdf_graph_2) == 1
assert (
test_doc,
Expand Down