def test_iots_traces(self): """ 测试标记迁移系统 s0 -?act0-> s1, s0 -!act1> s2, s1 -act2-> s3 是否可以正常变成图对象 """ state0 = State("s0") state1 = State("s1") state2 = State("s2") state3 = State("s3") action0 = Action("act0") action1 = Action("act1") action2 = Action("act2") transition0 = Transition(state0, action0, state1) transition1 = Transition(state0, action1, state2) transition2 = Transition(state1, action2, state3) iots = IOTS(state0, [state0, state1, state2, state3], [action0, action1, action2], [action0], [action1], [transition0, transition1, transition2]) ts_graph = iots_to_graph(iots) traces = iots_traces(ts_graph) print(traces) self.assertEqual(True, True)
def add_state(self, state_name): if self.states: return self.states.setdefault(state_name, State(state_name)) else: self.initial_state = State(state_name) self.states[state_name] = self.initial_state return self.initial_state
def test_lts_print(self): """ 用来验证lts格式化输出时是否正常 :return: """ state_a = State("a") state_b = State("b") state_c = State("c") init_state = state_a states = [state_a, state_b, state_c] act1 = Action("act1") act2 = Action("act2") actions = [act1, act2] hide_actions = [] transitions = [ Transition(state_a, act1, state_b), Transition(state_a, act2, state_c) ] lts = LTS(init_state, states, actions, hide_actions, transitions) print(lts) self.assertEqual(True, True)
def test_add_transiction_to_state(): initial_state = State('Idle') target = State('Target') event = Event('D1CL', 'Door closed') initial_state.add_transition(event, target) assert len(initial_state.transitions) == 1 assert initial_state.transitions[event.code] == target
def test_state_machine(): idle = State('idle') active = State('active') d1cl = Event('D1CL', 'Door 1 closed') idle.add_transition(d1cl, active) sm = StateMachine(idle) assert sm.start == sm.current_state == idle assert sm.all_states() == set([idle, active]) sm.handle('D1CL') assert sm.current_state == active
def play(self, event): if not State.result and Board.playables[ self.position[1]] == self.position[0]: self.itemconfig(self.circle, fill=Board.colourToPlay()) State.playMove(self.position) Board.playables[self.position[1]] -= 1 if State.findLongestLine(State.state, self.position) == 4: State.result = 4 else: print(self.position)
def state_machine(request): idle = State('Idle') a = State('A') b = State('B') idle.add_transition(Event('EVT1', 'Primer evento'), a) a.add_transition(Event('EVT2', 'Segundo evento'), b) sm = StateMachine(idle) return sm
def add_state(self, state_name): s = State(state_name) if not self.states: self.states = [s] self.state_machine = StateMachine(s) else: self.states.append(s)
def __init__(self, config, parent=None): QtWidgets.QSystemTrayIcon.__init__(self, parent) self.config = config self.state = State() icon_path = resource_path(f'assets/{Icon[self.state.status]}.png') self.setIcon(QtGui.QIcon(icon_path)) menu = QtWidgets.QMenu(parent) label_action = menu.addAction("Pyraficator-tray") label_action.triggered.connect(partial(SystemTrayIcon.open_in_browser, self, WEB_URL)) menu.addSeparator() self.status_action = menu.addAction(f"Status: {Status.Unknown}") menu.addSeparator() self.error_menu = menu.addMenu(f"Failed items") self.in_progress_menu = menu.addMenu(f"Items in progress") self.success_menu = menu.addMenu(f"Succeeded items") menu.addSeparator() exit_action = menu.addAction("Exit") exit_action.triggered.connect(self.exit) self.setContextMenu(menu) self.timer = QtCore.QTimer(self) self.timer.setInterval(config.refreshInterval) self.timer.timeout.connect(self.refresh_state) self.timer.start()
def __init__(self, handlers: List[MessageHandler], end_block: EndBlockHandler): self.sequence = 0 self.private_key, self.public_key = create_key_pair() self.state = State(tuple(), tuple(), None, None, 0) self.handlers: Dict[type, MessageHandler] = {h.message_type: h for h in handlers} self.end_block_handler = end_block
def __init__(self, name, *terminal_names, **init_state): self.name = name self.terminals = [Terminal(self, name) for name in terminal_names] self.states = [ State(self, name, val) for name, val in init_state.items() ] for term in self.terminals: setattr(self, term.name, term) for state in self.states: setattr(self, state.name, state)
def end_block(self, state: State, req) -> Tuple[State, ResponseEndBlock]: if req.height == state.blow_up_height: losses = list(state.losses) losses[state.potato_holder] += 1 losses = tuple(losses) blowup = state.blow_up_height + self.blow_up_inc elif state.blow_up_height is not None and state.blow_up_height < 0: # New game losses = state.losses blowup = state.last_block_height + self.blow_up_inc else: losses = state.losses blowup = state.blow_up_height return State(state.players, losses, state.potato_holder, blowup, req.height), ResponseEndBlock()
def test_lts_to_tsgrah(self): """ 测试标记迁移系统 s0 -act0-> s1, s0 -act1> s2, s1 -act2-> s3 是否可以正常变成图对象 """ state0 = State("s0") state1 = State("s1") state2 = State("s2") state3 = State("s3") action0 = Action("act0") action1 = Action("act1") action2 = Action("act2") transition0 = Transition(state0, action0, state1) transition1 = Transition(state0, action1, state2) transition2 = Transition(state1, action2, state3) lts = LTS(state0, [state0, state1, state2, state3], [action0, action1, action2], [transition0, transition1, transition2]) lts_graph = lts_to_graph(lts) self.assertEqual(True, True)
def sts_parser(file_name: str) -> STS: """ 根据*.ts文件中描述的输入输出迁移系统,生成对应的STS对象 :param file_name: .ts结尾的文件 :return: STS对象 """ init_s = parse_initial_state(file_name) s = parse_states(file_name) a = parse_actions(file_name) t = parse_transitions(file_name) # 构造所有状态对象 states = [State(name) for name in s] init_state = [s for s in states if s.state_name == init_s][0] in_actions = a[0] out_actions = a[1] hide_actions = a[2] # 输入动作、输出动作、内部动作对象 outs = [Action(name) for name in out_actions] ins = [Action(name) for name in in_actions] hides = [Action(name) for name in hide_actions] # 所有动作的对象 all_actions = outs + ins + hides # 动作名到动作的一个映射 act_map = dict() for act in all_actions: act_map[act.action_name] = act # 状态名到状态对象的一个映射 state_map = dict() for s in states: state_map[s.state_name] = s # 构造所有迁移对象集合 transitions = list() for first_state, act, second_state in t: transitions.append( Transition(state_map[first_state], act_map[act], state_map[second_state])) secure_level = parse_secure_level_with_sts(file_name) return STS(init_state, states, all_actions, ins, outs, transitions, secure_level)
def deliver_tx(self, state: State, message: Message) -> Tuple[State, ResponseDeliverTx]: assert type(message.data) is TossPotato if message.sender != state.players[state.potato_holder]: return state, ResponseDeliverTx(code=1, info='Not holding the potato') if message.data.receiver not in state.players: return state, ResponseDeliverTx(code=1, info='Target player does not exist') idx = state.players.index(message.data.receiver) return State( state.players, state.losses, idx, state.blow_up_height, state.last_block_height ), ResponseDeliverTx(code=0)
def deliver_tx(self, state: State, message: Message) -> Tuple[State, ResponseDeliverTx]: if message.sender in state.players: return state, ResponseDeliverTx(code=1, info='Already playing') if len(state.players) == 1: # We have enough players to start blowup = -1 holder = 1 else: blowup = state.blow_up_height holder = state.potato_holder return State( state.players + (message.sender, ), state.losses + (0, ), holder, blowup, state.last_block_height, ), ResponseDeliverTx(code=0)
def _transition_list_iots(trans_list: List) -> IOTS: if not trans_list: return old_init_state = trans_list[0][0] ins = set() outs = set() acts = set() states = set() for s1, act, s2 in trans_list: if act.action_type == ActionEnum.INPUT: ins.add(act.action_name) elif act.action_type == ActionEnum.OUTPUT: outs.add(act.action_name) acts.add(act.action_name) states.add(s1.state_name) states.add(s2.state_name) acts_map = dict() states_map = dict() states = [State(s) for s in list(states)] acts = [Action(a) for a in list(acts)] for s in states: states_map[s.state_name] = s for a in acts: acts_map[a.action_name] = acts new_init_state = states_map[old_init_state.state_name] outs = [acts_map[a] for a in list(outs)] ins = [acts_map[a] for a in list(ins)] transitions = list() for s1, act, s2 in trans_list: t = Transition(states_map[s1.state_name], acts_map[act.action_name], states_map[s2.state_name]) transitions.append(t) return IOTS(new_init_state, states, acts, ins, outs, transitions)
def lts_parser(file_name: str) -> LTS: """ 根据*.ts文件中描述的输入输出迁移系统,生成对应的LTS对象 :param file_name: .ts结尾的文件 :return: LTS对象 """ init_s = parse_initial_state(file_name) s = parse_states(file_name) a = parse_actions(file_name) t = parse_transitions(file_name) # 构造所有状态对象 states = [State(name) for name in s] init_state = [s for s in states if s.state_name == init_s][0] actions = a[0] + a[1] hide_actions = a[-1] # 所有可观察动作的对象 acts = [Action(name) for name in actions] hide_acts = [Action(name) for name in hide_actions] # 动作名到动作的一个映射 act_map = dict() for act in (acts + hide_acts): act_map[act.action_name] = act # 状态名到状态对象的一个映射 state_map = dict() for s in states: state_map[s.state_name] = s # 构造所有迁移对象集合 transitions = list() for first_state, act, second_state in t: transitions.append( Transition(state_map[first_state], act_map[act], state_map[second_state])) return LTS(init_state, states, acts, hide_acts, transitions)
#!/usr/bin/env python3 """ 该模块用来构建一些简单的迁移系统对象常量,方便进行测试程序的正确性 """ from core import Action from core import State from core import Transition from ts import IOTS """ SIMPLE_IOTS 表示如下迁移系统: s0->act1?->s1 s1->act2!->s2 s1->act3!->s3 """ s0, s1, s2, s3 = (State(s) for s in ("s0", "s1", "s2", "s3")) act1, act2, act3 = (Action(a) for a in ("act1", "act2", "act3")) states = [s0, s1, s2, s3] actions = [act1, act2, act3] ins = [act1] outs = [act2, act3] transitions = [ Transition(s0, act1, s1), Transition(s1, act2, s2), Transition(s1, act3, s3) ] SIMPLE_IOTS_TEST = IOTS(s0, states, actions, ins, outs, transitions)
def test_state_with_description_as_string(): s = State('Idle', 'En espera') assert str(s) == 'Idle (En espera)'
def test_state_as_string_with_desc(): s = State('idle', 'Estado de reposo') assert str(s) == 'idle (Estado de reposo)'
#!/usr/bin/env python # -*- coding: utf-8 -*- from core import State, Event, NetworkBus, StateMachine # States idle = State('idle', 'En espera') active = State('active', 'Activo') key1 = State('k1', 'keyboard key 1 pressed') key2 = State('k2', 'keyboard key 2 pressed') # Events door_closed = Event('D1CL', 'Door closed') key_one_pressed = Event('KEY1', 'Key 1 pressed') key_two_pressed = Event('KEY2', 'Key 2 pressed') open_batcave = Event('OPEN', 'Open batcave') # Transitions idle.add_transition(door_closed, active) active.add_transition(key_one_pressed, key1) key1.add_transition(key_two_pressed, key2) key2.add_action(open_batcave) key2.add_transition(open_batcave, idle) sm = StateMachine(idle) print('All states:', ', '.join([_.name for _ in sm.all_states()])) bus = NetworkBus() bus.subscribe(sm)
def test_create_state(): s = State('Idle') assert s.name == 'Idle' assert s.actions == [] assert len(s.transitions) == 0
def test_state_as_string(): s = State('idle') assert str(s) == 'idle (Idle state)'