Chapter 3. Scenario: Messaging example

Table of Contents

3.1. Source files
3.2. Reactive tasks
3.3. Sending messages
3.4. Requesting a collection of agents
3.5. Task scheduling
3.6. Agent parameters in habitat xml file / agent creation

This example shows the following features:

3.1. Source files

Carefully review the source files found in src/tryllian/scenarios/chat1. This directory contains the following files:

ChatTask.java (Task which sends chat messages)
ChatterAgent1.java (The agent)
CreateCopiesTask.java (Task which handles the a new ChatterAgent1)
ListenToChatterTask.java Task which receives chat messages

Build the example by typing in the scenarios directory:

build chat1

Run the example:

habitat chat1.xml

It shows agents chatting to each other.

3.2. Reactive tasks

The ListenToChatterTask is an example of a ReactiveTask; a task that does nothing until it receives a message. It has a MessageFilter to filter the messages it wants to receive. When a message is accepted by the MessageFilter, the method handleMessage() is called. This method prints a message.

3.3. Sending messages

ChatTask sends messages to other agents using SendMessageTask. (tryllian.afc.task.interaction.SendMessageTask)

3.4. Requesting a collection of agents

The ChatTask uses the tryllian.afc.task.find.Directory that it got using the tryllian.afc.task.AgentContext.getDirectory() to ask the system for all agents in the habitat.

3.5. Task scheduling

Tasks can be scheduled in a particular order (normally, they will be executed in parallel) by adding them to a Task Scheduler (tryllian.afc.task.TaskScheduler).

3.6. Agent parameters in habitat xml file / agent creation

Edit the chat1.xml file. After the following line,

<arg value="Mark"/>

add an extra argument to the second agent:

<arg value="3"/>

The arguments specified in the xml file are passed in a String[] to the constructor of the agent. The first argument refers to the name of the agent, the second argument specifies the number of extra agents to create. The agent creates other agents, by using tryllian.afc.task.standard.CreateAgentTask. Run the chat1 example again by typing

habitat chat1.xml

Watch the extra agents chatting.