示例#1
0
class ConnectionStateMachine:
    """HSMS Connection state machine."""
    def __init__(self, callbacks=None):
        """
        Initialize the hsms connection state machine.

        :param callbacks: callbacks for the state machine
        """
        self.callbacks = {}

        self.states = [
            STATE_NOT_CONNECTED, {
                'name':
                STATE_CONNECTED,
                'on_enter':
                self._on_enter_CONNECTED,
                'on_exit':
                self._on_exit_CONNECTED,
                'children': [
                    STATE_NOT_SELECTED, {
                        'name': STATE_SELECTED,
                        'on_enter': self._on_enter_CONNECTED_SELECTED
                    }
                ]
            }
        ]

        # transition 1
        self.machine = Machine(model=self,
                               states=self.states,
                               initial=STATE_NOT_CONNECTED,
                               auto_transitions=False)

        if callbacks:
            self.callbacks = callbacks

        self.machine.add_transition(
            'connect', STATE_NOT_CONNECTED,
            STATE_CONNECTED_NOT_SELECTED)  # transition 2
        self.machine.add_transition('disconnect', STATE_CONNECTED,
                                    STATE_NOT_CONNECTED)  # transition 3
        self.machine.add_transition('select', STATE_CONNECTED_NOT_SELECTED,
                                    STATE_CONNECTED_SELECTED)  # transition 4
        self.machine.add_transition(
            'deselect', STATE_CONNECTED_SELECTED,
            STATE_CONNECTED_NOT_SELECTED)  # transition 5
        self.machine.add_transition('timeoutT7', STATE_CONNECTED_NOT_SELECTED,
                                    STATE_NOT_CONNECTED)  # transition 6

    def _on_enter_CONNECTED(self):
        if "on_enter_CONNECTED" in self.callbacks:
            self.callbacks["on_enter_CONNECTED"]()

    def _on_exit_CONNECTED(self):
        if "on_exit_CONNECTED" in self.callbacks:
            self.callbacks["on_exit_CONNECTED"]()

    def _on_enter_CONNECTED_SELECTED(self):
        if "on_enter_CONNECTED_SELECTED" in self.callbacks:
            self.callbacks["on_enter_CONNECTED_SELECTED"]()
示例#2
0
    def test_blueprint_nested(self):
        c1 = NestedState('C_1', parent='C')
        c2 = NestedState('C_2', parent='C')
        c3 = NestedState('C_3', parent='C')
        c = NestedState('C', children=[c1, c2, c3])

        states = ['A', {'name': 'B', 'on_enter': 'chirp', 'children': ['1', '2', '3']},
                  c, 'D']
        # Define with list of dictionaries
        transitions = [
            {'trigger': 'walk', 'source': 'A', 'dest': 'B','before': 'before_state_change',
             'after': 'after_state_change' },
            {'trigger': 'run', 'source': 'B', 'dest': 'C'},
            {'trigger': 'sprint', 'source': 'C', 'dest': 'D'}
        ]
        m = Machine(states=states, transitions=transitions, before_state_change='before_state_change',
                    after_state_change='after_state_change', initial='A')

        m.before_state_change = MagicMock()
        m.after_state_change = MagicMock()

        self.assertEqual(len(m.blueprints['states']), 4)
        self.assertEqual(m.blueprints['states'][3], 'D')
        self.assertEqual(len(m.blueprints['transitions']), 3)
        # transition 'walk' before should contain two calls of the same method
        self.assertEqual(len(m.blueprints['transitions'][0]['before']), 2)
        self.assertEqual(len(m.blueprints['transitions'][0]['after']), 2)
        self.assertEqual(len(m.blueprints['transitions'][1]['before']), 1)
        self.assertEqual(m.blueprints['transitions'][2]['trigger'], 'sprint')

        m.add_transition('fly', 'D', 'A')
        self.assertEqual(len(m.blueprints['transitions']), 4)
        self.assertEqual(m.blueprints['transitions'][3]['source'], 'D')
示例#3
0
    def test_blueprint_simple(self):
        states = ['A', 'B', 'C', 'D']
        # Define with list of dictionaries
        transitions = [{
            'trigger': 'walk',
            'source': 'A',
            'dest': 'B'
        }, {
            'trigger': 'run',
            'source': 'B',
            'dest': 'C'
        }, {
            'trigger': 'sprint',
            'source': 'C',
            'dest': 'D'
        }]
        m = Machine(states=states,
                    transitions=transitions,
                    before_state_change='before_state_change',
                    after_state_change='after_state_change',
                    initial='A')

        self.assertEqual(len(m.blueprints['states']), 4)
        self.assertEqual(m.blueprints['states'][3], 'D')
        self.assertEqual(len(m.blueprints['transitions']), 3)
        self.assertEqual(m.blueprints['transitions'][2]['trigger'], 'sprint')

        m.add_transition('fly', 'D', 'A')
        self.assertEqual(len(m.blueprints['transitions']), 4)
        self.assertEqual(m.blueprints['transitions'][3]['source'], 'D')
示例#4
0
    def test_blueprint_nested(self):
        c1 = NestedState('C_1', parent='C')
        c2 = NestedState('C_2', parent='C')
        c3 = NestedState('C_3', parent='C')
        c = NestedState('C', children=[c1, c2, c3])

        states = [
            'A', {
                'name': 'B',
                'on_enter': 'chirp',
                'children': ['1', '2', '3']
            }, c, 'D'
        ]
        # Define with list of dictionaries
        transitions = [{
            'trigger': 'walk',
            'source': 'A',
            'dest': 'B',
            'before': 'before_state_change',
            'after': 'after_state_change'
        }, {
            'trigger': 'run',
            'source': 'B',
            'dest': 'C'
        }, {
            'trigger': 'sprint',
            'source': 'C',
            'dest': 'D'
        }]
        m = Machine(states=states,
                    transitions=transitions,
                    before_state_change='before_state_change',
                    after_state_change='after_state_change',
                    initial='A')

        m.before_state_change = MagicMock()
        m.after_state_change = MagicMock()

        self.assertEqual(len(m.blueprints['states']), 4)
        self.assertEqual(m.blueprints['states'][3], 'D')
        self.assertEqual(len(m.blueprints['transitions']), 3)
        # transition 'walk' before should contain two calls of the same method
        self.assertEqual(len(m.blueprints['transitions'][0]['before']), 2)
        self.assertEqual(len(m.blueprints['transitions'][0]['after']), 2)
        self.assertEqual(len(m.blueprints['transitions'][1]['before']), 1)
        self.assertEqual(m.blueprints['transitions'][2]['trigger'], 'sprint')

        m.add_transition('fly', 'D', 'A')
        self.assertEqual(len(m.blueprints['transitions']), 4)
        self.assertEqual(m.blueprints['transitions'][3]['source'], 'D')
示例#5
0
class SimpleFSA(annotator):
	
	_states = [
	{'name': 'waiting', 'on_enter': '_reset_positions'},
	{'name': 'analysing'},
	{'name': 'finished'}
	]

	def __init__(self, name):
		annotator.__init__(self, name)
		self.machine = Machine(model=self, states=SimpleFSA._states, initial='waiting')
		self.machine.add_transition(trigger='start_analysis', source='waiting', dest='analysing', after='_analyse')
		self.machine.add_transition('corrected', 'analysing', 'finished')

	def verify(self, line):
		return annotator._verify_string(self, line)
	
	def _analyse(self):
		annotator._correct(self)
示例#6
0
    def test_blueprint_simple(self):
        states = ['A', 'B', 'C', 'D']
        # Define with list of dictionaries
        transitions = [
            {'trigger': 'walk', 'source': 'A', 'dest': 'B'},
            {'trigger': 'run', 'source': 'B', 'dest': 'C'},
            {'trigger': 'sprint', 'source': 'C', 'dest': 'D'}
        ]
        m = Machine(states=states, transitions=transitions, before_state_change='before_state_change',
                    after_state_change='after_state_change', initial='A')

        self.assertEqual(len(m.blueprints['states']), 4)
        self.assertEqual(m.blueprints['states'][3], 'D')
        self.assertEqual(len(m.blueprints['transitions']), 3)
        self.assertEqual(m.blueprints['transitions'][2]['trigger'], 'sprint')

        m.add_transition('fly', 'D', 'A')
        self.assertEqual(len(m.blueprints['transitions']), 4)
        self.assertEqual(m.blueprints['transitions'][3]['source'], 'D')
class ConnectionStateMachine(object):
    states = [
        "NOT_CONNECTED", {
            'name': 'CONNECTED',
            'children': ['NOT_SELECTED', 'SELECTED']
        }
    ]

    def __init__(self, callbacks=None):
        self.machine = Machine(model=self,
                               states=ConnectionStateMachine.states,
                               initial='NOT_CONNECTED',
                               auto_transitions=False,
                               queued=True)  # transition 1

        if callbacks is None:
            self.callbacks = {}
        else:
            self.callbacks = callbacks

        self.machine.add_transition('connect', 'NOT_CONNECTED',
                                    'CONNECTED_NOT_SELECTED')  # transition 2
        self.machine.add_transition('disconnect', 'CONNECTED',
                                    'NOT_CONNECTED')  # transition 3
        self.machine.add_transition('select', 'CONNECTED_NOT_SELECTED',
                                    'CONNECTED_SELECTED')  # transition 4
        self.machine.add_transition('deselect', 'CONNECTED_SELECTED',
                                    'CONNECTED_NOT_SELECTED')  # transition 5
        self.machine.add_transition('timeoutT7', 'CONNECTED_NOT_SELECTED',
                                    'NOT_CONNECTED')  # transition 6

    def on_enter_CONNECTED(self):
        if "on_enter_CONNECTED" in self.callbacks:
            self.callbacks["on_enter_CONNECTED"]()

    def on_exit_CONNECTED(self):
        if "on_exit_CONNECTED" in self.callbacks:
            self.callbacks["on_exit_CONNECTED"]()

    def on_enter_CONNECTED_SELECTED(self):
        if "on_enter_CONNECTED_SELECTED" in self.callbacks:
            self.callbacks["on_enter_CONNECTED_SELECTED"]()
示例#8
0
class HFSA16(annotator):

	_states = [
	{'name': 'waiting', 'on_enter': '_reset_positions'},
	{'name': 'searching_for_spondeus', 'children': ['secondF', 'firstF', 'fourthF', 'thirdF', 'fifthF']},
	{'name': 'spondeus_found', 'on_enter': '_verify_output'},
	{'name': 'no_spondeus_found', 'on_enter': '_make_spondeus'},
	{'name': 'fallback', 'on_enter': '_search_whole'},
	{'name': 'correction', 'on_enter': '_correct'},
	'success',
	'failure'
	]
	
	def __init__(self, name):
		annotator.__init__(self, name)
		self.machine = Machine(model=self, states=HFSA16._states, initial='waiting')
		self.machine.add_transition(trigger='start_analysis', source='waiting', dest='searching_for_spondeus_secondF', after='_search_second')
		self.machine.add_transition('found_spondeus', 'searching_for_spondeus_secondF', 'spondeus_found')
		self.machine.add_transition('not_found', 'searching_for_spondeus_secondF', 'searching_for_spondeus_firstF', after='_search_first')
		self.machine.add_transition('found_spondeus', 'searching_for_spondeus_firstF', 'spondeus_found')
		self.machine.add_transition('not_found', 'searching_for_spondeus_firstF', 'searching_for_spondeus_fourthF', after='_search_fourth')
		self.machine.add_transition('found_spondeus', 'searching_for_spondeus_fourthF', 'spondeus_found')
		self.machine.add_transition('not_found', 'searching_for_spondeus_fourthF', 'searching_for_spondeus_thirdF', after='_search_third')
		self.machine.add_transition('found_spondeus', 'searching_for_spondeus_thirdF', 'spondeus_found')
		self.machine.add_transition('not_found', 'searching_for_spondeus_thirdF', 'searching_for_spondeus_fifthF', after='_search_fifth')
		self.machine.add_transition('found_spondeus', 'searching_for_spondeus_fifthF', 'spondeus_found')
		self.machine.add_transition('not_found', 'searching_for_spondeus_fifthF', 'no_spondeus_found')
		self.machine.add_transition('not_found', 'no_spondeus_found', 'fallback')
		self.machine.add_transition('verified', 'fallback', 'correction', unless=[self._is_successful])
		self.machine.add_transition('verified', 'fallback', 'success', conditions=[self._is_successful])
		self.machine.add_transition('verified', 'spondeus_found', 'success', conditions=[self._is_successful])
		self.machine.add_transition('verified', 'spondeus_found', 'correction', unless=[self._is_successful])
		self.machine.add_transition('corrected', 'correction', 'success', conditions=[self._is_successful])
		self.machine.add_transition('corrected', 'correction', 'failure', unless=[self._is_successful])
	
	def _search_second(self):
		fourth = self._search_long(4)
		fifth = self._search_long(5)
		if fourth and fifth:
			self.verse.scansion = '-** -- -** -** -** -X'
			self.found_spondeus()
		elif fourth:
			self.positions.append(4)
			self.not_found()
		elif fifth:
			self.positions.append(5)
			self.not_found()
		else:
			self.not_found()
		
	def _search_first(self):
		if self._search_long(2):
			self.verse.scansion = '-- -** -** -** -** -X'
			self.found_spondeus()
		else:
			self.not_found()
			
	def _search_fourth(self):
		tenth = self._search_long(10)
		eleventh = self._search_long(11)
		if tenth and eleventh:
			self.verse.scansion = '-** -** -** -- -** -X'
			self.found_spondeus()
		elif tenth:
			self.positions.append(10)
			self.not_found()
		elif eleventh:
			self.positions.append(11)
			self.not_found()
		else:
			self.not_found()

	def _search_third(self):
		seventh = self._search_long(7)
		eigth = self._search_long(8)
		if seventh and eigth:
			self.verse.scansion = '-** -** -- -** -** -X'
			self.found_spondeus()
		elif seventh:
			self.positions.append(7)
			self.not_found()
		elif eigth:
			self.positions.append(8)
			self.not_found()
		else:
			self.not_found()
			
	def _search_fifth(self):
		thirteenth = self._search_long(13)
		fourteenth = self._search_long(14)
		if thirteenth and fourteenth:
			self.verse.scansion = '-** -** -** -** -- -X'
			self.found_spondeus()
		elif thirteenth:
			self.positions.append(13)
			self.not_found()
		elif fourteenth:
			self.positions.append(14)
			self.not_found()
		else:
			self.not_found()
			
	def _make_spondeus(self):
		annotator._make_spondeus(self, 14)
示例#9
0
class HFSA15(annotator):

	_states = [
	{'name': 'waiting', 'on_enter': '_reset_positions'},
	{'name': 'searching_for_first_spondeus', 'children': ['secondF', 'firstF', 'fourthF', 'thirdF']}, 
	{'name': 'searching_for_second_spondeus', 'children': ['firstF', 'fourthF', 'thirdF', 'fifthF']},
	{'name': 'no_spondeus_found', 'on_enter': '_make_spondeus'},
	{'name': 'found_two_spondees', 'on_enter': '_make_scansion'}, 
	{'name': 'fallback', 'on_enter': '_search_whole'},
	{'name': 'correction', 'on_enter': '_correct'},
	'success',
	'failure'
	]

	def __init__ (self, name):
		annotator.__init__(self, name)
		self.machine = Machine(model=self, states=HFSA15._states, initial='waiting')
		self.machine.add_transition('start_analysis', 'waiting', 'searching_for_first_spondeus_secondF', after='_search_second')
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_secondF', 'searching_for_first_spondeus_firstF', unless=[self._found_first], after='_search_first')
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_secondF', 'searching_for_second_spondeus_firstF', conditions=[self._found_first], after='_search_first')
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_firstF', 'found_two_spondees', conditions=[self._found_second])
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_firstF', 'searching_for_second_spondeus_fourthF', unless=[self._found_second], after='_search_fourth')
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_firstF', 'searching_for_first_spondeus_fourthF', unless=[self._found_first], after='_search_fourth')
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_firstF', 'searching_for_second_spondeus_fourthF', conditions=[self._found_first], after='_search_fourth')
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_fourthF', 'found_two_spondees', conditions=[self._found_second])
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_fourthF', 'searching_for_second_spondeus_thirdF', unless=[self._found_second], after='_search_third')
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_fourthF', 'searching_for_first_spondeus_thirdF', unless=[self._found_first], after='_search_third')
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_fourthF', 'searching_for_second_spondeus_thirdF', conditions=[self._found_first], after='_search_third')
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_thirdF', 'found_two_spondees', conditions=[self._found_second])
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_thirdF', 'searching_for_second_spondeus_fifthF', unless=[self._found_second], after='_search_fifth')
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_thirdF', 'no_spondeus_found', unless=[self._found_first])
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_thirdF', 'searching_for_second_spondeus_fifthF', conditions=[self._found_first], after='_search_fifth')
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_fifthF', 'found_two_spondees', conditions=[self._found_second])
		#deal with implausible spondeus positions
		self.machine.add_transition('search_spondeus', 'found_two_spondees', 'no_spondeus_found')
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_fifthF', 'no_spondeus_found', unless=[self._found_second])
		self.machine.add_transition('not_found', 'no_spondeus_found', 'fallback')
		self.machine.add_transition('verified', 'fallback', 'correction', unless=[self._is_successful])
		self.machine.add_transition('verified', 'fallback', 'success', conditions=[self._is_successful])
		self.machine.add_transition('verified', 'found_two_spondees', 'success', conditions=[self._is_successful])
		self.machine.add_transition('verified', 'found_two_spondees', 'correction', unless=[self._is_successful])
		self.machine.add_transition('corrected', 'correction', 'success', conditions=[self._is_successful])
		self.machine.add_transition('corrected', 'correction', 'failure', unless=[self._is_successful])
		
	def _search_second(self):
		third = self._search_long(3)
		fourth = self._search_long(4)
		fifth = self._search_long(5)
		if third and fourth:
			self.positions.append(3)
			self.positions.append(4)
			self._set_found()
		elif fourth and fifth:
			self.positions.append(4)
			self.positions.append(5)
			self._set_found()
		else:
			if third:
				self.positions.append(3)
			if fourth:
				self.positions.append(4)
			if fifth:
				self.positions.append(5)
		self.search_spondeus()
		
	def _search_first(self):
		first = self._search_long(1)
		second = self._search_long(2)
		if first and second:
			self.positions.append(1)
			self.positions.append(2)
			self._set_found()
		elif first:
			self.positions.append(1)
		elif second:
			self.positions.append(2)
		self.search_spondeus()
		
	def _search_fourth(self):
		ninth = self._search_long(9)
		tenth = self._search_long(10)
		eleventh = self._search_long(11)
		if tenth and eleventh:
			self.positions.append(10)
			self.positions.append(11)
			self._set_found()
		elif ninth and tenth:
			self.positions.append(9)
			self.positions.append(10)
			self._set_found()
		else:
			if ninth:
				self.positions.append(9)
			if tenth:
				self.positions.append(10)
			if eleventh:
				self.positions.append(11)
		self.search_spondeus()
		
	def _search_third(self):
		sixth = self._search_long(6)
		seventh = self._search_long(7)
		eigth = self._search_long(8)
		if seventh and eigth:
			self.positions.append(7)
			self.positions.append(8)
			self._set_found()
		elif sixth and seventh:
			self.positions.append(6)
			self.positions.append(7)
			self._set_found()
		else:
			if sixth:
				self.positions.append(6)
			if seventh:
				self.positions.append(7)
			if eigth:
				self.positions.append(8)
		self.search_spondeus()
		
	def _search_fifth(self):
		twelfth = self._search_long(12)
		thirteenth = self._search_long(13)
		if twelfth and thirteenth:
			self.positions.append(12)
			self.positions.append(13)
			self._set_found()
		elif twelfth:
			self.positions.append(12)
		elif thirteenth:
			self.positions.append(13)
		self.search_spondeus()
		
	def _make_scansion(self):
		if 2 in self.positions:
			self.verse.scansion = '-- '
			if 4 in self.positions:
				self.verse.scansion+= '-- -** -** -** -X'
				self._verify_output()
			elif 7 in self.positions:
				self.verse.scansion+= '-** -- -** -** -X'
				self._verify_output()
			elif 10 in self.positions:
				self.verse.scansion+= '-** -** -- -** -X'
				self._verify_output()
			elif 13 in self.positions:
				self.verse.scansion+= '-** -** -** -- -X'
				self._verify_output()
			else:
				self.search_spondeus()
		elif 5 in self.positions:
			self.verse.scansion = '-** -- '
			if 7 in self.positions:
				self.verse.scansion+= '-- -** -** -X'
				self._verify_output()
			elif 10 in self.positions:
				self.verse.scansion+= '-** -- -** -X'
				self._verify_output()
			elif 13 in self.positions:
				self.verse.scansion+= '-** -** -- -X'
				self._verify_output()
			else:
				self.search_spondeus()
		elif 8 in self.positions:
			self.verse.scansion = '-** -** -- '
			if 10 in self.positions:
				self.verse.scansion+= '-- -** -X'
				self._verify_output()
			elif 13 in self.positions:
				self.verse.scansion+= '-** -- -X'
				self._verify_output()
			else:
				self.search_spondeus()
		elif 11 in self.positions:
			self.verse.scansion = '-** -** -** -- -- -X'
			self._verify_output()
		else:
			self.search_spondeus()
			
	def _make_spondeus(self):
		annotator._make_spondeus(self, 13)
示例#10
0
class HFSA14(annotator):

	_states = [
		{'name': 'waiting', 'on_enter': '_reset_positions'},
		{'name': 'searching_for_first_spondeus', 'children': ['secondF', 'firstF', 'fourthF']},
		{'name': 'searching_for_second_spondeus', 'children': ['firstF', 'fourthF', 'thirdF']},
		{'name': 'searching_for_third_spondeus', 'children': ['fourthF', 'thirdF', 'fifthF']},
		{'name': 'no_spondeus_found', 'on_enter': '_make_spondeus'},
		{'name': 'found_three_spondees', 'on_enter': '_make_scansion'},
		{'name': 'fallback', 'on_enter': '_search_whole'},
		{'name': 'correction', 'on_enter': '_correct'},
		'success',
		'failure'
	]

	def __init__ (self, name):
		annotator.__init__(self, name)
		self.machine = Machine(model=self, states=HFSA14._states, initial='waiting')
		self.machine.add_transition('start_analysis', 'waiting', 'searching_for_first_spondeus_secondF', after='_search_second')
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_secondF', 'searching_for_first_spondeus_firstF', unless=[self._found_first], after='_search_first')
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_firstF', 'searching_for_first_spondeus_fourthF', unless=[self._found_first], after='_search_fourth')
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_fourthF', 'no_spondeus_found', unless=[self._found_first])
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_secondF', 'searching_for_second_spondeus_firstF', conditions=[self._found_first], after='_search_first')
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_firstF', 'searching_for_second_spondeus_fourthF', unless=[self._found_second], after='_search_fourth')
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_fourthF', 'searching_for_second_spondeus_thirdF', unless=[self._found_second], after='_search_third')
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_thirdF', 'no_spondeus_found', unless=[self._found_second])
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_secondF', 'searching_for_second_spondeus_firstF', conditions=[self._found_first], after='_search_first')
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_firstF', 'searching_for_second_spondeus_fourthF', conditions=[self._found_first], after='_search_fourth')
		self.machine.add_transition('search_spondeus', 'searching_for_first_spondeus_fourthF', 'searching_for_second_spondeus_thirdF', conditions=[self._found_first], after='_search_third')
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_thirdF', 'searching_for_third_spondeus_fifthF', conditions=[self._found_second], after='_search_fifth')
		self.machine.add_transition('search_spondeus', 'searching_for_third_spondeus_fifthF', 'no_spondeus_found', unless=[self._found_third])
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_firstF', 'searching_for_third_spondeus_fourthF', conditions=[self._found_second], after='_search_fourth')
		self.machine.add_transition('search_spondeus', 'searching_for_third_spondeus_fourthF', 'searching_for_third_spondeus_thirdF', unless=[self._found_third], after='_search_third')
		self.machine.add_transition('search_spondeus', 'searching_for_third_spondeus_thirdF', 'searching_for_third_spondeus_fifthF', unless=[self._found_third], after='_search_fifth')
		self.machine.add_transition('search_spondeus', 'searching_for_second_spondeus_fourthF', 'searching_for_third_spondeus_thirdF', conditions=[self._found_second], after='_search_third')
		self.machine.add_transition('search_spondeus', 'searching_for_third_spondeus_fifthF', 'found_three_spondees', conditions=[self._found_third])
		self.machine.add_transition('search_spondeus', 'searching_for_third_spondeus_fourthF', 'found_three_spondees', conditions=[self._found_third])
		self.machine.add_transition('search_spondeus', 'searching_for_third_spondeus_thirdF', 'found_three_spondees', conditions=[self._found_third])
		self.machine.add_transition('search_spondeus', 'found_three_spondees', 'no_spondeus_found')
		self.machine.add_transition('not_found', 'no_spondeus_found', 'fallback')
		self.machine.add_transition('verified', 'fallback', 'correction', unless=[self._is_successful])
		self.machine.add_transition('verified', 'fallback', 'success', conditions=[self._is_successful])
		self.machine.add_transition('verified', 'found_three_spondees', 'success', conditions=[self._is_successful])
		self.machine.add_transition('verified', 'found_three_spondees', 'correction', unless=[self._is_successful])
		self.machine.add_transition('corrected', 'correction', 'success', conditions=[self._is_successful])
		self.machine.add_transition('corrected', 'correction', 'failure', unless=[self._is_successful])

	def _search_second(self):
		third = self._search_long(3)
		fourth = self._search_long(4)
		fifth = self._search_long(5)
		if third and fourth:
			self.positions.append(3)
			self.positions.append(4)
			self._set_found()
		elif fourth and fifth:
			self.positions.append(4)
			self.positions.append(5)
			self._set_found()
		else:
			if third:
				self.positions.append(3)
			if fourth:
				self.positions.append(4)
			if fifth:
				self.positions.append(5)
		self.search_spondeus()

	def _search_first(self):
		first = self._search_long(1)
		second = self._search_long(2)
		if first and second:
			self.positions.append(1)
			self.positions.append(2)
			self._set_found()
		elif first:
			self.positions.append(1)
		elif second:
			self.positions.append(2)
		self.search_spondeus()
		
	def _search_fourth(self):
		eigth = self._search_long(8)
		ninth = self._search_long(9)
		tenth = self._search_long(10)
		if eigth and ninth:
			self.positions.append(8)
			self.positions.append(9)
			self._set_found()
		elif ninth and tenth:
			self.positions.append(9)
			self.positions.append(10)
			self._set_found()
		else:
			if eigth:
				self.positions.append(8)
			if ninth:
				self.positions.append(9)
			if tenth:
				self.positions.append(10)
		self.search_spondeus()
		
	def _search_third(self):
		sixth = self._search_long(6)
		seventh = self._search_long(7)
		eigth = self._search_long(8)
		if seventh and eigth:
			self.positions.append(7)
			self.positions.append(8)
			self._set_found()
		elif sixth and seventh:
			self.positions.append(6)
			self.positions.append(7)
			self._set_found()
		else:
			if sixth:
				self.positions.append(6)
			if seventh:
				self.positions.append(7)
			if eigth:
				self.positions.append(8)
		self.search_spondeus()
		
	def _search_fifth(self):
		eleventh = self._search_long(11)
		twelfth = self._search_long(12)
		if eleventh and twelfth:
			self.positions.append(11)
			self.positions.append(12)
			self._set_found()
		elif eleventh:
			self.positions.append(11)
		elif twelfth:
			self.positions.append(12)
		self.search_spondeus()

	def _make_scansion(self):
		for x in range(1,13):
			if x not in self.positions:
				self.questions.append(x)
		#nr. 30
		if 8 in self.questions and 9 in self.questions and 11 in self.questions and 12 in self.questions:
			self.verse.scansion = '-- -- -- -** -** -X'
			self._verify_output()
		#nr. 31
		elif 6 in self.questions and 7 in self.questions and 11 in self.questions and 12 in self.questions:
			self.verse.scansion = '-- -- -** -- -** -X'
			self._verify_output()
		#nr. 32
		elif 6 in self.questions and 7 in self.questions and 9 in self.questions and 10 in self.questions:
			self.verse.scansion = '-- -- -** -** -- -X'
			self._verify_output()
		#nr. 33
		elif 2 in self.questions and 3 in self.questions and 11 in self.questions and 12 in self.questions:
			self.verse.scansion = '-** -- -- -- -** -X'
			self._verify_output()
		#nr. 34
		elif 2 in self.questions and 3 in self.questions and 9 in self.questions and 10 in self.questions:
			self.verse.scansion = '-** -- -- -** -- -X'
			self._verify_output()
		#nr. 35
		elif 2 in self.questions and 3 in self.questions and 7 in self.questions and 8 in self.questions:
			self.verse.scansion = '-** -- -** -- -- -X'
			self._verify_output()
		#nr. 36
		elif 2 in self.questions and 3 in self.questions and 5 in self.questions and 6 in self.questions:
			self.verse.scansion = '-** -** -- -- -- -X'
			self._verify_output()
		#nr. 37
		elif 4 in self.questions and 5 in self.questions and 11 in self.questions and 12 in self.questions:
			self.verse.scansion = '-- -** -- -- -** -X'
			self._verify_output()
		#nr. 38
		elif 4 in self.questions and 5 in self.questions and 9 in self.questions and 10 in self.questions:
			self.verse.scansion = '-- -** -- -** -- -X'
			self._verify_output()
		#nr. 39
		elif 4 in self.questions and 5 in self.questions and 7 in self.questions and 8 in self.questions:
			self.verse.scansion = '-- -** -** -- -- -X'
			self._verify_output()
		else:
			self.search_spondeus()

	def _make_spondeus(self):
		annotator._make_spondeus(self, 12)
示例#11
0
    ['stop', 'yellow', 'red'],
    ['go', 'red', 'green'],
    ['throw_rock', '*', 'broken'],
    ['evening', 'broken', 'broken_buzzing'],
    ['day', ['broken_sparking', 'broken_buzzing'], 'broken'],

    # Since you have to specify a transition, and a specific
    # state to pop to, this is not a push/pop
    # at all, simply nested states.
    ['pop_broken', 'broken', 'green']
]

machine = Machine(t, states=states, transitions=transitions, initial='green')

# Transitioning from multiple states via the same trigger - pity can't specify this in the transitions structure
machine.add_transition('powersurge', 'broken', 'broken_buzzing')
machine.add_transition('powersurge', 'broken_buzzing', 'broken_sparking')

print(t.state)
assert t.state == 'green'
print(machine.get_state(t.state))

t.slowdown()
t.stop()
print(t.state)
assert t.state == 'red'

# same test as in state-demo2-stack
t.throw_rock()  # this should push
assert t.state == 'broken'
t.evening()
示例#12
0
from ulity import china_time, send_email

# # Set up logging; The basic log level will be DEBUG
# import logging
# logging.basicConfig(level=logging.DEBUG)
# # Set transitions' log level to INFO; DEBUG messages will be omitted
# logging.getLogger('transitions').setLevel(logging.INFO)

threadLock = threading.Lock()
threads = []

hpg = HPG()
machine = Machine(hpg, states=states, initial='initial')
####
machine.add_transition('connect_chrome',
                       'initial',
                       'connectedChrome',
                       conditions='connectChrome')

#####
machine.add_transition('CheckLogin', '*', 'loginHPG', conditions='check_login')

machine.add_transition('QuereTask',
                       '*',
                       'quereedTask',
                       conditions='queue_task')

machine.add_transition('ReceiveTask',
                       '*',
                       'receivedTask',
                       conditions='receive_task')