【问题标题】:how to get agents on all containers jade?如何在所有容器上获取​​代理翡翠?
【发布时间】:2015-02-22 00:00:07
【问题描述】:

你好,我正在用玉石写我的论文

我使用 ams 发现主容器上的所有翡翠代理,但是当我尝试制作一些代理容器时,我未能搜索所有容器以获取所有代理

请帮我修复只发现当前容器的 ams 代理的代码

我用来在容器下创建代理的代码。

  Runtime rt= Runtime.instance();

    Profile p=new ProfileImpl();


    AgentContainer AgentContainere = rt.createMainContainer(p);


    AgentController[] tab=new AgentController[N];

    try {


        int k=0;
        for (int i = 0; i < N; i++) {

            if (i % 100 == 0) {
                p=new ProfileImpl();
                AgentContainere = rt.createMainContainer(p);
            }

            if ((i+1)%(N/NbrC)==0) {
                tab[i] = AgentContainere.createNewAgent(psoeudo+" - "+i, "Agents.KmeanAgent", new Object[]{K,NbrC,true,k});
                k++;
            }else
            tab[i] = AgentContainere.createNewAgent(psoeudo+" - "+i, "Agents.KmeanAgent", new Object[]{K,NbrC,false,N});    
        }


        for (AgentController tab1 : tab) {
            tab1.start();

        }

我的代理需要广播 aclmessage :

     try {
            currentCluster = new Point(p.getX(), p.getY());
            tableOfCenters[index] = currentCluster;
            AMSAgentDescription[] agents = null;
            boolean notstable = true;
            int found = 0;
            long sleeptime=7000;
            while (notstable) {
                try {
                    sleep(sleeptime);

                    SearchConstraints c = new SearchConstraints();
                    c.setMaxResults(new Long(-1));

                    agents = AMSService.search(this, new AMSAgentDescription(), c);
                    if (agents.length > found) {
                        found = agents.length;
                        sleeptime+=5000;

                    } else {
                        notstable = false;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            System.out.println(found + "the found agent");
            AID myId = getAID();
            ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
            int sendloop=0;
            msg.setContent(getName() + currentCluster + " index = " + index);
            for (AMSAgentDescription agent : agents) {

                AID sendTo = agent.getName();
                if (!sendTo.equals(myId) && !sendTo.getName().toLowerCase().contains("ams") && !sendTo.getName().toLowerCase().contains("df")) {
                    msg.addReceiver(sendTo);
                    sendloop++;
                    if (sendloop%10==0) {
                       send(msg);
                       System.out.println(msg);
                       msg.clearAllReceiver();
                    }
                }

            }
            if (sendloop%10!=0) {
                       send(msg);
                       System.out.println(msg);
                       msg.clearAllReceiver();
                    }




            System.out.println("********************");
            System.out.println(msg);
            System.out.println("********************");
        } catch (Exception e) {
            e.printStackTrace();
        }

【问题讨论】:

    标签: containers agents-jade


    【解决方案1】:

    您只需要向 AMS 代理发送请求,然后打印或使用代理列表做任何您想做的事情(玉中间件中的当前代理):

    第 1 步:向 AMS 发送请求:

        // Required imports
    
        import jade.domain.AMSService;
        import jade.domain.FIPAAgentManagement.*;
    
        ...
        AMSAgentDescription [] agents = null;
    
        try {
            SearchConstraints c = new SearchConstraints();
            c.setMaxResults ( new Long(-1) );
            agents = AMSService.search( this, new AMSAgentDescription (), c );
        }
        catch (Exception e) { ... }
    

    第 2 步:显示请求的结果:

    for (int i=0; i<agents.length;i++){
         AID agentID = agents[i].getName();
         System.out.println(agentID.getLocalName());
    }
    

    【讨论】:

    • 我已经在我的代码上这样做了,但是当我启动多个不起作用的容器时。
    • 嗯,这段代码最近测试过,效果很好,即使有多个容器,再检查一遍
    • 它有时会随机挂断!
    【解决方案2】:
    import jade.core.Agent;
    import jade.core.AID;
    
    import jade.domain.AMSService;
    import jade.domain.FIPAAgentManagement.*;
    
    public class SearchC extends Agent 
    {
        protected void setup() 
        {
            AMSAgentDescription [] agents = null;
            try {
                SearchConstraints c = new SearchConstraints();// object to searh                    //the container exist on the System
                c.setMaxResults (new Long(-1));//define infinity result to C
                agents = AMSService.search( this, new AMSAgentDescription (), c );//putt all agent found on the system to the agents list
            }
            catch (Exception e) {
                System.out.println( "Problem searching AMS: " + e );
                e.printStackTrace();
            }
    
            AID myID = getAID();// this methode to get the idesntification of //agents such as (Name , adress , host ....etc)
            for (int i=0; i<agents.length;i++)
            {
                AID agentID = agents[i].getName();
                System.out.println(
                    ( agentID.equals( myID ) ? "*** " : "    ")
                    + i + ": " + agentID.getName() 
                );
            }
            doDelete();// kill agent
            System.exit(0); // exit System
        }
    

    【讨论】:

    • 请添加对您的代码的解释,以便其他用户将来能够理解您的答案。
    猜你喜欢
    • 2010-10-05
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多