我是 JADE 新手,最近创建了一个服务代理,可以从两个词典(WordNet(r) 3.0 (2006) 和 Easton's 1897 Bible Dictionary)中查找单词的定义。 我想将ServiceAgent的所有服务和行为合并为一个。我的目标是最终拥有一个代理类和一个行为类,能够处理来自 dict.org 的任何字典的请求,但我不知道如何做到这一点。
我正在使用 Linux Red Hat,并且已经下载了 Apache ANT 1.9.2 和 Java JDK 1.5.0(如果有帮助的话)。
这是ServiceAgent.java代码:
package jadelab1;
import jade.core.*;
import jade.core.behaviours.*;
import jade.lang.acl.*;
import jade.domain.*;
import jade.domain.FIPAAgentManagement.*;
import java.net.*;
import java.io.*;
public class ServiceAgent extends Agent {
protected void setup () {
//services registration at DF
DFAgentDescription dfad = new DFAgentDescription();
dfad.setName(getAID());
//service no 1
ServiceDescription sd1 = new ServiceDescription();
sd1.setType("answers");
sd1.setName("wordnet");
//Serive no 2
ServiceDescription sd2 = new ServiceDescription();
sd2.setType("answers");
sd2.setName("Easton's 1897 Bible Dictionary");
//add them all
dfad.addServices(sd1);
dfad.addServices(sd2);
try {
DFService.register(this,dfad);
} catch (FIPAException ex) {
ex.printStackTrace();
}
addBehaviour(new AnotherDictionaryCyclicBehaviour(this));
addBehaviour(new WordnetCyclicBehaviour(this));
//doDelete();
}
protected void takeDown() {
//services deregistration before termination
try {
DFService.deregister(this);
} catch (FIPAException ex) {
ex.printStackTrace();
}
}
public String makeRequest(String serviceName, String word)
{
StringBuffer response = new StringBuffer();
try
{
URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream input;
url = new URL("http://dict.org/bin/Dict");
urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String content = "Form=Dict1&Strategy=*&Database=" + URLEncoder.encode(serviceName) + "&Query=" + URLEncoder.encode(word) + "&submit=Submit+query";
//forth
printout = new DataOutputStream(urlConn.getOutputStream());
printout.writeBytes(content);
printout.flush();
printout.close();
//back
input = new DataInputStream(urlConn.getInputStream());
String str;
while (null != ((str = input.readLine())))
{
response.append(str);
}
input.close();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
//cut what is unnecessary
return response.substring(response.indexOf("<hr>")+4, response.lastIndexOf("<hr>"));
}
}
class AnotherDictionaryCyclicBehaviour extends CyclicBehaviour {
ServiceAgent agent;
public AnotherDictionaryCyclicBehaviour(ServiceAgent agent) {
this.agent = agent;
}
public void action() {
MessageTemplate template = MessageTemplate.MatchOntology("Easton's 1897 Bible Dictionary");
ACLMessage message = agent.receive(template);
if (message == null) {
block();
}
else {
// Process the incoming message and handle the request for the new dictionary service
String content = message.getContent();
ACLMessage reply = message.createReply();
reply.setPerformative(ACLMessage.INFORM);
String response = "";
try
{
// Call the makeRequest method for the new dictionary service
response = agent.makeRequest("easton", content);
}
catch (NumberFormatException ex)
{
response = ex.getMessage();
}
reply.setContent(response);
agent.send(reply);
}
}
}
class WordnetCyclicBehaviour extends CyclicBehaviour
{
ServiceAgent agent;
public WordnetCyclicBehaviour(ServiceAgent agent)
{
this.agent = agent;
}
public void action()
{
MessageTemplate template = MessageTemplate.MatchOntology("wordnet");
ACLMessage message = agent.receive(template);
if (message == null)
{
block();
}
else
{
//process the incoming message
String content = message.getContent();
ACLMessage reply = message.createReply();
reply.setPerformative(ACLMessage.INFORM);
String response = "";
try
{
response = agent.makeRequest("wn",content);
}
catch (NumberFormatException ex)
{
response = ex.getMessage();
}
reply.setContent(response);
agent.send(reply);
}
}
}
我还包括 MyAgent.java 代码(以防万一):
package jadelab1;
import jade.core.*;
import jade.core.behaviours.*;
import jade.lang.acl.*;
import jade.domain.*;
import jade.domain.FIPAAgentManagement.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyAgent extends Agent {
protected void setup () {
displayResponse("Hello, I am " + getAID().getLocalName());
addBehaviour(new MyCyclicBehaviour(this));
//doDelete();
}
protected void takeDown() {
displayResponse("See you");
}
public void displayResponse(String message) {
JOptionPane.showMessageDialog(null,message,"Message",JOptionPane.PLAIN_MESSAGE);
}
public void displayHtmlResponse(String html) {
JTextPane tp = new JTextPane();
JScrollPane js = new JScrollPane();
js.getViewport().add(tp);
JFrame jf = new JFrame();
jf.getContentPane().add(js);
jf.pack();
jf.setSize(400,500);
jf.setVisible(true);
tp.setContentType("text/html");
tp.setEditable(false);
tp.setText(html);
}
}
class MyCyclicBehaviour extends CyclicBehaviour {
MyAgent myAgent;
public MyCyclicBehaviour(MyAgent myAgent) {
this.myAgent = myAgent;
}
public void action() {
ACLMessage message = myAgent.receive();
if (message == null) {
block();
} else {
String ontology = message.getOntology();
String content = message.getContent();
int performative = message.getPerformative();
if (performative == ACLMessage.REQUEST)
{
//I cannot answer but I will search for someone who can
DFAgentDescription dfad = new DFAgentDescription();
ServiceDescription sd = new ServiceDescription();
sd.setName(ontology);
dfad.addServices(sd);
try
{
DFAgentDescription[] result = DFService.search(myAgent, dfad);
if (result.length == 0) myAgent.displayResponse("No service has been found ...");
else
{
String foundAgent = result[0].getName().getLocalName();
myAgent.displayResponse("Agent " + foundAgent + " is a service provider. Sending message to " + foundAgent);
ACLMessage forward = new ACLMessage(ACLMessage.REQUEST);
forward.addReceiver(new AID(foundAgent, AID.ISLOCALNAME));
forward.setContent(content);
forward.setOntology(ontology);
myAgent.send(forward);
}
}
catch (FIPAException ex)
{
ex.printStackTrace();
myAgent.displayResponse("Problem occured while searching for a service ...");
}
}
else
{ //when it is an answer
myAgent.displayHtmlResponse(content);
}
}
}
}
我注意到 ServiceAgent 中的以下几行是最重要的: sd2.setName("伊斯顿 1897 年圣经词典"); MessageTemplate template = MessageTemplate.MatchOntology("伊斯顿 1897 年圣经词典"); 响应 = agent.makeRequest("easton", content);
这是我已经尝试过的:
修改后的ServiceAgent类:
public class ServiceAgent extends Agent {
private String dictionaryName;
protected void setup() {
// This should Get dictionary name from the arguments passed during agent creation
Object[] args = getArguments();
if (args != null && args.length > 0) {
dictionaryName = (String) args[0];
// rest of the original code here
} else {
System.out.println("No dictionary name provided. Agent will not provide any service.");
doDelete();
}
}
// rest of the original code here
public String makeRequest(String word) {
// rest of the original code here
response = agent.makeRequest(dictionaryName, content);
// rest of the original code here
}
}
以及修改后的 AnotherDictionaryCyclicBehaviour 类:
class AnotherDictionaryCyclicBehaviour extends CyclicBehaviour {
private String dictionaryName;
ServiceAgent agent;
public AnotherDictionaryCyclicBehaviour (ServiceAgent agent, String dictionaryName) {
this.agent = agent;
this.dictionaryName = dictionaryName;
}
public void action() {
MessageTemplate template = MessageTemplate.MatchOntology(dictionaryName);
// rest of the original code here
response = agent.makeRequest(content);
// rest of the original code here.
}
}
通过进行这些更改,代理及其行为应该变得更加通用。字典名称不再是硬编码的,因此用户应该能够在代理配置期间动态指定它。问题是,更改后,代理仍然无法识别其他词典。我想这个问题非常简单,但我仍然希望得到任何帮助。
您找到该问题的解决方案了吗?我也有同样的情况...