— Object system for conworld ontologies

This module implements a naive implementation of data structures for ontologies. These are extensible by users, so this module implements only some foundations of it: type system (metaclass) for fact ontologies.

The smallest goal of Defernia is to provide the system for defining fictional facts (also known as conworld). Fictional facts have several properties:

  • Most of facts are not momentary but continous; for example, some character’s height could be getting taller. Terrains also can be changed.
  • Facts have relationships each other; for example, there are two characters A and B, and A is possibly B’s father. Relationships are also called as predicates and such facts are also called as triples in ontology terminalogy.
  • Facts about relationships can deduce more complex facts by composition for example, “A is B’s father and B is C’s father; so A is C’s grandfather.”
  • Types of relationships (“predicates”) can be getting more.
  • Unreachable facts are mostly meaningless (for conworld at least).
  • Names are not self-given. Roses don’t name themselves as “roses” but just people name them “roses” or “壯美”. Roses just have red colors and sweet scents.
  • Facts can be incomplete. Some properties of a fact are possibly unknown.
  • While some relationships (“predicates”) can be completely inferenced, these can still exist redundantly to leave extra informations through unknown informations. For example, there are only 3 fact objects as A’s children, but still there could be needs of explicit data about the explicit number of chidren: 4 or 5, not 3 (rest of children are not defined yet).
  • As a side effect, when unknown fields are filled fully some relationships could have inconsistency. These must be detectable and should be able to be resolved easily.

To achieve our goal this module defines the specialized object system based on Python’s object system and metaclasses.

class*args, **kwargs)

The metaclass of fact objects. For example, the character Root Gorgias is an instance of Character and the type Character is an instance of Type.

It is a subtype of Python’s types.TypeType and Fact (which also is an instance of Type). It means instances of Type (namely, fact types) also share the same functionalities and properties with Fact instances (namely, facts). For example, the character Root Gorgias and the fact type Character both can be stored in a repository.


(dict) The dictionary of all fields. Keys are attribute names and values are descriptors.


(dict) The dictionary of reference fields. Keys are attribute names and values are descriptors.


(dict) The dictionary of value fields. Keys are attribute nameds and values are descriptors.


The fact object. All fact types are subtypes of Fact class.


Internally it maintains __fact__ attribute (similar concept to __dict__) to store internal status of fields.

The abstract base class for descriptors defined above.

  • name (basestring) – the human-readable name of the field e.g. 'Date of birth'
  • optional (bool) – set it False if it cannot be None. True by default

The descriptor for fact types. It can store ordinary Python objects. (If you don’t want to store ordinary Python objects but fact objects, use Reference instead.)

  • name (basestring) – the human-readable name of the field e.g. 'Date of birth'
  • type (types.TypeType) – the allowed type to set
  • optional (bool) – set it False if it cannot be None. True by default

The abstract base class for Reference and SelfReference.

The reference field. It can store fact object.


Because of evaluation order of Python’s class definition, you cannot define a self-referential field by this class. The following class definition doesn’t work:

class Character(Fact):

    father = Reference(Character)

In class definition time, there isn’t the class named Character, so it raises a NameError. In this case you have to use SelfReference instead.

  • name (basestring) – the human-readable name of the field e.g. 'Father'
  • type (Type) – the allowed type to set e.g. Character
  • optional (bool) – set it False if it cannot be None. True by default

The self-referential filed. Shares the same parameters with Descriptor.

The class decorator that registers a type as a predefined type. For example:

class Character(Type):

Or alternatively you can omit its key ('character' in the below):

class Character(Type):

If a key is omitted, it will be set automatically. For example, a class named ClassName will be 'class-name'.

Queries whether the given fact type is predefined or not.

Parameters:type (Type) – a type to query
Returns:True only if the type is predefined
Return type:bool

Returns the predefined type of the passed key.

Parameters:key (basestring) – the key of a predefined type
Returns:the predefined type
Return type:Type
Raises :LookupError if the key doesn’t exist

Returns the key name of the passed predefined type.

Parameters:type (Type) – a predefined type
Returns:the key name
Return type:basestring
Raises :ValueError if type is not predefined

Project Versions

Previous topic — Conworld

Next topic — Naming facts

This Page