#!/usr/bin/env python # coding: utf-8 # This notebook documents how to solve the famous [zebra puzzle](http://en.wikipedia.org/wiki/Zebra_puzzle) by A. Einstein with the help of semantic web tools for python. # # # It is based on theses Links/Projects # # - https://pythonhosted.org/Owlready2/index.html # - https://github.com/RDFLib/OWL-RL/issues/3 "Solving Einstein's riddle (zebra puzzle" # - Linked owl-files: # - https://github.com/RDFLib/OWL-RL/files/1533408/einsteins_riddle.owl.txt ← works # - created by [D. Ponomaryov](https://persons.iis.nsk.su/en/ponom/ontologies) # # For installation I did: # # ```bash # pip install owlready # ``` # # Note there is also https://github.com/RDFLib/OWL-RL/files/1533409/zebra.n3.txt but the N3-Format is not supported by Owlready. # # In[1]: import os import owlready2 as owl2 # # Solution # # Assumption: ontology file has been downloaded to `./ontology_data/`. # In[2]: data_path = "ontology_data" path1 = os.path.join(data_path, "einsteins_riddle.owl.txt") # In[3]: # get an overview what is inside the Ontology onto = owl2.get_ontology(path1).load() print("classes (concepts):\n", list(onto.classes()), "\n"*2) print("properties (roles):\n", list(onto.properties()), "\n"*2) print("individuals:\n", list(onto.individuals())) # ### Relations before calling the reasoner # In[4]: list(onto.lives_in.get_relations()) # In[5]: list(onto.owns.get_relations()) # ### Calling the reasoner and inspect relations again # In[6]: get_ipython().run_line_magic('time', 'owl2.sync_reasoner_pellet(infer_property_values=True, infer_data_property_values=True, debug=0)') # In[7]: rels = list(onto.lives_in.get_relations()) print(rels) # In[8]: rels = list(onto.owns.get_relations()) print(rels) # In[9]: for r in rels: print(f"n.{r[0].name}.owns: n.{r[1].name}") # ### Conclusion # # The puzzle could be solved easily by the reasoner # # --- # # Note: This Puzzle is also an example of [yamlpyowl](https://github.com/cknoll/yamlpyowl) – an experimental package to represent OWL-Ontologies in YAML: # # # #