0

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.

Community
  • 1
  • 1
msc87
  • 943
  • 3
  • 17
  • 39
  • Without more of your code, we have no way to know what the query actually ends up being. E.g., what does `URIRef(rno+ref_RaURI)` end up being? And we don't know what the data is, so we can't know whether the query should match or not. – Joshua Taylor May 24 '16 at 11:25
  • Some comments I added in the question to clarify – msc87 May 24 '16 at 11:37
  • Is "partiotion" supposed to be "partition" in your query? – Joshua Taylor May 24 '16 at 11:49
  • Also, you may need space before your dots after IRIS, e.g., "rno:Roundabout ." – Joshua Taylor May 24 '16 at 11:51
  • It is a typo in my ontology I guess "partition" :D – msc87 May 24 '16 at 11:57
  • I must say that this query used to worked. Now I want to optimize my code as I used to create the query every time in the for loop. I add the working query to my question as well. – msc87 May 24 '16 at 12:01

0 Answers0