Vishal Singh
Vishal Singh

Reputation: 21

ParseException: Expected {SelectQuery | ConstructQuery | DescribeQuery | AskQuery} (at char 57), (line:3, col:21)

I am trying to do a simple SPARQL query using RDFlib. I have created an RDF graph using Radlex ontology and I am trying to query in the graph '''

g = Graph()
FOAF = rdflib.Namespace("http://radlex.org/RID/")
patient = rdflib.term.URIRef("http://localhost/rdf/patient/") 
n1 = Namespace("http://localhost/rdf/patient/") 

g.add( (patient, FOAF.RID13159, n1.age) )
g.add( (patient, FOAF.RID13159, n1.name) )
g.add( (patient, FOAF.RID13159, n1.gender) )

g.add( (n1.name,  FOAF.RID13160, Literal("Radha")))
g.add( (n1.age,   FOAF.RID13163, Literal('21')))
g.add( (n1.gender,FOAF.RID13164, Literal("F"))) 

result = g.serialize(format='turtle')

qres = g.query(
          """SELECT ?Subject
         WHERE {
                ?Subject FOAF.RID13163 "F". 
               }"""
          )

'''

and I get this error

 ParseException                            Traceback (most recent call last)
<ipython-input-9-7aadbf996386> in <module>
     24              WHERE {
     25                     ?Subject FOAF.RID13163 "F".
---> 26                    }"""
     27               )
     28 # list( g.triples((None, FOAF.RID13160, None)) )

~\AppData\Local\Continuum\anaconda3\lib\site-packages\rdflib\graph.py in query(self, query_object, processor, result, initNs, initBindings, use_store_provided, **kwargs)
   1087 
   1088         return result(processor.query(
-> 1089             query_object, initBindings, initNs, **kwargs))
   1090 
   1091     def update(self, update_object, processor='sparql',

~\AppData\Local\Continuum\anaconda3\lib\site-packages\rdflib\plugins\sparql\processor.py in query(self, strOrQuery, initBindings, initNs, base, DEBUG)
     72 
     73         if not isinstance(strOrQuery, Query):
---> 74             parsetree = parseQuery(strOrQuery)
     75             query = translateQuery(parsetree, base, initNs)
     76         else:

ParseException: Expected {SelectQuery | ConstructQuery | DescribeQuery | AskQuery} (at char 57), (line:3, col:21)

I don't understand what does this error mean

Upvotes: 1

Views: 920

Answers (1)

Donny Winston
Donny Winston

Reputation: 2452

The error is saying that it doesn't recognize a syntactically correct query form.

In your case, you need to map from rdflib's use of Python object attribute access for expressing prefixed names -- in your example, FOAF.RID13163 -- to SPARQL's notation for prefixed names, which uses a colon. This means that FOAF.RID13163 in Python translates to FOAF:RID13163 in SPARQL. Otherwise, rdflib complains that it cannot find a valid query form.

Beyond this, there is another reason that your query will not run, even if you change FOAF.RID13163 to FOAF:RID13163: the FOAF namespace prefix will not be recognized by the query. If you add the line g.bind("FOAF", FOAF) before running your query, then your query will run.

Once your query runs, there are two other things I would like to point out. First, I think you mean for your predicate in the query triple to be FOAF:RID13164 -- that will return one result. Second, FOAF is a built-in namespace for rdflib (from rdflib.namespace import FOAF), and refers to the well-known Friend of a Friend vocabulary. I suggest you use another name (RID in your case?) so as not to confuse others who might need to work with your code.

Upvotes: 0

Related Questions