defernia.world.fact — 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 defernia.world.fact.Type(*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.

fields

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

reference_fields

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

value_fields

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

class defernia.world.fact.Fact(**kwargs)

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

Note

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

defernia.world.fact.Descriptor

The abstract base class for descriptors defined above.

Parameters:
  • 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
defernia.world.fact.Field

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.)

Parameters:
  • 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
defernia.world.fact.BaseReference

The abstract base class for Reference and SelfReference.

defernia.world.fact.Reference

The reference field. It can store fact object.

Note

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.

Parameters:
  • 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
defernia.world.fact.SelfReference

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

defernia.world.fact.predefined(type)

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

@predefined('character')
class Character(Type):
    pass

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

@predefined
class Character(Type):
    pass

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

defernia.world.fact.is_predefined(type)

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
defernia.world.fact.get_predefined_type(key)

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
defernia.world.fact.get_predefined_key(type)

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

defernia.world — Conworld

Next topic

defernia.world.name — Naming facts

This Page