1. The Agents

The agent module implements the base Agent class. All agents should be derived from this class. The agents can have special methods, which can be called by other agents and are decorated with provide(). To handle their conversations they have with other agents they use the ConversationRegister

class hamas.agents.Agent(mts, aid)

Bases: object

The base class for all other agents. An instance is already able to communicate to other instances. In particular, it uses a ConversationRegister and a MessageTransportSystem for the connunication with other agents.

Parameters:
aid

str – The unique agent identifier. Composed of the platform name and an agent name.

am_aid

str – The agent identifier of the AgentManager.

get_aid()
coroutine get_reply(message, timeout=5)

Opens a conversation and returns directly the reply

Sometimes it is more useful to get the reply instead of the future.

Parameters:
  • message (Message) – The request message to open the conversation.
  • timeout (float) – The amount of time the requesting agent is willing to wait
Returns:

Return type:

reply (Reply,list)

open_conversation(message, timeout=5)

This method transmits a request and returns a reply future.

It creates a Future with the reply of the addressed agent. When the addressed agent replies, the future’s result will be set. If the timeout is reached before any reply, an timeout exception will be thrown If the request is sent as broadcast, the communication will set the result with the received replies before the timeout after a certain timeout. This is a coroutine because it has to wait for send to finish.

Parameters:
  • message (Message) – The conversation opener, i.e. a remote process call
  • timeout (float) – After this amount of time the conversation times out
Returns:

The reply future contains the reply of the the call

Return type:

reply (Future)

platform_name

str – The first part of the agent’s indentifier, the platform name

coroutine receive(message)
coroutine remote_process_call(function, *args, recipient=None, timeout=5, routing='unicast', **kwargs)
coroutine send(message)

Send a message from this agent to another

Parameters:message (Message) – The message for the recipient
coroutine send_reply(content, message, performative=None)
class hamas.agents.ConversationRegister

Bases: object

A dictionary for storing the conversations. When a new conversation is created, by calling new_conversation(), a new asyncio.Queue is created and a asyncio.Future is returned. Every message has a conversation identifier, so the agent can group all messages with the same conversation ID in this register. The agent can put() new messages in this register, but it can also get() a message when it has time to process a message. When the agent considers a conversation as finished, it can set a result for this conversation in the corresponding future.

_queues

dict – A dictionary with the conversation ID conv_id as key and a asyncio.Queue as value.

_futs

dict – A dictionary with the conversation ID conv_id as key and a asyncio.Future as value.

__contains__(conv_id)

conv_id in self returns True or False, whether conv_id is an active conversation or not.

__len__()

len(self) returns the quantity of unfinished conversations.

coroutine get(conv_id)

Remove and return an item from the queue. If queue is empty, wait until an item is available.

Parameters:conv_id (bytes) – The key which allows access to the conversation.
Returns:
The incoming message associated with this
conversation.
Return type:msg(Message)
new_conversation(conv_id)

Allocate a new conversation including a asyncio.Queue and a asyncio.Future.

Parameters:conv_id (bytes) – The key which allows access to the conversation.
coroutine put(conv_id, msg)

Put a new message in the register.

Parameters:
  • conv_id (bytes) – The key which allows access to the conversation.
  • msg (Message) – The incoming message associated with this conversation.
set_result(conv_id, result)
hamas.agents.provide(func)

provide() acts as function decorator and is used for methods, that the agent provides as a service to other agents.