I have the following code which works:
for ref_roundaboutID, ref_roundaboutInfo in ref_roundabouts_dict.iteritems():
print ref_roundaboutID, ref_roundaboutInfo
ref_RaURI = ref_roundaboutInfo["URI"]
query = "SELECT distinct ?p ?a ?FID ?b ?NFID\
WHERE {?a rno:end_at ?b; rno:FID ?FID;\
rno:is_part_of ?p; rno:is_part_of ?st.\
?p rdf:type ?type.\
?type rdfs:subClassOf rno:Street_partiotion.\
?st rdf:type rno:Street.\
?b rdf:type rno:Node; rno:is_extent_of ?x; rno:FID ?NFID.\
?x rno:builds rno:%(v1)s.\
rno:%(v1)s rdf:type rno:Roundabout.\
FILTER(NOT EXISTS {?a rno:builds rno:%(v1)s}).\
FILTER(NOT EXISTS {?st rdf:type rno:detailed_partition}).\
FILTER(NOT EXISTS {?p rdf:type rno:Lane})}"%dict(v1=ref_RaURI)
## rno is the name space rno = Namespae(http://www.semanticweb.org/ontologies/2015/3/RNO_V5042#)
sparql.prepareQuery(query, initNs={"rno":rno})
ref_sectionInfo_around_RA=dict()
results = ref_rdf.query(query, initNs={"rno":rno})
for result in results:
sectionUri, linkUri, linkId, nodeUri, nodeId =result
print sectionUri, linkUri, linkId, nodeUri, nodeId
Now I want to optimize my code by preparing a query and ask it several times for different named individuals. So I stumbled upon initBindings which can replace a variable in constructed query by variables in a dictionary. My code is as follows:
query = "SELECT distinct ?p ?a ?FID ?b ?NFID\
WHERE {?a rno:end_at ?b; rno:FID ?FID;\
rno:is_part_of ?p; rno:is_part_of ?st.\
?p rdf:type ?type.\
?type rdfs:subClassOf rno:Street_partiotion.\
?st rdf:type rno:Street.\
?b rdf:type rno:Node; rno:is_extent_of ?x; rno:FID ?NFID.\
?x rno:builds ?ra.\
?ra rdf:type rno:Roundabout.\
FILTER(NOT EXISTS {?a rno:builds ?ra}).\
FILTER(NOT EXISTS {?st rdf:type rno:detailed_partition}).\
FILTER(NOT EXISTS {?p rdf:type rno:Lane})}"
sparql.prepareQuery(query, initNs={"rno":rno})
for ref_roundaboutID, ref_roundaboutInfo in ref_roundabouts_dict.iteritems():
print ref_roundaboutID, ref_roundaboutInfo
ref_RaURI = ref_roundaboutInfo["URI"]
print ref_RaURI
## ra_6
## rno is the name space rno = Namespae(http://www.semanticweb.org/ontologies/2015/3/RNO_V5042#)
print rno+ref_RaURI
##http://www.semanticweb.org/ontologies/2015/3/RNO_V5042#ra_6
## This is the uri of the nameindividual in my ontology.
print rdflib.URIRef(rno+ref_RaURI)
##http://www.semanticweb.org/ontologies/2015/3/RNO_V5042#ra_6
ref_sectionInfo_around_RA=dict()
results = ref_rdf.query(query, initNs={"rno":rno}, initBindings={'ra':URIRef(rno+ref_RaURI)})
for result in results:
sectionUri, linkUri, linkId, nodeUri, nodeId =result
print sectionUri, linkUri, linkId, nodeUri, nodeId
however, the query result is empty and I dont know what is wrong with the code. I also checked similar questions such as this and this but no success so far.