def setup_logging(fsm_obj, events=(), actions=(), force=False): """ """ def report(*p): print " ".join(p) def s_report(evt, *p): print evt.state_name, " ".join(p) # states... for e in events: event.bind(getattr(fsm_obj, e), rcurry(s_report, "...")) # TODO move this to the aspect module... # NOTE: this will log only explicit external (through the object) # method access.... def wrap_meth(obj, meth_name, pre=None, post=None, force=False): """ """ if not force and meth_name in vars(obj): raise TypeError, "will not overwrite object data." def wrapper(*p, **n): """ """ if pre != None: pre(obj, meth_name, *p, **n) res = getattr(super(obj.__class__, obj), meth_name)(*p, **n) if post != None: return post(obj, meth_name, res, *p, **n) return res setattr(obj, meth_name, wrapper) # actions... for m in actions: wrap_meth(fsm_obj, m, pre=lambda obj, meth, *p, **n: report("\n-->", meth, *[str(i) for i in p]), force=force)
fsm.transition(A, C, mode=fsm.MANUAL) fsm.transition(B, C, condition=pred_B2C) fsm.transition(B, B, condition=pred_B2B) print 'creating an fsm instance...' mfsm = FSM() print print 'preparing events:' print 'defining handler...' def f(evt): print ' ****', evt.__class__.__name__, ':', evt.state_name print 'binding events...' event.bind(mfsm.onEnterA, f) event.bind(mfsm.onExitA, f) event.bind(mfsm.onEnterB, f) event.bind(mfsm.onExitB, f) event.bind(mfsm.onEnterC, f) event.bind(mfsm.onExitC, f) print # start the FSM... if mfsm.__auto_change_state__: print 'starting the fsm...' # NOTE: if we did not have (or would not reach) a terminal # state this will never exit... mfsm.start()
def start(self): ''' start work... ''' print 'starting:', self event.bind(onPing, self.handler)