Example #1
0
def test_protocol_complex1_2():
    l = """

               in:next_episode ; (
                   out:no_more_episodes | 
                   (out:episode_start ;
                       (in:next_image ; (out:image | out:no_more_images))*)
               )

       """
    seq = [InputReceived("next_episode"),
           OutputProduced("episode_start"),
           InputReceived("next_image"),
           OutputProduced("image"),
           ]
    assert_seq(l, seq, (NeedMore, Enough), Enough)
Example #2
0
def handle_message_node(parsed, protocol, pc: LanguageChecker, agent, context):
    topic = parsed[FIELD_TOPIC]
    data = parsed[FIELD_DATA]

    if topic not in protocol.inputs:
        msg = f'Input channel "{topic}" not found in protocol. Known: {sorted(protocol.inputs)}'
        raise ExternalProtocolViolation(msg)

    klass = protocol.inputs[topic]
    try:
        data = decode_bytes_before_json_deserialization(data)
        ob = ipce_to_object(data, {}, {}, expect_type=klass)
    except BaseException as e:
        msg = f'Cannot deserialize object for topic "{topic}" expecting {klass}.'
        try:
            parsed = json.dumps(parsed, indent=2)
        except:
            parsed = str(parsed)
        msg += '\n\n' + contracts.indent(parsed, '|', 'parsed: |')
        raise DecodingError(msg) from e

    if parsed.get(FIELD_TIMING, None) is not None:
        timing = ipce_to_object(parsed[FIELD_TIMING], {}, {},
                                expect_type=TimingInfo)
    else:
        timing = TimingInfo()

    timing.received = local_time()

    context.set_last_timing(timing)
    # logger.info(f'Before push the state is\n{pc}')

    event = InputReceived(topic)
    expected = pc.get_expected_events()

    res = pc.push(event)

    # names = pc.get_active_states_names()
    # logger.info(f'After push of {event}: result \n{res} active {names}' )
    if isinstance(res, Unexpected):
        msg = f'Unexpected input "{topic}": {res}'
        msg += f'\nI expected: {expected}'
        msg += '\n' + format_obs(dict(pc=pc))
        logger.error(msg)
        raise ExternalProtocolViolation(msg)
    else:
        expect_fn = f'on_received_{topic}'
        call_if_fun_exists(agent,
                           expect_fn,
                           data=ob,
                           context=context,
                           timing=timing)
Example #3
0
def test_protocol_complex1_3d():
    l = """
                (
                    in:next_episode ; (

                        (out:episode_start ;
                            (in:next_image))
                    )
                )*            
            """
    seq = [
        InputReceived("next_image"),

    ]
    assert_seq(l, seq, (Unexpected,), Unexpected)
Example #4
0
def test_protocol_complex1_3b():
    l = """
                (
                    in:next_episode ; (
                        out:no_more_episodes | 
                        (out:episode_start ;
                            (in:next_image ; (out:image | out:no_more_images))*)
                    )
                )*            
            """
    seq = [
        InputReceived("next_image"),

    ]
    assert_seq(l, seq, (Unexpected,), Unexpected)
Example #5
0
def test_proto05():
    seq = [InputReceived("b")]
    assert_seq("in:a", seq, (Unexpected,), Unexpected)
Example #6
0
def test_proto3():
    seq = [InputReceived("a")]
    assert_seq("out:a", seq, (Unexpected,), Unexpected)
Example #7
0
def test_proto_in1():
    seq = [InputReceived("a")]
    assert_seq("in:a", seq, (Enough,), Enough)
Example #8
0
def test_basic_protocol1():
    l0 = basic_protocol.language
    seq = [InputReceived("set_config")]
    assert_seq(l0, seq, (NeedMore,), NeedMore)