Agent Properties
The HelloWorldAgent is friendly enough, but it doesn't have a whole lot of personality. In this chapter, we'll write an agent that is a little more outgoing.
Now that you've completed the HelloWorldAgent, you may have observed that writing an agent isn't too different from writing other programs in the Java programming language. What is it about agents that makes them so different? One difference between agents and regular programs is that they maintain an identity defined by agent properties.
Before our agent can start using its identity, we'll need to initialize its properties. In addition to the start() method that we used in the previous chapter we'll need to add an initProps() method where default values for the agent's properties can be set.
Take a look at the following code:
import com.cometway.ak.Agent;
/** Now say it again with more personality. */
public class HelloWorldAgent extends Agent
{
public void initProps()
{
setDefault("color", "red");
setDefault("greeting", "Hello, world!");
setDefault("language", "English");
}
public void start()
{
println(getString("greeting"));
println("I like to speak in " + getString("language") + ".");
println("My favorite color is " + getString("color") + ".");
}
}
Agents have many built-in methods for managing their own properties. In the initProps() method above, setDefault() is called to initialize the color, greeting, and language properties for our agent. Instead of the usual "Hello, World!" message, we can use getString() to retrieve agent properties making our message a little more personal.
Expert Note:
Agents automatically provide for storage of their properties by inheriting the com.cometway.props.Props class. Convenience methods inherited from Props provides a built-in way for agents to set and get their own properties.
Properties In Action
Having made the above changes to our HelloWorldAgent.java source file, all we need to do is compile it again. Just like before, use the jc command to compile.
[localhost:ak/demos/helloworld] cometdev% jc HelloWorldAgent.java
Compiling...
HelloWorldAgent.java
[localhost:ak/demos/helloworld] cometdev%
If your compiler output contains errors, fix and recompile until you have no more errors.
Now you are ready to run our new and improved HelloWorldAgent:
[localhost:ak/demos/helloworld] cometdev% ak HelloWorldAgent
[AK] Comet Way Agent Kernel 2.1 Final Candidate 4-10-2003
[000_AgentKernel] Starting on 2003/04/11 17:00:10.443 EDT
[000_AgentKernel] Creating agent com.cometway.xml.XMLStartupAgent
[001_XMLStartupAgent] Starting on 2003/04/11 17:00:10.472 EDT
[001_XMLStartupAgent] Loading agents from ak.xstartup
[001_XMLStartupAgent] Loading agents from .
[000_AgentKernel] Creating agent HelloWorldAgent
[1003_HelloWorldAgent] Starting on 2003/04/11 17:00:10.539 EDT
[1003_HelloWorldAgent] Hello, world!
[1003_HelloWorldAgent] I like to speak in English.
[1003_HelloWorldAgent] My favorite color is red.
[localhost:ak/demos/helloworld] cometdev%
This time around, our agent produces two additional lines of output that tells us it likes the color red and speaks English. Although this is still a very simple example, we now have an agent that creates its own identity that is recalled when its start() method is called.
Expert Note:
If you are having difficulties getting your code to compile, make sure to carefully check that you are using all the correct uppercase, lowercase, or capitalizations in the right places. Java is a case-sensitive language. Also make sure your source files are capitalized the same way as your classname or you will have problems. If you have been lazy about case in the past, now is the time to start paying attention.
Using XML Startup Files
Each time you have run the HelloWorldAgent you should have noticed some output from an agent named XMLStartupAgent who says it is loading agents from the file ak.xstartup. What is this file supposed to be?
To find out, type the following XML code into a new file named ak.xstartup:
<?xml version="1.0"?>
<props_list>
<props>
<classname>HelloWorldAgent</classname>
<color>green</color>
<greeting>?Hola, el Mundo!</greeting>
<language>Spanish</language>
<startup>true</startup>
</props>
</props_list>
This document contains the properties for a single agent's identity. The tags classname, color, greeting, language and startup each specify the value for an agent property. The only property that absolutely must be specified is the agent's classname. Without a classname, the Agent Kernel wouldn't know which agent class to create and start.
Once you have created the ak.xstartup XML file, you can use the ak command without any additional parameters to start the HelloWorldAgent:
[localhost:ak/demos/helloworld] cometdev% ak
[AK] Comet Way Agent Kernel 2.1 Final Candidate 4-10-2003
[000_AgentKernel] Starting on 2003/04/12 00:03:47.493 EDT
[000_AgentKernel] Creating agent com.cometway.xml.XMLStartupAgent
[001_XMLStartupAgent] Starting on 2003/04/12 00:03:47.507 EDT
[001_XMLStartupAgent] Loading agents from ak.xstartup
[000_AgentKernel] Creating agent HelloWorldAgent
[1003_HelloWorldAgent] Starting on 2003/04/12 00:03:47.547 EDT
[1003_HelloWorldAgent] ?Hola, el Mundo!
[1003_HelloWorldAgent] I like to speak in Spanish.
[1003_HelloWorldAgent] My favorite color is green.
[localhost:ak/demos/helloworld] cometdev%
As you can see, the values we supplied for our agent properties in the XML file were used instead of the default values set by initProps(). So even though our agent comes with sort of a default personality, using the ak.xstartup file allows us to easily change agent behaviors without requiring additional code.
Challenge: Try changing the values in your ak.xstartup file and using the ak command to test the results. See what happens when you remove one or more of the parameters. Does the value for the missing property revert back to the default value set in initProps()? What happens when you omit the classname tags? What happens when you use the command ak HelloWorldAgent?
Take your time to understand these basics before moving to the next chapter. Get comfortable changing the code around and testing the results. Mastering these basic skills are key to building much larger agent applications.
Description: A quick demonstration of how Props is used.
Updated: Mon May 19 06:31:21 EDT 2008