Working with contexts

Contexts are introduced in Adding Data to YOUR OpenWorm Database. Here we provide a little more…context.


Contexts were introduced to PyOpenWorm (POW) as a generic tool for grouping statements. We need to group statements to make statements about statements like “Who made these statements?” or “When were these statements made?”. That’s the main usage. Beyond that, we need a way to share statements. Contexts have identifiers by which we can naturally refer to contexts from other contexts.

POW need a way to represent contexts with the existing statement form. Other alternatives were considered, such as using Python’s context managers, but I (Mark) also wanted a way to put statements in a context that could also be carried with the subject of the statement. Using the wrapt package’s proxies allows to achieve this while keeping the interface of the wrapped object the same, which is useful since it doesn’t require a user of the object to know anything about contexts unless they need to change the context of a statement.

The remainder of this page will go into doing some useful things with contexts.

Classes and contexts

POW can load classes as well as instances from an RDF graph. The packages which define the classes must already be installed in the Python library path, and a few statements need to be in the graph you are loading from or in a graph imported (transitively) by that graph. The statements you need are these

:a_class_desc <> <> .
:a_class_desc <> :a_module .
:a_class_desc <> "AClassName" .

:a_module <> <> .
:a_module <> "" .

where :a_class_desc and :a_module are placeholders for objects which will typically be created by POW on the user’s behalf, and AClassName is the name of the class available at the top-level of the module These statements will be created in memory by POW when a module defining a DataObject-derived class is first processed by a Mapper which will happen after the module is imported.