Skip to content

Commit

Permalink
Add optional graph argument to Graph.cbd, use this new argument in ev…
Browse files Browse the repository at this point in the history
…alDescribeQuery
  • Loading branch information
mgberg committed Mar 28, 2023
1 parent 0d07f9b commit 138e4f2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)):
Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugins/sparql/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 138e4f2

Please sign in to comment.