2008年10月21日 星期二

JADE/SWT Integaration

JADE is a platform to develop agent based application based on Java language.
I used this package in my dissertation. In this post I would like record the integration between JADE and SWT user interface package.
In my agent application, the agent have to catch the user's eye when the agent wants to notify the user who is working arround a PC station.
So, Here is two questions that I have to deal with. The first one is to know the packages that I have to include in my application, the second one is how could I activate the user interface in my agent process.

The first question is easy to solve by using the plugin inspector and plugin depency user interface from eclipse platform.

  1. Find JFace plug-in in the list
  2. Double click on the plug-in then it will show the whole dependencies. We need all this package in the class path.
So, I added the following jar files into my class path
  • org.eclipse.core.commands_3.4.0.I20080509-2000.jar
  • org.eclipse.equinox.common_3.4.0.v20080421-2006.jar
  • org.eclipse.jface_3.4.0.I20080606-1300.jar
  • org.eclipse.osgi_3.4.0.v20080605-1900.jar
  • org.eclipse.swt_3.4.0.v3448f.jar
Then, I have to deal the activation question. In JADE programming, programers have to code their logic in the Behaviour class. Also, an agent can have more than one behaviour class at the same time.
I created a CyclicBehaviour class to linsten the incoming message from other agent. Once a specific message is caught by the agent then a user interface will be activate.
However, I encounter a problem when using the normal Behaviour to activate the SWT/JFace ApplicationWindow like the following code.

The behaviour class is list as following:

private class WorkerReactionBehaviour extends CyclicBehaviour
{


@Override
public void action() {
System.out.println("from worker agent: message collect");
ACLMessage msg = myAgent.receive();
if (msg != null) {
System.out.println("from agent:" + msg.getSender().getName());
String content =msg.getContent();
if(bGuiShow == false)
{
myAgent.addBehaviour(new GuiBehaviour());
gui.notifyUser(content);
}
}
else
System.out.println("worker agent: no message");
//test
//System.out.println("notified user ...");
//gui.notifyUser("test");
block(2000);

}

}

The user interface activate code is list as following:

public class WorkerGuiImpl extends ApplicationWindow implements WorkerGui {
public void show()
{
if(bShowed == true)
return;
bShowed = true;
this.setBlockOnOpen(true);
this.open();
Display.getCurrent().dispose();
}
}//show

The consequence is the agent behaviour will be block when the user interface shows up.
Hence, I have to make the user interface behaviour execute as a dedicated Java thread.
There is a ThreadedBehaviourFactory supported by JADE plateform to wrap the normal JADE behaviour into dedicated Java thread.
Just using the following code to solve the behaviour blocking problem.

ThreadedBehaviourFactory tbf = new
ThreadedBehaviourFactory();
myAgent.addBehaviour(tbf.wrap(new GuiBehaviour()));


Reference
  1. http://www.nabble.com/JADE-SWT-problem-to20093283.html

沒有留言: