how to look-up rdf object knowing subject or other way around?

1.4k views Asked by At

I'm using RDFLIB to build graphs among 3 data-sets (A, B, C) with ntriples.

Goal: graphs contain links among those data-set A->B, B->C and C->A, I want to check consistency of those links by making sure that links outgoing from A refer back to the same entries in A.

Problem: once I iterate over links in the A->B, I wanted to look-up corresponding entries (could be more than one) in the B->C and the same for the C->A, is there a way to look up objects by knowing the subject without iterating over all entries?

1

There are 1 answers

1
Manuel Salvadores On BEST ANSWER

is there a way to look up objects by knowing the subject without iterating over all entries?

The answer is Yes. And you can use to different mechanisms: (a) iterate with a restriction; or (b) issue an SPARQL query.

(a) constrain the graph and iterate

This solution uses a RDFLib triples function over the Graph object. See this reference.

#Parse the file
g = rdflib.Graph()
g.parse("yourdata.nquads")
subject = article = rdflib.term.URIRef("http://www.someuri.org/for/your/subject")

# (subject,None,None) represents a constrain to iterate over the graph. By setting
# any of the three elements in the triple you constrain by any combination of subject,
# predicate or object. In this case we only  constrain by subject.
for triple in g.triples((subject,None,None)):
    print triple

(b) issue a SPARQL query

A more standard solution using the SPARQL standard.

rdflib.plugin.register('sparql', rdflib.query.Processor,
                       'rdfextras.sparql.processor', 'Processor')
rdflib.plugin.register('sparql', rdflib.query.Result,
                       'rdfextras.sparql.query', 'SPARQLQueryResult')

 #Parse the file
g = rdflib.Graph()
g.parse("yourdata.nquads")

query = """
    SELECT ?pred ?obj WHERE {
         <http://www.someuri.org/for/your/subject> ?pred ?obj
    }
    """
for row in g.query(query):
    print "Predicate:%s Object:%s"%(row[0],row[1])