简体   繁体   English

使用 Java JADE 创建多个代理

[英]Create multiple agents with Java JADE

I am kind of new with JADE platform in eclipse.我对 Eclipse 中的 JADE 平台有点陌生。 I want to create multiple agents, I have put a for loop and incremented the counter to create the agents, It used to work well but when I added the ThiefAgent, it didn't work.我想创建多个代理,我放置了一个 for 循环并增加了创建代理的计数器,它曾经运行良好,但是当我添加 ThiefAgent 时,它不起作用。 It only creates one PoliceAgent and one ThiefAgent.它只创建一个 PoliceAgent 和一个 ThiefAgent。 The code works perfectly but it does not create many agents.该代码运行良好,但不会创建很多代理。

this is the main.java code:这是 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();
     }
     }

        }
      }

I tried to put one For loop for both agents but nothing changed.我试图为两个代理放置一个 For 循环,但没有任何改变。 What's the mistake?怎么了? Thanks in advance.提前致谢。

  1. Create PoliceAgent class:创建 PoliceAgent 类:

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

     public class ThiefAgent extends Agent{ @Override public void setup() { System.out.println("Thief agent name is: " +getAID().getName()); } }
  3. Create AgentWorker main class:创建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(); } } } }

I don't see mistakes in this code, but i write 3 classes an they work.我没有看到这段代码中的错误,但我写了 3 个类,它们都可以工作。 Since I don't see the whole picture, so I can only say that the problem could be in the class of agents and the problem of Jade.由于看不到全貌,所以只能说问题可能出在agent类和Jade的问题上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM