Codebase list python-fysom / HEAD
HEAD

Tree @HEAD (Download .tar.gz)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
.. image:: https://travis-ci.org/mriehl/fysom.png?branch=master
   :alt: Travis build status image
   :align: left
   :target: https://travis-ci.org/mriehl/fysom

.. image:: https://coveralls.io/repos/mriehl/fysom/badge.png?branch=master
    :target: https://coveralls.io/r/mriehl/fysom?branch=master
    :alt: Coverage status

.. image:: https://badge.fury.io/py/fysom.png
    :target: https://badge.fury.io/py/fysom
    :alt: Latest PyPI version



License
=======

MIT licensed. All credits go to Jake Gordon for the `original javascript
implementation <https://github.com/jakesgordon/javascript-state-machine/>`_
and to Mansour Behabadi for the `python
port <https://github.com/oxplot/fysom>`_.

Synopsis
========

This is basically Mansours' implementation with unit tests and a build process added.
It's also on PyPi (``pip install fysom``).
Fysom is built and tested on python 2.6 to 3.5 and PyPy.

Installation
============

From your friendly neighbourhood cheeseshop
-------------------------------------------

::

    pip install fysom

Developer setup
---------------

This module uses `PyBuilder <http://pybuilder.github.io>`_.
::

    sudo pip install pyb_init
    pyb-init github mriehl : fysom

Running the tests
-----------------

::

    pyb verify

Generating and using a setup.py
-------------------------------

::

    pyb
    cd target/dist/fysom-$VERSION
    ./setup.py bdist_rpm #build RPM

Looking at the coverage
-----------------------

::

    pyb
    cat target/reports/coverage

USAGE
=====

Basics
------

::

    from fysom import Fysom

    fsm = Fysom({ 'initial': 'green',
                  'events': [
                      {'name': 'warn', 'src': 'green', 'dst': 'yellow'},
                      {'name': 'panic', 'src': 'yellow', 'dst': 'red'},
                      {'name': 'calm', 'src': 'red', 'dst': 'yellow'},
                      {'name': 'clear', 'src': 'yellow', 'dst': 'green'} ] })

... will create an
object with a method for each event:

-  fsm.warn() - transition from 'green' to 'yellow'
-  fsm.panic() - transition from 'yellow' to 'red'
-  fsm.calm() - transition from 'red' to 'yellow'
-  fsm.clear() - transition from 'yellow' to 'green'

along with the following members:

-  fsm.current - contains the current state
-  fsm.isstate(s) - return True if state s is the current state
-  fsm.can(e) - return True if event e can be fired in the current state
-  fsm.cannot(e) - return True if event s cannot be fired in the current
   state


Shorter Syntax
--------------

It's possible to define event transitions as 3-tuples (event name,
source state, destination state) rather than dictionaries.
``Fysom`` constructor accepts also keyword arguments ``initial``,
``events``, ``callbacks``, and ``final``.

This is a shorter version of the previous example::

    fsm = Fysom(initial='green',
                events=[('warn',  'green',  'yellow'),
                        ('panic', 'yellow', 'red'),
                        ('calm',  'red',    'yellow'),
                        ('clear', 'yellow', 'green')])


Initialization
--------------

How the state machine should initialize can depend on your application
requirements, so the library provides a number of simple options.

By default, if you don't specify any initial state, the state machine
will be in the 'none' state and you would need to provide an event to
take it out of this state:
::

    fsm = Fysom({'events': [
                    {'name': 'startup', 'src': 'none',  'dst': 'green'},
                    {'name': 'panic', 'src': 'green', 'dst': 'red'},
                    {'name': 'calm', 'src': 'red', 'dst': 'green'}]})
    print fsm.current # "none"
    fsm.startup()
    print fsm.current # "green"

If you specify the name of your initial event (as in all the earlier
examples), then an implicit 'startup' event will be created for you and
fired when the state machine is constructed:
::

    fsm = Fysom({'initial': 'green',
                 'events': [
                     {'name': 'panic', 'src': 'green', 'dst': 'red'},
                     {'name': 'calm', 'src': 'red', 'dst': 'green'}]})
    print fsm.current # "green"

If your object already has a startup method, you can use a different
name for the initial event:
::

    fsm = Fysom({'initial': {'state': 'green', 'event': 'init'},
                 'events': [
                     {'name': 'panic', 'src': 'green', 'dst': 'red'},
                     {'name': 'calm',  'src': 'red', 'dst': 'green'}]})
    print fsm.current # "green"

Finally, if you want to wait to call the initial state transition event
until a later date, you can defer it:
::

    fsm = Fysom({'initial': {'state': 'green', 'event': 'init', 'defer': True},
                 'events': [
                     {'name': 'panic', 'src': 'green', 'dst': 'red'},
                     {'name': 'calm',  'src': 'red',   'dst': 'green'}]})
    print fsm.current # "none"
    fsm.init()
    print fsm.current # "green"

Of course, we have now come full circle, this last example pretty much
functions the same as the first example in this section where you simply
define your own startup event.

So you have a number of choices available to you when initializing your
state machine.

You can also indicate which state should be considered final.
This has no effect on the state machine, but lets you use a shorthand
method is_finished() that returns true if the state machine is in
this 'final' state:
::

    fsm = Fysom({'initial': 'green',
                 'final': 'red',
                 'events': [
                     {'name': 'panic', 'src': 'green', 'dst': 'red'},
                     {'name': 'calm',  'src': 'red',   'dst': 'green'}]})
    print fsm.current # "green"
    fsm.is_finished() # False
    fsm.panic()
    fsm.is_finished() # True


Dynamically generated event names
---------------------------------

Sometimes you have to compute the name of an event you want to trigger on the
fly. Instead of relying on `getattr` you can use the `trigger` method, which takes
a string (the event name) as a parameter, followed by any arguments/keyword
arguments you want to pass to the event method.
This is also arguably better if you're not sure if the event exists at all
(FysomError vs. AttributeError, see below).

::

    from fysom import Fysom

    fsm = Fysom({ 'initial': 'green',
                  'events': [
                      {'name': 'warn', 'src': 'green', 'dst': 'yellow'},
                      {'name': 'panic', 'src': 'yellow', 'dst': 'red'},
                      {'name': 'calm', 'src': 'red', 'dst': 'yellow'},
                      {'name': 'clear', 'src': 'yellow', 'dst': 'green'} ] })

    fsm.trigger('warn', msg="danger")  # equivalent to fsm.warn(msg="danger")
    fsm.trigger('unknown')  # FysomError, event does not exist
    fsm.unknown()  #  AttributeError, event does not exist


Multiple source and destination states for a single event
---------------------------------------------------------
::

    fsm = Fysom({'initial': 'hungry',
                 'events': [
                     {'name': 'eat', 'src': 'hungry', 'dst': 'satisfied'},
                     {'name': 'eat', 'src': 'satisfied', 'dst': 'full'},
                     {'name': 'eat', 'src': 'full', 'dst': 'sick'},
                     {'name': 'rest', 'src': ['hungry', 'satisfied', 'full', 'sick'], 'dst': 'hungry'}]})

This example will create an object with 2 event methods:

-  fsm.eat()
-  fsm.rest()

The rest event will always transition to the hungry state, while the eat
event will transition to a state that is dependent on the current state.

NOTE the rest event in the above example can also be specified as
multiple events with the same name if you prefer the verbose approach.

NOTE if an event can be triggered from any state, you can specify it
using the '*' wildcard, or even by omitting the src attribute from its
definition:
::

    fsm = Fysom({'initial': 'hungry',
                 'events': [
                     {'name': 'eat', 'src': 'hungry', 'dst': 'satisfied'},
                     {'name': 'eat', 'src': 'satisfied', 'dst': 'full'},
                     {'name': 'eat', 'src': 'full', 'dst': 'sick'},
                     {'name': 'eat_a_lot', 'src': '*', 'dst': 'sick'},
                     {'name': 'rest', 'dst': 'hungry'}]})

NOTE if an event will not change the current state, you can specify destination
using the '=' symbol. It's useful when using wildcard source or multiply sources:
::

    fsm = Fysom({'initial': 'hungry',
                 'events': [
                     {'name': 'eat', 'src': 'hungry', 'dst': 'satisfied'},
                     {'name': 'eat', 'src': 'satisfied', 'dst': 'full'},
                     {'name': 'eat', 'src': 'full', 'dst': 'sick'},
                     {'name': 'eat_a_little', 'src': '*', 'dst': '='},
                     {'name': 'eat_a_little', 'src': ['full', 'satisfied'], 'dst': '='},
                     {'name': 'eat_a_little', 'src': 'hungry', 'dst': '='},
                     {'name': 'rest', 'dst': 'hungry'}]})

Callbacks
---------

5 callbacks are available if your state machine has methods using the
following naming conventions:

-  onbefore\_event\_ - fired before the *event*
-  onleave\_state\_ - fired when leaving the old *state*
-  onenter\_state\_ - fired when entering the new *state*
-  onreenter\_state\_ - fired when reentering the old *state* (a reflexive transition i.e. src == dst)
-  onafter\_event\_ - fired after the *event*

You can affect the event in 2 ways:

-  return False from an onbefore\_event\_ handler to cancel the event.
   This will raise a fysom.Canceled exception.
-  return False from an onleave\_state\_ handler to perform an
   asynchronous state transition (see next section)

For convenience, the 2 most useful callbacks can be shortened:

-  on\_event\_ - convenience shorthand for onafter\_event\_
-  on\_state\_ - convenience shorthand for onenter\_state\_

In addition, a generic onchangestate() callback can be used to call a
single function for all state changes.

All callbacks will be passed one argument 'e' which is an object with
following attributes:

-  fsm Fysom object calling the callback
-  event Event name
-  src Source state
-  dst Destination state
-  (any other keyword arguments you passed into the original event
   method)
-  (any positional argument you passed in the original event method,
   in the 'args' attribute of the event)

Note that when you call an event, only one instance of 'e' argument is
created and passed to all 4 callbacks. This allows you to preserve data
across a state transition by storing it in 'e'. It also allows you to
shoot yourself in the foot if you're not careful.

Callbacks can be specified when the state machine is first created:
::

    def onpanic(e):
        print 'panic! ' + e.msg
    def oncalm(e):
        print 'thanks to ' + e.msg + ' done by ' + e.args[0]
    def ongreen(e):
        print 'green'
    def onyellow(e):
        print 'yellow'
    def onred(e):
        print 'red'
    fsm = Fysom({'initial': 'green',
                 'events': [
                     {'name': 'warn', 'src': 'green', 'dst': 'yellow'},
                     {'name': 'panic', 'src': 'yellow', 'dst': 'red'},
                     {'name': 'panic', 'src': 'green', 'dst': 'red'},
                     {'name': 'calm', 'src': 'red', 'dst': 'yellow'},
                     {'name': 'clear', 'src': 'yellow', 'dst': 'green'}],
                 'callbacks': {
                     'onpanic': onpanic,
                     'oncalm': oncalm,
                     'ongreen': ongreen,
                     'onyellow': onyellow,
                     'onred': onred }})

    fsm.panic(msg='killer bees')
    fsm.calm('bob', msg='sedatives in the honey pots')

Additionally, they can be added and removed from the state machine at any time:
::

    def printstatechange(e):
        print 'event: %s, src: %s, dst: %s' % (e.event, e.src, e.dst)

    del fsm.ongreen
    del fsm.onyellow
    del fsm.onred
    fsm.onchangestate = printstatechange

Asynchronous state transitions
------------------------------

Sometimes, you need to execute some asynchronous code during a state
transition and ensure the new state is not entered until you code has
completed.

A good example of this is when you run a background thread to download
something as result of an event. You only want to transition into the
new state after the download is complete.

You can return False from your onleave\_state\_ handler and the state
machine will be put on hold until you are ready to trigger the
transition using the transition() method.

Use as global machine
---------------------

To manipulating lots of objects with a small memory footprint, there
is a FysomGlobal class. Also a useful FysomGlobalMixin class to
give convenience access for the state machine methods.

A use case is using with Django, which has a cache mechanism holds
lots of model objects (database records) in memory, using global machine
can save a lot of memory, `here is a
compare <https://github.com/pytransitions/transitions/issues/146#issuecomment-325190021>`_.

The basic usage is same with Fysom, with slit difference and enhancement:

- Initial state will only be automatically triggered for class derived
  from FysomGlobalMixin. Or you need to trigger manually.
- The snake_case python naming conversion is supported.
- Conditions and conditional transitions are implemented.
- When an event/transition is canceled, the event object will be
  attached to the raised fysom.Canceled exception. By doing this,
  additional information can be passed through the exception.

Usage example:
::

    class Model(FysomGlobalMixin, object):
        GSM = FysomGlobal(
            events=[('warn',  'green',  'yellow'),
                    {
                        'name': 'panic',
                        'src': ['green', 'yellow'],
                        'dst': 'red',
                        'cond': [  # can be function object or method name
                            'is_angry',  # by default target is "True"
                            {True: 'is_very_angry', 'else': 'yellow'}
                        ]
                    },
                    ('calm',  'red',    'yellow'),
                    ('clear', 'yellow', 'green')],
            initial='green',
            final='red',
            state_field='state'
        )

        def __init__(self):
            self.state = None
            super(Model, self).__init__()

        def is_angry(self, event):
            return True

        def is_very_angry(self, event):
            return False

    obj = Model()
    obj.current  # 'green'
    obj.warn()
    obj.is_state('yellow')  # True
    # conditions and conditional transition
    obj.panic()
    obj.current  # 'yellow'
    obj.is_finished()  # False