道林.格雷 : "你喜欢什么类型的音乐 What sort of music would you like?"
温妮莎·艾芙斯 : "看这一刻我想成为什么样的人 Meaning who do I want to be at this moment."
FSM
有限状态机 Finite State Machine,又称有限状态自动机Finite State Automaton,又称有限自动机Finite Automaton,简称状态机 State Machine。简单的讲,状态机描述的是:有限个状态以及状态迁移过程中条件、事件、动作之间关系的一种数学模型。状态机可以分为很多种类型:
-
Acceptors and recognizers
- 原理:产生一个二元输出,说要么“是”要么“否”来回答输入是否被机器接受。所有FSM的状态被称为要么接受要么不接受。在所有输入都被处理了的时候,如果当前状态是接受状态,输入被接受,否则被拒绝。
-
Classifiers
- 原理:和Acceptor类似,但是有超过二元以上的输出。
-
Transducers
- 原理:基于输入的动作和状态决定下一个状态。
- Others
状态机的应用范围很广:分布式数据一致性(Paxos,Raft)、Akka框架、神经网络的动态模型、语言识别器。
业务开发中用得比较多的是Transducer模式,用来管理事件和状态之间的迁移关系,与业务代码解耦合。
Mathematical model
Transducer的数学模型是一个六元组 :
-
是输入字母表(符号的非空有限集合)。
-
是输出字母表(符号的非空有限集合)。
- S 是状态的非空有限集合。
- s0 是初始状态,它是S的元素。在非确定有限状态自动机中,是初始状态的集合。
-
是状态转移函数:
。
-
是输出函数。
Code
使用Python编写一个简单的FSM框架(Python用得还不太6,看官请见谅)。
State
class State(object):
def __init__(self, name):
if name is None:
raise RuntimeError('State name is none')
self.name = name
Event
class Event(object):
def __init__(self, name):
self.name = name
self.transitions = []
def add_transition(self, transition):
self.transitions.append(transition)
def get_transition(self, state):
for transition in self.transitions:
if transition.sourceState.name == state.name:
return transition
Transition
class Transition(object):
def __init__(self, sourceState, targetState, event):
self.sourceState = sourceState
self.targetState = targetState
self.event = event
Machine
class Machine(object):
def __init__(self, name, initialState, transitions):
self.name = name
self.initialState = initialState
self.currentState = initialState
self.transitions = transitions
self.states = []
self.init_states()
self.events = []
self.init_events()
def init_states(self):
for transition in self.transitions:
self.states.append(transition.sourceState)
self.states.append(transition.targetState)
def init_events(self):
for transition in self.transitions:
event = transition.event
event.add_transition(transition)
self.events.append(event)
logger.info("event: %s", event.name)
def get_event(self, name):
for event in self.events:
if event.name == name:
return event
def transit(self, event):
cevent = self.get_event(event)
transition = cevent.get_transition(self.currentState)
self.currentState = transition.targetState
print ("transit from %s to %s on %s " % (
transition.sourceState.name, transition.targetState.name, transition.event.name))
if __name__ == "__main__":
t1 = Transition(State('CLOSED'), State('OPENED'), Event('open'))
t2 = Transition(State('OPENED'), State('HEATED'), Event('heat'))
t3 = Transition(State('HEATED'), State('CLOSED'), Event('cool'))
transitions = []
transitions.append(t1)
transitions.append(t2)
transitions.append(t3)
machine = Machine('Coffee Machine No.1', State('CLOSED'), transitions)
machine.transit('open')
machine.transit('heat')
machine.transit('cool')
Penny Dreadful - S01E06
你喜欢什么类型的音乐 What sort of music would you like?
看这一刻我想成为什么样的人 Meaning who do I want to be at this moment.
道林.格雷,你想成为什么样的人 Dorian Gray,who do you want to be?
我自己,不受限制的自己 Myself,without limits.
那就放舞曲吧 Then put on music for dancing.
你还真是异常的冷靜 You have exceptional composure.
是吗 Do I?
我是说,镇静 Poise, I mean.
自制力 Control.
对 Yes.
如果没了自制力,你会怎样 What if you were to abandon it?
不可能的 I couldn't.
为什么 Why?
我们内心总有一些东西不会被释放出来 There are things within us all that can never be unleashed.
如果释放了会怎样 And what would happen if they were?
他们会吞噬我们 They would consume us.
我们将不复存在,不受控制的自我会取而代之 We would cease to be and another would exist in our place, without control.
无法无天 Without limits.