Python版
https://github.com/faif/python-patterns/blob/master/other/hsm/hsm.py
"""
Implementation of the HSM (hierarchical state machine) or
NFSM (nested finite state machine) C++ example from
http://www.eventhelix.com/RealtimeMantra/HierarchicalStateMachine.htm#.VwqLVEL950w
in Python
class UnsupportedMessageType(BaseException):
pass
class UnsupportedState(BaseException):
pass
class UnsupportedTransition(BaseException):
pass
class HierachicalStateMachine(object):
def \_\_init\_\_(self):
self.\_active\_state = Active(self) # Unit.Inservice.Active()
self.\_standby\_state = Standby(self) # Unit.Inservice.Standby()
self.\_suspect\_state = Suspect(self) # Unit.OutOfService.Suspect()
self.\_failed\_state = Failed(self) # Unit.OutOfService.Failed()
self.\_current\_state = self.\_standby\_state
self.states = {'active': self.\_active\_state,
'standby': self.\_standby\_state,
'suspect': self.\_suspect\_state,
'failed': self.\_failed\_state}
self.message\_types = {'fault trigger': self.\_current\_state.on\_fault\_trigger,
'switchover': self.\_current\_state.on\_switchover,
'diagnostics passed': self.\_current\_state.on\_diagnostics\_passed,
'diagnostics failed': self.\_current\_state.on\_diagnostics\_failed,
'operator inservice': self.\_current\_state.on\_operator\_inservice}
def \_next\_state(self, state):
try:
self.\_current\_state = self.states\[state\]
except KeyError:
raise UnsupportedState
def \_send\_diagnostics\_request(self):
return 'send diagnostic request'
def \_raise\_alarm(self):
return 'raise alarm'
def \_clear\_alarm(self):
return 'clear alarm'
def \_perform\_switchover(self):
return 'perform switchover'
def \_send\_switchover\_response(self):
return 'send switchover response'
def \_send\_operator\_inservice\_response(self):
return 'send operator inservice response'
def \_send\_diagnostics\_failure\_report(self):
return 'send diagnostics failure report'
def \_send\_diagnostics\_pass\_report(self):
return 'send diagnostics pass report'
def \_abort\_diagnostics(self):
return 'abort diagnostics'
def \_check\_mate\_status(self):
return 'check mate status'
def on\_message(self, message\_type): # message ignored
if message\_type in self.message\_types.keys():
self.message\_types\[message\_type\]()
else:
raise UnsupportedMessageType
class Unit(object):
def \_\_init\_\_(self, HierachicalStateMachine):
self.hsm = HierachicalStateMachine
def on\_switchover(self):
raise UnsupportedTransition
def on\_fault\_trigger(self):
raise UnsupportedTransition
def on\_diagnostics\_failed(self):
raise UnsupportedTransition
def on\_diagnostics\_passed(self):
raise UnsupportedTransition
def on\_operator\_inservice(self):
raise UnsupportedTransition
class Inservice(Unit):
def \_\_init\_\_(self, HierachicalStateMachine):
self.\_hsm = HierachicalStateMachine
def on\_fault\_trigger(self):
self.\_hsm.\_next\_state('suspect')
self.\_hsm.\_send\_diagnostics\_request()
self.\_hsm.\_raise\_alarm()
def on\_switchover(self):
self.\_hsm.\_perform\_switchover()
self.\_hsm.\_check\_mate\_status()
self.\_hsm.\_send\_switchover\_response()
class Active(Inservice):
def \_\_init\_\_(self, HierachicalStateMachine):
self.\_hsm = HierachicalStateMachine
def on\_fault\_trigger(self):
super(Active, self).perform\_switchover()
super(Active, self).on\_fault\_trigger()
def on\_switchover(self):
self.\_hsm.on\_switchover() # message ignored
self.\_hsm.next\_state('standby')
class Standby(Inservice):
def \_\_init\_\_(self, HierachicalStateMachine):
self.\_hsm = HierachicalStateMachine
def on\_switchover(self):
super(Standby, self).on\_switchover() #message ignored
self.\_hsm.\_next\_state('active')
class OutOfService(Unit):
def \_\_init\_\_(self, HierachicalStateMachine):
self.\_hsm = HierachicalStateMachine
def on\_operator\_inservice(self):
self.\_hsm.on\_switchover() # message ignored
self.\_hsm.send\_operator\_inservice\_response()
self.\_hsm.next\_state('suspect')
class Suspect(OutOfService):
def \_\_init\_\_(self, HierachicalStateMachine):
self.\_hsm = HierachicalStateMachine
def on\_diagnostics\_failed(self):
super(Suspect, self).send\_diagnostics\_failure\_report()
super(Suspect, self).next\_state('failed')
def on\_diagnostics\_passed(self):
super(Suspect, self).send\_diagnostics\_pass\_report()
super(Suspect, self).clear\_alarm() # loss of redundancy alarm
super(Suspect, self).next\_state('standby')
def on\_operator\_inservice(self):
super(Suspect, self).abort\_diagnostics()
super(Suspect, self).on\_operator\_inservice() # message ignored
class Failed(OutOfService):
'''No need to override any method.'''
def \_\_init\_\_(self, HierachicalStateMachine):
self.\_hsm = HierachicalStateMachine
Python转载版
手机扫一扫
移动阅读更方便
你可能感兴趣的文章