oxplot/fysom — pull request #2 by foobarto (2012-06-11 11:58:12+00:00)
Title: Per event+state callbacks and multiple dst event choices

Hey,

Have a peek at my changes to fysom.
Event callbacks can be assigned to specific event+src patterns - use "callback" key in the events dictionary and give reference to a function as a value.
It is possible now to have multiple dst states. This requires providing a "decision" key to the events dictionary where value must be a reference to a function that will return string with name of the target state. Thanks to this it is now possible to add logic which will allow for more advanced FSMs.

Example:

import random

def is_lamp_ok(e):
# pick randomly, flip a coin and return one of the states
# this function must return string with name of the dst state
return e.dst[random.randint(0, len(e.dst)-1)]

def lamp_callback(e):
print "This callback happens on switch event but only from on to off state."

def onchangestate(e):
print "Changing from", e.src, "to", e.dst

def onswitch(e):
print "Catch all switch events. State from ", e.src, "to", e.dst

d= {
"initial": "off",
"events": [
{"name": "switch", "src": "off", "dst": ["on", "broken"], "decision": is_lamp_ok},
{"name": "switch", "src": "on", "dst": "off", "callback": lamp_callback},
{"name": "breakit", "src": ["on", "off"], "dst": "broken"},
{"name": "fixit", "src": "broken", "dst": "off"},
{"name": "switch", "src": "broken", "dst": "broken"},
],
"callbacks":
{
"onchangestate": onchangestate,
"onswitch": onswitch,
}
}