From bd7483a77fab63ed5d2613cf696be8809cecc5c1 Mon Sep 17 00:00:00 2001 From: Fabien Amarger <881739+Murloc6@users.noreply.github.com> Date: Tue, 12 Sep 2023 16:51:50 +0200 Subject: [PATCH 1/2] feat: Add a Graph identifier setter needed for #2591 --- rdflib/graph.py | 8 ++++++++ test/test_graph/test_graph.py | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/rdflib/graph.py b/rdflib/graph.py index b3b2cf290..5e8d265d5 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -463,6 +463,14 @@ def store(self) -> Store: def identifier(self) -> _ContextIdentifierType: return self.__identifier + @identifier.setter + def identifier(self, identifier: IdentifiedNode): + if not isinstance(identifier, IdentifiedNode): + raise TypeError( + "The identifier for a Graph must be an IdentifiedNode" + ) + self.__identifier = identifier + @property def namespace_manager(self) -> NamespaceManager: """ diff --git a/test/test_graph/test_graph.py b/test/test_graph/test_graph.py index 665b4aea0..b5360c940 100644 --- a/test/test_graph/test_graph.py +++ b/test/test_graph/test_graph.py @@ -45,6 +45,17 @@ def test_property_identifier() -> None: assert id == graph.identifier +def test_property_setter_identifier() -> None: + """ + The ``identifier`` property setter works correctly. + """ + id = URIRef("example:a") + new_id = URIRef("example:b") + graph = Graph(identifier=id) + assert id == graph.identifier + graph.identifier = new_id + assert new_id == graph.identifier + def test_property_namespace_manager() -> None: """ The ``namespace_manager`` property works correctly. From cf17385340b0ec7ac5a4a95e0ea60e8d159ef61e Mon Sep 17 00:00:00 2001 From: Fabien Amarger <881739+Murloc6@users.noreply.github.com> Date: Tue, 12 Sep 2023 16:56:46 +0200 Subject: [PATCH 2/2] WIP: Add a add_named_graph method on Dataset TODO: add tests and other needed methods --- rdflib/graph.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rdflib/graph.py b/rdflib/graph.py index 5e8d265d5..6d7b2dc63 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -2530,6 +2530,13 @@ def add_graph( """alias of graph for consistency""" return self.graph(g) + def add_named_graph( + self, named_graph_identifier: IdentifiedNode, g: Optional[Union[_ContextIdentifierType, _ContextType, str]] + ) -> Graph: + new_g = self.graph(g) + new_g.identifier = named_graph_identifier + return new_g + def remove_graph( self: _DatasetT, g: Optional[Union[_ContextIdentifierType, _ContextType, str]] ) -> _DatasetT: