【问题标题】:Create multiple agents with Java JADE使用 Java JADE 创建多个代理
【发布时间】:2016-04-19 18:39:24
【问题描述】:

我对 Eclipse 中的 JADE 平台有点陌生。我想创建多个代理,我放了一个 for 循环并增加了计数器来创建代理,它曾经运行良好,但是当我添加 ThiefAgent 时,它不起作用。它只创建一个 PoliceAgent 和一个 ThiefAgent。 该代码运行良好,但不会创建很多代理。

这是 main.java 代码:

 public class Main {

   public static void main(String[] args) {

    /*********************************************************************/
    Runtime rt=Runtime.instance();
    ProfileImpl p=new ProfileImpl();
    p.setParameter(Profile.MAIN_HOST, "localhost");
    p.setParameter(Profile.GUI, "true");


    ContainerController cc1=rt.createMainContainer(p);


    /**************creation of 5 police agents*****************/

    for(int i=1; i<6; i++)
    {

       AgentController ac1;
    try {



        ac1=cc1.createNewAgent("PoliceAgent"+i, "Agents.PoliceAgent", null);


        ac1.start();    

    } catch (StaleProxyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }



         /**************creation of 3 thief agents*****************/

        for(int j=1; j<4; j++)
        {

       AgentController ac2;
    try {


        ac2=cc1.createNewAgent("ThiefAgent"+j, "Agents.ThiefAgent",    null);


        ac2.start();    

    } catch (StaleProxyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }
     }

        }
      }

我尝试为两个代理都放置一个 For 循环,但没有任何改变。 有什么错误?提前致谢。

【问题讨论】:

    标签: java eclipse agents-jade agents


    【解决方案1】:
    1. 创建 PoliceAgent 类:

      public class PoliceAgent extends Agent{
          @Override
          public void setup()
          {
              System.out.println("Police agent name is: " +getAID().getName());
          }
      }       
      
    2. 创建 ThiefAgent 类:

      public class ThiefAgent extends Agent{
          @Override
          public void setup()
          {
              System.out.println("Thief agent name is: " +getAID().getName());
          }
      }
      
    3. 创建 AgentWorker 主类:

      public class PoliceAgent{
      
         public static void main(String... args){
             Runtime runtime = Runtime.instance();
             Profile profile = new ProfileImpl();
             profile.setParameter(Profile.MAIN_HOST, "localhost");
             profile.setParameter(Profile.GUI, "true");
             ContainerController containerController = runtime.createMainContainer(profile);
      
             for(int i=1; i<6; i++){
                 AgentController policeAgentController;
                 try {
                     policeAgentController = containerController.createNewAgent("PoliceAgent"+i, "ua.agent.PoliceAgent", null);
                     policeAgentController.start();    
                 } catch (StaleProxyException e) {
                     e.printStackTrace();
                 }
             }
      
             for(int i=1; i<4; i++){
                 AgentController thiefAgentController;
                 try {
                     thiefAgentController = containerController.createNewAgent("ThiefAgent"+i, "ua.agent.ThiefAgent", null);
                     thiefAgentController.start();    
                 } catch (StaleProxyException e) {
                     e.printStackTrace();
                 }
              }
         }
      
      }
      

    我没有在这段代码中看到错误,但我编写了 3 个类并且它们可以工作。由于我没有看到全貌,所以我只能说问题可能出在代理类和Jade的问题上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      相关资源
      最近更新 更多