Can anyone explain/show how I can create swrls in Owlready and the Sparql to query the inferred facts

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

Can anyone explain/show how I can create swrls in Owlready and the Sparql to query the inferred facts

IWilliams
I see where the document explain how to create swrl rules, but I am not clear on how I can then query the inferred facts using SPARQL. I read about SQWRL, but it is not supported by Owlready. I tried creating swrl in Protege and run the SPARQL queries, but the no results were returned. Do the SPARQL in owlready2 uses a reasoner?
Reply | Threaded
Open this post in threaded view
|

Re: Can anyone explain/show how I can create swrls in Owlready and the Sparql to query the inferred facts

Jiba
Administrator
Hi,

Here is a small example that shows how to use SWRL rule and then to query the results with Owlready and with SPARQL.

Jiba



from owlready2 import *

onto = get_ontology("http://test.org/onto.owl")

with onto:
  class Animal(Thing): pass
  class Bird(Animal): pass
 
  class Organ(Thing): pass
  class Wing(Organ): pass

  class has_organ(Animal >> Organ): pass
 
  t = Animal("t")
  t.has_organ.append(Wing())
 
  imp = Imp()
  imp.set_as_rule("Animal(?a), Wing(?o), has_organ(?a, ?o) -> Bird(?a)")
 
sync_reasoner_pellet()

print(list(Bird.instances()))

graph = default_world.as_rdflib_graph()
print(list(graph.query_owlready("""
SELECT ?b WHERE {
?b <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://test.org/onto.owl#Bird> .
} """)))
Reply | Threaded
Open this post in threaded view
|

Re: Can anyone explain/show how I can create swrls in Owlready and the Sparql to query the inferred facts

IWilliams
Hi Jiba,

Thank you. I actually figured it out prior to your answer. I was planning to run some more tests before posting my solution.