Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1280233
  • 博文数量: 135
  • 博客积分: 10588
  • 博客等级: 上将
  • 技术积分: 1325
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-18 11:12
文章分类

全部博文(135)

文章存档

2013年(6)

2012年(3)

2011年(11)

2010年(7)

2009年(14)

2008年(6)

2007年(42)

2006年(46)

分类: Java

2006-08-27 09:46:07

四.外部应用程序调用Agent
JADE2.3以后的版本都提供了in-process接口来实现外部应用程序对agent的调用。
我们可以通过jade.core.Runtime.instance()来获得jade运行时的一个单独得实例。有两种方法可以用来创建一个jade主容器和一个jade远程容器。
先看简单的例子:
package inprocess;
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);
    }
   
}以上是要调用的agent
package inprocess;
 
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{
            Runtime rt = Runtime.instance();
            rt.setCloseVM(true);
            Profile pMain = new ProfileImpl(null, 8888, null);
            System.out.println("Launching a whole in-process platform..."+pMain);
            AgentContainer mc = rt.createMainContainer(pMain);
            // set now the default Profile to start a container
            ProfileImpl pContainer = new ProfileImpl(null, 8888, null);
            System.out.println("Launching the agent container ..."+pContainer);
            AgentController custom=mc.createNewAgent("custom","inprocess.CustomAgent",null);
            custom.start();           
         
       
        }
        catch (Exception e){
            e.printStackTrace();
        }
       
    }
    }//以上是调用者,在netbeans中调试通过。
 
看一个稍微复杂的例子:
package examples.inprocess;
 
import jade.core.Runtime;
import jade.core.Profile;
import jade.core.ProfileImpl;
 
import jade.wrapper.*;
 
/**
   This class is an example of how you can embed JADE runtime
   environment within your applications.
 
   @author Giovanni Rimassa - Universita' di Parma
 
 */
public class InProcessTest {
 
  // Simple class behaving as a Condition Variable
  public static class CondVar {
    private boolean value = false;
 
    synchronized void waitOn() throws InterruptedException {
      while(!value) {
       wait();
      }
    }
 
    synchronized void signal() {
      value = true;
      notifyAll();
    }
 
  } // End of CondVar class
 
 
  // This class is a custom agent, accepting an Object through the
  // object-to-agent communication channel, and displying it on the
  // standard output.
  public static class CustomAgent extends jade.core.Agent {
 
    public void setup() {
      // Accept objects through the object-to-agent communication
      // channel, with a maximum size of 10 queued objects
      setEnabledO2ACommunication(true, 10);
 
      // Notify blocked threads that the agent is ready and that
      // object-to-agent communication is enabled
      Object[] args = getArguments();
      if(args.length > 0) {
       CondVar latch = (CondVar)args[0];
       latch.signal();
      }
 
      // Add a suitable cyclic behaviour...
      addBehaviour(new jade.core.behaviours.CyclicBehaviour() {
 
       public void action() {
         // Retrieve the first object in the queue and print it on
         // the standard output
         Object obj = getO2AObject();
         if(obj != null) {
           System.out.println("Got an object from the queue: [" + obj + "]");
         }
         else
           block();
       }
 
      });
    }
 
    public void takeDown() {
      // Disables the object-to-agent communication channel, thus
      // waking up all waiting threads
      setEnabledO2ACommunication(false, 0);
    }
 
  } // End of CustomAgent class
 
  public static void main(String args[]) {
 
    try {
 
      Runtime rt = Runtime.instance();//获取jade运行时
 
      // Exit the JVM when there are no more containers around
      rt.setCloseVM(true);
 
      // 看运行参数中是否有-container
      if(args.length > 0) {
       if(args[0].equalsIgnoreCase("-container")) {
         // 创建一个默认的profile
         Profile p = new ProfileImpl(false);
         //p.setParameter(Profile.MAIN, "false");
 
         // Create a new non-main container, connecting to the default
         // main container (i.e. on this host, port 1099)
      System.out.println("Launching the agent container ..."+p);
         AgentContainer ac = rt.createAgentContainer(p);
 
         // 创建一个新的agent
         AgentController dummy = ac.createNewAgent("inProcess", "jade.tools.DummyAgent.DummyAgent", new Object[0]);
 
         // 启动它
         System.out.println("Starting up a DummyAgent...");
         dummy.start();
 
         //10
         Thread.sleep(10000);
 
         // 杀死这个agent
         System.out.println("Killing DummyAgent...");
         dummy.kill();
 
         // 在同一虚拟机上创建另一个容器,NB
         // NB. 两个容器不能共享同一个 Profile对象!!! -->
         // 所以需再创建一个profile对象
         p = new ProfileImpl(false);
         //p.putProperty(Profile.MAIN, "false");
         AgentContainer another = rt.createAgentContainer(p);
 
         // 用两个参数创建一个移动agnet
         Object[] arguments = new Object[2];
         arguments[0] = "Hello World!";
         arguments[1]=dummy;
         AgentController mobile = another.createNewAgent("Johnny", "examples.mobile.MobileAgent", arguments);
         mobile.start();
 
         return;
       }
      }
 
      // 8888端口运行一个完整的平台t
      // create a default Profile
      Profile pMain = new ProfileImpl(null, 8888, null);
 
      System.out.println("Launching a whole in-process platform..."+pMain);
      AgentContainer mc = rt.createMainContainer(pMain);
 
      // 使用默认的profile启动一个容器
      ProfileImpl pContainer = new ProfileImpl(null, 8888, null);
      System.out.println("Launching the agent container ..."+pContainer);
      AgentContainer cont = rt.createAgentContainer(pContainer);
      System.out.println("Launching the agent container after ..."+pContainer);
 
      System.out.println("Launching the rma agent on the main container ...");
      AgentController rma = mc.createNewAgent("rma", "jade.tools.rma.rma", new Object[0]);
      rma.start();
 
      // Launch a custom agent, taking an object via the
      // object-to-agent communication channel. Notice how an Object
      // is passed to the agent, to achieve a startup synchronization:
      // this Object is used as a POSIX 'condvar' or a Win32
      // 'EventSemaphore' object...
 
      CondVar startUpLatch = new CondVar();
 
      AgentController custom = mc.createNewAgent("customAgent", CustomAgent.class.getName(), new Object[] { startUpLatch });
      custom.start();
 
      // Wait until the agent starts up and notifies the Object
      try {
       startUpLatch.waitOn();
      }
      catch(InterruptedException ie) {
       ie.printStackTrace();
      }
          
 
      // Put an object in the queue, asynchronously
      System.out.println("Inserting an object, asynchronously...");
      custom.putO2AObject("Message 1", AgentController.ASYNC);
      System.out.println("Inserted.");
 
      // Put an object in the queue, synchronously
      System.out.println("Inserting an object, synchronously...");
      custom.putO2AObject(mc, AgentController.SYNC);
      System.out.println("Inserted.");
 
    }
    catch(Exception e) {
      e.printStackTrace();
    }
 
  }
 
}
阅读(4815) | 评论(4) | 转发(1) |
给主人留下些什么吧!~~

chinaunix网友2009-05-03 10:43:19

楼上的可以参考JADE的管理员手册administratorsguide.pdf,讲述如何在别的机器上启动容器注册到远程主容器。如果是外部程序启动,如本文所讲述,可以看下面这几行: Profile p = new ProfileImpl(false); //p.setParameter(Profile.MAIN, "false"); // Create a new non-main container, connecting to the default // main container (i.e. on this host, port 1099) System.out.println("Launching the agent container ..."+p); AgentContainer ac = rt.createAgentContainer(p); 显然,只要对Profile p进行修改就可以了,文中的Profile是默认的,就用本机作为host,在Profile中

sand2000312008-10-16 03:09:41

开头说的启动一个远程容器,请问一台机子启动主容器,怎么才能在另一台机子上启动它的辅容器。急