Friday, January 25, 2008

How to use JadeGateway to bridge between a Servlet Container and JADE Platform

JadeGateway class provides a communication bridge between an external application and the JADE platform. I will show a simple version of using JadeGateway. Later posts will elaborate more on the best way to implement this.
The reason I am starting this is, there is no clear documentation out there to explain this stuff. So, hopefully this will help someone to not go through the pain.

By the way, I just extended the bookTrading example in the jade distribution to get this to work.

1. Create JSP form




Title



Cost







2. Create a simple bookTradingServlet which will send an ACLMessage to the agent. we will be using a simple OneShotBehaviour

- Create your servlet class
- Declare a private variable
private JadeGateway gateway = null;
- Initialize JADE gateway

public void init(ServletConfig config) throws ServletException
{
JadeGateway.init(null, null);
}

The first parameter should be null if you want to utilize the internal Agent class, which is supplied by default.
The second parameter is null unless you want to set some container parameters in the newly created container.

- in the doPost method, create ACLMessage, Behaviour and execute JADEGateway

protected void process(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String title = "Sent Buyer Agent a Request :-)";
String bookTitle = req.getParameter("Title");
String cost = req.getParameter("Cost");

try
{

JadeGateway.execute(new OneShotBehaviour()
{
public void action()
{
final ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
AID buyer = new AID("Buyer@LA7750389:1099/JADE", false);
msg.addReceiver(buyer);
msg.setConversationId("book-trade-setup");
msg.setContent("TESTING Message via servlet");
myAgent.send(msg);
}
});
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}

out.println(ServletUtilities.headWithTitle(title) +
"\n" +
"

" + title + "

\n" +
"
    \n" +
    "
  • param1: "
    + bookTitle + "\n" +
    "
  • param2: "
    + cost + "\n" + "\n" +
    "
\n" +
"");
}


3. Now you need to add a Request Processor in the Agent that you want this message to be processed:
-- Put this in the setup method of the agent
// Perform the request
addBehaviour(new ProcessExternalRequests());

4. Create a Behaviour to process the requests.
- Remember the conversation id we used in the servlet should match the messages we are trying to pull out. This way we are looking for messages that we are interested in.

private class ProcessExternalRequests extends CyclicBehaviour {
private MessageTemplate mt;

public void action() {
System.out.println("Processing ProcessExternalRequests:action");
// create a message template
mt = MessageTemplate.MatchConversationId("book-trade-setup");
ACLMessage msg = myAgent.receive(mt);
if(msg != null)
{
if(msg.getPerformative() == ACLMessage.REQUEST)
{
String content = msg.getContent();
System.out.println("Agent " +
myAgent.getAID().getLocalName() +
" Content = " + content
);
}
}
else
{
block();
}
}
}

Voila. We have an interface into the external world.

Hope this helps.

Thursday, January 24, 2008

OK Lets Start

First post. Multi Agent Systems and its coverage.