我想在同一台PC上使用JADE将一个代理程序的文件发送给另一个代理程序。 这里执行期间会出现一些错误。
*** Uncaught Exception for agent a ***
ERROR: Agent a died without being properly terminated !!!
java.lang.RuntimeException: Uncompilable source code - incompatible types: java.lang.String cannot be converted to byte[]
State was 2
ERROR: Agent b died without being properly terminated !!!
State was 2
at sendmessage.A.sendMessage(A.java:36)
at sendmessage.A.setup(A.java:25)
at jade.core.Agent$ActiveLifeCycle.init(Agent.java:1490)
at jade.core.Agent.run(Agent.java:1436)
at java.lang.Thread.run(Thread.java:745)
Nov 20, 2015 4:21:34 PM jade.core.messaging.MessagingService removeLocalAliases
INFO: Removing all local alias entries for agent a
Nov 20, 2015 4:21:34 PM jade.core.messaging.MessagingService removeGlobalAliases
INFO: Removing all global alias entries for agent a
*** Uncaught Exception for agent b ***
java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: sendmessage.B.MyBehaviour.receive
at sendmessage.B$MyBehaviour.action(B.java:40)
at jade.core.behaviours.Behaviour.actionWrapper(Behaviour.java:344)
at jade.core.Agent$ActiveLifeCycle.execute(Agent.java:1500)
at jade.core.Agent.run(Agent.java:1439)
at java.lang.Thread.run(Thread.java:745)
Nov 20, 2015 4:21:34 PM jade.core.messaging.MessagingService removeLocalAliases
INFO: Removing all local alias entries for agent b
Nov 20, 2015 4:21:34 PM jade.core.messaging.MessagingService removeGlobalAliases
INFO: Removing all global alias entries for agent b
Nov 20, 2015 4:21:42 PM jade.core.messaging.MessagingService removeLocalAliases
INFO: Removing all local alias entries for agent rma
Nov 20, 2015 4:21:42 PM jade.core.messaging.MessagingService removeGlobalAliases
INFO: Removing all global alias entries for agent rma
发件人:谁使用JADE将文件发送给其他代理商。 包sendmessage;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
*
* @author Administrator
*/
public class A extends Agent {
protected void setup() {
sendMessage();
this.addBehaviour(new MyBehaviour(this));
}
private void sendMessage() {
AID r = new AID("b", AID.ISLOCALNAME);
// ACLMessage acl = new ACLMessage(ACLMessage.REQUEST);
// acl.addReceiver(r);
// acl.setContent("hello, my name is sender!");
// this.send(acl);
String fileName = "a.txt";// get file name
byte[] fileContent = "f://a.txt";// read file content
ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
msg.addReceiver(r);
msg.setByteSequenceContent(fileContent);
msg.addUserDefinedParameter("file-name", fileName);
send(msg);
}
private static class MyBehaviour extends Behaviour {
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.INFORM);
private static int finish;
public MyBehaviour(A aThis) {
}
@Override
public void action() {
ACLMessage acl = myAgent.receive(mt);
if (acl != null) {
System.out.println(myAgent.getLocalName() + " received a reply: " + acl.getContent() + "from " + acl.getSender());
finish = 1;
} else {
this.block();
}
}
@Override
public boolean done() {
return finish == 1;
}
}
}
接收方:通过使用JADE从发送代理接收文件
package sendmessage;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import java.io.File;
/**
*
* @author Administrator
*/
public class B extends Agent {
protected void setup() {
this.addBehaviour(new MyBehaviour(this));
}
private static class MyBehaviour extends Behaviour {
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.REQUEST);
public MyBehaviour(B aThis) {
}
@Override
public void action() {
// ACLMessage acl = myAgent.receive(mt);
// if (acl!= null) {
// System.out.println(myAgent.getLocalName()+ " received a message: "+acl.getContent());
// ACLMessage rep = acl.createReply();
// rep.setPerformative(ACLMessage.INFORM);
// rep.setContent("ok, i received a message!");
// myAgent.send(rep);
ACLMessage msg = receive("Yes Received your file");
if (msg != null) {
String fileName = msg.getUserDefinedParameter("a.txt");
File f = "a.txt"; // create file called fileName
byte[] fileContent = msg.getByteSequenceContent();
// write fileContent to f
}
else {
this.block();
}
}
@Override
public boolean done() {
return false;
}
}
}
答案 0 :(得分:1)
如果您打算在周围交换文件,可能需要为您的代理系统创建一个Ontology。 JADE提供了使用OntologyBean类作为代理通信方法(真正简化事物)的表示的能力。
BeanOntology类以谓词,概念等形式添加自定义语言。在这些自定义语言描述中,您可以放置一个对象以及您可能希望与其一起指定的任何其他信息。如果您按照JADE提供的文档进行操作实际上非常简单 - > tutorial here
祝你好运!