class StateMachine: def __init__(self): self.handlers = {} # 狀態轉移函數字典 self.startState = None # 初始狀態 self.endState = [] # 最終狀態集合 # 參數name爲狀態名,handler爲狀態轉移函數,end_state代表是否爲最終狀態 def add_state(self, name, handler, end_state=0): name = name.upper() self.handlers[name] = handler if end_state: self.endState.append(name) def set_start(self, name): self.startState = name.upper() def run(self, cargo): try: handler = self.handlers[self.startState] except: raise Exception("must call .set_start() before .run()") if not self.endState: raise Exception("at least one state must be an end_state") while True: (newState, cargo) = handler(cargo) if newState.upper() in self.endState: print("reached ", newState) break else: handler = self.handlers[newState.upper()] # 有限狀態集合 positive_adjectives = ["great", "super", "fun", "entertaining", "easy"] negative_adjectives = ["boring", "difficult", "ugly", "bad"] # 自定義狀態轉變函數 def start_transitions(txt): # 用指定分隔符對字符串進行切片,默認爲空格分割,參數num指定分割次數 # 將"Python is XXX"語句分割爲"Python"和以後的"is XXX" splitted_txt = txt.split(None, 1) word, txt = splitted_txt if len(splitted_txt) > 1 else (txt, "") if word == "Python": newState = "Python_state" # 若是第一個詞是Python則可轉換到"Python狀態" else: newState = "error_state" # 若是第一個詞不是Python則進入終止狀態 return (newState, txt) # 返回新狀態和餘下的語句txt def python_state_transitions(txt): splitted_txt = txt.split(None, 1) word, txt = splitted_txt if len(splitted_txt) > 1 else (txt, "") if word == "is": newState = "is_state" else: newState = "error_state" return (newState, txt) def is_state_transitions(txt): splitted_txt = txt.split(None, 1) word, txt = splitted_txt if len(splitted_txt) > 1 else (txt, "") if word == "not": newState = "not_state" elif word in positive_adjectives: newState = "pos_state" elif word in negative_adjectives: newState = "neg_state" else: newState = "error_state" return (newState, txt) def not_state_transitions(txt): splitted_txt = txt.split(None, 1) word, txt = splitted_txt if len(splitted_txt) > 1 else (txt, "") if word in positive_adjectives: newState = "neg_state" elif word in negative_adjectives: newState = "pos_state" else: newState = "error_state" return (newState, txt) if __name__ == "__main__": m = StateMachine() m.add_state("Start", start_transitions) # 添加初始狀態 m.add_state("Python_state", python_state_transitions) m.add_state("is_state", is_state_transitions) m.add_state("not_state", not_state_transitions) m.add_state("neg_state", None, end_state=1) # 添加最終狀態 m.add_state("pos_state", None, end_state=1) m.add_state("error_state", None, end_state=1) m.set_start("Start") # 設置開始狀態 m.run("Python is great") m.run("Python is not fun") m.run("Perl is ugly") m.run("Pythoniseasy")
摘自 有限狀態機(Python)html