I try to load the ontology OntoCAPE, available in: https://www.avt.rwth-aachen.de/cms/AVT/Forschung/Software/~ipts/OntoCape/lidx/1/
For that I run this code, once I copied all the ontology files in C:/ as requested by OntoCAPE specs.: ------------------------------------------------------------- from owlready2 import * onto = get_ontology('C:/OntoCAPE/OntoCAPE/OntoCAPE.owl').load() ------------------------------------------------------------- And this comes out: ------------------------------------------------------------- * Owlready2 * Warning: optimized Cython parser module 'owlready2_optimized' is not available, defaulting to slower Python implementation Traceback (most recent call last): File "C:/Users/Administrador/Google Drive//Py_Onto/main.py", line 5, in <module> onto = get_ontology('C:/OntoCAPE/OntoCAPE/OntoCAPE.owl').load() File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-packages\owlready2\namespace.py", line 808, in load imported_ontologies = [self.world.get_ontology(self._unabbreviate(abbrev_iri)).load() for abbrev_iri in self.world._get_obj_triples_sp_o(self.storid, owl_imports)] File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-packages\owlready2\namespace.py", line 808, in <listcomp> imported_ontologies = [self.world.get_ontology(self._unabbreviate(abbrev_iri)).load() for abbrev_iri in self.world._get_obj_triples_sp_o(self.storid, owl_imports)] File "C:\Users\Administrador\AppData\Local\Programs\Python\Python38\lib\site-packages\owlready2\namespace.py", line 778, in load fileobj = open(f, "rb") OSError: [Errno 22] Invalid argument: 'file:/C:/OntoCAPE/OntoCAPE/model/process_model.owl' Process finished with exit code 1 ------------------------------------------------------------- Anyone knows why? Thanks in advance |
Administrator
|
Hi,
The OntoCAPE ontology is split in many OWL files. The main file imports other OWL files, which in turn import other OWL files, etc, and all these files are in different subdirectories. To load it with Owlready, you need to add all these subdirectories to onto_path, the list of directories in which OWL files are searched. This can be automatically done in Python as follows (replace "/tmp/OntoCAPE" with your OntoCAPE directory, which must also include the meta_model): import sys, os from owlready2 import * for root, dirs, files in os.walk("/tmp/OntoCAPE", topdown = False): for dir in dirs: dir = os.path.join(root, dir) if ".svn" in dir: continue onto_path.append(dir) ontoCAPE = get_ontology("/tmp/OntoCAPE/OntoCAPE.owl").load() Notice the use of "topdown = False", which is require because some subdirectories have the same name that some ontologies... So, by starting with the bottom, Owlready will first search in the deepest subdirectories. Jiba |
This post was updated on .
Thanks so much Jean Baptiste, it seems to load now but when I try to access the content in the ontology it comes out like there is none, I mean if for instance I want to see the classes, or search something it's like there is nothing loaded.
Sorry by I'm quite newbie with Python and trying to learn, but do I need to define every class or sth in Python for being able to access it? Thanks for your help, it means a lot pxk87 |
In reply to this post by Jiba
Also, the onto_path created has the paths, created with your code above, like this:
C:/OntoCAPE/OntoCAPE\\applications\\aspen_plus Is it possible to read them like this anyway? Thanks pxk87 |
In reply to this post by Jiba
Hi again,
I have been working on this and when you load an ontology that has imports it seems to mess up with everything. For example, once loaded, I type: list(ontoCAPE.classes()) and the return is an empty list, but if I search by 'iri' it shows everything that is inside the ontology, classes, individuals, properties... The thing with ontoCAPE is that you load the main file, and this one imports other ontologies and these ones do the same with others, so it's importing in a "branch mode" and maybe owlready is not prepared to do so? I would like to be able to access the entities and properties as I can do with lighter ontologies (1 file), is it possible? Thanks for you support, it's greatly appreciated. Regards |
Administrator
|
Hi,
Indeed, ontoCAPE includes several files, and the main file is almost empty and just imports the other files. Consequently, ontoCAPE.classes() is empty, and ontoCAPE.<classname> will return None. If you want to access to a single class, you can use one of these two syntaxes: >>> IRIS["file:/C:/OntoCAPE/OntoCAPE/material/material.owl#thermodynamicBehavior"] or : >>> ontoCAPE_material = get_ontology("file:/C:/OntoCAPE/OntoCAPE/material/material.owl#") >>> ontoCAPE_material.thermodynamicBehavior And, to list all classes, you can either use default_world (it will list all classes available, not just those in ontoCAPE, if you loaded or created other ontologies): >>> list(default_world.classes()) or search anything with an IRI starting by "file:/C:/OntoCAPE/OntoCAPE/" as follows: >>> default_world.search(iri = "file:/C:/OntoCAPE/OntoCAPE/*") and then filter to keep only classes. Jiba |
Hi!
Thanks for your answer, it works well and I can see all the classes with default_world, but I still have trouble to access the content in the ontology, like for example: -If I want to know the subclasses of a class(the same happens with get_instances_of): print(ontoCAPE.get_children_of(ontoCAPE.Object)) I get this message: Traceback (most recent call last): File "D:/Py_Onto/cape.py", line 56, in <module> print(ontoCAPE.get_children_of(ontoCAPE.Object)) File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\owlready2\namespace.py", line 1100, in get_children_of return [self.world._get_by_storid(o) for o in self._get_obj_triples_po_s(Class._rdfs_is_a, Class.storid)] AttributeError: 'NoneType' object has no attribute '_rdfs_is_a' -Something similar happens when I want to see the property of an individual for example: print(ontoCAPE.EUR.hasDimension) #it should give amount_of_money as an answer I get this: Traceback (most recent call last): File "D:/Py_Onto/cape.py", line 56, in <module> print(ontoCAPE.EUR.hasDimension) AttributeError: 'NoneType' object has no attribute 'hasDimension' -And also if I want to know the type of any entity, the type is always "None": print(type(ontoCAPE.EUR)) I get this: <class 'NoneType'> All these errors don't happen with other ontologies that are more lightweight, the entities and properties are well defined and I can ask owlready for anything and I get the right answer. How could I do the same with ontoCAPE? Thanks for your help. Regards |
Administrator
|
Hi,
(NB I no longer manage to download the OntoCAPE ontology, using your previous link, so the solutions below are untested) > -If I want to know the subclasses of a class(the same happens with get_instances_of): > > print(ontoCAPE.get_children_of(ontoCAPE.Object)) > > AttributeError: 'NoneType' object has no attribute '_rdfs_is_a' The error message suggests me that ontoCAPE.Object is None. Could your verify that? > -Something similar happens when I want to see the property of an individual > for example: > > print(ontoCAPE.EUR.hasDimension) #it should give amount_of_money as an answer Similarly, I think that ontoCAPE.EUR is None. I it define in the ontoCAPE namespace or another one? The problem is that ontoCAPE is divided in several file and namespace, and you need to use the right namespace for each entity. Jiba |
Hi, thanks for your answer,
Yeah there seem to be a problem with the ontoCAPE page, I am sharing mine with you in this link: https://drive.google.com/open?id=1kgGpTuj294tjL8rSUXJgqzakVk_sJ8X3 Yeah, the error is that the entities or properties when you try to access to them have no type. What do you suggest, to load every file separately and using a different namespace for each one of them? I have tried to merge ontoCAPE in one file with Protégé, but the problem persist because each entity keeps the prefix from the file it comes from, i.e. the file or ontology where it was located. Thanks for your help Regards |
Hi, I hope everything is going well.
did you have the chance to have a look to the NoneType error? I would really appreciate an answer and any kind of suggestion. Thanks. Regards |
Administrator
|
Sorry, I'm very busy currently...
You can search for entities as follows (I use Object as an example): >>> default_world.search(iri = "*Object") [fundamental_concepts.Object, fundamental_concepts.involvesObject, fundamental_concepts.hasTargetObject, mereology.PartOfCompositeObject, mereology.CompositeObject, loop.sameObject, mathematical_model.ModeledObject] >>> default_world.search(iri = "*Object")[0].iri 'file:/C:/OntoCAPE/meta_model/fundamental_concepts/fundamental_concepts.owl#Object' Then, you can access to Object either via its ontology: fundamental_concepts = get_ontology("file:/C:/OntoCAPE/meta_model/fundamental_concepts/fundamental_concepts.owl#") print(fundamental_concepts.Object) or directly via its IRI: print(IRIS["file:/C:/OntoCAPE/meta_model/fundamental_concepts/fundamental_concepts.owl#Object"]) Jiba |
Hi,
I understand and appreciate your answer even been busy, thank you so much. Is there any way of loading all OntoCAPE files (more than 80) on just one namespace, i.e. instead of loading every single file in a different ontology and having to know every time which entity/property belongs to which ontology, being able to access any entity/property using only one prefix (e.g. "ontoCAPE.Object" or "ontoCAPE.EUR.hasDimension")? Thanks |
Administrator
|
Hi,
I am sorry, there is currently no way to "combine" or "agregate" the many ontologies in OntoCAPE. However, you may try a "dirty" solution in Python, by creating an agregating class based on __getattr__() that delegates any call to a list of other objects. Jiba |
Hi! Hope you are doing well and healthy
Thanks for the idea, I will give it a try, could you please exemplify ho I can do that? Thanks Regards |
Administrator
|
Hi,
I think you can try something like this (untested) : class Aggregator: def __init__(self, sources): self.sources = sources def __getattr__(self, attr): for source in self.sources: r = getattr(source, attr) if not r is None: return r Jiba |
Free forum by Nabble | Edit this page |