From 138e4f245975eab3b07360e07ddcc513a6fe9c33 Mon Sep 17 00:00:00 2001 From: Matt Goldberg Date: Mon, 27 Mar 2023 13:12:55 -0400 Subject: [PATCH] Add optional graph argument to Graph.cbd, use this new argument in evalDescribeQuery --- rdflib/graph.py | 10 +++++++--- rdflib/plugins/sparql/evaluate.py | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/rdflib/graph.py b/rdflib/graph.py index 4a96e6d37c..0a0dc48783 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -1816,7 +1816,7 @@ def do_de_skolemize2(t: _TripleType) -> _TripleType: return retval - def cbd(self, resource: _SubjectType) -> Graph: + def cbd(self, resource: _SubjectType, target_graph: Optional[Graph] = None) -> Graph: """Retrieves the Concise Bounded Description of a Resource from a Graph Concise Bounded Description (CBD) is defined in [1] as: @@ -1842,10 +1842,14 @@ def cbd(self, resource: _SubjectType) -> Graph: [1] https://www.w3.org/Submission/CBD/ :param resource: a URIRef object, of the Resource for queried for - :return: a Graph, subgraph of self + :param target_graph: Optionally, a graph to add the CBD to; otherwise, a new graph is created for the CBD + :return: a Graph, subgraph of self if no graph was provided otherwise the provided graph """ - subgraph = Graph() + if target_graph is None: + subgraph = Graph() + else: + subgraph = target_graph def add_to_cbd(uri: _SubjectType) -> None: for s, p, o in self.triples((uri, None, None)): diff --git a/rdflib/plugins/sparql/evaluate.py b/rdflib/plugins/sparql/evaluate.py index 4f8d687b41..83fb74f449 100644 --- a/rdflib/plugins/sparql/evaluate.py +++ b/rdflib/plugins/sparql/evaluate.py @@ -630,7 +630,7 @@ def evalDescribeQuery(ctx: QueryContext, query) -> Dict[str, Union[str, Graph]]: # Get a CBD for all resources identified to describe for resource in to_describe: # type error: Item "None" of "Optional[Graph]" has no attribute "cbd" - graph += ctx.graph.cbd(resource) # type: ignore[union-attr] + ctx.graph.cbd(resource, graph) # type: ignore[union-attr] res: Dict[str, Union[str, Graph]] = {} res["type_"] = "DESCRIBE"