Beispiel #1
0
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())
Beispiel #2
0
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())
Beispiel #3
0
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())
Beispiel #4
0
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))
Beispiel #5
0
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))
Beispiel #6
0
def example_5():

  from miros.activeobject import Factory

  # the statechart's event callback methods
  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)

  # Factory is a type of ActiveObject, so it will have it's methods
  chart = Factory('factory_class_example')

  fc = chart.create(state='fc'). \
    catch(signal=signals.B, handler=trans_to_fc). \
    catch(signal=signals.INIT_SIGNAL, handler=trans_to_fc1). \
    to_method()

  fc1 = chart.create(state='fc1'). \
    catch(signal=signals.A, handler=trans_to_fc2). \
    to_method()

  fc2 = chart.create(state='fc2'). \
    catch(signal=signals.A, handler=trans_to_fc1). \
    to_method()

  chart.nest(fc,  parent=None). \
        nest(fc1, parent=fc). \
        nest(fc2, parent=fc)

  chart.start_at(fc)
  chart.post_fifo(Event(signal=signals.A))
  time.sleep(0.01)
  pp(chart.spy())
  print(chart.to_code(fc))
  print(chart.to_code(fc1))
  print(chart.to_code(fc2))
Beispiel #7
0
def example_5():

    from miros.activeobject import Factory

    # the statechart's event callback methods
    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)

    # Factory is a type of ActiveObject, so it will have it's methods
    chart = Factory('factory_class_example')

    fc = chart.create(state='fc'). \
      catch(signal=signals.B, handler=trans_to_fc). \
      catch(signal=signals.INIT_SIGNAL, handler=trans_to_fc1). \
      to_method()

    fc1 = chart.create(state='fc1'). \
      catch(signal=signals.A, handler=trans_to_fc2). \
      to_method()

    fc2 = chart.create(state='fc2'). \
      catch(signal=signals.A, handler=trans_to_fc1). \
      to_method()

    chart.nest(fc,  parent=None). \
          nest(fc1, parent=fc). \
          nest(fc2, parent=fc)

    chart.start_at(fc)
    chart.post_fifo(Event(signal=signals.A))
    time.sleep(0.01)
    pp(chart.spy())
    print(chart.to_code(fc))
    print(chart.to_code(fc1))
    print(chart.to_code(fc2))
Beispiel #8
0
def augment_example():
    import time
    from miros.hsm import pp
    from miros.activeobject import Factory
    from miros.event import signals, Event, return_status

    def process_a_generic(chart, e):
        chart.scribble('processing a generic')
        return return_status.HANDLED

    def process_b_generic(chart, e):
        chart.scribble('processing b generic')
        return return_status.HANDLED

    # overrides the generic hook while in the specific state
    def process_a_specific(chart, e):
        chart.scribble('processing a specific')
        return return_status.HANDLED

    chart = Factory('ultimate_hook_example')
    chart.augment(other=0, name='counter')
    assert (chart.counter == 0)
    generic = chart.create(state='generic'). \
      catch(signal=signals.a, handler=process_a_generic). \
      catch(signal=signals.b, handler=process_b_generic). \
      to_method()

    specific = chart.create(state='specific'). \
        catch(signal=signals.a, handler=process_a_specific). \
        to_method()

    chart.nest(generic, parent=None). \
          nest(specific, parent=generic)

    chart.start_at(specific)
    chart.post_fifo(Event(signal=signals.b))
    chart.post_fifo(Event(signal=signals.a))
    time.sleep(0.001)
    pp(chart.spy())
Beispiel #9
0
def augment_example():
  import time
  from miros.hsm import pp
  from miros.activeobject import Factory
  from miros.event import signals, Event, return_status

  def process_a_generic(chart, e):
    chart.scribble('processing a generic')
    return return_status.HANDLED

  def process_b_generic(chart, e):
    chart.scribble('processing b generic')
    return return_status.HANDLED

  # overrides the generic hook while in the specific state
  def process_a_specific(chart, e):
    chart.scribble('processing a specific')
    return return_status.HANDLED

  chart = Factory('ultimate_hook_example')
  chart.augment(other=0, name='counter')
  assert(chart.counter == 0)
  generic = chart.create(state='generic'). \
    catch(signal=signals.a, handler=process_a_generic). \
    catch(signal=signals.b, handler=process_b_generic). \
    to_method()

  specific = chart.create(state='specific'). \
      catch(signal=signals.a, handler=process_a_specific). \
      to_method()

  chart.nest(generic, parent=None). \
        nest(specific, parent=generic)

  chart.start_at(specific)
  chart.post_fifo(Event(signal=signals.b))
  chart.post_fifo(Event(signal=signals.a))
  time.sleep(0.001)
  pp(chart.spy())
Beispiel #10
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())
Beispiel #11
0
    if routing_key is None:
      routing_key = '.'
    else:
      routing_key = '.' + routing_key

    for channel in self.channels:
      ip = channel.extension.ip_address
      channel.basic_publish(
        exchange=NetworkTool.exchange_name,
        routing_key=ip + routing_key,
        body=message,
        mandatory=False,
        immediate=False)

if __name__ == "__main__":
  pp('line to appease PEP8/lint F401 noise')

  # Set the encryption key for this session
  NetworkTool.encryption_key = b'lV5vGz-Hekb3K3396c9ZKRkc3eDIazheC4kow9DlKY0='

  # Custom receive callbacks
  def custom_rx_callback(ch, method, properties, body):
    print(" [+] {}:{}".format(method.routing_key, body))

  def ergotic_rx_callback(ch, method, properties, body):
    print(" [+e] {}:{}".format(method.routing_key, body))
  # Get the user input
  tranceiver_type = sys.argv[1:]
  if not tranceiver_type:
    sys.stderr.write("Usage: {} [rx]/[tx]/[er]\n".format(sys.argv[0]))
Beispiel #12
0
def reminder3():
  import time
  from miros.hsm import pp
  from miros.activeobject import Factory
  from miros.event import signals, Event, return_status

  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 polling_init(chart, e):
    return chart.trans(processing)

  def processing_init(chart, e):
    return chart.trans(idle)

  def idle_data_ready(chart, e):
    return chart.trans(busy)

  def busy_entry(chart, e):
    chart.busy_count, chart.busy_count = 0, 0
    return return_status.HANDLED

  def busy_time_out_hook(chart, e):
    '''specific TIME_OUT hook for busy state'''
    status = return_status.HANDLED
    chart.scribble("busy")
    chart.busy_count += 1
    if(chart.busy_count >= 2):
      status = chart.trans(idle)
    return status

  chart = Factory('reminder')
  chart.augment(other=0, name="processing_count")
  chart.augment(other=0, name="busy_count")

  polling = chart.create(state="polling"). \
              catch(signal=signals.INIT_SIGNAL, handler=polling_init). \
              catch(signal=signals.TIME_OUT, handler=polling_time_out_hook). \
              to_method()

  processing = chart.create(state="processing"). \
                catch(signal=signals.INIT_SIGNAL, handler=processing_init). \
                to_method()

  idle = chart.create(state="idle"). \
          catch(signal=signals.DATA_READY, handler=idle_data_ready). \
          to_method()

  busy = chart.create(state="busy"). \
          catch(signal=signals.ENTRY_SIGNAL, handler=busy_entry). \
          catch(signal=signals.TIME_OUT, handler=busy_time_out_hook). \
              to_method()

  chart.nest(polling, parent=None). \
        nest(processing, parent=polling). \
        nest(idle, parent=processing). \
        nest(busy, parent=polling)

  chart.start_at(polling)
  chart.post_fifo(Event(signal=signals.TIME_OUT), times=20, period=0.1)
  time.sleep(1.0)
  pp(chart.spy())
  print(chart.trace())
Beispiel #13
0
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())
Beispiel #14
0
def multiunit_example():

    from miros.activeobject import Factory
    from miros.event import signals, Event, return_status
    import time

    # This statechart tests topology B in a multichart situation,
    # statechart built using a factory
    #
    #  +------- fb --------------s-----+
    #  |  +---- fb1 -------t-------+   |
    #  |  | i/pub(BB)              |   l --> BB
    #  |  |  +- fb11---------+     |   |
    #  |  |  |               |     |   |
    #  |  |  |               <-b-+ <-a-+
    #  |  |  +---------------+   +-+   |
    #  |  +------------------------+   |
    #  +-------------------------------+
    #

    def trans_to_fb(chart, e):
        return chart.trans(fb)

    def trans_to_fb1(chart, e):
        return chart.trans(fb1)

    def trans_to_fb11(chart, e):
        return chart.trans(fb11)

    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

    b_chart = Factory('b_chart')
    fb = b_chart.create(state='fb'). \
            catch(signal=signals.a, handler=trans_to_fb1). \
            to_method()

    fb1 = b_chart.create(state='fb1'). \
            catch(signal=signals.b, handler=trans_to_fb11). \
            catch(signal=signals.INIT_SIGNAL, handler=publish_BB). \
            to_method()

    fb11 = b_chart.create(state='fb11'). \
            to_method()

    b_chart.nest(fb, parent=None). \
            nest(fb1, parent=fb). \
            nest(fb11, parent=fb1)

    def trans_to_fc(chart, e):
        return chart.trans(fc)

    def trans_to_fc1(chart, e):
        return chart.trans(fc1)

    def bb_handler(chart, e):
        status = return_status.UNHANDLED
        if (e.signal == signals.BB):
            chart.scribble(e.payload)
            status = chart.trans(fc)
        return status

    def trans_to_fc2(chart, e):
        return chart.trans(fc2)

    # The following state chart is used to test topology C
    # in a multichart situation, statechart built using the factory
    #
    #        +------------------ fc ---------------+
    #        |   +----- fc1----+   +-----fc2-----+ |
    #        | * |             |   |             | +----+
    #        | | |             +-a->             | |    |
    #        | +->             <-a-+             | |    BB
    #        |   |             |   |             | |    |
    #        |   |             |   |             | <----+
    #        |   +-------------+   +-------------+ |
    #        +-------------------------------------+
    #

    c_chart = Factory('c_chart')
    fc = c_chart.create(state='fc'). \
          catch(signal=signals.INIT_SIGNAL, handler=trans_to_fc1). \
          catch(signal=signals.BB, handler=bb_handler). \
          to_method()

    fc1 = c_chart.create(state='fc1'). \
          catch(signal=signals.a, handler=trans_to_fc2). \
          to_method()

    fc2 = c_chart.create(state='fc2'). \
          catch(signal=signals.a, handler=trans_to_fc1). \
          to_method()

    c_chart.nest(fc,  parent=None). \
            nest(fc1, parent=fc). \
            nest(fc2, parent=fc)

    # subscribe to BB signals sent to the active fabric
    # c_chart.subscribe(Event(signal=signals.BB))
    c_chart.subscribe(signals.BB)

    # Start up the charts and post an event to how they interact
    c_chart.start_at(fc)
    b_chart.start_at(fb)
    b_chart.post_fifo(Event(signal=signals.a))

    time.sleep(0.1)
    print(c_chart.trace())
    pp(c_chart.spy())
    print(b_chart.trace())
    pp(b_chart.spy())
Beispiel #15
0
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())
Beispiel #16
0
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())
Beispiel #17
0
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())
Beispiel #18
0
def reminder2():

    import time
    from miros.hsm import pp
    from miros.activeobject import Factory
    from miros.event import signals, Event, return_status

    def polling_time_out(chart, e):
        chart.scribble("polling")
        chart.post_fifo(Event(signal=signals.PROCESS))
        return return_status.HANDLED

    def polling_process(chart, e):
        return chart.trans(processing)

    def processing_entry(chart, e):
        chart.processing_count += 1
        chart.scribble("processing")
        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 processing_poll(chart, e):
        return chart.trans(polling)

    def processing_exit(chart, e):
        return return_status.HANDLED

    def busy_entry(chart, e):
        chart.busy_count = 0
        return return_status.HANDLED

    def busy_time_out(chart, e):
        chart.scribble("busy")
        chart.busy_count += 1
        status = return_status.HANDLED
        if chart.busy_count >= 2:
            status = chart.trans(polling)
        return status

    chart = Factory('reminder_pattern_needed_2')
    chart.augment(other=0, name="processing_count")
    chart.augment(other=0, name="busy_count")

    polling = chart.create(state="polling"). \
                catch(signal=signals.TIME_OUT, handler=polling_time_out). \
                catch(signal=signals.PROCESS,  handler=polling_process). \
                to_method()

    processing = chart.create(state="processing"). \
                catch(signal=signals.ENTRY_SIGNAL, handler=processing_entry). \
                catch(signal=signals.INIT_SIGNAL, handler=processing_init). \
                catch(signal=signals.EXIT_SIGNAL, handler=processing_exit). \
                catch(signal=signals.POLL, handler=processing_poll). \
                to_method()

    busy = chart.create(state="busy"). \
            catch(signal=signals.ENTRY_SIGNAL, handler=busy_entry). \
            catch(signal=signals.TIME_OUT, handler=busy_time_out). \
            to_method()

    chart.nest(polling,    parent=None). \
          nest(processing, parent=None). \
          nest(busy, parent=processing)

    chart.start_at(polling)
    chart.post_fifo(Event(signal=signals.TIME_OUT), times=20, period=0.1)
    time.sleep(1.0)
    pp(chart.spy())
    print(chart.trace())
Beispiel #19
0
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())
Beispiel #20
0
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())
Beispiel #21
0
def multiunit_example():

  from miros.activeobject import Factory
  from miros.event import signals, Event, return_status
  import time

  # This statechart tests topology B in a multichart situation,
  # statechart built using a factory
  #  
  #  +------- fb --------------s-----+
  #  |  +---- fb1 -------t-------+   |
  #  |  | i/pub(BB)              |   l --> BB
  #  |  |  +- fb11---------+     |   |
  #  |  |  |               |     |   |
  #  |  |  |               <-b-+ <-a-+
  #  |  |  +---------------+   +-+   |
  #  |  +------------------------+   |
  #  +-------------------------------+
  #

  def trans_to_fb(chart, e):
    return chart.trans(fb)

  def trans_to_fb1(chart, e):
    return chart.trans(fb1)

  def trans_to_fb11(chart, e):
    return chart.trans(fb11)

  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

  b_chart = Factory('b_chart')
  fb = b_chart.create(state='fb'). \
          catch(signal=signals.a, handler=trans_to_fb1). \
          to_method()

  fb1 = b_chart.create(state='fb1'). \
          catch(signal=signals.b, handler=trans_to_fb11). \
          catch(signal=signals.INIT_SIGNAL, handler=publish_BB). \
          to_method()

  fb11 = b_chart.create(state='fb11'). \
          to_method()

  b_chart.nest(fb, parent=None). \
          nest(fb1, parent=fb). \
          nest(fb11, parent=fb1)

  def trans_to_fc(chart, e):
    return chart.trans(fc)

  def trans_to_fc1(chart, e):
    return chart.trans(fc1)

  def bb_handler(chart, e):
    status = return_status.UNHANDLED
    if(e.signal == signals.BB):
      chart.scribble(e.payload)
      status = chart.trans(fc)
    return status

  def trans_to_fc2(chart, e):
    return chart.trans(fc2)

  # The following state chart is used to test topology C
  # in a multichart situation, statechart built using the factory
  #
  #        +------------------ fc ---------------+
  #        |   +----- fc1----+   +-----fc2-----+ |
  #        | * |             |   |             | +----+
  #        | | |             +-a->             | |    |
  #        | +->             <-a-+             | |    BB
  #        |   |             |   |             | |    |
  #        |   |             |   |             | <----+
  #        |   +-------------+   +-------------+ |
  #        +-------------------------------------+
  #

  c_chart = Factory('c_chart')
  fc = c_chart.create(state='fc'). \
        catch(signal=signals.INIT_SIGNAL, handler=trans_to_fc1). \
        catch(signal=signals.BB, handler=bb_handler). \
        to_method()

  fc1 = c_chart.create(state='fc1'). \
        catch(signal=signals.a, handler=trans_to_fc2). \
        to_method()

  fc2 = c_chart.create(state='fc2'). \
        catch(signal=signals.a, handler=trans_to_fc1). \
        to_method()

  c_chart.nest(fc,  parent=None). \
          nest(fc1, parent=fc). \
          nest(fc2, parent=fc)

  # subscribe to BB signals sent to the active fabric
  # c_chart.subscribe(Event(signal=signals.BB))
  c_chart.subscribe(signals.BB)

  # Start up the charts and post an event to how they interact
  c_chart.start_at(fc)
  b_chart.start_at(fb)
  b_chart.post_fifo(Event(signal=signals.a))

  time.sleep(0.1)
  print(c_chart.trace())
  pp(c_chart.spy())
  print(b_chart.trace())
  pp(b_chart.spy())
Beispiel #22
0
def reminder3():
    import time
    from miros.hsm import pp
    from miros.activeobject import Factory
    from miros.event import signals, Event, return_status

    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 polling_init(chart, e):
        return chart.trans(processing)

    def processing_init(chart, e):
        return chart.trans(idle)

    def idle_data_ready(chart, e):
        return chart.trans(busy)

    def busy_entry(chart, e):
        chart.busy_count, chart.busy_count = 0, 0
        return return_status.HANDLED

    def busy_time_out_hook(chart, e):
        '''specific TIME_OUT hook for busy state'''
        status = return_status.HANDLED
        chart.scribble("busy")
        chart.busy_count += 1
        if (chart.busy_count >= 2):
            status = chart.trans(idle)
        return status

    chart = Factory('reminder')
    chart.augment(other=0, name="processing_count")
    chart.augment(other=0, name="busy_count")

    polling = chart.create(state="polling"). \
                catch(signal=signals.INIT_SIGNAL, handler=polling_init). \
                catch(signal=signals.TIME_OUT, handler=polling_time_out_hook). \
                to_method()

    processing = chart.create(state="processing"). \
                  catch(signal=signals.INIT_SIGNAL, handler=processing_init). \
                  to_method()

    idle = chart.create(state="idle"). \
            catch(signal=signals.DATA_READY, handler=idle_data_ready). \
            to_method()

    busy = chart.create(state="busy"). \
            catch(signal=signals.ENTRY_SIGNAL, handler=busy_entry). \
            catch(signal=signals.TIME_OUT, handler=busy_time_out_hook). \
                to_method()

    chart.nest(polling, parent=None). \
          nest(processing, parent=polling). \
          nest(idle, parent=processing). \
          nest(busy, parent=polling)

    chart.start_at(polling)
    chart.post_fifo(Event(signal=signals.TIME_OUT), times=20, period=0.1)
    time.sleep(1.0)
    pp(chart.spy())
    print(chart.trace())
Beispiel #23
0
def deferred1():
    import random
    import time
    from datetime import datetime
    from miros.hsm import pp
    from miros.activeobject import Factory
    from miros.event import signals, Event, return_status

    def processing_entry(chart, e):
        chart.defer(e)
        chart.scribble("deferred at {}". \
            format(datetime.now().strftime("%M:%S:%f")))
        return return_status.HANDLED

    def processing_init(chart, e):
        return chart.trans(idle)

    def idle_entry(chart, e):
        chart.recall()
        chart.scribble("recalled at {}". \
            format(datetime.now().strftime("%M:%S:%f")))
        return return_status.HANDLED

    def idle_new_request(chart, e):
        return chart.trans(receiving)

    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 receiving_received(chart, e):
        return chart.trans(authorizing)

    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 authorizing_authorized(chart, e):
        return chart.trans(idle)

    chart = Factory('deferred')

    processing = chart.create(state="processing"). \
                  catch(signal=signals.NEW_REQUEST, handler=processing_entry). \
                  catch(signal=signals.INIT_SIGNAL, handler=processing_init). \
                  to_method()

    idle = chart.create(state='idle'). \
            catch(signal=signals.ENTRY_SIGNAL, handler=idle_entry). \
            catch(signal=signals.NEW_REQUEST, handler=idle_new_request). \
            to_method()

    receiving = chart.create(state='receiving'). \
                  catch(signal=signals.ENTRY_SIGNAL, handler=receiving_entry). \
                  catch(signal=signals.RECEIVED, handler=receiving_received). \
                  to_method()

    authorizing = chart.create(state='authorizing'). \
                    catch(signal=signals.ENTRY_SIGNAL,
                        handler=authorizing_entry). \
                    catch(signal=signals.COMPLETED,
                        handler=authorizing_authorized). \
                    to_method()

    chart.nest(processing, parent=None). \
          nest(idle, parent=processing). \
          nest(receiving, parent=processing). \
          nest(authorizing, parent=processing)

    chart.start_at(processing)

    def burst_event(event, bursts, fastest_time, slowest_time):
        for i in range(bursts):
            time.sleep(random.uniform(fastest_time, slowest_time))
            chart.post_fifo(event)

    burst_event(Event(signal=signals.NEW_REQUEST),
                bursts=15,
                fastest_time=0.2,
                slowest_time=1.0)

    print(chart.trace())
    time.sleep(6)
    pp(chart.spy())
Beispiel #24
0
  chart_name="producer",
  rabbit_user="******",
  rabbit_password="******",
  ip="192.168.1.75",
  port=5672
)

producer_outer = chart.create(state='producer_outer'). \
  catch(signal=signals.INIT_SIGNAL, handler=producer_outer_init). \
  catch(signal=signals.B, handler=producer_outer_B). \
  to_method()

c1 = chart.create(state='c1'). \
  catch(signal=signals.A, handler=c1_A). \
  to_method()

c2 = chart.create(state='c2'). \
  catch(signal=signals.A, handler=c2_A). \
  to_method()

chart.nest(producer_outer, parent=None). \
  nest(c1, parent=producer_outer). \
  nest(c2, parent=producer_outer)

chart.start_at(producer_outer)
chart.post_fifo(Event(signal=signals.B))
time.sleep(0.1)
pp(chart.spy())
print(chart.trace())
chart.connection.close()
Beispiel #25
0
def reminder2():

  import time
  from miros.hsm import pp
  from miros.activeobject import Factory
  from miros.event import signals, Event, return_status

  def polling_time_out(chart, e):
    chart.scribble("polling")
    chart.post_fifo(
      Event(signal=signals.PROCESS))
    return return_status.HANDLED

  def polling_process(chart, e):
    return chart.trans(processing)

  def processing_entry(chart, e):
    chart.processing_count += 1
    chart.scribble("processing")
    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 processing_poll(chart, e):
    return chart.trans(polling)

  def processing_exit(chart, e):
    return return_status.HANDLED

  def busy_entry(chart, e):
    chart.busy_count = 0
    return return_status.HANDLED

  def busy_time_out(chart, e):
    chart.scribble("busy")
    chart.busy_count += 1
    status = return_status.HANDLED
    if chart.busy_count >= 2:
      status = chart.trans(polling)
    return status

  chart = Factory('reminder_pattern_needed_2')
  chart.augment(other=0, name="processing_count")
  chart.augment(other=0, name="busy_count")

  polling = chart.create(state="polling"). \
              catch(signal=signals.TIME_OUT, handler=polling_time_out). \
              catch(signal=signals.PROCESS,  handler=polling_process). \
              to_method()

  processing = chart.create(state="processing"). \
              catch(signal=signals.ENTRY_SIGNAL, handler=processing_entry). \
              catch(signal=signals.INIT_SIGNAL, handler=processing_init). \
              catch(signal=signals.EXIT_SIGNAL, handler=processing_exit). \
              catch(signal=signals.POLL, handler=processing_poll). \
              to_method()

  busy = chart.create(state="busy"). \
          catch(signal=signals.ENTRY_SIGNAL, handler=busy_entry). \
          catch(signal=signals.TIME_OUT, handler=busy_time_out). \
          to_method()

  chart.nest(polling,    parent=None). \
        nest(processing, parent=None). \
        nest(busy, parent=processing)

  chart.start_at(polling)
  chart.post_fifo(Event(signal=signals.TIME_OUT), times=20, period=0.1)
  time.sleep(1.0)
  pp(chart.spy())
  print(chart.trace())
  nest(advance, parent=deceit_in_detail). \
  nest(circle_and_fire, parent=advance). \
  nest(skirmish, parent=deceit_in_detail). \
  nest(waiting_to_lure, parent=skirmish). \
  nest(feigned_retreat, parent=deceit_in_detail). \
  nest(marshal, parent=deceit_in_detail). \
  nest(waiting_to_advance, parent=marshal)

if __name__ == '__main__':
    print(archer.name)
    archer.live_trace = True
    archer.time_compression = 100
    archer.start_at(battle)
    archer.post_fifo(Event(signal=signals.Senior_Advance_War_Cry))
    time.sleep(0.1)
    pp('')
    archer.cancel_events(Event(signal=signals.Second))

    time.sleep(0.2)

    oha = OtherHorseArcher()
    oha.start_at(empathy)
    oha.post_fifo(Event(signal=signals.Other_Retreat_Ready_War_Cry))
    oha.post_fifo(Event(signal=signals.Retreat_War_Cry))
    oha.post_fifo(Event(signal=signals.Advance_War_Cry))
    oha.post_fifo(Event(signal=signals.Advance_War_Cry))
    oha.post_fifo(Event(signal=signals.Other_Advance_War_Cry))
    oha.post_fifo(Event(signal=signals.Other_Retreat_Ready_War_Cry))
    oha.post_fifo(Event(signal=signals.Advance_War_Cry))
    oha.post_fifo(Event(signal=signals.Advance_War_Cry))
    oha.complete_circuit()
Beispiel #27
0
def deferred1():
  import random
  import time
  from datetime import datetime
  from miros.hsm import pp
  from miros.activeobject import Factory
  from miros.event import signals, Event, return_status

 
  def processing_entry(chart, e):
    chart.defer(e)
    chart.scribble("deferred at {}". \
        format(datetime.now().strftime("%M:%S:%f")))
    return return_status.HANDLED

  def processing_init(chart, e):
    return chart.trans(idle)

  def idle_entry(chart, e):
    chart.recall()
    chart.scribble("recalled at {}". \
        format(datetime.now().strftime("%M:%S:%f")))
    return return_status.HANDLED

  def idle_new_request(chart, e):
    return chart.trans(receiving)

  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 receiving_received(chart, e):
    return chart.trans(authorizing)

  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 authorizing_authorized(chart, e):
    return chart.trans(idle)

  chart = Factory('deferred')

  processing = chart.create(state="processing"). \
                catch(signal=signals.NEW_REQUEST, handler=processing_entry). \
                catch(signal=signals.INIT_SIGNAL, handler=processing_init). \
                to_method()

  idle = chart.create(state='idle'). \
          catch(signal=signals.ENTRY_SIGNAL, handler=idle_entry). \
          catch(signal=signals.NEW_REQUEST, handler=idle_new_request). \
          to_method()

  receiving = chart.create(state='receiving'). \
                catch(signal=signals.ENTRY_SIGNAL, handler=receiving_entry). \
                catch(signal=signals.RECEIVED, handler=receiving_received). \
                to_method()

  authorizing = chart.create(state='authorizing'). \
                  catch(signal=signals.ENTRY_SIGNAL, 
                      handler=authorizing_entry). \
                  catch(signal=signals.COMPLETED,
                      handler=authorizing_authorized). \
                  to_method()

  chart.nest(processing, parent=None). \
        nest(idle, parent=processing). \
        nest(receiving, parent=processing). \
        nest(authorizing, parent=processing)

  chart.start_at(processing)

  def burst_event(event, bursts, fastest_time, slowest_time):
    for i in range(bursts):
      time.sleep(random.uniform(fastest_time, slowest_time))
      chart.post_fifo(event)

  burst_event(Event(signal=signals.NEW_REQUEST),
               bursts=15,
               fastest_time=0.2,
               slowest_time=1.0)

  print(chart.trace())
  time.sleep(6)
  pp(chart.spy())
  nest(advance, parent=deceit_in_detail). \
  nest(circle_and_fire, parent=advance). \
  nest(skirmish, parent=deceit_in_detail). \
  nest(waiting_to_lure, parent=skirmish). \
  nest(feigned_retreat, parent=deceit_in_detail). \
  nest(marshal, parent=deceit_in_detail). \
  nest(waiting_to_advance, parent=marshal)

if __name__ == '__main__':
  print(archer.name)
  archer.live_trace = True
  archer.time_compression = 100
  archer.start_at(battle)
  archer.post_fifo(Event(signal=signals.Senior_Advance_War_Cry))
  time.sleep(0.1)
  pp('')
  archer.cancel_events(Event(signal=signals.Second))

  time.sleep(0.2)

  oha = OtherHorseArcher()
  oha.start_at(empathy)
  oha.post_fifo(Event(signal=signals.Other_Retreat_Ready_War_Cry))
  oha.post_fifo(Event(signal=signals.Retreat_War_Cry))
  oha.post_fifo(Event(signal=signals.Advance_War_Cry))
  oha.post_fifo(Event(signal=signals.Advance_War_Cry))
  oha.post_fifo(Event(signal=signals.Other_Advance_War_Cry))
  oha.post_fifo(Event(signal=signals.Other_Retreat_Ready_War_Cry))
  oha.post_fifo(Event(signal=signals.Advance_War_Cry))
  oha.post_fifo(Event(signal=signals.Advance_War_Cry))
  oha.complete_circuit()