diff options
author | Nicholas Car <nicholas.car@csiro.au> | 2020-03-13 21:16:15 +1000 |
---|---|---|
committer | Nicholas Car <nicholas.car@csiro.au> | 2020-03-13 21:16:15 +1000 |
commit | a7863d25dc47730f4ef8bdca91c4e35c7f7b1ccf (patch) | |
tree | c123e33a55b7c24db9ad93daf59729c7087958d7 /rdflib | |
parent | 0e5efef78702575e4abff4d8076eac4e2bd9d5f0 (diff) | |
download | rdflib-a7863d25dc47730f4ef8bdca91c4e35c7f7b1ccf.tar.gz |
adds cbd() function to Graph() and tests and dependency on black
Diffstat (limited to 'rdflib')
-rw-r--r-- | rdflib/graph.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/rdflib/graph.py b/rdflib/graph.py index 4a27e6de..eb0a556e 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -1269,6 +1269,44 @@ class Graph(Node): return retval + def cbd(self, resource): + """Retrieves the Concise Bounded Description of a Resource from a Graph + + Concise Bounded Description (CBD) is defined in [1] as: + + Given a particular node (the starting node) in a particular RDF graph (the source graph), a subgraph of that + particular graph, taken to comprise a concise bounded description of the resource denoted by the starting node, + can be identified as follows: + + 1. Include in the subgraph all statements in the source graph where the subject of the statement is the + starting node; + 2. Recursively, for all statements identified in the subgraph thus far having a blank node object, include + in the subgraph all statements in the source graph where the subject of the statement is the blank node + in question and which are not already included in the subgraph. + 3. Recursively, for all statements included in the subgraph thus far, for all reifications of each statement + in the source graph, include the concise bounded description beginning from the rdf:Statement node of + each reification. + + This results in a subgraph where the object nodes are either URI references, literals, or blank nodes not + serving as the subject of any statement in the 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 + """ + subgraph = Graph() + + def add_to_cbd(uri): + for s, p, o in self.triples((uri, None, None)): + subgraph.add((s, p, o)) + # recurse 'down' through ll Blank Nodes + if type(o) == BNode and not (o, None, None) in subgraph: + add_to_cbd(o) + add_to_cbd(resource) + + return subgraph + class ConjunctiveGraph(Graph): |