2021-2022-1 20211420《信息安全专业导论》 python模拟进程状态

参考博客

代码实现模拟进程(码云

def InitializationError(param):
    pass


class Statestart:
    def __init__(self):
        self.handlers = {}  # 状态转移函数字典
        self.startState = None  # 初始状态
        self.endStates = []  # 最终状态集合

    # 参数name为状态名,handler为状态转移函数,end_state表明是否为最终状态
    def add_state(self, name, handler, end_state=0):
        name = name.upper()  # 转换为大写
        self.handlers[name] = handler
        if end_state:
            self.endStates.append(name)

    def set_start(self, name):
        self.startState = name.upper()

    def run(self, appgo):
        try:
            handler = self.handlers[self.startState]
        except:
            raise InitializationError("must call .set_start() before .run()")
        if not self.endStates:
            raise InitializationError("at least one state must be an end_state")

        # 从Start状态开始进行处理
        while True:
            (newState, appgo) = handler(appgo)  # 经过状态转移函数变换到新状态
            if newState.upper() in self.endStates:  # 如果跳到终止状态,则打印状态并结束循环
                print("reached ", newState)
                break
            else:  # 否则将转移函数切换为新状态下的转移函数
                handler = self.handlers[newState.upper()]

# 有限状态集合
positive_adjectives = ["new", "ready", "running", "waiting", "terminated"]
negative_adjectives = ["unnew", "unready", "unrunning", "unwaiting", "unterminated"]


# 自定义状态转变函数
def start_transitions(txt):
    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 and word == "new":
        newState = "pos_state"
    elif word in positive_adjectives and word == "ready":
        newState = "pos_state"
    elif word in positive_adjectives and word == "running":
        newState = "pos_state"
    elif word in positive_adjectives and word == "waiting":
        newState = "pos_state"
    elif word in positive_adjectives and word == "terminated":
        newState = "pos_state"
    elif word in negative_adjectives and word == "unnew":
        newState = "neg_state"
    elif word in negative_adjectives and word == "unready":
        newState = "neg_state"
    elif word in negative_adjectives and word == "unrunning":
        newState = "neg_state"
    elif word in negative_adjectives and word == "unwaiting":
        newState = "neg_state"
    elif word in negative_adjectives and word == "unterminated":
        newState = "neg_state"
    else:
        newState = "error_state"
    if word == "not":
        pass
    elif newState == "error_state":
        print('程序状态错误')
    else:
        print("程序状态转换至",word)
    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 and word == "new":
        newState = "neg_state"
    elif word in positive_adjectives and word == "ready":
        newState = "neg_state"
    elif word in positive_adjectives and word == "running":
        newState = "neg_state"
    elif word in positive_adjectives and word == "waiting":
        newState = "neg_state"
    elif word in positive_adjectives and word == "terminated":
        newState = "neg_state"
    elif word in negative_adjectives and word == "unnew":
        newState = "pos_state"
    elif word in negative_adjectives and word == "unready":
        newState = "pos_state"
    elif word in negative_adjectives and word == "unrunning":
        newState = "pos_state"
    elif word in negative_adjectives and word == "unwaiting":
        newState = "pos_state"
    elif word in negative_adjectives and word == "unterminated":
        newState = "pos_state"
    else:
        newState = "error_state"
    if word in negative_adjectives:
        print("程序状态转换至",word)
    elif newState == "error_state":
        print('程序状态错误')
    else:
        print("程序状态转换至 not",word)
    return (newState, txt)


if __name__ == "__main__":
    m = Statestart()
    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)

    option = input("请输入是否执行程序(Yes执行/输入其他内容终止程序):")
    while option == "Yes":
        m.set_start("Start")  # 设置开始状态
        state = input("请输入程序状态(new, ready, running, waiting, terminated或加前缀un)(例如:Python is xxx或Python is not xxx):")
        m.run(state)
        option = input("请输入是否执行程序(Yes执行/输入其他内容终止程序):")
    else:
        print("程序终止")

运行效果

posted @ 2021-11-22 13:47  シバ鳥  阅读(28)  评论(0编辑  收藏  举报