cybertools/stateful
2012-03-03 11:54:13 +01:00
..
__init__.py added simple 'stateful' (sort of mini-workflow) package 2007-01-04 11:11:32 +00:00
base.py fall back to initial state if current state is not valid, e.g. because of changes in states definition 2012-03-03 11:54:13 +01:00
definition.py allow for more than one 'doBefore' function in a transition 2011-03-15 08:53:59 +00:00
interfaces.py add state-dependent control of actions and a doBefore handler for transitions 2009-02-19 09:36:05 +00:00
publishing.py work in progress: work items 2008-12-21 11:08:03 +00:00
README.txt rename 'publishing' states definition to 'simple_publishing' for unification of naming with loops.organize.stateful 2008-05-16 10:09:05 +00:00
tests.py added simple 'stateful' (sort of mini-workflow) package 2007-01-04 11:11:32 +00:00

================
Stateful Objects
================

  ($Id$)

  >>> from cybertools.stateful.definition import StatesDefinition
  >>> from cybertools.stateful.definition import State, Transition
  >>> from cybertools.stateful.definition import registerStatesDefinition
  >>> from cybertools.stateful.base import Stateful

We start with a simple demonstration class that provides stateful
behaviour directly.

  >>> class Demo(Stateful):
  ...     pass

  >>> demo = Demo()

The default states definition has the `started` state as its initial
state.

  >>> demo.getState()
  'started'
  >>> demo.getStateObject().title
  'Started'

We can now execute the `finish` Transition.

  >>> demo.doTransition('finish')
  >>> demo.getState()
  'finished'

More complex states definitions
-------------------------------

We'll use a predefined simple publishing workflow that.

  >>> from cybertools.stateful.publishing import simplePublishing
  >>> registerStatesDefinition(simplePublishing())

  >>> demo = Demo()
  >>> demo.statesDefinition = 'simple_publishing'
  >>> demo.getState()
  'draft'

  >>> [t.title for t in demo.getAvailableTransitions()]
  ['publish', 'hide', 'archive', 'remove']

If we try to execute a transition that is not an outgoing transition
of the current state we get an error.

  >>> demo.doTransition('retract')
  Traceback (most recent call last):
  ...
  ValueError: Transition 'retract' is not reachable from state 'draft'.
  >>> demo.getState()
  'draft'


Stateful Adapters
=================

Objects that show stateful behaviour need not be derived from the Stateful
class, for persistent objects one can also provide a stateful adapter.

  >>> from persistent import Persistent
  >>> class Demo(Persistent):
  ...     pass

  >>> demo = Demo()

  >>> from zope import component
  >>> from cybertools.stateful.base import StatefulAdapter
  >>> component.provideAdapter(StatefulAdapter)

We can now retrieve a stateful adapter using the IStateful interface.

  >>> from cybertools.stateful.interfaces import IStateful

  >>> statefulDemo = IStateful(demo)
  >>> statefulDemo.getState()
  'started'
  >>> statefulDemo.getStateObject().title
  'Started'

  >>> statefulDemo.doTransition('finish')
  >>> statefulDemo.getState()
  'finished'

If we make a new adapter for the same persistent object we get
back the state that is stored with the object.

  >>> statefulDemo = IStateful(demo)
  >>> statefulDemo.getState()
  'finished'