def __init__(self): self.m = StateMachine() #Declaration of all the states self.m.add_state("StateA", self.StateA_transitions) self.m.add_state("StateB", self.StateB_transitions) self.m.add_state("StateC", self.StateC_transitions) self.m.set_start("StateA")
def __init__(self, parent=None, tempControllerAddress=None, interfaceLock=None, **options): tk.Frame.__init__(self, parent) self.tempControllerAddress = tempControllerAddress self.sm = StateMachine(self, tempControllerAddress, interfaceLock) self.pack(**options) self.makeWidgets()
def __init__(self): self.screen = pygame.display.get_surface() self.gamestate = StateMachine() self.data = None self.video = Video() self.audio = Audio(self) self.running = False self.all_maps_loaded = False self.force_bg_music = False self.clock = pygame.time.Clock() self.playtime = 0.0 self.dt = 0.0 self.key_timer = 0.0 self.state_timer = 0.0 self.debugfont = pygame.font.SysFont(DEBUGFONT, DEBUGFONTSIZE) self.key_input = None self.mouse_input = None self.show_debug = False self.debug_mode = False self.fps = FPS threading.Thread(target=self.load_all_maps).start()
def __init__(self): self.stateMachine = StateMachine() self.stateMachine.add_state("NEW", self._new_file) self.stateMachine.add_state("SAM", self._sam) self.stateMachine.add_state("BLOCK", self._block) self.stateMachine.add_state("CODEBLOCK-START", self._codeblock_start) self.stateMachine.add_state("CODEBLOCK", self._codeblock) self.stateMachine.add_state("PARAGRAPH-START", self._paragraph_start) self.stateMachine.add_state("PARAGRAPH", self._paragraph) self.stateMachine.add_state("RECORD-START", self._record_start) self.stateMachine.add_state("RECORD", self._record) self.stateMachine.add_state("LIST-ITEM", self._list_item) self.stateMachine.add_state("NUM-LIST-ITEM", self._num_list_item) self.stateMachine.add_state("BLOCK-INSERT", self._block_insert) self.stateMachine.add_state("END", None, end_state=1) self.stateMachine.set_start("NEW") self.current_paragraph = None self.doc = DocStructure() self.source = None self.patterns = { 'comment': re.compile(r'\s*#.*'), 'block-start': re.compile(r'(\s*)([a-zA-Z0-9-_]+):(?:\((.*?)\))?(.*)'), 'codeblock-start': re.compile(r'(\s*)```(.*)'), 'codeblock-end': re.compile(r'(\s*)```\s*$'), 'paragraph-start': re.compile(r'\w*'), 'blank-line': re.compile(r'^\s*$'), 'record-start': re.compile(r'\s*[a-zA-Z0-9-_]+::(.*)'), 'list-item': re.compile(r'(\s*)(\*\s+)(.*)'), 'num-list-item': re.compile(r'(\s*)([0-9]+\.\s+)(.*)'), 'block-insert': re.compile(r'(\s*)>>\(.*?\)\w*') }
def test_hooks_order(self): result = [] def hook(s): def h(): result.append(s) return h sm = StateMachine() sm.add( State('online', is_starting_state=True, will_exit=hook('onwex'), exited=hook('onex'))) sm.add(State('offline', will_enter=hook('ofwen'), entered=hook('ofen'))) sm.add( Transition('online', ['offline'], before=hook('tb'), after=hook('ta'))) sm.transition_to('offline') assert result == ['tb', 'onwex', 'ofwen', 'onex', 'ofen', 'ta']
def hard_instance() -> StateMachine: ''' Creates an MDP that is as hard as possible for VI to find a good approximation. The MDP should be built on top of the StateMachine framework available in the file statemachine.py The restrictions are as follows: 1. The state machine contains six states, named s1 to s6. The initial state is s1. Only one action, a1, is available in s6; this action costs 0 and loops on state s6. 2. The cost of any action is always in the interval [0,1]. 3. The probability of a transition from one state to another is either 0 or at least 0.1. ''' # DONE transition_functions = [ SMTransition("s6", "a1", 0, [("s6", 1)]), SMTransition("s1", "a2", 1, [("s2", 0.8), ("s5", 0.2)]), SMTransition("s2", "a3", 1, [("s3", 1)]), SMTransition("s3", "a4", 1, [("s4", 1)]), SMTransition("s4", "a5", 1, [("s2", 1)]), SMTransition("s5", "a6", 1, [("s6", 1)]), # SMTransition("s2", "a2", 1, [("s2", 1)]), # SMTransition("s3", "a3", 1, [("s3", 1)]), # SMTransition("s4", "a4", 1, [("s4", 1)]), # SMTransition("s5", "a5", 1, [("s5", 1)]), # SMTransition("s1", "a6", 1, [("s2", 0.2),("s3",0.2),("s4",0.2),("s5",0.2),("s6",0.2)]) ] return StateMachine(transition_functions, "s1")
def test_transition(self): sm = StateMachine() sm.add(State('online', is_starting_state=True)) sm.add(State('offline')) sm.add(Transition('online', ['offline'])) sm.transition_to('offline')
def test_new_transition_needs_existing_to_state(self): sm = StateMachine() sm.add(State('t')) with pytest.raises(AssertionError): t = Transition('f', ['t']) sm.add(t)
def test_transitions_arent_two_way(self): sm = StateMachine() sm.add(State('online', is_starting_state=True)) sm.add(State('offline')) sm.add(Transition('offline', ['online'])) assert not sm.can_transition_to('offline')
def test_accept_move_to_self_with_transition(self): state_machine = StateMachine() state_a = state_machine.define_initial_state('a') state_machine.define_transition(state_a, state_a) walker = state_machine.start() self.assertTrue(walker.move_to(state_a)) self.assertEqual(state_a, walker.get_state())
def test_add_Transition(self): sm = StateMachine() sm.add(State('f')) sm.add(State('t')) t = Transition('f', ['t']) sm.add(t) assert sm.transitions == {'f': t}
def __init__(self): self.m = StateMachine() #Declaration of all the states self.m.add_state("Still_state", self.still_state_transitions) self.m.add_state("Moving_state", self.moving_state_transitions) self.m.add_state("Bumping_state", self.bumping_state_transitions) self.m.add_state("Holding_state", self.holding_state_transitions) self.m.add_state("Crash_state", self.crash_state_transitions) self.m.set_start("Still_state")
def _build_brain(self): brain = StateMachine() brain.add_state(SpiderStateShoting(self)) brain.add_state(SpiderStateWaiting(self)) brain.add_state(SpiderStateDodging(self)) brain.set_state('waiting') return brain
def amplify(init, program, num): machine = StateMachine(operators, program) for tpl in perm(range(num)): output = init for signal in tpl: machine.send(signal, output) output = next(machine.run()) machine.reset() yield output
def test_starts_in_initial_state(self): state_machine = StateMachine() state_a = state_machine.define_initial_state('a') state_b = state_machine.define_state('b') state_machine.define_transition(state_a, state_b) walker = state_machine.start() self.assertEqual(state_a, walker.get_state())
def __init__(self, context): self.ctx = context self.m = StateMachine() self.m.add_state("init", self.sm_init) # m.add_state("idle", sm_idle) self.m.add_state("start", self.sm_start) self.m.add_state("end", self.sm_end, end_state=1) self.m.set_start("init") self.m.run(self.ctx)
def test_cant_transition_where_cant_transition_to(self): sm = StateMachine() sm.add(State('online', is_starting_state=True)) sm.add(State('offline')) sm.add(Transition('offline', ['online'])) assert not sm.can_transition_to('offline') with pytest.raises(Exception): sm.transition_to('offline')
def test_can_transition(self): sm = StateMachine() sm.add(State('online', is_starting_state=True)) sm.add(State('offline')) sm.add(State('error')) sm.add(Transition('online', ['offline', 'error'])) assert sm.can_transition_to('offline') assert sm.can_transition_to('error')
def test_cant_transition_without_starting_state(self): sm = StateMachine() sm.add(State('online')) sm.add(State('offline')) sm.add(State('error')) sm.add(Transition('online', ['offline', 'error'])) with pytest.raises(Exception): sm.can_transition_to('offline') sm.can_transition_to('error')
def __init__(self, startCoordinates, name=''): GameEntity.__init__(self, startCoordinates, name) self.frameNumber = 1 self.imageFrame = 0 self.frameTime = 0. self.animationSpeed = 0.5 + randint(-3, 3) / 10.0 self.frameWidth = 0 self.speed = 0 self.width = self.height = 0 self.brain = StateMachine() self.actionQueue = []
def startthegoddamnedgame(): m = StateMachine() m.add_state("GameStarts", game_started) m.add_state("p1TurnStart", p1_turn_start) m.add_state("p2TurnStart", p2_turn_start) m.add_state("p1TurnEnd", p1_turn_end) m.add_state("p2TurnEnd", p2_turn_end) m.add_state("p1Win", p1_win) m.add_state("p2Win", p2_win) m.add_state("Game_Over", None, end_state=1) m.set_start("GameStarts") m.run(allTiles)
def _build_brain(self): brain = StateMachine() shoting_state = RobotStateShoting(self) waiting_state = RobotStateWaiting(self) dodging_state = RobotStateDodging(self) brain.add_state(shoting_state) brain.add_state(waiting_state) brain.add_state(dodging_state) brain.set_state('waiting') return brain
def __init__(self, name): self.name = name self.location = None self.gold = 0 self.fatigue = 0 self.thirsty = 0 self.fsm = StateMachine(self) self.fsm.set_current_state(states.GoHomeAndSleepTilRested()) self.pocket_limit = 10 self.thirsty_limit = 10 self.fatigue_limit = 10
def test_rejects_move_to_self_without_transition(self): state_machine = StateMachine() state_a = state_machine.define_initial_state('a') state_b = state_machine.define_state('b') state_machine.define_transition(state_a, state_b) # There is no transition from a to a, so move_to() should return false, # however we should still be in state a after. walker = state_machine.start() self.assertFalse(walker.move_to(state_a)) self.assertEqual(state_a, walker.get_state())
def test_rejects_move_without_transition(self): state_machine = StateMachine() state_a = state_machine.define_initial_state('a') state_b = state_machine.define_state('b') state_c = state_machine.define_state('c') state_machine.define_transition(state_a, state_b) state_machine.define_transition(state_b, state_c) walker = state_machine.start() self.assertFalse(walker.move_to(state_c)) self.assertEqual(state_a, walker.get_state())
def build_brain(self): brain = StateMachine() shooting_state = TankStateShooting(self) waiting_state = TankStateWaiting(self) dodging_state = TankStateDodging(self) brain.add_state(shooting_state) brain.add_state(waiting_state) brain.add_state(dodging_state) brain.set_state('waiting') self._brain = brain
def initSearchStateMachine(): print("Creating STATA MACHINE for searching!") m = StateMachine() m.add_state("START_Search", StartSearch_state) m.add_state("DocSearcher_LoadDocumentsCluster", DocSearcher_LoadDocumentsCluster_state) m.add_state("DocSearcher_waitInput", DocSearcher_waitInput_state) m.add_state("DocSearcher_search", DocSearcher_search_state) m.add_state("DocSearcher_showResults", DocSearcher_showResults_state) m.add_state("End", EndSearch_state, end_state=1) m.set_start("START_Search") return m
def parseBMRB(f): # This is the actual program m = StateMachine() m.add_state("Start", start) m.add_state("open_file", open_file) m.add_state("read_lines", read_lines) m.add_state("get_sequence", get_sequence) m.add_state("get_CS", get_CS) m.add_state("get_headers", get_headers) m.add_state("get_uniProt", get_uniProt) m.add_state("end_reading", end_reading, end_state=1) m.add_state("empty_file", empty_file) m.add_state("no_file", no_file) m.set_start("Start") a = m.run(f)[2] return a
def chained_amplify(init, program, lower, upper): upper += 1 machines = [StateMachine(operators, program) for _ in range(lower, upper)] for tpl in perm(range(lower, upper)): output = init amps = cycle([machine.run(False) for machine in machines]) for signal, machine in zip(tpl, machines): machine.send(signal) machine_cycle = cycle([machine for machine in machines]) for amp, machine in zip(amps, machine_cycle): try: machine.send(output) output = next(amp) except StopIteration: break [machine.reset() for machine in machines] yield output
def __init__(self): # These attributes are set by the parse method self.doc = None self.para = None self.current_string = None self.flow = None self.stateMachine = StateMachine() self.stateMachine.add_state("PARA", self._para) self.stateMachine.add_state("ESCAPE", self._escape) self.stateMachine.add_state("END", None, end_state=1) self.stateMachine.add_state("ANNOTATION-START", self._annotation_start) self.stateMachine.add_state("CITATION-START", self._citation_start) self.stateMachine.add_state("BOLD-START", self._bold_start) self.stateMachine.add_state("ITALIC-START", self._italic_start) self.stateMachine.add_state("CODE-START", self._code_start) self.stateMachine.add_state("QUOTES-START", self._quotes_start) self.stateMachine.add_state("INLINE-INSERT", self._inline_insert) self.stateMachine.add_state("CHARACTER-ENTITY", self._character_entity) self.stateMachine.set_start("PARA") self.patterns = { 'escape': re.compile(r'\\', re.U), 'escaped-chars': re.compile(r'[\\\(\{\}\[\]_\*,\.\*`"&]', re.U), 'annotation': re.compile( r'(?<!\\)\{(?P<text>.*?)(?<!\\)\}(\(\s*(?P<type>\S*?\s*[^\\"\']?)(["\'](?P<specifically>.*?)["\'])??\s*(\((?P<namespace>\w+)\))?\s*(~(?P<language>[\w-]+))?\))?', re.U), 'bold': re.compile(r'\*(?P<text>((?<=\\)\*|[^\*])*)(?<!\\)\*', re.U), 'italic': re.compile(r'_(?P<text>((?<=\\)_|[^_])*)(?<!\\)_', re.U), 'code': re.compile(r'`(?P<text>(``|[^`])*)`', re.U), 'quotes': re.compile(r'"(?P<text>((?<=\\)"|[^"])*)(?<!\\)"', re.U), 'inline-insert': re.compile(r'>\((?P<attributes>.*?)\)', re.U), 'character-entity': re.compile(r'&(\#[0-9]+|#[xX][0-9a-fA-F]+|[\w]+);'), 'citation': re.compile( r'(\[\s*\*(?P<id>\S+)(\s+(?P<id_extra>.+?))?\])|(\[\s*\#(?P<name_name>\S+)(\s+(?P<extra>.+?))?\])|(\[\s*(?P<citation>.*?)\])', re.U) }