def test_you_can_post_the_same_signal_over_and_over(): ao = ActiveObject() spy = [] ao.augment(other=spy, name="spy_log") ao.start_at(posted_event_snitch) ao.post_fifo(Event(signal=signals.F), times=1, period=0.1) ao.post_lifo(Event(signal=signals.F), times=1, period=0.1) time.sleep(0.3) assert (ao.f_signal == 2)
def test_that_it_can_post_events(): '''inspect with your eyes''' ao = ActiveObject() spy = [] ao.augment(other=spy, name="spy_log") ao.start_at(posted_event_snitch) assert (ao.thread.is_alive() is True) ao.post_lifo( Event(signal=signals.F), times=5, period=0.1, deferred=False, ) time.sleep(1) assert (ao.spy_full() == [ 'START', 'SEARCH_FOR_SUPER_SIGNAL:posted_event_snitch', 'ENTRY_SIGNAL:posted_event_snitch', 'INIT_SIGNAL:posted_event_snitch', '<- Queued:(0) Deferred:(0)', 'F:posted_event_snitch', 'F:posted_event_snitch:HOOK', '<- Queued:(0) Deferred:(0)', 'F:posted_event_snitch', 'F:posted_event_snitch:HOOK', '<- Queued:(0) Deferred:(0)', 'F:posted_event_snitch', 'F:posted_event_snitch:HOOK', '<- Queued:(0) Deferred:(0)', 'F:posted_event_snitch', 'F:posted_event_snitch:HOOK', '<- Queued:(0) Deferred:(0)', 'F:posted_event_snitch', 'F:posted_event_snitch:HOOK', '<- Queued:(0) Deferred:(0)' ]) assert (ao.f_signal == 5)
def ultimate_hook_example2(): import time from miros.hsm import spy_on, pp from miros.activeobject import ActiveObject from miros.event import signals, Event, return_status @spy_on def outer_state(chart, e): status = return_status.UNHANDLED if (e.signal == signals.BEHAVIOR_NAME): # your code would go here chart.scribble("your outer_state code here") status = return_status.HANDLED else: chart.temp.fun = chart.top status = return_status.SUPER return status @spy_on def inner_state(chart, e): if (e.signal == signals.BEHAVIOR_NAME): # your code would go here chart.scribble("your inner_state code here") status = return_status.HANDLED else: chart.temp.fun = outer_state status = return_status.SUPER return status ao = ActiveObject() ao.start_at(inner_state) ao.post_fifo(Event(signal=signals.BEHAVIOR_NAME)) time.sleep(0.001) pp(ao.spy())
def test_start_stop_c(fabric_fixture): ao1 = ActiveObject() ao1.start_at(c2_s3) time.sleep(0.2) ao1.post_fifo(Event(signal=signals.A)) time.sleep(0.2) pp(ao1.spy_full())
def example_1(): @spy_on def c(chart, e): status = return_status.UNHANDLED if (e.signal == signals.INIT_SIGNAL): status = chart.trans(c1) elif (e.signal == signals.BB): status = chart.trans(c) else: status, chart.temp.fun = return_status.SUPER, chart.top return status @spy_on def c1(chart, e): status = return_status.UNHANDLED if (e.signal == signals.A): status = chart.trans(c2) else: status, chart.temp.fun = return_status.SUPER, c return status @spy_on def c2(chart, e): status = return_status.UNHANDLED if (e.signal == signals.A): status = chart.trans(c1) else: status, chart.temp.fun = return_status.SUPER, c return status ao = ActiveObject() ao.start_at(c2) ao.post_fifo(Event(signal=signals.A)) time.sleep(0.01) # give your active object a moment to respond pp(ao.spy())
def test_posting_too_many_events_will_raise_exception(fabric_fixture): ao = ActiveObject() ao.start_at(posted_event_snitch) with pytest.raises(ActiveObjectOutOfPostedEventResources) as execinfo: assert (execinfo is not None) for i in range(1000): ao.post_fifo(Event(signal=signals.A), times=300, period=0.1) time.sleep(0.2) # we issue an exception, but we still run as well as we can # the existing threads will work assert (ao.a_signal > 0) ao.cancel_events(Event(signal=signals.A)) assert (len(ao.posted_events_queue) is 0)
def test_you_can_cancel_all_events_of_the_same_name(fabric_fixture): ao = ActiveObject() ao.start_at(posted_event_snitch) # run 2 times fast thread_id_a = ao.post_fifo(Event(signal=signals.A), deferred=True, times=200, period=0.3) assert(thread_id_a is not None) # run forever with a period of 1 second thread_id_f = ao.post_lifo(Event(signal=signals.A), deferred=True, times=200, period=0.3) # run forever with a period of 1 second thread_id_f = ao.post_fifo(Event(signal=signals.A), deferred=False, times=200, period=0.3) assert(thread_id_f is not None) # run forever with a period of 1 second thread_id_g = ao.post_lifo(Event(signal=signals.G), deferred=True, period=1.0) assert(thread_id_g is not None) # run 2 times fast thread_id_b = ao.post_fifo(Event(signal=signals.B), deferred=False, times=200, period=0.1) assert(thread_id_b is not None) assert(len(ao.posted_events_queue) == 5) ao.cancel_events(Event(signal=signals.A)) assert(len(ao.posted_events_queue) == 2) time.sleep(0.1) assert(ao.a_signal == 1)
def test_that_it_can_post_events(fabric_fixture): '''inspect with your eyes''' ao = ActiveObject() ao.start_at(posted_event_snitch) assert(ao.thread.is_alive() is True) ao.post_lifo( Event(signal=signals.F), times=5, period=0.1, deferred=False, ) time.sleep(1) assert(ao.spy_full() == ['START', 'SEARCH_FOR_SUPER_SIGNAL:posted_event_snitch', 'ENTRY_SIGNAL:posted_event_snitch', 'INIT_SIGNAL:posted_event_snitch', '<- Queued:(0) Deferred:(0)', 'F:posted_event_snitch', 'F:posted_event_snitch:HOOK', '<- Queued:(0) Deferred:(0)', 'F:posted_event_snitch', 'F:posted_event_snitch:HOOK', '<- Queued:(0) Deferred:(0)', 'F:posted_event_snitch', 'F:posted_event_snitch:HOOK', '<- Queued:(0) Deferred:(0)', 'F:posted_event_snitch', 'F:posted_event_snitch:HOOK', '<- Queued:(0) Deferred:(0)', 'F:posted_event_snitch', 'F:posted_event_snitch:HOOK', '<- Queued:(0) Deferred:(0)'] ) assert(ao.f_signal == 5)
def example_1(): @spy_on def c(chart, e): status = return_status.UNHANDLED if(e.signal == signals.INIT_SIGNAL): status = chart.trans(c1) elif(e.signal == signals.BB): status = chart.trans(c) else: status, chart.temp.fun = return_status.SUPER, chart.top return status @spy_on def c1(chart, e): status = return_status.UNHANDLED if(e.signal == signals.A): status = chart.trans(c2) else: status, chart.temp.fun = return_status.SUPER, c return status @spy_on def c2(chart, e): status = return_status.UNHANDLED if(e.signal == signals.A): status = chart.trans(c1) else: status, chart.temp.fun = return_status.SUPER, c return status ao = ActiveObject() ao.start_at(c2) ao.post_fifo(Event(signal=signals.A)) time.sleep(0.01) # give your active object a moment to respond pp(ao.spy())
def test_you_can_post_the_same_signal_over_and_over(fabric_fixture): ao = ActiveObject() ao.start_at(posted_event_snitch) ao.post_fifo(Event(signal=signals.F), times=1, period=0.1) ao.post_lifo(Event(signal=signals.F), times=1, period=0.1) time.sleep(0.3) assert(ao.f_signal == 2)
def test_posting_too_many_events_will_raise_exception(fabric_fixture): ao = ActiveObject() ao.start_at(posted_event_snitch) with pytest.raises(ActiveObjectOutOfPostedEventResources) as execinfo: assert(execinfo is not None) for i in range(1000): ao.post_fifo(Event(signal=signals.A), times=300, period=0.1) time.sleep(0.2) # we issue an exception, but we still run as well as we can # the existing threads will work assert(ao.a_signal > 0) ao.cancel_events(Event(signal=signals.A)) assert(len(ao.posted_events_queue) is 0)
def test_that_it_defer(fabric_fixture): '''inspect with your eyes''' ao = ActiveObject() ao.start_at(posted_event_snitch) assert (ao.thread.is_alive() is True) ao.post_lifo( Event(signal=signals.F), times=1, period=3.0, deferred=True, ) time.sleep(0.2) assert (ao.spy_full() == [ 'START', 'SEARCH_FOR_SUPER_SIGNAL:posted_event_snitch', 'ENTRY_SIGNAL:posted_event_snitch', 'INIT_SIGNAL:posted_event_snitch', '<- Queued:(0) Deferred:(0)' ]) assert (ao.f_signal == 0)
def ultimate_hook_example3(): import time from miros.hsm import spy_on, pp from miros.activeobject import ActiveObject from miros.event import signals, Event, return_status @spy_on def outer_state(chart, e): status = return_status.UNHANDLED if(e.signal == signals.BEHAVIOR_NAME): # your code would go here chart.scribble("your outer_state code here") status = return_status.HANDLED else: chart.temp.fun = chart.top status = return_status.SUPER return status @spy_on def inner_state(chart, e): #if(e.signal == signals.BEHAVIOR_NAME): # # your code would go here # chart.scribble("your inner_state code here") # status = return_status.HANDLED #else: # chart.temp.fun = outer_state # status = return_status.SUPER chart.temp.fun = outer_state status = return_status.SUPER return status ao = ActiveObject() ao.start_at(inner_state) ao.post_fifo(Event(signal=signals.BEHAVIOR_NAME)) time.sleep(0.001) pp(ao.spy())
def test_you_can_cancel_an_event_thread(fabric_fixture): ao = ActiveObject() ao.start_at(posted_event_snitch) # print("") # run 2 times fast thread_id_a = ao.post_fifo(Event(signal=signals.A), deferred=False, times=20, period=0.1) # run forever with a period of 1 second thread_id_f = ao.post_fifo(Event(signal=signals.F), deferred=True, times=10, period=1.0) # run forever with a period of 1 second thread_id_g = ao.post_fifo(Event(signal=signals.G), deferred=True, period=1.0) # run 2 times fast thread_id_b = ao.post_fifo(Event(signal=signals.B), deferred=False, times=200, period=0.1) # pp(ao.posted_events_queue) assert (len(ao.posted_events_queue) == 4) ao.cancel_event(thread_id_f) time.sleep(0.2) # pp(ao.posted_events_queue) assert (len(ao.posted_events_queue) == 3) time.sleep(0.5) ao.cancel_event(thread_id_g) ao.cancel_event(thread_id_a) assert (len(ao.posted_events_queue) == 1) ao.cancel_event(thread_id_b) assert (len(ao.posted_events_queue) == 0) # demonstrate the the f signal was cancelled before it ran assert (ao.f_signal == 0) # demonstrate the the g signal was cancelled before it ran assert (ao.g_signal == 0) # demonstrate that a and b did run assert (ao.a_signal >= 1)
def test_you_can_cancel_an_event_thread(fabric_fixture): ao = ActiveObject() ao.start_at(posted_event_snitch) # print("") # run 2 times fast thread_id_a = ao.post_fifo(Event(signal=signals.A), deferred=False, times=20, period=0.1) # run forever with a period of 1 second thread_id_f = ao.post_fifo(Event(signal=signals.F), deferred=True, times=10, period=1.0) # run forever with a period of 1 second thread_id_g = ao.post_fifo(Event(signal=signals.G), deferred=True, period=1.0) # run 2 times fast thread_id_b = ao.post_fifo(Event(signal=signals.B), deferred=False, times=200, period=0.1) # pp(ao.posted_events_queue) assert(len(ao.posted_events_queue) == 4) ao.cancel_event(thread_id_f) time.sleep(0.2) # pp(ao.posted_events_queue) assert(len(ao.posted_events_queue) == 3) time.sleep(0.5) ao.cancel_event(thread_id_g) ao.cancel_event(thread_id_a) assert(len(ao.posted_events_queue) == 1) ao.cancel_event(thread_id_b) assert(len(ao.posted_events_queue) == 0) # demonstrate the the f signal was cancelled before it ran assert(ao.f_signal == 0) # demonstrate the the g signal was cancelled before it ran assert(ao.g_signal == 0) # demonstrate that a and b did run assert(ao.a_signal >= 1) assert(ao.b_signal >= 1)
def example_3(): # create the specific behavior we want in our state chart def trans_to_fc(chart, e): return chart.trans(fc) def trans_to_fc1(chart, e): return chart.trans(fc1) def trans_to_fc2(chart, e): return chart.trans(fc2) # create the states fc = state_method_template('fc') fc1 = state_method_template('fc1') fc2 = state_method_template('fc2') # build an active object, which has an event processor ao = ActiveObject() # write the design information into the fc state ao.register_signal_callback(fc, signals.BB, trans_to_fc) ao.register_signal_callback(fc, signals.INIT_SIGNAL, trans_to_fc1) ao.register_parent(fc, ao.top) # write the design information into the fc1 state ao.register_signal_callback(fc, signals.BB, trans_to_fc) ao.register_signal_callback(fc1, signals.A, trans_to_fc2) ao.register_parent(fc1, fc) # write the design information into the fc2 state ao.register_signal_callback(fc2, signals.A, trans_to_fc1) ao.register_parent(fc2, fc) # start up the active object what what it does ao.start_at(fc2) ao.post_fifo(Event(signal=signals.A)) time.sleep(0.01) pp(ao.spy()) print(ao.to_code(fc)) print(ao.to_code(fc1)) print(ao.to_code(fc2))
def t_question(): ''' +----------------------------- s -------------------------------+ | +-------- s1 ---------+ +-------- s2 -------+ | | | exit / b() | | entry / c() | | | | +--- s11 ----+ | | +---- s21 -----+ | | | | | exit / a() | | | | entry / e() | | | | | | | | | | | | | | | | | +- T [g()] / t() -> | | | | | | +------------+ | | +-----------/--+ | | | | | | *-- / d() -+ | | | +---------------------+ +-------------------+ | +---------------------------------------------------------------+ ''' import time from miros.hsm import spy_on, pp from miros.activeobject import ActiveObject from miros.event import signals, Event, return_status @spy_on def s_state(chart, e): status = return_status.UNHANDLED if (e.signal == signals.ENTRY_SIGNAL): status = return_status.HANDLED elif (e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, chart.top return status @spy_on def s1_state(chart, e): def b(chart): chart.scribble("Running b()") def g(chart): chart.scribble("Running g() -- the guard, which return False") return False def t(chart): chart.scribble("Running t() -- funtion run on event") status = return_status.UNHANDLED if (e.signal == signals.ENTRY_SIGNAL): status = return_status.HANDLED elif (e.signal == signals.EXIT_SIGNAL): b(chart) status = return_status.HANDLED elif (e.signal == signals.T): if g(chart): t(chart) status = chart.trans(s2_state) else: status, chart.temp.fun = return_status.SUPER, s_state return status @spy_on def s11_state(chart, e): def a(chart): chart.scribble("Running a()") status = return_status.UNHANDLED if (e.signal == signals.ENTRY_SIGNAL): status = return_status.HANDLED elif (e.signal == signals.EXIT_SIGNAL): a(chart) status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, s1_state return status @spy_on def s2_state(chart, e): def c(chart): chart.scribble("running c()") def d(chart): chart.scribble("running d()") status = return_status.UNHANDLED if (e.signal == signals.ENTRY_SIGNAL): c(chart) status = return_status.HANDLED elif (e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED elif (e.signal == signals.INIT_SIGNAL): d(chart) status = chart.trans(s21_state) else: status, chart.temp.fun = return_status.SUPER, s_state return status @spy_on def s21_state(chart, e): def e_function(chart): chart.scribble("running e()") status = return_status.UNHANDLED if (e.signal == signals.ENTRY_SIGNAL): e_function(chart) status = return_status.HANDLED elif (e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, s2_state return status ao = ActiveObject(name="T_question") ao.start_at(s11_state) ao.clear_spy() ao.post_fifo(Event(signal=signals.T)) time.sleep(0.2) pp(ao.spy())
def test_you_can_cancel_all_events_of_the_same_name(fabric_fixture): ao = ActiveObject() ao.start_at(posted_event_snitch) # run 2 times fast thread_id_a = ao.post_fifo(Event(signal=signals.A), deferred=True, times=200, period=0.3) assert (thread_id_a is not None) # run forever with a period of 1 second thread_id_f = ao.post_lifo(Event(signal=signals.A), deferred=True, times=200, period=0.3) # run forever with a period of 1 second thread_id_f = ao.post_fifo(Event(signal=signals.A), deferred=False, times=200, period=0.3) assert (thread_id_f is not None) # run forever with a period of 1 second thread_id_g = ao.post_lifo(Event(signal=signals.G), deferred=True, period=1.0) assert (thread_id_g is not None) # run 2 times fast thread_id_b = ao.post_fifo(Event(signal=signals.B), deferred=False, times=200, period=0.1) assert (thread_id_b is not None) assert (len(ao.posted_events_queue) == 5) ao.cancel_events(Event(signal=signals.A)) assert (len(ao.posted_events_queue) == 2) time.sleep(0.1) assert (ao.a_signal == 1)
def t_question(): ''' +----------------------------- s -------------------------------+ | +-------- s1 ---------+ +-------- s2 -------+ | | | exit / b() | | entry / c() | | | | +--- s11 ----+ | | +---- s21 -----+ | | | | | exit / a() | | | | entry / e() | | | | | | | | | | | | | | | | | +- T [g()] / t() -> | | | | | | +------------+ | | +-----------/--+ | | | | | | *-- / d() -+ | | | +---------------------+ +-------------------+ | +---------------------------------------------------------------+ ''' import time from miros.hsm import spy_on, pp from miros.activeobject import ActiveObject from miros.event import signals, Event, return_status @spy_on def s_state(chart, e): status = return_status.UNHANDLED if(e.signal == signals.ENTRY_SIGNAL): status = return_status.HANDLED elif(e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, chart.top return status @spy_on def s1_state(chart, e): def b(chart): chart.scribble("Running b()") def g(chart): chart.scribble("Running g() -- the guard, which return False") return False def t(chart): chart.scribble("Running t() -- funtion run on event") status = return_status.UNHANDLED if(e.signal == signals.ENTRY_SIGNAL): status = return_status.HANDLED elif(e.signal == signals.EXIT_SIGNAL): b(chart) status = return_status.HANDLED elif(e.signal == signals.T): if g(chart): t(chart) status = chart.trans(s2_state) else: status, chart.temp.fun = return_status.SUPER, s_state return status @spy_on def s11_state(chart, e): def a(chart): chart.scribble("Running a()") status = return_status.UNHANDLED if(e.signal == signals.ENTRY_SIGNAL): status = return_status.HANDLED elif(e.signal == signals.EXIT_SIGNAL): a(chart) status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, s1_state return status @spy_on def s2_state(chart, e): def c(chart): chart.scribble("running c()") def d(chart): chart.scribble("running d()") status = return_status.UNHANDLED if(e.signal == signals.ENTRY_SIGNAL): c(chart) status = return_status.HANDLED elif(e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED elif(e.signal == signals.INIT_SIGNAL): d(chart) status = chart.trans(s21_state) else: status, chart.temp.fun = return_status.SUPER, s_state return status @spy_on def s21_state(chart, e): def e_function(chart): chart.scribble("running e()") status = return_status.UNHANDLED if(e.signal == signals.ENTRY_SIGNAL): e_function(chart) status = return_status.HANDLED elif(e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, s2_state return status ao = ActiveObject(name="T_question") ao.start_at(s11_state) ao.clear_spy() ao.post_fifo(Event(signal=signals.T)) time.sleep(0.2) pp(ao.spy())
def test_start_stop(fabric_fixture): ao = ActiveObject(name="bob") ao.start_at(g1_s22_active_objects_graph) assert(ao.thread.is_alive() is True) time.sleep(0.2) ao.stop() assert(ao.spy_full() == ['START', 'SEARCH_FOR_SUPER_SIGNAL:g1_s22_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s1_active_objects_graph', 'ENTRY_SIGNAL:g1_s1_active_objects_graph', 'ENTRY_SIGNAL:g1_s22_active_objects_graph', 'INIT_SIGNAL:g1_s22_active_objects_graph', 'POST_FIFO:D', '<- Queued:(1) Deferred:(0)', 'D:g1_s22_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s1_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s22_active_objects_graph', 'EXIT_SIGNAL:g1_s22_active_objects_graph', 'INIT_SIGNAL:g1_s1_active_objects_graph', 'POST_FIFO:E', '<- Queued:(1) Deferred:(0)', 'E:g1_s1_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s01_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s1_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s0_active_objects_graph', 'EXIT_SIGNAL:g1_s1_active_objects_graph', 'ENTRY_SIGNAL:g1_s0_active_objects_graph', 'ENTRY_SIGNAL:g1_s01_active_objects_graph', 'POST_FIFO:A', 'POST_LIFO:F', 'INIT_SIGNAL:g1_s01_active_objects_graph', '<- Queued:(2) Deferred:(0)', 'F:g1_s01_active_objects_graph', 'F:g1_s0_active_objects_graph', 'EXIT_SIGNAL:g1_s01_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s01_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s2111_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s0_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s211_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s21_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s1_active_objects_graph', 'EXIT_SIGNAL:g1_s0_active_objects_graph', 'ENTRY_SIGNAL:g1_s1_active_objects_graph', 'ENTRY_SIGNAL:g1_s21_active_objects_graph', 'ENTRY_SIGNAL:g1_s211_active_objects_graph', 'ENTRY_SIGNAL:g1_s2111_active_objects_graph', 'INIT_SIGNAL:g1_s2111_active_objects_graph', '<- Queued:(1) Deferred:(0)', 'A:g1_s2111_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s321_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s2111_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s32_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s3_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s22_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s1_active_objects_graph', 'EXIT_SIGNAL:g1_s2111_active_objects_graph', 'EXIT_SIGNAL:g1_s211_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s211_active_objects_graph', 'EXIT_SIGNAL:g1_s21_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s21_active_objects_graph', 'ENTRY_SIGNAL:g1_s22_active_objects_graph', 'ENTRY_SIGNAL:g1_s3_active_objects_graph', 'ENTRY_SIGNAL:g1_s32_active_objects_graph', 'ENTRY_SIGNAL:g1_s321_active_objects_graph', 'INIT_SIGNAL:g1_s321_active_objects_graph', '<- Queued:(0) Deferred:(0)', 'stop_active_object:g1_s321_active_objects_graph', 'stop_active_object:g1_s32_active_objects_graph', 'stop_active_object:g1_s3_active_objects_graph', 'stop_active_object:g1_s22_active_objects_graph', 'stop_active_object:g1_s1_active_objects_graph', '<- Queued:(0) Deferred:(0)']) trace_target = \ ''' [2018-11-06 05:40:38.272609] [bob] e->start_at() top->g1_s22_active_objects_graph [2017-11-06 05:40:37.216059] [bob] e->D() g1_s22_active_objects_graph->g1_s1_active_objects_graph [2017-11-06 05:40:37.216059] [bob] e->E() g1_s1_active_objects_graph->g1_s01_active_objects_graph [2017-11-06 05:40:37.216059] [bob] e->F() g1_s01_active_objects_graph->g1_s2111_active_objects_graph [2017-11-06 05:40:37.216059] [bob] e->A() g1_s2111_active_objects_graph->g1_s321_active_objects_graph [2017-11-06 05:40:37.216059] [bob] e->stop_active_object() g1_s321_active_objects_graph->g1_s321_active_objects_graph ''' with stripped(ao.trace()) as owt, \ stripped(trace_target) as twt: for target_item, other_item in zip(twt, owt): # print(target_item) assert(target_item == other_item) ao.clear_spy() assert(ao.spy_full() == [])
def test_start_stop(fabric_fixture): ao = ActiveObject(name="bob") ao.start_at(g1_s22_active_objects_graph) assert (ao.thread.is_alive() is True) time.sleep(0.2) ao.stop() assert (ao.spy_full() == [ 'START', 'SEARCH_FOR_SUPER_SIGNAL:g1_s22_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s1_active_objects_graph', 'ENTRY_SIGNAL:g1_s1_active_objects_graph', 'ENTRY_SIGNAL:g1_s22_active_objects_graph', 'INIT_SIGNAL:g1_s22_active_objects_graph', 'POST_FIFO:D', '<- Queued:(1) Deferred:(0)', 'D:g1_s22_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s1_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s22_active_objects_graph', 'EXIT_SIGNAL:g1_s22_active_objects_graph', 'INIT_SIGNAL:g1_s1_active_objects_graph', 'POST_FIFO:E', '<- Queued:(1) Deferred:(0)', 'E:g1_s1_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s01_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s1_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s0_active_objects_graph', 'EXIT_SIGNAL:g1_s1_active_objects_graph', 'ENTRY_SIGNAL:g1_s0_active_objects_graph', 'ENTRY_SIGNAL:g1_s01_active_objects_graph', 'POST_FIFO:A', 'POST_LIFO:F', 'INIT_SIGNAL:g1_s01_active_objects_graph', '<- Queued:(2) Deferred:(0)', 'F:g1_s01_active_objects_graph', 'F:g1_s0_active_objects_graph', 'EXIT_SIGNAL:g1_s01_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s01_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s2111_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s0_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s211_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s21_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s1_active_objects_graph', 'EXIT_SIGNAL:g1_s0_active_objects_graph', 'ENTRY_SIGNAL:g1_s1_active_objects_graph', 'ENTRY_SIGNAL:g1_s21_active_objects_graph', 'ENTRY_SIGNAL:g1_s211_active_objects_graph', 'ENTRY_SIGNAL:g1_s2111_active_objects_graph', 'INIT_SIGNAL:g1_s2111_active_objects_graph', '<- Queued:(1) Deferred:(0)', 'A:g1_s2111_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s321_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s2111_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s32_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s3_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s22_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s1_active_objects_graph', 'EXIT_SIGNAL:g1_s2111_active_objects_graph', 'EXIT_SIGNAL:g1_s211_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s211_active_objects_graph', 'EXIT_SIGNAL:g1_s21_active_objects_graph', 'SEARCH_FOR_SUPER_SIGNAL:g1_s21_active_objects_graph', 'ENTRY_SIGNAL:g1_s22_active_objects_graph', 'ENTRY_SIGNAL:g1_s3_active_objects_graph', 'ENTRY_SIGNAL:g1_s32_active_objects_graph', 'ENTRY_SIGNAL:g1_s321_active_objects_graph', 'INIT_SIGNAL:g1_s321_active_objects_graph', '<- Queued:(0) Deferred:(0)' ]) #, #'stop_active_object:g1_s321_active_objects_graph', #'stop_active_object:g1_s32_active_objects_graph', #'stop_active_object:g1_s3_active_objects_graph', #'stop_active_object:g1_s22_active_objects_graph', #'stop_active_object:g1_s1_active_objects_graph', #'<- Queued:(0) Deferred:(0)']) trace_target = \ ''' [2018-11-06 05:40:38.272609] [bob] e->start_at() top->g1_s22_active_objects_graph [2017-11-06 05:40:37.216059] [bob] e->D() g1_s22_active_objects_graph->g1_s1_active_objects_graph [2017-11-06 05:40:37.216059] [bob] e->E() g1_s1_active_objects_graph->g1_s01_active_objects_graph [2017-11-06 05:40:37.216059] [bob] e->F() g1_s01_active_objects_graph->g1_s2111_active_objects_graph [2017-11-06 05:40:37.216059] [bob] e->A() g1_s2111_active_objects_graph->g1_s321_active_objects_graph [2017-11-06 05:40:37.216059] [bob] e->stop_active_object() g1_s321_active_objects_graph->g1_s321_active_objects_graph ''' with stripped(ao.trace()) as owt, \ stripped(trace_target) as twt: for target_item, other_item in zip(twt, owt): # print(target_item) assert (target_item == other_item) ao.clear_spy() assert (ao.spy_full() == [])
def example_4(): # create the specific behavior we want in our state chart def trans_to_fc(chart, e): return chart.trans(fc) def trans_to_fc1(chart, e): return chart.trans(fc1) def trans_to_fc2(chart, e): return chart.trans(fc2) @spy_on def fc(chart, e): status = return_status.UNHANDLED if(e.signal == signals.ENTRY_SIGNAL): status = return_status.HANDLED elif(e.signal == signals.INIT_SIGNAL): status = trans_to_fc1(chart, e) elif(e.signal == signals.BB): status = trans_to_fc(chart, e) elif(e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, chart.top return status @spy_on def fc1(chart, e): status = return_status.UNHANDLED if(e.signal == signals.ENTRY_SIGNAL): status = return_status.HANDLED elif(e.signal == signals.INIT_SIGNAL): status = return_status.HANDLED elif(e.signal == signals.A): status = trans_to_fc2(chart, e) elif(e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, fc return status @spy_on def fc2(chart, e): status = return_status.UNHANDLED if(e.signal == signals.ENTRY_SIGNAL): status = return_status.HANDLED elif(e.signal == signals.INIT_SIGNAL): status = return_status.HANDLED elif(e.signal == signals.A): status = trans_to_fc1(chart, e) elif(e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, fc return status # # create the states # fc = state_method_template('fc') # fc1 = state_method_template('fc1') # fc2 = state_method_template('fc2') # build an active object, which has an event processor ao = ActiveObject() # write the design information into the fc state # ao.register_signal_callback(fc, signals.BB, trans_to_fc) # ao.register_signal_callback(fc, signals.INIT_SIGNAL, trans_to_fc1) # ao.register_parent(fc, ao.top) # # write the design information into the fc1 state # ao.register_signal_callback(fc, signals.BB, trans_to_fc) # ao.register_signal_callback(fc1, signals.A, trans_to_fc2) # ao.register_parent(fc1, fc) # # write the design information into the fc2 state # ao.register_signal_callback(fc2, signals.A, trans_to_fc1) # ao.register_parent(fc2, fc) # start up the active object what what it does ao.start_at(fc2) ao.post_fifo(Event(signal=signals.A)) time.sleep(0.01) pp(ao.spy())
def example_2(): @spy_on def tc(chart, e): with chart.signal_callback(e, tc) as fn: status = fn(chart, e) if (status == return_status.UNHANDLED): with chart.parent_callback() as parent: status, chart.temp.fun = return_status.SUPER, parent return status @spy_on def tc1(chart, e): with chart.signal_callback(e, tc1) as fn: status = fn(chart, e) if (status == return_status.UNHANDLED): with chart.parent_callback() as parent: status, chart.temp.fun = return_status.SUPER, parent return status @spy_on def tc2(chart, e): with chart.signal_callback(e, tc2) as fn: status = fn(chart, e) if (status == return_status.UNHANDLED): with chart.parent_callback() as parent: status, chart.temp.fun = return_status.SUPER, parent return status def trans_to_tc(chart, e): return chart.trans(tc) def trans_to_tc1(chart, e): return chart.trans(tc1) def trans_to_tc2(chart, e): return chart.trans(tc2) def do_nothing(chart, e): return return_status.HANDLED ao = ActiveObject() ao.register_signal_callback(tc, signals.BB, trans_to_tc) ao.register_signal_callback(tc, signals.ENTRY_SIGNAL, do_nothing) ao.register_signal_callback(tc, signals.EXIT_SIGNAL, do_nothing) ao.register_signal_callback(tc, signals.INIT_SIGNAL, trans_to_tc1) ao.register_parent(tc, ao.top) ao.register_signal_callback(tc1, signals.A, trans_to_tc2) ao.register_signal_callback(tc1, signals.ENTRY_SIGNAL, do_nothing) ao.register_signal_callback(tc1, signals.EXIT_SIGNAL, do_nothing) ao.register_signal_callback(tc1, signals.INIT_SIGNAL, do_nothing) ao.register_parent(tc1, tc) ao.register_signal_callback(tc2, signals.A, trans_to_tc1) ao.register_signal_callback(tc2, signals.ENTRY_SIGNAL, do_nothing) ao.register_signal_callback(tc2, signals.EXIT_SIGNAL, do_nothing) ao.register_signal_callback(tc2, signals.INIT_SIGNAL, do_nothing) ao.register_parent(tc2, tc) ao.start_at(tc2) ao.post_fifo(Event(signal=signals.A)) time.sleep(0.01) pp(ao.spy())
def example_4(): # create the specific behavior we want in our state chart def trans_to_fc(chart, e): return chart.trans(fc) def trans_to_fc1(chart, e): return chart.trans(fc1) def trans_to_fc2(chart, e): return chart.trans(fc2) @spy_on def fc(chart, e): status = return_status.UNHANDLED if (e.signal == signals.ENTRY_SIGNAL): status = return_status.HANDLED elif (e.signal == signals.INIT_SIGNAL): status = trans_to_fc1(chart, e) elif (e.signal == signals.BB): status = trans_to_fc(chart, e) elif (e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, chart.top return status @spy_on def fc1(chart, e): status = return_status.UNHANDLED if (e.signal == signals.ENTRY_SIGNAL): status = return_status.HANDLED elif (e.signal == signals.INIT_SIGNAL): status = return_status.HANDLED elif (e.signal == signals.A): status = trans_to_fc2(chart, e) elif (e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, fc return status @spy_on def fc2(chart, e): status = return_status.UNHANDLED if (e.signal == signals.ENTRY_SIGNAL): status = return_status.HANDLED elif (e.signal == signals.INIT_SIGNAL): status = return_status.HANDLED elif (e.signal == signals.A): status = trans_to_fc1(chart, e) elif (e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, fc return status # # create the states # fc = state_method_template('fc') # fc1 = state_method_template('fc1') # fc2 = state_method_template('fc2') # build an active object, which has an event processor ao = ActiveObject() # write the design information into the fc state # ao.register_signal_callback(fc, signals.BB, trans_to_fc) # ao.register_signal_callback(fc, signals.INIT_SIGNAL, trans_to_fc1) # ao.register_parent(fc, ao.top) # # write the design information into the fc1 state # ao.register_signal_callback(fc, signals.BB, trans_to_fc) # ao.register_signal_callback(fc1, signals.A, trans_to_fc2) # ao.register_parent(fc1, fc) # # write the design information into the fc2 state # ao.register_signal_callback(fc2, signals.A, trans_to_fc1) # ao.register_parent(fc2, fc) # start up the active object what what it does ao.start_at(fc2) ao.post_fifo(Event(signal=signals.A)) time.sleep(0.01) pp(ao.spy())
def test_publish_subscribe(fabric_fixture): c1 = ActiveObject("c1") c2 = ActiveObject("c2") b = ActiveObject("b") c1.subscribe(signals.BB) c2.subscribe(Event(signal=signals.BB)) c1.subscribe(Event(signal=signals.CC)) c2.subscribe(Event(signal=signals.CC)) b.live_trace = True c1.live_trace = True c2.live_trace = True c1.start_at(c2_s2) c2.start_at(c2_s2) b.start_at(b1_s2) time.sleep(0.2) c1.post_fifo(Event(signal=signals.CC)) time.sleep(0.1) assert(c1.spy_full() == [ 'SUBSCRIBING TO:(BB, TYPE:fifo)', 'SUBSCRIBING TO:(CC, TYPE:fifo)', 'START', 'SEARCH_FOR_SUPER_SIGNAL:c2_s2', 'SEARCH_FOR_SUPER_SIGNAL:c2_s1', 'ENTRY_SIGNAL:c2_s1', 'ENTRY_SIGNAL:c2_s2', 'INIT_SIGNAL:c2_s2', '<- Queued:(0) Deferred:(0)', 'BB:c2_s2', 'BB:c2_s1', 'EXIT_SIGNAL:c2_s2', 'SEARCH_FOR_SUPER_SIGNAL:c2_s2', 'EXIT_SIGNAL:c2_s1', 'ENTRY_SIGNAL:c2_s1', 'INIT_SIGNAL:c2_s1', '<- Queued:(0) Deferred:(0)', 'CC:c2_s1', '<- Queued:(0) Deferred:(0)' ] ) assert(b.spy_full() == ['START', 'SEARCH_FOR_SUPER_SIGNAL:b1_s2', 'SEARCH_FOR_SUPER_SIGNAL:b1_s1', 'ENTRY_SIGNAL:b1_s1', 'ENTRY_SIGNAL:b1_s2', 'INIT_SIGNAL:b1_s2', 'PUBLISH:(BB, PRIORITY:1000)', '<- Queued:(0) Deferred:(0)'] ) assert(c2.spy_full() == ['SUBSCRIBING TO:(BB, TYPE:fifo)', 'SUBSCRIBING TO:(CC, TYPE:fifo)', 'START', 'SEARCH_FOR_SUPER_SIGNAL:c2_s2', 'SEARCH_FOR_SUPER_SIGNAL:c2_s1', 'ENTRY_SIGNAL:c2_s1', 'ENTRY_SIGNAL:c2_s2', 'INIT_SIGNAL:c2_s2', '<- Queued:(0) Deferred:(0)', 'BB:c2_s2', 'BB:c2_s1', 'EXIT_SIGNAL:c2_s2', 'SEARCH_FOR_SUPER_SIGNAL:c2_s2', 'EXIT_SIGNAL:c2_s1', 'ENTRY_SIGNAL:c2_s1', 'INIT_SIGNAL:c2_s1', '<- Queued:(0) Deferred:(0)'] )
def test_import(fabric_fixture): ao = ActiveObject() assert (ao is not None)
def example_2(): @spy_on def tc(chart, e): with chart.signal_callback(e, tc) as fn: status = fn(chart, e) if(status == return_status.UNHANDLED): with chart.parent_callback() as parent: status, chart.temp.fun = return_status.SUPER, parent return status @spy_on def tc1(chart, e): with chart.signal_callback(e, tc1) as fn: status = fn(chart, e) if(status == return_status.UNHANDLED): with chart.parent_callback() as parent: status, chart.temp.fun = return_status.SUPER, parent return status @spy_on def tc2(chart, e): with chart.signal_callback(e, tc2) as fn: status = fn(chart, e) if(status == return_status.UNHANDLED): with chart.parent_callback() as parent: status, chart.temp.fun = return_status.SUPER, parent return status def trans_to_tc(chart, e): return chart.trans(tc) def trans_to_tc1(chart, e): return chart.trans(tc1) def trans_to_tc2(chart, e): return chart.trans(tc2) def do_nothing(chart, e): return return_status.HANDLED ao = ActiveObject() ao.register_signal_callback(tc, signals.BB, trans_to_tc) ao.register_signal_callback(tc, signals.ENTRY_SIGNAL, do_nothing) ao.register_signal_callback(tc, signals.EXIT_SIGNAL, do_nothing) ao.register_signal_callback(tc, signals.INIT_SIGNAL, trans_to_tc1) ao.register_parent(tc, ao.top) ao.register_signal_callback(tc1, signals.A, trans_to_tc2) ao.register_signal_callback(tc1, signals.ENTRY_SIGNAL, do_nothing) ao.register_signal_callback(tc1, signals.EXIT_SIGNAL, do_nothing) ao.register_signal_callback(tc1, signals.INIT_SIGNAL, do_nothing) ao.register_parent(tc1, tc) ao.register_signal_callback(tc2, signals.A, trans_to_tc1) ao.register_signal_callback(tc2, signals.ENTRY_SIGNAL, do_nothing) ao.register_signal_callback(tc2, signals.EXIT_SIGNAL, do_nothing) ao.register_signal_callback(tc2, signals.INIT_SIGNAL, do_nothing) ao.register_parent(tc2, tc) ao.start_at(tc2) ao.post_fifo(Event(signal=signals.A)) time.sleep(0.01) pp(ao.spy())
def test_publish_subscribe(fabric_fixture): c1 = ActiveObject("c1") c2 = ActiveObject("c2") b = ActiveObject("b") c1.subscribe(signals.BB) c2.subscribe(Event(signal=signals.BB)) c1.subscribe(Event(signal=signals.CC)) c2.subscribe(Event(signal=signals.CC)) b.live_trace = True c1.live_trace = True c2.live_trace = True c1.start_at(c2_s2) c2.start_at(c2_s2) b.start_at(b1_s2) time.sleep(0.2) c1.post_fifo(Event(signal=signals.CC)) time.sleep(0.1) assert(c1.spy_full() == \ ['POST_LIFO:SUBSCRIBE_META_SIGNAL', 'POST_LIFO:SUBSCRIBE_META_SIGNAL', 'START', 'SEARCH_FOR_SUPER_SIGNAL:c2_s2', 'SEARCH_FOR_SUPER_SIGNAL:c2_s1', 'ENTRY_SIGNAL:c2_s1', 'ENTRY_SIGNAL:c2_s2', 'INIT_SIGNAL:c2_s2', '<- Queued:(2) Deferred:(0)', 'SUBSCRIBE_META_SIGNAL:c2_s2', 'SUBSCRIBE_META_SIGNAL:c2_s1', 'SUBSCRIBING TO:(CC, TYPE:fifo)', '<- Queued:(1) Deferred:(0)', 'SUBSCRIBE_META_SIGNAL:c2_s2', 'SUBSCRIBE_META_SIGNAL:c2_s1', 'SUBSCRIBING TO:(BB, TYPE:fifo)', '<- Queued:(0) Deferred:(0)', 'BB:c2_s2', 'BB:c2_s1', 'EXIT_SIGNAL:c2_s2', 'SEARCH_FOR_SUPER_SIGNAL:c2_s2', 'EXIT_SIGNAL:c2_s1', 'ENTRY_SIGNAL:c2_s1', 'INIT_SIGNAL:c2_s1', '<- Queued:(0) Deferred:(0)', 'CC:c2_s1', '<- Queued:(0) Deferred:(0)'] ) assert (b.spy_full() == [ 'START', 'SEARCH_FOR_SUPER_SIGNAL:b1_s2', 'SEARCH_FOR_SUPER_SIGNAL:b1_s1', 'ENTRY_SIGNAL:b1_s1', 'ENTRY_SIGNAL:b1_s2', 'INIT_SIGNAL:b1_s2', 'POST_LIFO:PUBLISH_META_SIGNAL', '<- Queued:(1) Deferred:(0)', 'PUBLISH_META_SIGNAL:b1_s2', 'PUBLISH_META_SIGNAL:b1_s1', 'PUBLISH:(BB, PRIORITY:1000)', '<- Queued:(0) Deferred:(0)' ]) assert (c2.spy_full() == [ 'POST_LIFO:SUBSCRIBE_META_SIGNAL', 'POST_LIFO:SUBSCRIBE_META_SIGNAL', 'START', 'SEARCH_FOR_SUPER_SIGNAL:c2_s2', 'SEARCH_FOR_SUPER_SIGNAL:c2_s1', 'ENTRY_SIGNAL:c2_s1', 'ENTRY_SIGNAL:c2_s2', 'INIT_SIGNAL:c2_s2', '<- Queued:(2) Deferred:(0)', 'SUBSCRIBE_META_SIGNAL:c2_s2', 'SUBSCRIBE_META_SIGNAL:c2_s1', 'SUBSCRIBING TO:(CC, TYPE:fifo)', '<- Queued:(1) Deferred:(0)', 'SUBSCRIBE_META_SIGNAL:c2_s2', 'SUBSCRIBE_META_SIGNAL:c2_s1', 'SUBSCRIBING TO:(BB, TYPE:fifo)', '<- Queued:(0) Deferred:(0)', 'BB:c2_s2', 'BB:c2_s1', 'EXIT_SIGNAL:c2_s2', 'SEARCH_FOR_SUPER_SIGNAL:c2_s2', 'EXIT_SIGNAL:c2_s1', 'ENTRY_SIGNAL:c2_s1', 'INIT_SIGNAL:c2_s1', '<- Queued:(0) Deferred:(0)' ])