五.JADE中如何使用DF(Directory Facilitator)agent提供的黄页服务。这个比较好理解,DF相当于一个目录服务器,每个提供服务的Agent可以向DF注册其服务,然后,其他的Agent可以从DF中查询该类服务,也可以订阅这类服务,如果是后者,那么一旦这类服务被注册到DF中,则订阅方就可以收到。例程如下:
文件一, DFRegisterAgent.java 服务提供方
package yellowpages;
import jade.core.Agent;
import jade.core.AID;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAAgentManagement.Property;
/**
*
* @author Administrator
*/
public class DFRegisterAgent extends Agent {
protected void setup() {
String serviceName = "unknown";
// Read the name of the service to register as an argument
Object[] args = getArguments();
if (args != null && args.length > 0) {
serviceName = (String) args[0];
}
// Register the service
System.out.println("Agent "+getLocalName()+" registering service \""+serviceName+"\" of type \"weather-forecast\"");
try {
DFAgentDescription dfd = new DFAgentDescription();
dfd.setName(getAID());
ServiceDescription sd = new ServiceDescription();
sd.setName(serviceName);
sd.setType("weather-forecast");
// Agents that want to use this service need to "know" the weather-forecast-ontology
sd.addOntologies("weather-forecast-ontology");
// Agents that want to use this service need to "speak" the FIPA-SL language
sd.addLanguages(FIPANames.ContentLanguage.FIPA_SL);
sd.addProperties(new Property("country", "Italy"));
dfd.addServices(sd);
DFService.register(this, dfd);
}
catch (FIPAException fe) {
fe.printStackTrace();
}
}
}
文件二.
DFSubscribeAgent.java订阅方
package yellowpages;
import jade.core.Agent;
import jade.core.AID;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAAgentManagement.Property;
import jade.domain.FIPAAgentManagement.SearchConstraints;
import jade.proto.SubscriptionInitiator;
import jade.lang.acl.ACLMessage;
import jade.util.leap.Iterator;
/**
*
* @author Administrator
*/
/** Creates a new instance of DFSubscribeAgent */
public class DFSubscribeAgent extends Agent {
protected void setup() {
// Build the description used as template for the subscription
DFAgentDescription template = new DFAgentDescription();
ServiceDescription templateSd = new ServiceDescription();
templateSd.setType("weather-forecast");
templateSd.addProperties(new Property("country", "Italy"));
template.addServices(templateSd);
SearchConstraints sc = new SearchConstraints();
// We want to receive 10 results at most
sc.setMaxResults(new Long(10));
addBehaviour(new SubscriptionInitiator(this, DFService.createSubscriptionMessage(this, getDefaultDF(), template, sc)) {
protected void handleInform(ACLMessage inform) {
System.out.println("Agent "+getLocalName()+": Notification received from DF");
try {
DFAgentDescription[] results = DFService.decodeNotification(inform.getContent());
if (results.length > 0) {
for (int i = 0; i < results.length; ++i) {
DFAgentDescription dfd = results[i];
AID provider = dfd.getName();
// The same agent may provide several services; we are only interested
// in the weather-forcast one
Iterator it = dfd.getAllServices();
while (it.hasNext()) {
ServiceDescription sd = (ServiceDescription) it.next();
if (sd.getType().equals("weather-forecast")) {
System.out.println("Weather-forecast service for Italy found:");
System.out.println("- Service \""+sd.getName()+"\" provided by agent "+provider.getName());
}
}
}
}
System.out.println();
}
catch (FIPAException fe) {
fe.printStackTrace();
}
}
} );
}
}
这样,编译后,我们为了体现发布服务和订阅服务时间上的一致关系,我们从命令行来运行这些类。假设已编译成功,文件标志在某目录下yellowpages\ DFSubscribeAgent.class 和yellowpages\ DFRegisterAgent.class,我们在yellowpages目录下运行命令
java jade.Boot –gui service1: yellowpages.DFRegisterAgent
系统运行主容器,同时控制台上显示Agent service1 registering service “noname” of type "weather-forecast"
然后我们在图形界面下,start new Agent,AgentName:subscriber ClassName:yellowpages.DFSubscribeAgent 添加后,控制台上会显示相关启动后的信息,但不会显示收到服务的消息。然后再start new Agent,AgentName:service2 ClassName:yellowpages.DFRegisterAgent 参数设为:rainny,这是控制台上显示注册服务成功的消息和订阅到服务的消息。显示如下:
JADE架构模型: