|
|
|
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 .NET. What is it about agents
that makes them so different? One difference between agents
and regular programs is that agents 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 will add an initProps()
method where default values for the agent's properties are set.
Take a look at the following code:
using Agent = com.neokernel.nk.Agent; namespace HelloWorldAgent { /// <summary> /// Now say it again with more personality. /// </summary> public class HelloWorldAgent : Agent { public override void initProps()
{
setDefault("color", "red");
setDefault("greeting", "Hello, world!");
setDefault("language", "English");
}
public override 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
that make our message a little more personal.
Expert Note: Agents automatically provide for storage of their
properties by inheriting the com.neokernel.props.Props
class. Convenience methods inherited from Props provide
a built-in way for agents to set and get their own properties.
|
|
Having made the above changes to our HelloWorldAgent.cs
source file, all we need to do is compile it again. Just like
before, use the Build Solution
command from the Build menu
to compile.
Now you are ready to run our new and improved HelloWorldAgent.
Just as before, run the agent by copying the HelloWorldAgent.dll
to the folder with the neokernel.exe, and typing neokernel
at the dos prompt to start the Neokernel and load your new
agent:
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. C# is a case-sensitive language. Also make sure your source
files are capitalized the same as your classname
property in your XML startup file or you will have problems. If
you have been lazy about case in the past, now is the time to
start paying attention.
|
|
Each time you have run the HelloWorldAgent you should
have noticed some output that says agents are being loaded from
the file neokernel_props.xml. What is this file supposed
to be?
To find out, type the following XML code into a new file named
neokernel_props.xml:
<?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>
</xml> |
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 mandatory property
is the agent's classname. Without a classname,
the Agent Kernel doesn't know which agent class to start
If
you don't have an neokernel_props.xml file in your
startup directory, the server will search the .DLL modules in
the startup directory for agents, and will start the agents
it finds using their default properties.
Once you have created the neokernel_props.xml file,
you can run the neokernel.exe executable without
any additional parameters to start the HelloWorldAgent:
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 a default personality defined in initProps(),
the neokernel_props.xml file lets us change agent
configurations without modifying code.
|
|