不用命令行的形式运行agent
可以通过jade.core.Runtime类可以使外部应用程序调用jade运行环境 ,不要命令行的形式 下面有个简单的例子: InprocessTest类:这个类主要是用来生成运行agent的环境的 package main3;import jade.core.Runtime;import jade.core.Profile;import jade.
可以通过jade.core.Runtime类可以使外部应用程序调用jade运行环境 ,不要命令行的形式
下面有个简单的例子:
InprocessTest类:这个类主要是用来生成运行agent的环境的
package main3;
import jade.core.Runtime;
import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.behaviours.SimpleBehaviour;
import jade.wrapper.*;
import jade.core.Agent;
/**
*
* @author gj
*/
public class InprocessTest {
public static void main(String args[]) {
try{
//通过jade.core.Runtime类可以使外部应用程序调用jade运行环境
Runtime rt = Runtime.instance();
//在这个JVM中,当最后一个容器停止的时候,关闭JVM
rt.setCloseVM(true);
//创建一个代理对象
Profile pMain = new ProfileImpl(null, 8888, null);
System.out.println("Launching a whole in-process platform..."+pMain);
//通过代理对象在当前JVM中创建一个主容器,可以向这个容器中加入agent
AgentContainer mc = rt.createMainContainer(pMain);
//rt.createAgentContainer(Profile p)通过代理对象在当前JVM中创建一个新的agent容器,可以向这个容器中加入agent
// set now the default Profile to start a container
ProfileImpl pContainer = new ProfileImpl(null, 8888, null);
System.out.println("Launching the agent container ..."+pContainer);
//在 容器中创建一个新的agent
//第一个参数是:agent的名字
//第二个参数是:agent类的路径
//第三个参数是:
AgentController custom=mc.createNewAgent("custom","main3.CustomAgent",null);
//启动agent
custom.start();
}
catch (Exception e){
e.printStackTrace();
}
}
}
CustomAgent类:这个类是个简单的agent行为类
package main3;
import jade.core.*;
import jade.core.behaviours.*;
/**
*
* @author Administrator
*/
public class CustomAgent extends Agent{
public void setup(){
SimpleBehaviour helloBehaviour=new SimpleBehaviour(this) {
boolean finished=false;
public void action() {
System.out.println("Hello World Behaviour run: 你好,世界!");
System.out.println("-----我是:-----");
System.out.println("我的本地名称是:"+getLocalName());
System.out.println("我全局唯一的标志名称为:"+getName() );
finished=true;
}
public boolean done() {
return finished;
}
};
addBehaviour(helloBehaviour);
}
}
如果你想用webapp来开发工程也是一样的做法,只要把main写成一个JB,然后调用它就可以了
更多推荐
所有评论(0)