- 論壇徽章:
- 0
|
一個(gè)Agent如何以程序的方式聲稱另一個(gè)Agent:可以通過以下方式進(jìn)行創(chuàng)建:
String name = "Alice" ;
AgentContainer c = getContainerController();
try {
AgentController a = c.createNewAgent( name,
"Pong", null );
a.start();
}
catch (Exception e){}
其中,createNewAgent方法的第一個(gè)參數(shù)是要?jiǎng)?chuàng)建的Agent的名稱,第二個(gè)是Agent的類名,實(shí)際使用時(shí)要包括他所在的命名空間(包名),第三個(gè)參數(shù)是要傳入的參數(shù)的名稱。
例子:package jadePrime.acl;
/**
*
Program which creates another Agent and sends
------------ it some messages
comm2.java
*
*/
import
jade.core.Agent;
import
jade.core.behaviours.*;
import
jade.core.AID;
import
jade.wrapper.AgentContainer;
import
jade.wrapper.AgentController;//這兩個(gè)是必須要引用的包
import
jade.lang.acl.*;
public class
Comm2 extends Agent {
String
name = "Alice"
;
AID
alice = new
AID( name, AID.ISLOCALNAME );
protected
void setup(){
AgentContainer
c=getContainerController();
System.out.println("find
container!");
try{
AgentController
a=c.createNewAgent(name,"jadePrime.acl.Pong",null);
a.start();
System.out.println("++++pong
has created:"+alice);
}
catch
(Exception e){
System.out.println("Create
Agent Error!");
addBehaviour(new
SimpleBehaviour(this){
int
n=0;
public
void action()
{
ACLMessage
msg = new ACLMessage(ACLMessage.INFORM);
msg.setContent("Message
#" + n );
msg.addReceiver(alice);
System.out.println("+++
Sending: " + n);
send(msg);
block(
1000 );
}
public
boolean done(){return ++n>3;}
});
}
}
}
當(dāng)我們創(chuàng)建一個(gè)Comm2的Agent時(shí),在控制臺(tái)上會(huì)打印出創(chuàng)建alice成功的消息。
本文來自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u/17663/showart_197141.html |
|