有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将代理从不同平台注册到位于JADE中远程的不同平台?

我有两台运行代理的电脑。两者都通过局域网(或wifi)连接。我想让这些特工沟通。我找到的一种方法是提供代理的完整地址。下面是代码片段

AID a = new AID("A@192.168.14.51:1099/JADE",AID.ISGUID);
a.addAddresses("http://192.168.14.51:7778/acc");
msg.addReceiver(a);
send(msg);

但是,一旦我在一个平台上启动代理,我希望其他平台上的代理能够在其黄页上注册服务,以便我可以从相同的服务列表中搜索合适的代理。我找了一下,但什么也找不到。请给我建议,我如何才能做到这一点


共 (1) 个答案

  1. # 1 楼答案

    嗯,你在找DF联盟。据我所知,这只是“连接”DFs。 “jade all examples”文件夹中的yelloPages包中有一个示例。它创建寄存器、订阅者、搜索者和subDF代理。注册代理使用soe属性注册代理,其他代理执行其工作。SubDF创建涉及DF Federation的子DF。对于您,我将代码修改为:

    接下来的三个代理在端口1099上运行,如下所示:

    (一)

     package examples.yellowPages;
    
        import jade.core.Agent;
        import jade.core.AID;
        import jade.domain.DFService;
        import jade.domain.FIPAException;
        import jade.domain.FIPANames;
        import jade.domain.FIPAAgentManagement.DFAgentDescription;
        import jade.domain.FIPAAgentManagement.ServiceDescription;
        import jade.domain.FIPAAgentManagement.Property;
    
        /**
           This example shows how to register an application specific service in the Yellow Pages
           catalogue managed by the DF Agent so that other agents can dynamically discover it.
           In this case in particular we register a "Weather-forecast" service for 
           Italy. The name of this service is specified as a command line argument.
           @author Giovanni Caire - TILAB
         */
        public class DFRegisterAgent extends Agent {
    
          protected void setup() {
            String serviceName = "unknown";
    
            // Read the name of the service to register as an argument
            Object[] args = getArguments();
            if (args != null && args.length > 0) {
                serviceName = (String) args[0];
            }
    
            // Register the service
            System.out.println("Agent "+getLocalName()+" registering service \""+serviceName+"\" of type \"weather-forecast\"");
            try {
                DFAgentDescription dfd = new DFAgentDescription();
                dfd.setName(getAID());
                ServiceDescription sd = new ServiceDescription();
                sd.setName(serviceName);
                sd.setType("weather-forecast");
                // Agents that want to use this service need to "know" the weather-forecast-ontology
                sd.addOntologies("weather-forecast-ontology");
                // Agents that want to use this service need to "speak" the FIPA-SL language
                sd.addLanguages(FIPANames.ContentLanguage.FIPA_SL);
                sd.addProperties(new Property("country", "Italy"));
                dfd.addServices(sd);
    
                DFService.register(this, dfd);
            }
            catch (FIPAException fe) {
                fe.printStackTrace();
            }
          } 
        }
    

    (二)

    package examples.yellowPages;
    
    import jade.core.Agent;
    import jade.core.AID;
    import jade.domain.DFService;
    import jade.domain.FIPAException;
    import jade.domain.FIPANames;
    import jade.domain.FIPAAgentManagement.DFAgentDescription;
    import jade.domain.FIPAAgentManagement.ServiceDescription;
    import jade.domain.FIPAAgentManagement.SearchConstraints;
    import jade.util.leap.Iterator;
    
    /**
       This example shows how to search for services provided by other agents 
       and advertised in the Yellow Pages catalogue managed by the DF agent.
       In this case in particular we search for agents providing a 
       "Weather-forecast" service.
       @author Giovanni Caire - TILAB
     */
    public class DFSearchAgent extends Agent {
    
      protected void setup() {
        // Search for services of type "weather-forecast"
        System.out.println("Agent "+getLocalName()+" searching for services of type \"weather-forecast\"");
        try {
            // Build the description used as template for the search
            DFAgentDescription template = new DFAgentDescription();
            ServiceDescription templateSd = new ServiceDescription();
            templateSd.setType("weather-forecast");
            template.addServices(templateSd);
    
            SearchConstraints sc = new SearchConstraints();
            // We want to receive 10 results at most
            sc.setMaxResults(new Long(10));
    
            DFAgentDescription[] results = DFService.search(this, template, sc);
            if (results.length > 0) {
                System.out.println("Agent "+getLocalName()+" found the following weather-forecast services:");
                for (int i = 0; i < results.length; ++i) {
                    DFAgentDescription dfd = results[i];
                    AID provider = dfd.getName();
                    // The same agent may provide several services; we are only interested
                    // in the weather-forcast one
                    Iterator it = dfd.getAllServices();
                    while (it.hasNext()) {
                        ServiceDescription sd = (ServiceDescription) it.next();
                        if (sd.getType().equals("weather-forecast")) {
                            System.out.println("- Service \""+sd.getName()+"\" provided by agent "+provider.getName());
                        }
                    }
                }
            }   
            else {
                System.out.println("Agent "+getLocalName()+" did not find any weather-forecast service");
            }
        }
        catch (FIPAException fe) {
            fe.printStackTrace();
        }
      } 
    }
    

    (三)

    package examples.yellowPages;
    
    import jade.core.Agent;
    import jade.core.AID;
    import jade.domain.DFService;
    import jade.domain.FIPAException;
    import jade.domain.FIPANames;
    import jade.domain.FIPAAgentManagement.DFAgentDescription;
    import jade.domain.FIPAAgentManagement.ServiceDescription;
    import jade.domain.FIPAAgentManagement.Property;
    import jade.domain.FIPAAgentManagement.SearchConstraints;
    import jade.proto.SubscriptionInitiator;
    import jade.lang.acl.ACLMessage;
    import jade.util.leap.Iterator;
    
    /**
       This example shows how to subscribe to the DF agent in order to be notified 
       each time a given service is published in the yellow pages catalogue.
       In this case in particular we want to be informed whenever a service of type
       "Weather-forecast" for Italy becomes available.
       @author Giovanni Caire - TILAB
     */
    public class DFSubscribeAgent extends Agent {
    
      protected void setup() {
            // Build the description used as template for the subscription
            DFAgentDescription template = new DFAgentDescription();
            ServiceDescription templateSd = new ServiceDescription();
            templateSd.setType("weather-forecast");
            templateSd.addProperties(new Property("country", "Italy"));
            template.addServices(templateSd);
    
            SearchConstraints sc = new SearchConstraints();
            // We want to receive 10 results at most
            sc.setMaxResults(new Long(10));
    
            addBehaviour(new SubscriptionInitiator(this, DFService.createSubscriptionMessage(this, getDefaultDF(), template, sc)) {
                protected void handleInform(ACLMessage inform) {
                System.out.println("Agent "+getLocalName()+": Notification received from DF");
                try {
                        DFAgentDescription[] results = DFService.decodeNotification(inform.getContent());
                    if (results.length > 0) {
                        for (int i = 0; i < results.length; ++i) {
                            DFAgentDescription dfd = results[i];
                            AID provider = dfd.getName();
                            // The same agent may provide several services; we are only interested
                            // in the weather-forcast one
                            Iterator it = dfd.getAllServices();
                            while (it.hasNext()) {
                                ServiceDescription sd = (ServiceDescription) it.next();
                                if (sd.getType().equals("weather-forecast")) {
                                    System.out.println("Weather-forecast service for Italy found:");
                                    System.out.println("- Service \""+sd.getName()+"\" provided by agent "+provider.getName());
                                }
                            }
                        }
                    }   
                    System.out.println();
                }
                catch (FIPAException fe) {
                    fe.printStackTrace();
                }
                }
            } );
      } 
    }
    

    4)这是最后一个。它创建DF并在DFRegister代理中注册,即完成DF联合。我在1331端口上运行了这个。请记住更改IP地址。(u可以使用-local-port 1331在不同的端口上运行代理。 记住在此之前运行以前的代理 您可以将它放在不同的eclipse项目中并运行它

    import jade.core.*;
    import jade.core.behaviours.*;
    import jade.domain.FIPAAgentManagement.*;
    import jade.domain.FIPAException;
    import jade.domain.DFService;
    import jade.domain.FIPANames;
    import jade.util.leap.Iterator;
    
    /**
    This is an example of an agent that plays the role of a sub-df by 
    automatically registering with a parent DF.
    Notice that exactly the same might be done by using the GUI of the DF.
    <p>
    This SUBDF inherits all the functionalities of the default DF, including
    its GUI.
    @author Giovanni Rimassa - Universita` di Parma
    @version $Date: 2003-12-03 17:57:03 +0100 (mer, 03 dic 2003) $ $Revision: 4638 $
    */
    
    public class SubDF2 extends jade.domain.df {
    
    
      public void setup() {
    
       // Input df name
       int len = 0;
       byte[] buffer = new byte[1024];
    
       try {
    
    //     AID parentName = getDefaultDF();
            AID parentName = new AID("df@10.251.216.135:1099/JADE");
            parentName.addAddresses("http://NikhilChilwant:7778/acc");
    
         //Execute the setup of jade.domain.df which includes all the default behaviours of a df 
         //(i.e. register, unregister,modify, and search).
         super.setup();
    
         //Use this method to modify the current description of this df. 
         setDescriptionOfThisDF(getDescription());
    
         //Show the default Gui of a df.
         super.showGui();
    
         DFService.register(this,parentName,getDescription());
         addParent(parentName,getDescription());
             System.out.println("Agent: " + getName() + " federated with default df.");
    
    
             DFAgentDescription template = new DFAgentDescription();
                ServiceDescription templateSd = new ServiceDescription();
                templateSd.setType("weather-forecast");
                templateSd.addProperties(new Property("country", "Italy"));
                template.addServices(templateSd);
    
                SearchConstraints sc = new SearchConstraints();
                // We want to receive 10 results at most
                sc.setMaxResults(new Long(10));
                DFAgentDescription[] results = DFService.search(this,parentName, template, sc);
            /*  if (results.length > 0) {*/
                    System.out.println("SUB DF ***Agent "+getLocalName()+" found the following weather-forecast services:");
                    for (int i = 0; i < results.length; ++i) {
                        DFAgentDescription dfd = results[i];
                        AID provider = dfd.getName();
                        // The same agent may provide several services; we are only interested
                        // in the weather-forcast one
                        Iterator it = dfd.getAllServices();
                        while (it.hasNext()) {
                            ServiceDescription sd = (ServiceDescription) it.next();
                            if (sd.getType().equals("weather-forecast")) {
                                System.out.println("- Service \""+sd.getName()+"\" provided by agent "+provider.getName());
                            }
                        }
                    }/*}*/
    
    
                    String serviceName = "unknown2";
                    DFAgentDescription dfd = new DFAgentDescription();
                    dfd.setName(getAID());
                    ServiceDescription sd = new ServiceDescription();
                    sd.setName(serviceName);
                    sd.setType("weather-forecast2");
                    // Agents that want to use this service need to "know" the weather-forecast-ontology
                    sd.addOntologies("weather-forecast-ontology2");
                    // Agents that want to use this service need to "speak" the FIPA-SL language
                    sd.addLanguages(FIPANames.ContentLanguage.FIPA_SL);
                    sd.addProperties(new Property("country2", "Italy2"));
                    dfd.addServices(sd);
    
                    DFService.register(this, parentName,dfd);
    
        }catch(FIPAException fe){fe.printStackTrace();}
      }
    
      private DFAgentDescription getDescription()
      {
         DFAgentDescription dfd = new DFAgentDescription();
         dfd.setName(getAID());
         ServiceDescription sd = new ServiceDescription();
         sd.setName(getLocalName() + "-sub-df");
         sd.setType("fipa-df");
         sd.addProtocols(FIPANames.InteractionProtocol.FIPA_REQUEST);
         sd.addOntologies("fipa-agent-management");
         sd.setOwnership("JADE");
         dfd.addServices(sd);
         return dfd;
      }
    
    }
    

    运行代码后,您可以看到,subDF代理能够找到在其联邦DF上注册的代理

    您也可以在这里下载完整的代码:http://tinyurl.com/Agent-on-different-platforms