def wta_entry(archer, e): archer.yell(Event(signal=signals.Other_Ready_War_Cry, payload=archer.name)) ready = True archer.snoop_scribble("{} is waiting with {} arrows".format( archer.name, archer.arrows)) time_to_wait = random.randint(130, 300) for name, other in archer.others.items(): if other.dead() is not True: ready &= other.waiting() else: archer.snoop_scribble("{} thinks {} is dead".format( archer.name, name)) if ready is False: archer.snoop_scribble( "{} is impatient he will attack in {} seconds".format( archer.name, time_to_wait)) archer.post_fifo(Event(signal=signals.Advance_War_Cry), times=1, period=archer.to_time(time_to_wait), deferred=True) else: archer.snoop_scribble("{} thinks unit is ready to attack".format( archer.name)) archer.post_fifo(Event(signal=signals.Advance_War_Cry)) return return_status.HANDLED
def on_outer_init(chart, e): chart.post_fifo(Event(signal=signals.to_inner), times=1, period=random.randint(2, 7), deferred=True) chart.transmit(Event(signal=signals.other_to_outer)) return return_status.HANDLED
def fr_entry(archer, e): archer.yell( Event(signal=signals.Other_Retreat_War_Cry, payload=archer.name)) archer.scribble('fire on knights') archer.scribble('fire on footman') if archer.arrows == 0: archer.post_fifo(Event(signal=signals.Out_Of_Arrows)) return return_status.HANDLED
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 didt_entry(archer, e): archer.arrows = HorseArcher.MAXIMUM_ARROW_CAPACITY archer.ticks = 0 archer.post_fifo(Event(signal=signals.Second), times=0, period=archer.to_time(1.0), deferred=True) return return_status.HANDLED archer.cancel_events(Event(signal=signals.Second)) return return_status.HANDLED
def skirmish_entry(archer, e): '''The Horse Archer will trigger an Ammunition_Low event if he has less than 10 arrows when he begins skirmishing''' # a Knight could charge at him sometime between 40-120 sec # once he enters the skirmish state archer.post_fifo(Event(signal=signals.Officer_Lured), times=1, period=archer.to_time(random.randint(40, 200)), deferred=True) if archer.arrows < 10: archer.post_fifo(Event(signal=signals.Ammunition_Low)) return return_status.HANDLED
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 g1_s01_active_objects_graph(chart, e): status = return_status.UNHANDLED if (e.signal == signals.ENTRY_SIGNAL): chart.post_fifo(Event(signal=signals.A)) chart.post_lifo(Event(signal=signals.F)) chart.recall() status = return_status.HANDLED elif (e.signal == signals.EXIT_SIGNAL): status = return_status.HANDLED elif (e.signal == signals.C): status = chart.trans(g1_s22_active_objects_graph) else: status, chart.temp.fun = return_status.SUPER, g1_s0_active_objects_graph return status
def advance_entry(archer, e): archer.yell( Event(signal=signals.Other_Advance_War_Cry, payload=archer.name)) if len(archer.others) >= 1: first_name_of_others = next(iter(archer.others)) print(archer.others[first_name_of_others].trace()) archer.others[first_name_of_others].clear_trace() archer.snoop_scribble("advancing with {} arrows".format(archer.arrows)) archer.post_fifo(Event(signal=signals.Close_Enough_For_Circle), times=1, period=archer.to_time(3.0), deferred=True) return return_status.HANDLED
def skirmish_entry(archer, e): archer.yell( Event(signal=signals.Other_Skirmish_War_Cry, payload=archer.name)) # a Knight could charge at him sometime between 40-120 sec # once he enters the skirmish state archer.post_fifo(Event(signal=signals.Officer_Lured), times=1, period=archer.to_time(random.randint(40, 200)), deferred=True) if archer.arrows < 10: archer.post_fifo(Event(signal=signals.Ammunition_Low)) return return_status.HANDLED
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 didt_other_skirmish_war_cry(archer, e): '''A horse archer heard another's Skirmish_War_Cry, so they give the command to and introspect on the state of their unit''' archer.post_fifo(Event(signal=signals.Skirmish_War_Cry)) name = e.payload archer.others[name].dispatch(e) return archer.trans(skirmish)
def receiving_entry(chart, e): chart.scribble("receiving") chart.post_fifo(Event(signal=signals.RECEIVED), times=1, period=1.0, deferred=True) return return_status.HANDLED
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_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 authorizing_entry(chart, e): chart.scribble("authorizing") chart.post_fifo(Event(signal=signals.COMPLETED), times=1, period=2.0, deferred=True) return return_status.HANDLED
def skirmish_officer_lured(archer, e): '''If Horse Archer lures an enemy officer they issue a Retreat_War_Cry event.''' print("Knight Charging") archer.scribble("Knight Charging") archer.post_fifo(Event(signal=signals.Retreat_War_Cry)) return return_status.HANDLED
def publish_BB(chart, e): chart.publish( Event( signal=signals.BB, payload="information from b_chart riding within the BB signal") ) return return_status.HANDLED
def polling_time_out_hook(chart, e): '''generic TIME_OUT ultimate hook for all states, injects artificial event DATA_READY''' chart.scribble("polling") chart.processing_count += 1 if (chart.processing_count >= 3): chart.post_fifo(Event(signal=signals.DATA_READY)) return return_status.HANDLED
def marshal_ready(archer, e): ready = True for name, other in archer.others.items(): if other.dead() is not True: ready &= other.waiting() if ready: archer.post_fifo(Event(signal=signals.Advance_War_Cry)) return archer.trans(waiting_to_advance)
def wta_entry(archer, e): archer.arrows = HorseArcher.MAXIMUM_ARROW_CAPACITY archer.post_fifo(Event(signal=signals.Advance_War_Cry), times=1, period=archer.to_time(random.randint(30, 120)), deferred=True) return return_status.HANDLED
def processing_init(chart, e): status = return_status.HANDLED if chart.processing_count >= 3: chart.processing_count = 0 status = chart.trans(busy) else: chart.post_fifo(Event(signal=signals.POLL)) return status
def advance_entry(archer, e): '''Upon entering the advanced state wait 3 seconds then issue Close_Enough_For_Circle war cry''' archer.post_fifo(Event(signal=signals.Close_Enough_For_Circle), times=1, period=archer.to_time(3.0), deferred=True) return return_status.HANDLED
def wtl_entry(archer, e): archer.yell( Event(signal=signals.Other_Retreat_Ready_War_Cry, payload=archer.name)) archer.snoop_scribble("{} arrows".format(archer.arrows)) archer.scribble('put away bow') archer.scribble('pull scimitar') archer.snoop_scribble("acts scared") return return_status.HANDLED
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 fr_second(archer, e): if archer.ticks % 3 == 0: if random.randint(1, 10) <= 8: archer.arrows = archer.arrows - 1 if archer.arrows >= 1 else 0 archer.scribble('arrows left {}') if archer.arrows == 0: archer.post_fifo(Event(signal=signals.Out_Of_Arrows)) archer.ticks += 1 return return_status.HANDLED
def didt_entry(archer, e): '''Load up on arrows and start tracking time within this tactic''' archer.arrows = HorseArcher.MAXIMUM_ARROW_CAPACITY archer.ticks = 0 archer.post_fifo(Event(signal=signals.Second), times=0, period=archer.to_time(1.0), deferred=True) return return_status.HANDLED
def caf_second(archer, e): if (archer.ticks % 6 == 0): archer.arrows -= random.randint(1, 3) archer.arrows = 0 if archer.arrows < 0 else archer.arrows archer.scribble('arrows left {}'.format(archer.arrows)) if archer.arrows < 20: archer.post_fifo(Event(signal=signals.Skirmish_War_Cry)) archer.ticks += 1 return return_status.HANDLED
def pending_on_pistons_timeout(reactor, e): status = return_status.HANDLED all_ready = True for piston in reactor.pistons: piston.dispatch(e) all_ready &= piston.armed if all_ready: reactor.post_fifo(Event(signal=signals.FIRE)) status = return_status.HANDLED return status
def marshal_entry(archer, e): archer.scribble("halt horse") archer.scribble("identify next marshal point") archer.scribble("field wrap wounds on self and horse") archer.scribble("drink water") archer.post_fifo(Event(signal=signals.Ready), times=1, period=archer.to_time(3), deferred=True) return return_status.HANDLED