def test_scribble_to_learn_nature_of_event_processor(): ao = ActiveObject(name="Testing") ao.start_at(s11_state) time.sleep(0.1) ao.post_fifo(Event(signal=signals.T)) time.sleep(0.5) pp(ao.spy())
def test_live_spys(fabric_fixture): tazor = ActiveObject() tazor.live_spy = True tazor.live_trace = True tazor.start_at(arming) time.sleep(0.1) tazor.post_fifo(Event(signal=signals.READY)) time.sleep(0.1)
def test_trace_testing(fabric_fixture): tazor = ActiveObject() tazor.start_at(arming) time.sleep(0.4) target_with_timestamp = tazor.trace() print(target_with_timestamp) other_with_timestamp = tazor.trace() with stripped(target_with_timestamp) as twt, \ stripped(other_with_timestamp) as owt: for target_item, other_item in zip(twt, owt): print(target_item) assert(target_item == other_item) with stripped('[2017-11-05 15:17:39.424492] [75c8c] e->BATTERY_CHARGE() armed->armed') as swt: assert(swt == '[75c8c] e->BATTERY_CHARGE() armed->armed') assert(tazor.state.fun.__name__ == 'armed')
@spy_on def c2(chart, e): status = return_status.UNHANDLED if (e.signal == signals.ENTRY_SIGNAL): print("c2 entered") status = return_status.HANDLED elif (e.signal == signals.A): status = chart.trans(c1) else: chart.temp.fun = c status = return_status.SUPER return status if __name__ == "__main__": ao = ActiveObject('start_example') print("calling: start_at(c2)") ao.start_at(c2) time.sleep(0.2) print(ao.trace()) # print what happened from the start_at call ao.clear_trace() # clear our instrumentation print("sending B, then A, then A:") ao.post_fifo(Event(signal=signals.B)) ao.post_fifo(Event(signal=signals.A)) ao.post_fifo(Event(signal=signals.A)) time.sleep(0.2) print(ao.trace()) # print what happened
def test_to_code(): ts1 = state_method_template('ts1') tc2_s2 = state_method_template('tc2_s2') tc2_s3 = state_method_template('tc2_s3') def trans_to_s1(chart, e): return chart.trans(ts1) def trans_to_c2_s3(chart, e): return chart.trans(tc2_s3) def trans_to_c2_s2(chart, e): return chart.trans(tc2_s2) def handled(chart, e): return return_status.HANDLED ao = ActiveObject() ao.register_signal_callback(ts1, signals.BB, trans_to_s1) ao.register_signal_callback(ts1, signals.ENTRY_SIGNAL, handled) ao.register_signal_callback(ts1, signals.EXIT_SIGNAL, handled) ao.register_signal_callback(ts1, signals.INIT_SIGNAL, trans_to_c2_s2) ao.register_parent(ts1, ao.top) # commented out line needed for test to work ao.register_signal_callback(tc2_s2, signals.A, trans_to_c2_s3) ao.register_signal_callback(tc2_s2, signals.ENTRY_SIGNAL, handled) ao.register_signal_callback(tc2_s2, signals.EXIT_SIGNAL, handled) ao.register_signal_callback(tc2_s2, signals.INIT_SIGNAL, handled) ao.register_parent(tc2_s2, ts1) # commented out lines needed for test to work ao.register_signal_callback(tc2_s3, signals.A, trans_to_c2_s2) ao.register_signal_callback(tc2_s3, signals.ENTRY_SIGNAL, handled) ao.register_signal_callback(tc2_s3, signals.EXIT_SIGNAL, handled) ao.register_signal_callback(tc2_s3, signals.INIT_SIGNAL, handled) ao.register_parent(tc2_s3, ts1) expected_ts1_as_flat_code = \ ''' @spy_on def ts1(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_c2_s2(chart, e) elif(e.signal == signals.BB): status = trans_to_s1(chart, e) elif(e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, chart.top return status ''' assert(ao.to_code(ts1) == expected_ts1_as_flat_code) expected_tc2_s2_as_flat_code = \ ''' @spy_on def tc2_s2(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_c2_s3(chart, e) elif(e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, ts1 return status ''' assert(ao.to_code(tc2_s2) == expected_tc2_s2_as_flat_code) expected_tc2_s3_as_flat_code = \ ''' @spy_on def tc2_s3(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_c2_s2(chart, e) elif(e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED else: status, chart.temp.fun = return_status.SUPER, ts1 return status ''' assert(ao.to_code(tc2_s3) == expected_tc2_s3_as_flat_code) ao.start_at(tc2_s2) ao.post_fifo(Event(signal=signals.A)) ao.post_fifo(Event(signal=signals.A)) ao.post_fifo(Event(signal=signals.BB)) time.sleep(0.1) assert(ao.spy() == \ ['START', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s2', 'SEARCH_FOR_SUPER_SIGNAL:ts1', 'ENTRY_SIGNAL:ts1', 'ENTRY_SIGNAL:tc2_s2', 'INIT_SIGNAL:tc2_s2', '<- Queued:(0) Deferred:(0)', 'A:tc2_s2', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s3', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s2', 'EXIT_SIGNAL:tc2_s2', 'ENTRY_SIGNAL:tc2_s3', 'INIT_SIGNAL:tc2_s3', '<- Queued:(2) Deferred:(0)', 'A:tc2_s3', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s2', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s3', 'EXIT_SIGNAL:tc2_s3', 'ENTRY_SIGNAL:tc2_s2', 'INIT_SIGNAL:tc2_s2', '<- Queued:(1) Deferred:(0)', 'BB:tc2_s2', 'BB:ts1', 'EXIT_SIGNAL:tc2_s2', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s2', 'EXIT_SIGNAL:ts1', 'ENTRY_SIGNAL:ts1', 'INIT_SIGNAL:ts1', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s2', 'ENTRY_SIGNAL:tc2_s2', 'INIT_SIGNAL:tc2_s2', '<- Queued:(0) Deferred:(0)'])
def test_interior_postings_example(fabric_fixture): ao = ActiveObject() ao.start_at(middle) time.sleep(0.4) ao.post_fifo(Event(signal=signals.D)) time.sleep(0.1) # if you don't wait it won't look like it is working pp(ao.spy) assert(ao.spy_full() == ['START', 'SEARCH_FOR_SUPER_SIGNAL:middle', 'SEARCH_FOR_SUPER_SIGNAL:outer', 'ENTRY_SIGNAL:outer', 'ENTRY_SIGNAL:middle', 'INIT_SIGNAL:middle', '<- Queued:(0) Deferred:(0)', 'A:middle', 'SEARCH_FOR_SUPER_SIGNAL:inner', 'ENTRY_SIGNAL:inner', 'POST_DEFERRED:B', 'INIT_SIGNAL:inner', '<- Queued:(0) Deferred:(1)', 'A:inner', 'A:middle', 'EXIT_SIGNAL:inner', 'SEARCH_FOR_SUPER_SIGNAL:inner', 'ENTRY_SIGNAL:inner', 'POST_DEFERRED:B', 'INIT_SIGNAL:inner', '<- Queued:(0) Deferred:(2)', 'A:inner', 'A:middle', 'EXIT_SIGNAL:inner', 'SEARCH_FOR_SUPER_SIGNAL:inner', 'ENTRY_SIGNAL:inner', 'POST_DEFERRED:B', 'INIT_SIGNAL:inner', '<- Queued:(0) Deferred:(3)', 'D:inner', 'D:middle', 'D:outer', 'RECALL:B', 'POST_FIFO:B', 'D:outer:HOOK', '<- Queued:(1) Deferred:(2)', 'B:inner', 'B:middle', 'B:outer', 'EXIT_SIGNAL:inner', 'EXIT_SIGNAL:middle', 'EXIT_SIGNAL:outer', 'ENTRY_SIGNAL:outer', 'RECALL:B', 'POST_FIFO:B', 'INIT_SIGNAL:outer', '<- Queued:(1) Deferred:(1)', 'B:outer', 'EXIT_SIGNAL:outer', 'ENTRY_SIGNAL:outer', 'RECALL:B', 'POST_FIFO:B', 'INIT_SIGNAL:outer', '<- Queued:(1) Deferred:(0)', 'B:outer', 'EXIT_SIGNAL:outer', 'ENTRY_SIGNAL:outer', 'INIT_SIGNAL:outer', '<- Queued:(0) Deferred:(0)'])
def test_tazor_example(fabric_fixture): tazor = ActiveObject() tazor.start_at(arming) time.sleep(0.4) tazor.post_fifo(Event(signal=signals.TRIGGER_PULLED)) time.sleep(0.1) # if you don't wait it won't look like it is working assert(tazor.spy_full() == ['START', 'SEARCH_FOR_SUPER_SIGNAL:arming', 'SEARCH_FOR_SUPER_SIGNAL:tazor_operating', 'ENTRY_SIGNAL:tazor_operating', 'ENTRY_SIGNAL:arming', 'INIT_SIGNAL:arming', '<- Queued:(0) Deferred:(0)', 'BATTERY_CHARGE:arming', 'SEARCH_FOR_SUPER_SIGNAL:armed', 'ENTRY_SIGNAL:armed', 'POST_DEFERRED:CAPACITOR_CHARGE', 'INIT_SIGNAL:armed', '<- Queued:(0) Deferred:(1)', 'BATTERY_CHARGE:armed', 'BATTERY_CHARGE:arming', 'EXIT_SIGNAL:armed', 'SEARCH_FOR_SUPER_SIGNAL:armed', 'ENTRY_SIGNAL:armed', 'POST_DEFERRED:CAPACITOR_CHARGE', 'INIT_SIGNAL:armed', '<- Queued:(0) Deferred:(2)', 'BATTERY_CHARGE:armed', 'BATTERY_CHARGE:arming', 'EXIT_SIGNAL:armed', 'SEARCH_FOR_SUPER_SIGNAL:armed', 'ENTRY_SIGNAL:armed', 'POST_DEFERRED:CAPACITOR_CHARGE', 'INIT_SIGNAL:armed', '<- Queued:(0) Deferred:(3)', 'TRIGGER_PULLED:armed', 'TRIGGER_PULLED:arming', 'TRIGGER_PULLED:tazor_operating', 'RECALL:CAPACITOR_CHARGE', 'POST_FIFO:CAPACITOR_CHARGE', 'TRIGGER_PULLED:tazor_operating:HOOK', '<- Queued:(1) Deferred:(2)', 'CAPACITOR_CHARGE:armed', 'CAPACITOR_CHARGE:arming', 'CAPACITOR_CHARGE:tazor_operating', 'EXIT_SIGNAL:armed', 'EXIT_SIGNAL:arming', 'EXIT_SIGNAL:tazor_operating', 'ENTRY_SIGNAL:tazor_operating', 'RECALL:CAPACITOR_CHARGE', 'POST_FIFO:CAPACITOR_CHARGE', 'INIT_SIGNAL:tazor_operating', '<- Queued:(1) Deferred:(1)', 'CAPACITOR_CHARGE:tazor_operating', 'EXIT_SIGNAL:tazor_operating', 'ENTRY_SIGNAL:tazor_operating', 'RECALL:CAPACITOR_CHARGE', 'POST_FIFO:CAPACITOR_CHARGE', 'INIT_SIGNAL:tazor_operating', '<- Queued:(1) Deferred:(0)', 'CAPACITOR_CHARGE:tazor_operating', 'EXIT_SIGNAL:tazor_operating', 'ENTRY_SIGNAL:tazor_operating', 'INIT_SIGNAL:tazor_operating', '<- Queued:(0) Deferred:(0)']) print(tazor.trace())
def test_graffiti_spy(): ao = ActiveObject(name="scribbled_on") ao.start_at(g1_s22_active_objects_graph) time.sleep(0.2) pp(ao.spy())
def test1_trans_topology_a(): ao = ActiveObject() ao.start_at(outer) pp(ao.spy_full()) assert(ao.spy_full() == ['START', 'SEARCH_FOR_SUPER_SIGNAL:outer', 'ENTRY_SIGNAL:outer', 'INIT_SIGNAL:outer', '<- Queued:(0) Deferred:(0)'] ) pp(ao.spy_full()) print(ao.trace()) event_w = Event(signal=signals.WaitComplete) ao.clear_trace() ao.post_fifo(event_w) import time time.sleep(0.1) pp(ao.spy_rtc()) print(ao.trace()) # stop the threads ao.stop() # clear the spy and the trace ao.clear_spy() ao.clear_trace() # post a number of events and see what happens event_wait_complete = Event(signal=signals.WaitComplete) event_reset_chart = Event(signal=signals.ResetChart) ao.post_fifo(event_wait_complete) ao.post_fifo(event_reset_chart) ao.post_fifo(event_wait_complete) ao.post_fifo(event_reset_chart) time.sleep(0.3) print(ao.trace()) pp(ao.spy_full())
import time from miros import spy_on from miros import ActiveObject from miros import signals, Event, return_status @spy_on def some_state(chart, e): status = return_status.UNHANDLED if(e.signal == signals.Destroy_This_Chart): chart.stop() chart.scribble("Terminating Thread") status = return_status.HANDLED else: chart.temp.fun = chart.top status = return_status.SUPER return status if __name__ == "__main__": ao = ActiveObject('some_state') ao.live_spy = True ao.start_at(some_state) time.sleep(0.1) assert(ao.thread.is_alive() == True) ao.post_fifo(Event(signal=signals.Destroy_This_Chart)) time.sleep(0.1) assert(ao.thread.is_alive() == False)
@spy_on def source_state(chart, e): status = return_status.UNHANDLED if (e.signal == signals.SIGNAL_NAME): if guard(): action() chart.post_fifo(Event(signal=signals.EVT_A)) status = chart.trans(target_state) else: chart.temp.fun = chart.top status = return_status.SUPER return status @spy_on def target_state(chart, e): chart.temp.fun = chart.top status = return_status.SUPER return status if __name__ == "__main__": # event arrow example ao = ActiveObject('eae') ao.live_trace = True ao.start_at(source_state) ao.post_fifo( Event(signal=signals.SIGNAL_NAME, payload=OptionalPayload(x='1'))) time.sleep(0.01)
def test_graffiti_spy(): ao = ActiveObject(name="scribbled_on") ao.start_at(g1_s22_active_objects_graph) time.sleep(0.2) ao.stop() pp(ao.spy())
def test_creating_functions_from_a_template(): ts1 = state_method_template('ts1') tc2_s2 = state_method_template('tc2_s2') tc2_s3 = state_method_template('tc2_s3') def trans_to_s1(chart, e): return chart.trans(ts1) def trans_to_c2_s3(chart, e): return chart.trans(tc2_s3) def trans_to_c2_s2(chart, e): return chart.trans(tc2_s2) def handled(chart, e): return return_status.HANDLED ao = ActiveObject() ao.register_signal_callback(ts1, signals.BB, trans_to_s1) ao.register_signal_callback(ts1, signals.ENTRY_SIGNAL, handled) ao.register_signal_callback(ts1, signals.EXIT_SIGNAL, handled) ao.register_signal_callback(ts1, signals.INIT_SIGNAL, trans_to_c2_s2) ao.register_parent(ts1, ao.top) ao.register_signal_callback(tc2_s2, signals.A, trans_to_c2_s3) ao.register_signal_callback(tc2_s2, signals.ENTRY_SIGNAL, handled) ao.register_signal_callback(tc2_s2, signals.EXIT_SIGNAL, handled) ao.register_signal_callback(tc2_s2, signals.INIT_SIGNAL, handled) ao.register_parent(tc2_s2, ts1) ao.register_signal_callback(tc2_s3, signals.A, trans_to_c2_s2) ao.register_signal_callback(tc2_s3, signals.ENTRY_SIGNAL, handled) ao.register_signal_callback(tc2_s3, signals.EXIT_SIGNAL, handled) ao.register_signal_callback(tc2_s3, signals.INIT_SIGNAL, handled) ao.register_parent(tc2_s3, ts1) ao.start_at(tc2_s2) ao.post_fifo(Event(signal=signals.A)) ao.post_fifo(Event(signal=signals.A)) ao.post_fifo(Event(signal=signals.BB)) time.sleep(0.1) assert(ao.spy() == \ ['START', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s2', 'SEARCH_FOR_SUPER_SIGNAL:ts1', 'ENTRY_SIGNAL:ts1', 'ENTRY_SIGNAL:tc2_s2', 'INIT_SIGNAL:tc2_s2', '<- Queued:(0) Deferred:(0)', 'A:tc2_s2', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s3', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s2', 'EXIT_SIGNAL:tc2_s2', 'ENTRY_SIGNAL:tc2_s3', 'INIT_SIGNAL:tc2_s3', '<- Queued:(2) Deferred:(0)', 'A:tc2_s3', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s2', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s3', 'EXIT_SIGNAL:tc2_s3', 'ENTRY_SIGNAL:tc2_s2', 'INIT_SIGNAL:tc2_s2', '<- Queued:(1) Deferred:(0)', 'BB:tc2_s2', 'BB:ts1', 'EXIT_SIGNAL:tc2_s2', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s2', 'EXIT_SIGNAL:ts1', 'ENTRY_SIGNAL:ts1', 'INIT_SIGNAL:ts1', 'SEARCH_FOR_SUPER_SIGNAL:tc2_s2', 'ENTRY_SIGNAL:tc2_s2', 'INIT_SIGNAL:tc2_s2', '<- Queued:(0) Deferred:(0)'])
# simple_state_1.py import time from miros import Event from miros import signals from miros import ActiveObject from miros import return_status def outer_state(chart, e): chart.temp.fun = chart.top status = return_status.SUPER return status def inner_state(chart, e): chart.temp.fun = outer_state status = return_status.SUPER return status if __name__ == '__main__': ao = ActiveObject('bob') ao.start_at(outer_state) time.sleep(1)
print("hello from outer_state") status = return_status.HANDLED elif (e.signal == signals.INIT_SIGNAL): print("init") status = chart.trans(inner_state) else: chart.temp.fun = chart.top status = return_status.SUPER return status @spy_on def inner_state(chart, e): status = return_status.UNHANDLED # the event process automatically sends # an event named ENTRY_SIGNAL when a state is entered if (e.signal == signals.ENTRY_SIGNAL): print("hello from inner_state") status = return_status.HANDLED else: chart.temp.fun = outer_state status = return_status.SUPER return status if __name__ == '__main__': ao = ActiveObject('ao') ao.live_trace = True ao.start_at(outer_state) time.sleep(0.01)
@spy_on def a(chart, e): status = return_status.UNHANDLED if (e.signal == signals.SIGNAL_NAME): if guard(): action() status = return_status.HANDLED else: chart.temp.fun = chart.top status = return_status.SUPER return status @spy_on def a1(chart, e): chart.temp.fun = a status = return_status.SUPER return status if __name__ == "__main__": # simple hook example ao = ActiveObject(name="she") ao.live_trace = True ao.start_at(a1) ao.post_fifo( Event(signal=signals.SIGNAL_NAME, payload=OptionalPayload(x=2))) # starting another thread, let it run for a moment before we shut down time.sleep(0.01) print(ao.state_name)
def augmenting_state_methods_after_creation_and_test(request): def trans_to_s1(chart, e): return chart.trans(s1) def trans_to_c2_s3(chart, e): return chart.trans(s3) def trans_to_c2_s2(chart, e): return chart.trans(s2) def handled(chart, e): return return_status.HANDLED ao = ActiveObject() ao.register_signal_callback(s1, signals.BB, trans_to_s1) ao.register_signal_callback(s1, signals.ENTRY_SIGNAL, handled) ao.register_signal_callback(s1, signals.EXIT_SIGNAL, handled) ao.register_signal_callback(s1, signals.INIT_SIGNAL, trans_to_c2_s2) ao.register_parent(s1, ao.top) ao.register_signal_callback(s2, signals.A, trans_to_c2_s3) ao.register_signal_callback(s2, signals.ENTRY_SIGNAL, handled) ao.register_signal_callback(s2, signals.EXIT_SIGNAL, handled) ao.register_signal_callback(s2, signals.INIT_SIGNAL, handled) ao.register_parent(s2, s1) ao.register_signal_callback(s3, signals.A, trans_to_c2_s2) ao.register_signal_callback(s3, signals.ENTRY_SIGNAL, handled) ao.register_signal_callback(s3, signals.EXIT_SIGNAL, handled) ao.register_signal_callback(s3, signals.INIT_SIGNAL, handled) ao.register_parent(s3, s1) ao.start_at(s2) ao.post_fifo(Event(signal=signals.A)) ao.post_fifo(Event(signal=signals.A)) ao.post_fifo(Event(signal=signals.BB)) time.sleep(0.10) yield(ao)
status = chart.trans(off) elif (e.signal == signals.too_hot): status = chart.trans(idling) else: chart.temp.fun = chart.top status = return_status.SUPER return status @spy_on def idling(chart, e): status = return_status.UNHANDLED if (e.signal == signals.too_cold): status = chart.trans(heating) else: chart.temp.fun = chart.top status = return_status.SUPER return status if __name__ == "__main__": ao = ActiveObject('simple_fsm_2') ao.live_trace = True ao.start_at(off) ao.post_fifo(Event(signal=signals.bake_pressed)) ao.post_fifo(Event(signal=signals.off_pressed)) ao.post_fifo(Event(signal=signals.bake_pressed)) ao.post_fifo(Event(signal=signals.too_hot)) ao.post_fifo(Event(signal=signals.too_cold)) time.sleep(0.01)