Hello Jiba! Thanks a lot for developing & maintaining Owlready2. It's been amazingly useful!
I'm a fairly new user and still find some concepts unclear, especially when reading and writing local files. My use of Owlready2 is purely from a Knowledge Graph perspective.
I'm initializing my ontology like so: And tried different ways of saving it:
Exporting the world works
Specifying the output file works
Saving without arguments will not work
However if I initialize the ontology differently, this behaviour can change. It is also conditional on many parameters. For instance, if entities are added with different base uri, the following can occur: (this is not my own traceback, I encountered a similar issue and expended too much time already trying to replicate the error - will document it if I find it again :
Could you provide a bit more insight into which parameters and process are best for handling local ontologies and load/save them to files? Specifically, here are a list of questions.
Thank you very much, and many cheers!
* Saving without arguments will not work
Ontology.save() without arguments requires that you specify the onto_path variable (with onto_path.append("xxx_path")). It works like PYTHON_PATH and sys.path, but for ontologies and not for Python modules). Ontology.save() without arguments will save the ontology in the directory onto_path, and the filename is determined by the last part of the ontology's IRI.
* Why is load() required when creating an ontology?
load() is required when loading an ontology from Internet or from a local file. On the contrary, it should not be called when creating an ontology from scratch in Python.
* How are the base URI and ontology name used in the saving process?
When saving an ontology, the IRI of the ontology is written inside the file (or the quadstore). As a consequence, you cannot just rename the OWL file to change the IRI of the ontology : the content of the file must also be changed.
* Are there principles, enforced by Owlready2 or not, that dictate how IRI or individual names should be written?
IRI must starts with http:// or https:// (Owlready2 accepts file:// but many tools do not).
In addition, many special characters are not supported in IRI, but Owlready2 is actually quite tolerant and often accepts them.
My personal recommendation are to use IRI of the form "http://website.xxx/path/ontoname.owl#entity", but other forms are acceptable and fully supported, e.g. "http://website.xxx/path/ontoname/entity".
* What are the pros and cons of loading/saving ontologies VS worlds?
Savings ontologies produces multiples OWL or RDF files, it is slower, but the files can be opened with other tools (like Protégé).
Saving world produces a single file, is faster, but the file can only be opened with Owlready2.
Thank you very much for all these clear answers!
Now I have some code to re-work a bit in order to leverage this new knowledge ;)
This post was updated on .
I've been reformatting my code to integrate these new recommendations. However, I run into errors for the two main options I've been trying, namely setting onto_path as relative or absolute.
Onto_path absolute path, saving with onto.save(path), with argument as built inside owlready2
Using an onto_path relative path with or witout arguments, and using an absolute path without arguments
These 3 methods will produce a permissionError I can't explain (I could manually read or write through the same python script using "with open(, 'w') as f: f.write(...)" without any problem).
So I though I was following better guidelines but I instead encounter a loss of functionality, with the only saving mechanism working being to and from the sqlite database. However I'd need to make an actual, rdfxml, file saving work.
Have you any insight on what I am doing wrong?
Here is a longer traceback for easier tracking
For the second problem, the cause is that you set "/" as the onto path. Thus, Owlready will try to save the ontology in the / directory. But either you are using Linux (or Mac) and / is only available for root user, or you are using Windows and / does not exist (it is more or less equivalent to C:/). You need to specify a writeable directory in onto_path if you want to save ontology.
For the first problem, the path is writeable (but notice that the path variable is actually not used, since it is not used in the construction of the p variable later). The problem seems related to the translation in RDF/XML ; Owlready encounter an entity for which it cannot find an IRI. If you send me the ontology, I can investigate it further.
|Free forum by Nabble||Edit this page|