Table of Contents
This section shows the basics of the agent development process by describing the steps needed to create a simple agent. A simple pre-compiled example, called Hello World, is provided with the ADK. This agent does nothing more than say Hello Agent World! We will show you how to code, compile and run this example.
To start an agent application, you need the following:
Depending on your platform, habitat.bat or habitat.sh
A description of the habitat, specified in an XML file.
One or more agents, stored in dna files.
The Hello World example is found in the scenarios subdirectory of the ADK root directory, along with several other precomipled examples. Each example has its own habitat description file. The habitat description file is an XML document that lists the agents that need to be created on startup. The agents are stored in dna files in the agents directory. Here, you will find the helloagent.dna, which contains the agent for this example.
Habitats are started by invoking the habitat script, either habitat.bat or habitat.sh. One of the arguments you can start the habitat script with is the name of the XML file that specifies the initial contents of the habitat. (Other arguments are described in the previous chapter) If no xml file is given as argument, the habitat is started with no new agents. If no agents from a previous run are present in the database, the habitat will be empty.
To run the Hello World example: start a shell, go the scenarios directory of the ADK installation and type:
habitat helloworld.xml
This prints the following output:
tainer:~/src/tryllian/adk-3.0b3/scenarios boud$ ./habitat.sh helloworld.xml Tryllian's Agent Habitat (C) Copyright Tryllian Solutions B.V. 2005 ADK v3.0 Using /Users/boud/src/tryllian/adk-3.0b3 as logging directory. Remote plugins allowed: false Starting plugin: com.tryllian.messenger.jxta.JXTAMessengerPlugin Primary name of this host: tainer.local Primary address of this host: 172.16.1.8 tainer.local resolves to 172.16.1.8 172.16.1.8 resolves to 172.16.1.8 *** Resolved primary IP address does not match primary host name *** *** Potential problem detected *** *** Consult the developers guide *** Attempting to guess my IP-address .! Guessed my IP: 172.16.1.8 But this computer has a defective network configuration. NetConfig Hostname: 172.16.1.8 NetConfig IP Address: 172.16.1.8 Registering: com.tryllian.messenger.jxta.JXTAMessengerPlugin[RemoteStub [ref: [endpoint:[172.16.1.8:49281](local),objID:[1]]]] Habitat ID: 60000003-0000c350bec7fbd4 Habitat name: tainer ----------------------------------------- Restored 0 checkpointed agents and created 1 agent from habitat XML file. [hello-agent] Hello World
To stop the habitat, press Control-C.
As you can see from the output, a habitat was started. By reading the helloworld.xml file a habitat was then set up. This file specifies that one agent needs to be created, the HelloAgent. This agent prints the tradiotional friendly message, and goes on printing it.
We will first examine the XML file format in more detail before explaining the Java code needed to write the HelloAgent application.
The building blocks of the habitat are agents. The XML habitat description files allow you to specify which of these components you want to create. There is only one habitat per VM. Habitat is merely a word for the agent container: a collection of agents that provides services. For more details, refer to the Developer's Guide.
The XML file for the Hello World example is as follows:
<!DOCTYPE habitat SYSTEM "http://www.tryllian.com/xml/habitat_1_1.dtd">
<habitat>
<agent dna="scenarios/agents/helloworld.dna"/>
</habitat>
You can see that there are several tags are included here, let's go through a brief description of each. Note that XML is case sensitive.
The <habitat> tag starts the construction of the habitat XML file the same way a <html> tag starts an html file.
The <agent> tag creates an agent. You have to specify the DNA file of the agent. This is where the bytecode for the agent and all other resources the agent needs is stored. There can be zero or more agents in a habitat, each with a different DNA file if needed.
The starting point of everny agent is a Java class that extends the Agent interface from the tryllian.afc.agent package.
This example shows the following features:
Agent structure / classes
Tasks
Compiling, signing and deploying an agent
Review the source file located in scenarios/src/tryllian/scenarios/helloworld:
| HelloWorldAgent.java (The agent class) |
The agent class is a shell that has no intelligence or behavior. This is provided to the agent by adding tasks that tell the agent how to perform certain behavior. As an agent developer, you will create agent behavior by writing and combining tasks.
You therefore can only make an agent actually do something by adding a task to it. In this case there is the LogTask that prints "Hello World", wrapped in a PeriodicalTask that will execute any other tasks in a continuous loop. This task is proactive; it starts to execute when it is added to the agent. Other tasks are reactive: they start to execute in reaction to an event, such as receiving a message.
We will modify the message and observe the difference after compiling this agent and putting it in a new DNA file.
To compile and package agents, we use the ANT build system (see http://jakarta.apache.org/ant/manual/). It is also possible to create agents without using ANT, but that takes considerably more effort and this example will only use ANT. ANT uses two files to coordinate the build process: build.properties, which contains some variables, for instance definitions for class paths and a choice of compiler, and build.xml which is actually a program that determines how the various targets are to be built.
The code of the HelloWorldAgent:
package tryllian.scenarios.helloworld;
import tryllian.afc.agent.Agent;
import tryllian.afc.task.standard.LogTask;
import tryllian.afc.task.standard.PeriodicalTask;
/**
* Simple example agent that will print "Hello World".
*/
public class HelloWorldAgent extends Agent
implements tryllian.are.TransientAgent
{
public HelloWorldAgent(String[] args) {
// Set name if specified in habitat xml file
if (args != null && args.length > 0) {
setName(args[0]);
}
else {
setName("hello-agent");
}
// Print "Hello World" continuously. Change this.
addTask(new PeriodicalTask(new LogTask("Bonjour, tout le monde!")));
}
}
The call to ANT is wrapped in a shell script or a batch file, depending upon the host OS. This makes recreating agents very simple: to compile the Hello World example and create the agent, type:
build helloworld
The build process consists of the following steps:
Compiling the Java classes.
Packaging the class files in one or more jar file.
Creating the agent dna file (combining a descriptor file and jar files together).
After building, a file called helloworld.dna will be built from your new code in the agents directory. To run your agent, type
habitat helloworld.xml
and observe the messages!
You can find more information in the Developers Guide, the ARE Specifications document, and of course in the other examples, as described in the ADK Scenarios document.