summaryrefslogtreecommitdiff
path: root/rdflib
diff options
context:
space:
mode:
authorMatt Goldberg <59745812+mgberg@users.noreply.github.com>2023-04-11 17:12:27 -0400
committerGitHub <noreply@github.com>2023-04-11 23:12:27 +0200
commit81d13d432b7e49b557b5de11691bdeaed31a9b06 (patch)
tree20a100ea3f8ae83a98e26ee3dc4670b6ce8cd420 /rdflib
parent5c3c78af33530298a7db2350304ce253370e91c1 (diff)
downloadrdflib-81d13d432b7e49b557b5de11691bdeaed31a9b06.tar.gz
feat: add optional `target_graph` argument to `Graph.cbd` and use it for DESCRIBE queries (#2322)
Add optional keyword only `target_graph` argument to `rdflib.graph.Graph.cbd` and use this new argument in `evalDescribeQuery`. This makes it possible to compute a concise bounded description without creating a new graph to hold the result, and also without potentially having to copy it to another final graph. Co-authored-by: Iwan Aucamp <aucampia@gmail.com>
Diffstat (limited to 'rdflib')
-rw-r--r--rdflib/graph.py12
-rw-r--r--rdflib/plugins/sparql/evaluate.py2
2 files changed, 10 insertions, 4 deletions
diff --git a/rdflib/graph.py b/rdflib/graph.py
index c6f0fd36..6e2e50af 100644
--- a/rdflib/graph.py
+++ b/rdflib/graph.py
@@ -1814,7 +1814,9 @@ class Graph(Node):
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:
@@ -1840,10 +1842,14 @@ class Graph(Node):
[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 764250c8..08dd02d5 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, target_graph=graph) # type: ignore[union-attr]
res: Dict[str, Union[str, Graph]] = {}
res["type_"] = "DESCRIBE"