Querying the shared nodes in a RDF graph

2019-08-25 01:52发布

问题:

I have a graph of RDF data, that is the result of a SPARQL query in rdflib, but this question is valid just on any endpoint too. The graph looks like the picture below.

I want to find a way to query the nodes that are shared between two clusters. Those are basically the nodes that are:

  • Subject to two objects
  • Object to two subjects
  • Object to a subject, and, then subject to another object

I tried with Graph.subjects() and Graph.objects() on rdflib it seems to me that they are only iterable and I have to iterate the whole graph three times, for each of the above scenarios, and it would result in a lot of double counting.

I was wondering if anyone has an idea on how to do this in a better way, perhaps within SPARQL to begin with.