class Person(mongoengine.Document): name = mongoengine.StringField(default='Billy') sleeping = State(initial=True) running = State() cleaning = State() run = Event(from_states=sleeping, to_state=running) cleanup = Event(from_states=running, to_state=cleaning) sleep = Event(from_states=(running, cleaning), to_state=sleeping) @before('sleep') def do_one_thing(self): print("{} is sleepy".format(self.name)) @before('sleep') def do_another_thing(self): print("{} is REALLY sleepy".format(self.name)) @after('sleep') def snore(self): print("Zzzzzzzzzzzz") @after('sleep') def snore(self): print("Zzzzzzzzzzzzzzzzzzzzzz")
def test_any_repetition(self): edge_a = Edge('a', 'state_a') edge_b = Edge('b', 'state_b') state_a = State('state_a') state_b = State('state_b') edge_out_a = Edge('a', 'state_a') edge_out_a_e = Edge('', 'state_a') edge_out_b = Edge('b', 'state_b') edge_out_b_e = Edge('', 'state_b') state_out_a = State('state_a', [edge_out_a, edge_out_b]) state_out_b = State('state_b', [edge_out_a, edge_out_b]) in_edges = [edge_a, edge_b] new_end_states = [state_a, state_b] new_states = [state_a, state_b] repetition = (0, float('inf')) is_optional = False in_edges_out = [edge_out_a, edge_out_b, edge_out_a_e, edge_out_b_e] new_end_states_out = [state_out_a, state_out_b] new_states_out = [state_out_a, state_out_b] expected = [in_edges_out, new_end_states_out, new_states_out] actual = repetition_applicator.apply_repetition( in_edges, new_end_states, new_states, repetition, is_optional) logger.debug( f'Actual result:\nin_edges: {actual[0]}\nnew_end_states: {actual[1]}\nnew_states: {actual[2]}' ) logger.debug(f'start_edges') self.assertTrue(edges_equal(actual[0], expected[0])) logger.debug(f'end_states') assert_states_equal(actual[1], expected[1]) logger.debug(f'states') assert_states_equal(actual[2], expected[2])
def test_edge_traversal_order(self): machine = StateMachine(states=[ State(START_STATE, Edge('', 'a')), State('a', [Edge('a', 'a'), Edge('', FINAL_STATE)]), State(FINAL_STATE) ]) start_record = gen_test_state_record(START_STATE, 0, 0) a_record = gen_test_state_record('a', 0, 0) end_record = gen_test_state_record(FINAL_STATE, 0, 0) state_records = [start_record, a_record] result = (True, state_records, 0) self.assertEqual(machine.accepts_partial('aab'), result) start_record = gen_test_state_record(START_STATE, 0, 0) a_record = gen_test_state_record('a', 0, 1) a_record_2 = gen_test_state_record('a', 1, 1) state_records = [start_record, a_record, a_record_2] result = (True, state_records, 1) self.assertEqual(machine.accepts_partial('aab'), result) start_record = gen_test_state_record(START_STATE, 0, 0) a_record = gen_test_state_record('a', 0, 1) a_record_2 = gen_test_state_record('a', 1, 2) a_record_3 = gen_test_state_record('a', 2, 2) state_records = [start_record, a_record, a_record_2, a_record_3] result = (True, state_records, 2) self.assertEqual(machine.accepts_partial('aab'), result) self.assertEqual(machine.accepts_partial('aab'), (False, None, -1))
def test_sub_machines(self): machine = StateMachine(id_='a', states=[ State(START_STATE, Edge('', 'b')), State('b', Edge('a', FINAL_STATE), is_automata=True), State(FINAL_STATE, Edge('', START_STATE)) ]) submachine_b = StateMachine(id_='b', states=[ State(START_STATE, Edge('b', FINAL_STATE)), State(FINAL_STATE, Edge('', START_STATE)) ]) machine.register_automata(submachine_b) start_record = StateRecord('a', START_STATE, 0, 0) nested_start_record = StateRecord('b', START_STATE, 0, 1) submachine_record = StateRecord('a', 'b_internal', 0, 1, [nested_start_record]) b_record = StateRecord('a', 'b', 1, 2) state_records = [start_record, submachine_record, b_record] result = (True, state_records, 2) self.assertEqual(machine.accepts_partial('ba'), result)
def test_continuation_with_internal_repetition(self): machine = StateMachine(states=[ State(START_STATE, Edge('', 'a')), State('a', [Edge('a', 'a'), Edge('', FINAL_STATE)]), State(FINAL_STATE) ]) start_record = gen_test_state_record(START_STATE, 0, 0) a_record = gen_test_state_record('a', 0, 0) state_records = [start_record, a_record] result = (True, state_records, 0) self.assertEqual(machine.accepts_partial('aab'), result) start_record = gen_test_state_record(START_STATE, 0, 0) a_record = gen_test_state_record('a', 0, 1) a_record_2 = gen_test_state_record('a', 1, 1) state_records = [start_record, a_record, a_record_2] result = (True, state_records, 1) self.assertEqual(machine.accepts_partial('aab'), result) start_record = gen_test_state_record(START_STATE, 0, 0) a_record = gen_test_state_record('a', 0, 1) a_record_2 = gen_test_state_record('a', 1, 2) a_record_3 = gen_test_state_record('a', 2, 2) state_records = [start_record, a_record, a_record_2, a_record_3] result = (True, state_records, 2) self.assertEqual(machine.accepts_partial('aab'), result) self.assertEqual(machine.accepts_partial('aab'), (False, None, -1))
def test_continuation_works(self): machine = StateMachine(states=[ State(START_STATE, Edge('a', FINAL_STATE)), State(FINAL_STATE, Edge('', START_STATE)) ]) start_record = gen_test_state_record(START_STATE, 0, 1) state_records = [start_record] result = (True, state_records, 1) self.assertEqual(machine.accepts_partial('aaa'), result) end_record = gen_test_state_record(FINAL_STATE, 1, 1) start_record = gen_test_state_record(START_STATE, 0, 1) start_record2 = gen_test_state_record(START_STATE, 1, 2) state_records = [start_record, end_record, start_record2] result = (True, state_records, 2) self.assertEqual(machine.accepts_partial('aaa'), result) start_record = gen_test_state_record(START_STATE, 0, 1) end_record = gen_test_state_record(FINAL_STATE, 1, 1) start_record2 = gen_test_state_record(START_STATE, 1, 2) end_record2 = gen_test_state_record(FINAL_STATE, 2, 2) start_record3 = gen_test_state_record(START_STATE, 2, 3) state_records = [ start_record, end_record, start_record2, end_record2, start_record3 ] result = (True, state_records, 3) self.assertEqual(machine.accepts_partial('aaa'), result) self.assertEqual(machine.accepts_partial('aaa'), (False, None, -1))
def test_N_repetition(self): edge = Edge('a', 'state_a') state = State('state_a') edge_out = Edge('a', 'state_a') edge_2_out = Edge('a', 'state_a_2') state_out = State('state_a', [edge_2_out]) state_2_out = State('state_a_2') in_edges = [edge] new_end_states = [state] new_states = [state] repetition = (2, 2) is_optional = False in_edges_out = [edge_out] new_end_states_out = [state_2_out] new_states_out = [state_out, state_2_out] expected = [in_edges_out, new_end_states_out, new_states_out] actual = repetition_applicator.apply_repetition( in_edges, new_end_states, new_states, repetition, is_optional) logger.debug( f'Actual result:\nin_edges: {actual[0]}\nnew_end_states: {actual[1]}\nnew_states: {actual[2]}' ) logger.debug(f'start_edges') self.assertTrue(edges_equal(actual[0], expected[0])) logger.debug(f'end_states') assert_states_equal(actual[1], expected[1]) logger.debug(f'states') assert_states_equal(actual[2], expected[2])
def test_N_plus_repetition(self): in_edge = Edge('a', 'state_a') in_state = State('state_a') edge = Edge('a', 'state_a') edge2 = Edge('a', 'state_a_2') edge3 = Edge('a', 'state_a_3') edge3e = Edge('', 'state_a_3') state = State('state_a', [edge2]) state2 = State('state_a_2', [edge3]) state3 = State('state_a_3', [edge3]) in_edges = [in_edge] new_end_states = [in_state] new_states = [in_state] repetition = (3, float('inf')) is_optional = False in_edges_out = [edge] new_end_states_out = [state3] new_states_out = [state, state2, state3] expected = [in_edges_out, new_end_states_out, new_states_out] actual = repetition_applicator.apply_repetition( in_edges, new_end_states, new_states, repetition, is_optional) logger.debug( f'Actual result:\nin_edges: {actual[0]}\nnew_end_states: {actual[1]}\nnew_states: {actual[2]}' ) logger.debug(f'start_edges') self.assertTrue(edges_equal(actual[0], expected[0])) logger.debug(f'end_states') assert_states_equal(actual[1], expected[1]) logger.debug(f'states') assert_states_equal(actual[2], expected[2])
class TicketState: arrived = State(initial=True) waiting = State() running = State() stopped = State() wait = Event(from_states=(arrived, running, stopped), to_state=waiting) run = Event(from_states=waiting, to_state=running) stop = Event(from_states=(running, waiting, arrived), to_state=stopped) def __init__(self, name): self.name = name @after('wait') def wait_info(self): print(f'{self.name} WAITINGUJE') @after('run') def run_info(self): print(f'{self.name} RUNNINGUJE') @after('terminate') def block_info(self): print(f'{self.name} TERMINATORUJE')
def test_parenthized_alternative_repeated_alternative_tokens_case(self): elements = ['abc', '/', ['2*', ['"def"', '/', '"ghi"']]] edge = Edge('abc', 'abc') edge2 = Edge('def', 'def') edge3 = Edge('ghi', 'ghi') edge2_2 = Edge('def', 'def_2') edge3_2 = Edge('ghi', 'ghi_2') edge2_2 = Edge('def', 'def_2') edge2_e = Edge('def', 'def_2') edge3_2 = Edge('ghi', 'ghi_2') state = State('abc', is_automata=True) state2 = State('def', [edge2_2, edge3_2]) state3 = State('ghi', [edge2_2, edge3_2]) state2_2 = State('def_2', [edge2_2, edge3_2]) state3_2 = State('ghi_2', [edge2_2, edge3_2]) start_edges = [edge, edge2, edge3] end_states = [state, state2_2, state3_2] states = [state, state2, state3, state2_2, state3_2] expected = (start_edges, end_states, states) actual = choices_parser.recursive_parse_elements(elements) logger.debug( f'Actual result:\nin_edges: {actual[0]}\nnew_end_states: {actual[1]}\nnew_states: {actual[2]}' ) logger.debug(f'start_edges') self.assertTrue(edges_equal(actual[0], expected[0])) logger.debug(f'end_states') assert_states_equal(actual[1], expected[1]) logger.debug(f'states') assert_states_equal(actual[2], expected[2])
def __init__(self): """Set up the states""" self.tcp_established = State( name='TCPEstablished', enter_action=lambda: print('Entered TCPEstablished state'), transitions=[ Transition(event=Event.open, target=lambda: self.tcp_listen), ]) self.tcp_listen = State( name='TCPListen', enter_action=lambda event: print('Entered TCPListen state'), transitions=[ Transition(event=Event.acknowledge, action=self.send_ack, target=lambda: self.tcp_listen), Transition(event=Event.close, target=lambda: self.tcp_closed), ]) self.tcp_closed = State( name='TCPClosed', enter_action=lambda event: print('Entered TCPClosed state'), ) states = [ self.tcp_established, self.tcp_listen, self.tcp_closed ] super().__init__(states)
def test_states_manager_add_state_method(): """Testing the StatesManager add_state method """ # Setup state1 = State(name="state1", routine_function=fun_routine, decision_function=fun_decision, entry_function=fun_entry, exit_function=fun_exit, is_async=False) state2 = State(name="state2", routine_function=fun_routine, decision_function=fun_decision, entry_function=fun_entry, exit_function=fun_exit, is_async=False) state3 = State(name="state3", routine_function=fun_routine, decision_function=fun_decision, entry_function=fun_entry, exit_function=fun_exit, next_states=[state1], is_async=False) state_manager = StatesManager() # Execute state_manager.add_state(state1, state2, state3) # Verify assert len(state_manager.states) == 3
class Person(): name = 'Billy' sleeping = State(initial=True) running = State() cleaning = State() run = Event(from_states=sleeping, to_state=running) cleanup = Event(from_states=running, to_state=cleaning) sleep = Event(from_states=(running, cleaning), to_state=sleeping) @before('sleep') def do_one_thing(self): print("{} is sleepy".format(self.name)) @before('sleep') def do_another_thing(self): print("{} is REALLY sleepy".format(self.name)) @after('sleep') def snore(self): print("Zzzzzzzzzzzz") @after('sleep') def big_snore(self): print("Zzzzzzzzzzzzzzzzzzzzzz")
class Person: name = "Jim" sleeping = State(initial=True) running = State() cleaning = State() run = Event(from_states=sleeping, to_state=running) cleanup = Event(from_states=running, to_state=cleaning) sleep = Event(from_states=(running, cleaning), to_state=sleeping) @before("sleep") def sleep_before(self): print("After the busy day start. {} is going to sleep.".format( self.name)) @after("sleep") def sleep_after(self): print("After finish the whole day. {} sleep well".format(self.name)) @before('run') def run_before(self): print('exec before_run') @after('run') def run_after(self): print('exec after_run') @before("cleanup") def cleanup_before(self): print("exec before_cleanup") @after("cleanup") def cleanup_after(self): print("exec after_cleanup")
class Puppy(Base): __tablename__ = 'puppies' id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True) name = sqlalchemy.Column(sqlalchemy.String) sleeping = State(initial=True) running = State() cleaning = State() run = Event(from_states=sleeping, to_state=running) cleanup = Event(from_states=running, to_state=cleaning) sleep = Event(from_states=(running, cleaning), to_state=sleeping) @before('sleep') def do_one_thing(self): print("{} is sleepy".format(self.name)) @before('sleep') def do_another_thing(self): print("{} is REALLY sleepy".format(self.name)) @after('sleep') def snore(self): print("Zzzzzzzzzzzz") @after('sleep') def snore(self): print("Zzzzzzzzzzzzzzzzzzzzzz")
def test_state_machine_init_method(): """Basic testing for the State Machine init method """ # Setup state_machine = StateMachine() # Setup def decision(): return 'state2' def decision2(): return 'state3' state1 = State(name="state1", routine_function=fun_routine, decision_function=decision, entry_function=fun_entry, exit_function=fun_exit, is_async=False) state2 = State(name="state2", routine_function=fun_routine, decision_function=decision2, entry_function=fun_entry, exit_function=fun_exit, is_async=False) state3 = State(name="state3", routine_function=fun_routine, decision_function=decision, is_async=False) state_machine.add_states(state3, state2, state1) state_machine.initial_state = "state1"
def __init__(self, ctx=None, action=None): ThreadSafe.__init__(self) State.__init__(self, ctx, action) self.update_robot(action) self.look_target = (0.0, 0.0, 0.0) self.set_thread_safe_all( ("update_robot", "action", "odo_reset", "look_target"))
def __init__(self, name, is_visible=True, is_active=True, _is_receiving_events=True): State.__init__(self, name) self._visible_data = {} self._screens = [] self._fsm_list = [] self.is_visible = is_visible self.is_active = is_active self._is_receiving_events = _is_receiving_events
class Robot(): name = 'R2-D2' sleeping = State(initial=True) running = State() cleaning = State() run = Event(from_states=sleeping, to_state=running) cleanup = Event(from_states=running, to_state=cleaning) sleep = Event(from_states=(running, cleaning), to_state=sleeping)
class Dog(object): sleeping = State(initial=True) running = State() run = Event(from_states=sleeping, to_state=running) sleep = Event(from_states=(running,), to_state=sleeping) @before('run') def on_run(self): things_done.append("Dog.ran")
def test_accepts_repeated_symbols(self): machine = StateMachine(states=[ State(START_STATE, Edge('a', FINAL_STATE)), State(FINAL_STATE, Edge('', START_STATE)) ]) self.assertTrue(machine.accepts('a')[0]) machine.reset() self.assertTrue(machine.accepts('aa')[0]) machine.reset() self.assertTrue(machine.accepts('aaa')[0])
class Person(mongoengine.Document): name = mongoengine.StringField(default='Billy') sleeping = State(initial=True) running = State() cleaning = State() run = Event(from_states=sleeping, to_state=running) cleanup = Event(from_states=running, to_state=cleaning) sleep = Event(from_states=(running, cleaning), to_state=sleeping)
def test_notify_observers(self): m = StateMachine() m.notify_observers = MagicMock() s = State('test state', m) e = Event() s.notify_observers(e) self.assertEqual(1, m.notify_observers.call_count) self.assertEqual(e, m.notify_observers.call_args[0][0])
def test_accepts_both_optional_paths(self): machine = StateMachine(states=[ State(START_STATE, [Edge('a', 'b'), Edge('a', 'c')]), State('b', Edge('b', FINAL_STATE)), State('c', Edge('c', FINAL_STATE)), State(FINAL_STATE) ]) self.assertTrue(machine.accepts('ab')[0]) machine.reset() self.assertTrue(machine.accepts('ac')[0])
def test_accepts_repeated_alpha(self): machine = StateMachine(states=[ State(START_STATE, Edge('ALPHA', FINAL_STATE, True)), State(FINAL_STATE, Edge('', START_STATE)) ]) self.assertTrue(machine.accepts('a')[0]) machine.reset() self.assertTrue(machine.accepts('ab')[0]) machine.reset() self.assertTrue(machine.accepts('abc')[0]) machine.reset() self.assertFalse(machine.accepts('a2c')[0])
def test_accepts_optional_symbols(self): machine = StateMachine(states=[ State(START_STATE, [Edge('a', FINAL_STATE), Edge('a', 'b')]), State('b', Edge('b', FINAL_STATE)), State(FINAL_STATE) ]) self.assertTrue(machine.accepts('a')[0]) machine.reset() self.assertTrue(machine.accepts('ab')[0]) machine.reset() self.assertFalse(machine.accepts('b')[0])
class Person(object): sleeping = State(initial=True) running = State() cleaning = State() run = Event(from_states=sleeping, to_state=running) cleanup = Event(from_states=running, to_state=cleaning) sleep = Event(from_states=(running, cleaning), to_state=sleeping) @before('run') def on_run(self): things_done.append("Person.ran")
class Penguin(Base): __tablename__ = 'penguins' id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True) name = sqlalchemy.Column(sqlalchemy.String) sleeping = State(initial=True) running = State() cleaning = State() run = Event(from_states=sleeping, to_state=running) cleanup = Event(from_states=running, to_state=cleaning) sleep = Event(from_states=(running, cleaning), to_state=sleeping)
def test_accepts_repeated_digit(self): machine = StateMachine(states=[ State(START_STATE, Edge('DIGIT', FINAL_STATE, True)), State(FINAL_STATE, Edge('', START_STATE)) ]) self.assertTrue(machine.accepts('1')[0]) machine.reset() self.assertTrue(machine.accepts('12')[0]) machine.reset() self.assertTrue(machine.accepts('123')[0]) machine.reset() self.assertFalse(machine.accepts('1a3')[0])
class Runner(mongoengine.Document): name = mongoengine.StringField(default='Billy') sleeping = State(initial=True) running = State() cleaning = State() run = Event(from_states=sleeping, to_state=running) cleanup = Event(from_states=running, to_state=cleaning) sleep = Event(from_states=(running, cleaning), to_state=sleeping) @before('run') def check_sneakers(self): return False
def test_trivial_case(self): rulename = 'rulename' elements = 'abc' edge = Edge('abc', 'abc') final_edge = Edge('', FINAL_STATE) state = State('abc', final_edge, is_automata=True) start_state = State(START_STATE, edge) final_state = State(FINAL_STATE) states = [start_state, state, final_state] expected = StateMachine(rulename, states) actual = choices_parser.parse_elements(rulename, elements) assert_state_machines_equal(actual, expected)
def __init__(self, ctx=None, action=None): ThreadSafe.__init__(self) State.__init__(self, ctx, action) self.update_robot(action) self.look_target = (0.0, 0.0, 0.0) self.set_thread_safe_all(("update_robot", "action", "odo_reset", "look_target"))