抽象的藝術-狀態機

浩哥001發表於2017-03-15

道林.格雷 : “你喜歡什麼型別的音樂 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的狀態被稱為要麼接受要麼不接受。在所有輸入都被處理了的時候,如果當前狀態是接受狀態,輸入被接受,否則被拒絕。
    • 606px-Fsm_parsing_word_nice.svg.png
  • Classifiers

    • 原理:和Acceptor類似,但是有超過二元以上的輸出。
  • Transducers

    • 原理:基於輸入的動作和狀態決定下一個狀態。
    • Fsm_Moore_model_door_control.svg.png
  • Others

狀態機的應用範圍很廣:分散式資料一致性(Paxos,Raft)、Akka框架、神經網路的動態模型、語言識別器。

業務開發中用得比較多的是Transducer模式,用來管理事件和狀態之間的遷移關係,與業務程式碼解耦合。

Mathematical model

Transducer的數學模型是一個六元組 :screenshot.png

  • screenshot.png 是輸入字母表(符號的非空有限集合)。
  • screenshot.png 是輸出字母表(符號的非空有限集合)。
  • S 是狀態的非空有限集合。
  • s0 是初始狀態,它是S的元素。在非確定有限狀態自動機中,是初始狀態的集合。
  • screenshot.png 是狀態轉移函式: screenshot.png
  • screenshot.png 是輸出函式。

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.


相關文章