Getting a list of all JADE containers

谁说胖子不能爱 提交于 2019-12-09 22:35:02

问题


I want to get a list of all containers in the current platform. This question is similar, but the answer is obsolete and the method is by querying to the AMS agent. Is there any simpler way out than to communicate via ACL messages which I think is complex, there should be a way out to get a simple list of containers. Thanks for your help


回答1:


You can achieve this by using the AMSSubscriber class and listen to the events when a container is added or removed. See sample code below:

public class myAgent extends Agent {

  private ArrayList<ContainerID> availableContainers = new ArrayList<ContainerID>();

  /**
   * Agent initializations
  **/
  protected void setup() {

    AMSSubscriber subscriber = new AMSSubscriber(){
      protected void installHandlers(Map handlers){
        EventHandler addedHandler = new EventHandler(){
          public void handle(Event event){
              AddedContainer addedContainer = (AddedContainer) event;
              availableContainers.add(addedContainer.getContainer());
          }
        };
    handlers.put(IntrospectionVocabulary.ADDEDCONTAINER,addedHandler);


        EventHandler removedHandler = new EventHandler(){
          public void handle(Event event){
              RemovedContainer removedContainer = (RemovedContainer) event;
              ArrayList<ContainerID> temp = new ArrayList<ContainerID>(availableContainers);
              for(ContainerID container : temp){
                  if(container.getID().equalsIgnoreCase(removedContainer.getContainer().getID()))
                      availableContainers.remove(container);
              }
          }
        };
        handlers.put(IntrospectionVocabulary.REMOVEDCONTAINER,removedHandler);
      }
    };
    addBehaviour(subscriber);
  }
}

Reference: 1) Developing multi-agent systems with JADE By Fabio Luigi Bellifemine, Giovanni Caire, Dominic Greenwood (page 111) 2) Jade API



来源:https://stackoverflow.com/questions/9094522/getting-a-list-of-all-jade-containers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!