예제 #1
0
    def setup_class(cls):
        """Set up the test."""
        cls.node = LocalNode()

        cls.public_key_1 = "mailbox1"
        cls.mailbox1 = MailBox(OEFLocalConnection(cls.public_key_1, cls.node))

        cls.mailbox1.connect()

        # register a service.
        request_id = 1
        service_id = ''
        cls.data_model = DataModel("foobar", attributes=[])
        service_description = Description({
            "foo": 1,
            "bar": "baz"
        },
                                          data_model=cls.data_model)
        register_service_request = OEFMessage(
            oef_type=OEFMessage.Type.REGISTER_SERVICE,
            id=request_id,
            service_description=service_description,
            service_id=service_id)
        msg_bytes = OEFSerializer().encode(register_service_request)
        envelope = Envelope(to=DEFAULT_OEF,
                            sender=cls.public_key_1,
                            protocol_id=OEFMessage.protocol_id,
                            message=msg_bytes)
        cls.mailbox1.send(envelope)
예제 #2
0
def test_react():
    """Tests income messages."""
    node = LocalNode()
    agent_name = "MyAgent"
    private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem")
    crypto = Crypto(private_key_pem_path=private_key_pem_path)
    public_key = crypto.public_key
    mailbox = MailBox(OEFLocalConnection(public_key, node))

    msg = DefaultMessage(type=DefaultMessage.Type.BYTES, content=b"hello")
    message_bytes = DefaultSerializer().encode(msg)

    envelope = Envelope(to="Agent1",
                        sender=public_key,
                        protocol_id="default",
                        message=message_bytes)

    agent = AEA(agent_name,
                mailbox,
                private_key_pem_path=private_key_pem_path,
                directory=str(Path(CUR_PATH, "data", "dummy_aea")))
    t = Thread(target=agent.start)
    try:
        t.start()
        agent.mailbox.inbox._queue.put(envelope)
        time.sleep(1)
        handler = agent.resources \
            .handler_registry.fetch_by_skill('default', "dummy")
        assert envelope in handler.handled_envelopes, \
            "The envelope is not inside the handled_envelopes."
    finally:
        agent.stop()
        t.join()
예제 #3
0
def test_run_agent():
    """Test that we can set up and then run the agent."""
    agent_name = "dummyagent"
    agent = DummyAgent(agent_name)
    mailbox = MailBox(OEFLocalConnection("mypbk", LocalNode()))
    agent.mailbox = mailbox
    assert agent.name == agent_name
    assert isinstance(agent.crypto, Crypto)
    assert agent.agent_state == AgentState.INITIATED,\
        "Agent state must be 'initiated'"

    agent.mailbox.connect()
    assert agent.agent_state == AgentState.CONNECTED,\
        "Agent state must be 'connected'"

    assert isinstance(agent.inbox, InBox)
    assert isinstance(agent.outbox, OutBox)

    agent_thread = Thread(target=agent.start)
    agent_thread.start()
    time.sleep(1)

    try:
        assert agent.agent_state == AgentState.RUNNING,\
            "Agent state must be 'running'"
    finally:
        agent.stop()
        agent.mailbox.disconnect()
        agent_thread.join()
예제 #4
0
    def setup_class(cls):
        """Set up the test."""
        cls.node = LocalNode()

        cls.public_key_1 = "mailbox1"
        cls.mailbox1 = MailBox(OEFLocalConnection(cls.public_key_1, cls.node))

        cls.mailbox1.connect()
예제 #5
0
def test_initialiseAeA():
    """Tests the initialisation of the AeA."""
    node = LocalNode()
    public_key_1 = "mailbox1"
    mailbox1 = MailBox(OEFLocalConnection(public_key_1, node))
    myAea = AEA("Agent0", mailbox1, directory=str(Path(CUR_PATH, "aea")))
    assert AEA("Agent0", mailbox1), "Agent is not inisialised"
    print(myAea.context)
    assert myAea.context == myAea._context, "Cannot access the Agent's Context"
    myAea.setup()
    assert myAea.resources is not None,\
        "Resources must not be None after setup"
예제 #6
0
    def __init__(self, name: str, gym_env: gym.Env,
                 proxy_env_queue: Queue) -> None:
        """
        Instantiate the agent.

        :param name: the name of the agent
        :param gym_env: gym environment
        :param proxy_env_queue: the queue of the proxy environment
        :return: None
        """
        super().__init__(name, timeout=0)
        self.proxy_env_queue = proxy_env_queue
        self.mailbox = MailBox(GymConnection(self.crypto.public_key, gym_env))
예제 #7
0
def test_handle():
    """Tests handle method of an agent."""
    node = LocalNode()
    agent_name = "MyAgent"
    private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem")
    crypto = Crypto(private_key_pem_path=private_key_pem_path)
    public_key = crypto.public_key
    mailbox = MailBox(OEFLocalConnection(public_key, node))

    msg = DefaultMessage(type=DefaultMessage.Type.BYTES, content=b"hello")
    message_bytes = DefaultSerializer().encode(msg)

    envelope = Envelope(to="Agent1",
                        sender=public_key,
                        protocol_id="unknown_protocol",
                        message=message_bytes)

    agent = AEA(agent_name,
                mailbox,
                private_key_pem_path=private_key_pem_path,
                directory=str(Path(CUR_PATH, "data", "dummy_aea")))
    t = Thread(target=agent.start)
    try:
        t.start()
        agent.mailbox.inbox._queue.put(envelope)
        env = agent.mailbox.outbox._queue.get(block=True, timeout=5.0)
        assert env.protocol_id == "default", \
            "The envelope is not the expected protocol (Unsupported protocol)"

        #   DECODING ERROR
        msg = "hello".encode("utf-8")
        envelope = Envelope(to=public_key,
                            sender=public_key,
                            protocol_id='default',
                            message=msg)
        agent.mailbox.inbox._queue.put(envelope)
        #   UNSUPPORTED SKILL
        msg = FIPASerializer().encode(
            FIPAMessage(performative=FIPAMessage.Performative.ACCEPT,
                        message_id=0,
                        dialogue_id=0,
                        destination=public_key,
                        target=1))
        envelope = Envelope(to=public_key,
                            sender=public_key,
                            protocol_id="fipa",
                            message=msg)
        agent.mailbox.inbox._queue.put(envelope)
    finally:
        agent.stop()
        t.join()
예제 #8
0
def test_mailBox():
    """Tests if the mailbox is connected."""
    node = LocalNode()
    public_key_1 = "mailbox1"
    mailbox1 = MailBox(OEFLocalConnection(public_key_1, node))
    mailbox1.connect()
    assert mailbox1.is_connected,\
        "Mailbox cannot connect to the specific Connection(OEFLocalConnection)"
    mailbox1.disconnect()
예제 #9
0
def run(ctx: Context, connection_name: str):
    """Run the agent."""
    _try_to_load_agent_config(ctx)
    agent_name = cast(str, ctx.agent_config.agent_name)
    private_key_pem_path = cast(str, ctx.agent_config.private_key_pem_path)
    if private_key_pem_path == "":
        private_key_pem_path = _create_temporary_private_key_pem_path()
    else:
        _try_validate_private_key_pem_path(private_key_pem_path)
    crypto = Crypto(private_key_pem_path=private_key_pem_path)
    public_key = crypto.public_key
    connection_name = ctx.agent_config.default_connection if connection_name is None else connection_name
    _try_to_load_protocols(ctx)
    try:
        connection = _setup_connection(connection_name, public_key, ctx)
    except AEAConfigException as e:
        logger.error(str(e))
        exit(-1)
        return

    logger.debug("Installing all the dependencies...")
    for d in ctx.get_dependencies():
        logger.debug("Installing {}...".format(d))
        try:
            subp = subprocess.Popen(
                [sys.executable, "-m", "pip", "install", d])
            subp.wait(30.0)
        except Exception:
            logger.error(
                "An error occurred while installing {}. Stopping...".format(d))
            exit(-1)

    mailbox = MailBox(connection)
    agent = AEA(agent_name,
                mailbox,
                private_key_pem_path=private_key_pem_path,
                directory=str(Path(".")))
    try:
        agent.start()
    except KeyboardInterrupt:
        logger.info("Interrupted.")
    except Exception as e:
        logger.exception(e)
    finally:
        agent.stop()
예제 #10
0
def test_act():
    """Tests the act function of the AeA."""
    node = LocalNode()
    agent_name = "MyAgent"
    private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem")
    crypto = Crypto(private_key_pem_path=private_key_pem_path)
    public_key = crypto.public_key
    mailbox = MailBox(OEFLocalConnection(public_key, node))

    agent = AEA(agent_name,
                mailbox,
                private_key_pem_path=private_key_pem_path,
                directory=str(Path(CUR_PATH, "data", "dummy_aea")))
    t = Thread(target=agent.start)
    try:
        t.start()
        time.sleep(1)

        behaviour = agent.resources.behaviour_registry.fetch("dummy")
        assert behaviour[0].nb_act_called > 0, "Act() wasn't called"
    finally:
        agent.stop()
        t.join()
예제 #11
0
def test_communication():
    """Test that two mailbox can communicate through the node."""
    node = LocalNode()
    mailbox1 = MailBox(OEFLocalConnection("mailbox1", node))
    mailbox2 = MailBox(OEFLocalConnection("mailbox2", node))

    mailbox1.connect()
    mailbox2.connect()

    msg = DefaultMessage(type=DefaultMessage.Type.BYTES, content=b"hello")
    msg_bytes = DefaultSerializer().encode(msg)
    envelope = Envelope(to="mailbox2",
                        sender="mailbox1",
                        protocol_id=DefaultMessage.protocol_id,
                        message=msg_bytes)
    mailbox1.send(envelope)

    msg = FIPAMessage(0, 0, 0, FIPAMessage.Performative.CFP, query=None)
    msg_bytes = FIPASerializer().encode(msg)
    envelope = Envelope(to="mailbox2",
                        sender="mailbox1",
                        protocol_id=FIPAMessage.protocol_id,
                        message=msg_bytes)
    mailbox1.send(envelope)

    msg = FIPAMessage(0, 0, 0, FIPAMessage.Performative.PROPOSE, proposal=[])
    msg_bytes = FIPASerializer().encode(msg)
    envelope = Envelope(to="mailbox2",
                        sender="mailbox1",
                        protocol_id=FIPAMessage.protocol_id,
                        message=msg_bytes)
    mailbox1.send(envelope)

    msg = FIPAMessage(0, 0, 0, FIPAMessage.Performative.ACCEPT)
    msg_bytes = FIPASerializer().encode(msg)
    envelope = Envelope(to="mailbox2",
                        sender="mailbox1",
                        protocol_id=FIPAMessage.protocol_id,
                        message=msg_bytes)
    mailbox1.send(envelope)

    msg = FIPAMessage(0, 0, 0, FIPAMessage.Performative.DECLINE)
    msg_bytes = FIPASerializer().encode(msg)
    envelope = Envelope(to="mailbox2",
                        sender="mailbox1",
                        protocol_id=FIPAMessage.protocol_id,
                        message=msg_bytes)
    mailbox1.send(envelope)

    time.sleep(5.0)

    envelope = mailbox2.inbox.get(block=True, timeout=1.0)
    msg = DefaultSerializer().decode(envelope.message)
    assert envelope.protocol_id == "default"
    assert msg.get("content") == b"hello"
    envelope = mailbox2.inbox.get(block=True, timeout=1.0)
    msg = FIPASerializer().decode(envelope.message)
    assert envelope.protocol_id == "fipa"
    assert msg.get("performative") == FIPAMessage.Performative.CFP
    envelope = mailbox2.inbox.get(block=True, timeout=1.0)
    msg = FIPASerializer().decode(envelope.message)
    assert envelope.protocol_id == "fipa"
    assert msg.get("performative") == FIPAMessage.Performative.PROPOSE
    envelope = mailbox2.inbox.get(block=True, timeout=1.0)
    msg = FIPASerializer().decode(envelope.message)
    assert envelope.protocol_id == "fipa"
    assert msg.get("performative") == FIPAMessage.Performative.ACCEPT
    envelope = mailbox2.inbox.get(block=True, timeout=1.0)
    msg = FIPASerializer().decode(envelope.message)
    assert envelope.protocol_id == "fipa"
    assert msg.get("performative") == FIPAMessage.Performative.DECLINE

    mailbox1.disconnect()
    mailbox2.disconnect()
예제 #12
0
def test_connection():
    """Test that two mailbox can connect to the node."""
    node = LocalNode()
    mailbox1 = MailBox(OEFLocalConnection("mailbox1", node))
    mailbox2 = MailBox(OEFLocalConnection("mailbox2", node))

    mailbox1.connect()
    mailbox2.connect()

    mailbox1.disconnect()
    mailbox2.disconnect()