Re: Catch-22 on Imported Classes?

Posted by Mike Bergman on
URL: http://owlready.306.s1.nabble.com/Catch-22-on-Imported-Classes-tp1798p1806.html

Hi All,

Sorry to clog the list; I have figured out this 'Catch-22' issue I first posted. I withdraw my request for help.

As a total Python newbie, I first had to struggle with the questions of Python scope called LEGB. That was only a minor cause of the problems here. What I discovered were mismatches I was making in the interplay of base ontology, imported ontologies, namespaces, and internal 'Worlds' in owlready2.

Here, in brief, is what I learned. I'm sure my points are not fully correct, so I encourage anyone to add to or modify this list:

First, when loading an ontology, I give it a 'world' namespace assigned to the 'World' internal global namespace for owlready2. Since I am only doing bespoke development in Python at a given time, I can afford to claim the entire space and perhaps lessen other naming problems. Maybe this is superfluous, but I have found it to be a recipe that works for me.

Second, I needed to make sure that internal version IRIs among the working and imported ontologies were consistent. Since I'm versioning my ontologies, I had a mismatched declaration.

Third, when one imports an ontology into the working ontology (declaring the working ontology being step one), all ontologies available to the import are available to the working ontology. However, if one wants to modify or add items to these imported ontologies as opposed to simply calling them (as was my use case of wanting to delete the temporary owl:Thing placeholder when importing a new class), each one needs to be explicitly declared.

Fourth, it appears essential to declare the namespaces for these imports under the current working ontology. Thus, here are the example declarations that ended up working for me:


kb = world.get_ontology(kbpedia).load()
rc = kb.get_namespace('http://kbpedia.org/kko/rc/')              

skos = world.get_ontology(skos_file).load()
kb.imported_ontologies.append(skos)
core = world.get_namespace('http://www.w3.org/2004/02/skos/core#')

kko = world.get_ontology(kko_file).load()
kb.imported_ontologies.append(kko)
kko = kb.get_namespace('http://kbpedia.org/ontologies/kko#')


Last, once declared, it also appears essential to be cognizant that these separate namespaces need to be addressed explicitly in the code. One challenge is to make sure that variables passed in loops get the proper Python class type when doing class operations such as the following:


 var1 = getattr(rc, id_frag)
 var1.is_a.remove(owl.Thing)


The above needs to be repeated if there are more than one namespaces beyond 'rc'.

More than one namespace in the working ontology does complicate matters quite a bit, but that is also the more realistic architecture and design approach, at least for my own work.

I welcome corrections or expansions of these guidelines!

Thanks!

Best, Mike