Worlds

Owlready2 stores every triples in a ‘World’ object, and it can handles several Worlds in parallel. ‘default_world’ is the World used by default.

Persistent world: storing the quadstore in an SQLite3 file

Owlready2 uses an optimized quadstore. By default, the quadstore is stored in memory, but it can also be stored in an SQLite3 file. This allows persistance: all ontologies loaded and created are stored in the file, and can be reused later. This is interesting for big ontogolies: loading huge ontologies can take time, while opening the SQLite3 file takes only a fraction of second, even for big files. It also avoid to load huge ontologies in memory, if you only need to access a few entities from these ontologies.

The .set_backend() method of World sets the SQLite3 filename associated to the quadstore, for example:

>>> default_world.set_backend(filename = "/path/to/your/file.sqlite3")

Note

If the quad store is not empty when calling .set_backend(), RDF triples are automatically copied. However, this operation can have a high performance cost (especially if there are many triples).

When using persistence, the .save() method of World must be called for saving the actual state of the quadstore in the SQLite3 file:

>>> default_world.save()

Storing the quadstore in a file does not reduce the performance of Owlready2 (actually, it seems that Owlready2 performs a little faster when storing the quadstore on the disk).

Using several isolated Worlds

Owlready2 can support several, isolated, Worlds. This is interesting if you want to load several version of the same ontology, for example before and after reasoning.

A new World can be created using the World class:

>>> my_world = World()
>>> my_second_world = World(filename = "/path/to/quadstore.sqlite3")

Ontologies are then created and loaded using the .get_ontology() methods of the World (when working with several Worlds, this method replaces the get_ontology() global function):

>>> onto = my_world.get_ontology("http://test.org/onto/").load()

The World object can be used as a pseudo-dictionary for accessing entities using their IRI. (when working with several Worlds, this method replaces the IRIS global pseudo-dictionary):

>>> my_world["http://test.org/onto/my_iri"]

Finally, the reasoner can be executed on a specific World:

>>> sync_reasoner(my_world)

Working with RDFlib for performing SPARQL queries

Owlready2 uses an optimized RDF quadstore. This quadstore can also be accessed as an RDFlib graph as follows:

>>> graph = default_world.as_rdflib_graph()

In particular, the RDFlib graph can be used for performing SPARQL queries:

>>> r = list(graph.query("""SELECT ?p WHERE {
  <http://www.semanticweb.org/jiba/ontologies/2017/0/test#ma_pizza> <http://www.semanticweb.org/jiba/ontologies/2017/0/test#price> ?p .
}"""))