2015-02-22 14 views
6

hi pracuję nad moimi tezami pod jadeitemjak zdobyć agentów na wszystkich pojemnikach jade?

Używam ams do odkrycia wszystkich agentów jade na głównym kontenerze, ale gdy próbowałem zrobić kilka kontenerów agentów, nie przeszukałem wszystkich kontenerów, aby uzyskać dostęp do wszystkich agentów

proszę mi pomóc ustalające mój kod, który odkryć tylko agentem AMS bieżącym kontenerze

że kod używam do tworzenia agenta pod pojemniki.

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(); 

     } 

że mój agent, który musi nadawać się 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(); 
     } 

Odpowiedz

6

wystarczy wysłać zapytanie do agenta AMS, a następnie wydrukować lub zrobić cokolwiek chcesz z listy agenta (obecny środek jade w middleware):

krok 1: wysłać zapytanie do 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) { ... } 

krok 2: showi ng wynik zapytania:

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

ja już to zrobił na moim kodu, ale kiedy uruchomić wiele pojemników, które nie działają. –

+0

cóż, ten kod jest testowany niedawno, działa tak dobrze, nawet z wieloma kontenerami, sprawdź ponownie – steevn

+0

Zawiesza się czasami losowo !! –

3
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 
    } 
+1

Dodaj wyjaśnienie kodu, aby inni użytkownicy mogli zrozumieć Twoją odpowiedź w przyszłości. – Brody

Powiązane problemy