FILTER IN SPARQL

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

FILTER IN SPARQL

jos lehmann
Hi there

I am trying to run a SPARQL query over all subclasses of a given class YYY by Query 1 below.
This would allow me to get YYY listed in the output rather than just getting the list of only YYY's suclasses, as achieved by Query 2 below.

Query 1 does not work though. ( See error at the bottom of this Email).

Is there a mistake in Query 1 or are there Owlready2 limitations on the filter used in Query 1 or both?

Thank you in advance for the advise.

KR, Jos Lehmann



Query 1

entities2 = list(default_world.sparql("""
        PREFIX owl: <http://www.w3.org/2002/07/owl#>
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX xxx: <http://www.semanticweb.org/aga/ontologies/2017/9/untitled-ontology-9#>
 
        SELECT DISTINCT ?subject ?supertype
            WHERE {
               {?subject rdfs:subClassOf ?supertype} .
           
                FILTER (?supertype= xxx:YYY)
            }
       
        """))



Query 2

entities2 = list(default_world.sparql("""
        PREFIX owl: <http://www.w3.org/2002/07/owl#>
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX xxx: <http://www.semanticweb.org/aga/ontologies/2017/9/untitled-ontology-9#>
 
        SELECT DISTINCT ?subject
            WHERE {
                {?subject rdfs:subClassOf xxx:YYY}  
           
            }
       
        """))



Error Query 1

---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-105-6d8b0562ea78> in <module>
     13             }
     14
---> 15         """))
     16
     17 entities2

~/dss/code-envs/python/py3-IE-update-1/lib/python3.6/site-packages/owlready2/sparql/main.py in execute(self, params)
    384   def execute(self, params = ()):
    385
--> 386     for l in PreparedQuery.execute(self, params):
    387       l2 = []
    388       i = 0

~/dss/code-envs/python/py3-IE-update-1/lib/python3.6/site-packages/owlready2/sparql/main.py in execute(self, params)
    379     sql_params = [self.world._to_rdf(param)[0] for param in params]
    380     for i in self.parameter_datatypes: sql_params.append(self.world._to_rdf(params[i])[1])
--> 381     return self.world.graph.execute(self.sql, sql_params)
    382
    383 class PreparedSelectQuery(PreparedQuery):

OperationalError: near "WITH": syntax error
Reply | Threaded
Open this post in threaded view
|

Re: FILTER IN SPARQL

jos lehmann
Hi there

I'm answering my own question, the problem in Query 1 were the parentheses.

KR, Jos Lehmann


Working version of Query 1:

entities2 = list(default_world.sparql("""
        PREFIX owl: <http://www.w3.org/2002/07/owl#>
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX xxx: <http://www.semanticweb.org/aga/ontologies/2017/9/untitled-ontology-9#>
 
        SELECT ?subject ?supertype  
            WHERE {?subject rdfs:subClassOf ?supertype .
                FILTER (?supertype = xxx:YYY)}

        """))