首页 技术 正文
技术 2022年11月12日
0 收藏 490 点赞 3,225 浏览 4252 个字

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- single source 'message type' for state transition changes
- message type considered, messages (comment) not considered to avoid complexity
"""class UnsupportedMessageType(BaseException):
passclass UnsupportedState(BaseException):
passclass UnsupportedTransition(BaseException):
passclass 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 UnsupportedMessageTypeclass 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 UnsupportedTransitionclass 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 ignoredclass Failed(OutOfService):
'''No need to override any method.''' def __init__(self, HierachicalStateMachine):
self._hsm = HierachicalStateMachine

Python转载版

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844