コード例 #1
0
 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")
コード例 #2
0
 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()
コード例 #3
0
    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()
コード例 #4
0
    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*')
        }
コード例 #5
0
    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']
コード例 #6
0
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")
コード例 #7
0
    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')
コード例 #8
0
    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)
コード例 #9
0
    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')
コード例 #10
0
    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())
コード例 #11
0
    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}
コード例 #12
0
 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")
コード例 #13
0
    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
コード例 #14
0
ファイル: day_7.py プロジェクト: Dragon-God/AdventOfCode
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
コード例 #15
0
    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())
コード例 #16
0
ファイル: seabattle.py プロジェクト: oivantsiv/seabattle
 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)
コード例 #17
0
    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')
コード例 #18
0
    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')
コード例 #19
0
    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')
コード例 #20
0
 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 = []
コード例 #21
0
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)
コード例 #22
0
    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
コード例 #23
0
ファイル: miner.py プロジェクト: 18252438781/codelab
    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
コード例 #24
0
    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())
コード例 #25
0
    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())
コード例 #26
0
ファイル: tank.py プロジェクト: hugoaguirre/learningpygame
    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
コード例 #27
0
ファイル: SearchSM.py プロジェクト: Berni1557/PDMSPython
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
コード例 #28
0
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
コード例 #29
0
ファイル: day_7.py プロジェクト: Dragon-God/AdventOfCode
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
コード例 #30
0
    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)
        }