question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggested method on Graph for BNode Closure

See original GitHub issue

wwai…@gmail.com, 2010-07-22T16:05:40.000Z

def bnc(self, triple, *av, **kw):
        """                                                                                                                   
        Return the BNode closure(s) for triples that are matched                                                              
        by the given "triple". Any additional positional or keyword                                                           
        arguments are passed to the constructor for the new graph.                                                            
        """
        result = Graph(*av, **kw)
        for s,p,o in self.triples(triple):
            result.add((s,p,o))
            if isinstance(o, BNode):
                result += self.bnc((o, None, None))
        return result

Comment 1 by wwai…@gmail.com

Supporting methods:

    def one(self, triple):
        """
        Return one matching "triple" or "None"
        """
        for statement in self.triples(triple):
            return statement

    def exists(self, triple):
        """
        Return "True" if "triple" exists in the graph, "False" otherwise
        """
        statement = self.one(triple)
        if statement is not None:
            return True
        return False

Comment 2 by wwai…@gmail.com

improved original function that won’t loop in case of silly bnode loops (thanks gromgull):

   def bnc(self, triple, *av, **kw):
        """                                                                                                                   
        Return the BNode closure(s) for triples that are matched                                                              
        by the given "triple". Any additional positional or keyword                                                           
        arguments are passed to the constructor for the new graph.                                                            
        """
        result = Graph(*av, **kw)
        for s,p,o in self.triples(triple):
            result.add((s,p,o))
            if isinstance(o, BNode) and not result.exists((o, None, None)):
                result += self.bnc((o, None, None))
        return result

Comment 3 by wwai…@gmail.com

This code comes from:

http://knowledgeforge.net/pdw/ordf/file/tip/ordf/graph.py

Comment 4 by vfaronov

Graphs support the in operator, so exists is unnecessary, I believe.

Comment 5 by drewpca

one() should raise an exception instead of returning None. The functionality is the same either way (though the exception can include a helpful message), but with None, every caller is obligated to check the type of the output. Forgetting to do so will lead to TypeErrors later on (when someone tries to unpack the None value).

‘bnc’ should have a descriptive name, possibly ‘bnode_closure’ or ‘bnode_closure_graph’. Why doesn’t a subject (or predicate) bnode cause recursion? (None, FOAF.name, Literal(“Drew”)) might be the IFP that I use to find a person, and wouldn’t bnode_closure be the right way to get the other triples about that person? If I’m getting the use case wrong, please revise the docstring to explain why only objects are checked for BNode.

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Reactions:1
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
white-geckocommented, Feb 27, 2019

I like the bnc idea. maybe it should also include the whole closure of the subject, if it is a blank node.

0reactions
gjhigginscommented, Dec 23, 2021

Implemented in #1502

Read more comments on GitHub >

github_iconTop Results From Across the Web

Transitive closure of a graph - GeeksforGeeks
1. Transitive Closure of a Graph using DFS · 2. Check for transitive property in a given Undirected Graph · 3. Detect cycle...
Read more >
Closure-Tree: An Index Structure for Graph Queries - CiteSeerX
Our indexing technique, called Closure-tree, orga- nizes graphs hierarchically where each node summarizes its descendants by a graph closure.
Read more >
Transitive closure of a Graph - Tutorialspoint
Transitive Closure it the reachability matrix to reach from vertex u to vertex v of a graph. One graph is given, we have...
Read more >
Directed closure coefficient and its patterns | PLOS ONE
The clustering coefficient was originally proposed to measure the cliquishness of a neighbourhood in an undirected graph [17]. Let G = (V,E) be ......
Read more >
Closure-Tree: An Index Structure for Graph Queries
Our indexing technique, called Closure-tree, organizes graphs hierarchically where each node summarizes its descendants by a graph closure.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found