Re: How does 'is_a' work with the search() method?

Posted by kuoks on
URL: http://owlready.306.s1.nabble.com/How-does-is-a-work-with-the-search-method-tp2421p2428.html

Hi Jiba,

Many thanks for the fast response.

I am sorry to report that, when I tried the SPARQL query, I got the following error:

>>> print([x[0] for x in list(default_world.sparql("""SELECT ?x { ?x a owl:Class. FILTER(ISIRI(?x)). FILTER NOT EXISTS { ?x rdfs:subClassOf ?y. FILTER(ISIRI(?y) && ?y != owl:Thing) } }"""))])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kskuo/GitHub/NASA-IMPACT/kg_scripts/venv/lib/python3.8/site-packages/owlready2/sparql/main.py", line 281, in execute
    for l in PreparedQuery.execute(self, params):
  File "/Users/kskuo/GitHub/NASA-IMPACT/kg_scripts/venv/lib/python3.8/site-packages/owlready2/sparql/main.py", line 277, in execute
    return self.world.graph.execute(self.sql, sql_params)
sqlite3.OperationalError: near "ANDq2": syntax error

My more experienced friend helped me and got something similar to work as follows, after loading the SWEET ontology with owlready2 (ver. 0.30):

>>> import rdflib
>>> graph = default_world.as_rdflib_graph()
>>> r = list( graph.query("""
... PREFIX owl: <http://www.w3.org/2002/07/owl#>
... PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
... SELECT ?x { ?x a owl:Class. FILTER(ISIRI(?x)). FILTER NOT EXISTS { ?x rdfs:subClassOf ?y. FILTER(ISIRI(?y) && ?y != owl:Thing) } }
... """))
>>> r
[(rdflib.term.URIRef('http://sweetontology.net/phen/Phenomena'),), (rdflib.term.URIRef('http://sweetontology.net/proc/Process'),), (rdflib.term.URIRef('http://sweetontology.net/prop/Property'),), (rdflib.term.URIRef('http://sweetontology.net/realm/Realm'),), (rdflib.term.URIRef('http://sweetontology.net/state/State'),), (rdflib.term.URIRef('http://sweetontology.net/repr/Representation'),), (rdflib.term.URIRef('http://sweetontology.net/human/HumanActivity'),), (rdflib.term.URIRef('http://sweetontology.net/matr/Substance'),), (rdflib.term.URIRef('http://www.w3.org/ns/sosa/Sensor'),), (rdflib.term.URIRef('http://sweetontology.net/matrParticle/Nucleon'),), (rdflib.term.URIRef('http://sweetontology.net/phenCryo/PhysicalProcess'),), (rdflib.term.URIRef('http://sweetontology.net/realmGeol/GeologicFeature'),), (rdflib.term.URIRef('http://sweetontology.net/procChemical/Sorption'),), (rdflib.term.URIRef('http://sweetontology.net/reprSciModel/Sample'),), (rdflib.term.URIRef('https://schema.org/Organization'),)]

About searching for subclasses, i.e. 'onto.search(subclass_of = Thing)', it was not the first thing I tried. I tried 'onto.search(is_a = Thing)' first, as stated in my initial post. That hung too. Is it due to the same reason, i.e. going through all descendants of 'Thing'?

Thanks,
Kuo