aiomas.agent

This module implements the base class for agents (Agent) and containers for agents (Container).

Every agent must live in a container. A container can contain one ore more agents. Containers are responsible for making connections to other containers and agents. They also provide a factory function for spawning new agent instances and registering them with the container.

Thus, the Agent base class is very light-weight. It only has a name, a reference to its container and an RPC router (see aiomas.rpc).

class aiomas.agent.Container(addr, clock=None, codec=None, extra_serializers=())[source]

Container for agents.

It can instantiate new agents via spawn() and can create connections to other agents (via connect()).

In order to destroy a container and close all of its sockets, call shutdown().

When a container is created, it also creates a server socket and binds it to addr which is a ('host', port) tuple (see the host and port parameters of asyncio.BaseEventLoop.create_server() for details).

You can optionally also pass a codec class. Note that containers can only “talk” to containers using the same codec.

You can also pass a list of extra_serializers for the codec. The list entires need to be callables that return a tuple with the arguments for add_serializer().

To decouple a multi-agent system from the system clock, you can pass an optional clock object which should be an instance of BaseClock. This makes it easier to integrate your system with other simulators that may provide a clock for you or to let your MAS run as fast as possible. By default, AsyncioClock will be used.

codec

The codec used by this container. Instance of aiomas.codecs.Codec.

clock

The clock of the container. Instance of aiomas.clocks.BaseClock.

spawn(agent_type, *args, **kwargs)[source]

Create an instance of agent_type, passing a reference to this container, a name and the provided args and kwargs to it.

This is equivalent to agent = agent_type(container, name, *args, **kwargs), but also registers the agent with the container.

connect(url)[source]

Connect to the argent available at url and return a proxy to it.

url is a string <protocol>://<addr>//<agent-id> (e.g., 'tcp://localhost:5555/0').

shutdown(async=False)[source]

Close the container’s server socket and the RPC services for all outgoing TCP connections.

If async is left to False, this method calls asyncio.BaseEventLoop.run_until_complete() in order to wait until all sockets are closed.

If the event loop is already running when you call this method, set async to True. The return value then is a coroutine that you need to yield from in order to actually shut the container down:

yield from container.shutdown(async=True)
validate_aid(aid)[source]

Return the class name for the agent represented by aid if it exists or None.

class aiomas.agent.Agent(container, addr)[source]

Base class for all agents.

router

Descriptor that creates an RPC Router for every agent instance.

You can override this in a sub-class if you need to. (Usually, you don’t.)

container

The Container that the agent lives in.

addr

The agent’s address.