Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add create_acl_with_sender syntax sugar #128

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mango/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .messages.message import create_acl, Performatives
from .messages.message import create_acl, create_acl_with_sender, Performatives
from .agent.core import Agent, AgentAddress
from .agent.role import Role, RoleAgent, RoleContext
from .container.factory import (
Expand Down
33 changes: 33 additions & 0 deletions mango/messages/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,36 @@ def create_acl(
for key, value in acl_metadata.items():
setattr(message, key, value)
return message


def create_acl_with_sender(
content,
receiver_addr: AgentAddress,
sender_addr: AgentAddress,
acl_metadata: None | dict[str, Any] = None,
is_anonymous_acl=False,
):
"""
create_acl function, which does not require to pass the receiver_addr twice

self.context.send_message(
**create_acl_with_sender(
"message content",
receiver_addr,
self.context.addr,
acl_metadata={
"in_reply_to": "reply_to",
},
)
)
"""
return {
"content": create_acl(
content,
receiver_addr,
sender_addr,
acl_metadata,
is_anonymous_acl,
),
"receiver_addr": receiver_addr,
}
19 changes: 18 additions & 1 deletion tests/unit_tests/core/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from mango import activate, create_acl, create_tcp_container
from mango import activate, create_acl, create_acl_with_sender, create_tcp_container
from mango.agent.core import Agent


Expand Down Expand Up @@ -71,6 +71,23 @@ async def test_send_acl_message():
# THEN
assert agent2.test_counter == 1

@pytest.mark.asyncio
async def test_send_acl_message_wrapper():
# GIVEN
c = create_tcp_container(addr=("127.0.0.1", 5555))
agent = c.register(MyAgent())
agent2 = c.register(MyAgent())

async with activate(c) as c:
await agent.send_message(
**create_acl_with_sender("", receiver_addr=agent2.addr, sender_addr=agent.addr)
)
msg = await agent2.inbox.get()
_, content, meta = msg
agent2.handle_message(content=content, meta=meta)

# THEN
assert agent2.test_counter == 1

@pytest.mark.asyncio
async def test_schedule_message():
Expand Down
Loading