Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Agents: Not just for Bond anymore

Learn what agents are and how to create them using IBM's Aglet Workbench

  • Print
  • Feedback

Page 3 of 6

Let's trace a single mail carrier's voyage on a typical morning mail delivery, and outline agent concepts at work in the process of the mail carrier earning a living.

As each new day dawns, the post office (which could be considered a static agent) spawns an army of postal carriers, assigning them each a pile of mail to deliver. At this point, the Creation event has occurred in the life of one of these newly spawned postal carrier agents, name of Marvin. In response to this event, Marvin the mail carrier executes a series of behaviors that marks the top of the morning. First, he gulps down a few cups of coffee and then packs his assigned pile of mail into his mailbag. After packing his bags, Marvin plans an optimal route (that is, after consulting a map and the tactics of a few traveling salespeople) and begins his daily deliveries. In Java, Marvin's Creation event handler might look as follows:

public void onCreation(MailPile pile) {
    while (sleepy()) {
        drinkCoffee();
    }
    mailbag = new MailBag(pile);
    route = new Route(mailbag);
    dispatch(route.firstStop());
}


The next interesting event in Marvin's day occurs when he arrives at the first house along his route. At this point, Marvin searches his mailbag for mail addressed to the resident and inserts it into the resident's mailbox. He then takes a break (remember, this is a federal employee) and continues on to his next destination. An interesting exception to the usual behavior occurs when Marvin completes his route and arrives back at the post office. He performs his end-of-day routine, such as clocking out.

public void onArrival() {
    if (getCurrentAddress().equals("Post Office")) {
        retire();
    } else {
        deliverMail(mailbag.getMailForAddress(getCurrentAddress()));
        loiter();
        dispatch(route.nextStop());
    }
}


Consider another type of agent in this world, the bane of mail carriers everywhere: the watchdog. Spike the watchdog is generally a static agent, obediently waiting for intruders to tread on his master's property, at which point he will not hesitate to thrash them severely. To model this behavior, we will use message-passing between our Marvin the mailman agent and Spike the watchdog agent.

public void onAgentArriving(Agent newArrival) {
    if (!newArrival().equals(getOwner())) {
        newArrival.sendMessage(new Message("Woof"));
    }
}


To maintain the integrity of his pantlegs, Marvin must somehow respond to messages of this variety. A typical message handler for this message might read as follows:

public void handleMessage(Message message) {
    if (message.kind().equals("Woof")) {
        message.sendReply("SitBoySit");
        eludeAttackingHound();
    }
}



Mail carrier lifecycle


Marvin's daily travels consist of the following steps, illustrated in the diagram Mail carrier lifecycle:

  1. Post Office spawns an army of mail carriers.
  2. Mail carrier migrates to first destination in itinerary.
  3. Mail carrier interacts with agents at first stop, delivers his mail, and continues to the next stop.
  4. Mail carrier returns to the Post Office.


Now that we have demonstrated the concepts behind mobile agents, we will compare them to other paradigms of distributed computing and determine which classes of applications lend themselves to agent implementations.

  • Print
  • Feedback

Resources