Example #1
0
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())
Example #2
0
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())
Example #3
0
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_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_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)'])
Example #6
0
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 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_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)'])