Exemplo n.º 1
0
    def _setup(self):

        self.agentdb = dict()

        AAD = AmsAgentDescription()
        AAD.name = self.getAID()
        AAD.ownership = "SPADE"
        AAD.state = "active"

        self.agentdb[self.getAID().getName()] = AAD

        if self.getDF():
            AAD = AmsAgentDescription()
            AAD.name = self.getDF()
            AAD.ownership = "SPADE"
            AAD.state = "active"
            self.agentdb[self.getDF().getName()] = AAD

        db = self.DefaultBehaviour()
        #db.setPeriod(0.25)
        #self.setDefaultBehaviour(db)
        mt = Behaviour.ACLTemplate()
        mt.setOntology("FIPA-Agent-Management")
        mt.setPerformative("request")
        mt.setProtocol('fipa-request')
        self.addBehaviour(db, Behaviour.MessageTemplate(mt))

        self.wui.start()
Exemplo n.º 2
0
        def _process(self):
            error = False
            msg = self._receive(True)
            if msg is not None:
                self.myAgent.DEBUG("AMS received message " + str(msg), "info",
                                   "ams")
                if msg.getPerformative().lower() == 'request':
                    if msg.getOntology() and msg.getOntology().lower(
                    ) == "fipa-agent-management":
                        if msg.getLanguage().lower() == "fipa-sl0":
                            content = self.sl0parser.parse(msg.getContent())
                            ACLtemplate = Behaviour.ACLTemplate()
                            ACLtemplate.setConversationId(
                                msg.getConversationId())
                            ACLtemplate.setSender(msg.getSender())
                            template = (Behaviour.MessageTemplate(ACLtemplate))

                            if "action" in content:
                                self.myAgent.DEBUG(
                                    "AMS: " + str(content.action) +
                                    " request. " + str(content), "info", "ams")
                                if "register" in content.action \
                                        or "deregister" in content.action:
                                    self.myAgent.addBehaviour(
                                        AMS.RegisterBehaviour(msg, content),
                                        template)
                                elif "get-description" in content.action:
                                    self.myAgent.addBehaviour(
                                        AMS.PlatformBehaviour(msg, content),
                                        template)
                                elif "search" in content.action:
                                    self.myAgent.addBehaviour(
                                        AMS.SearchBehaviour(msg, content),
                                        template)
                                elif "modify" in content.action:
                                    self.myAgent.addBehaviour(
                                        AMS.ModifyBehaviour(msg, content),
                                        template)
                            else:
                                reply = msg.createReply()
                                reply.setSender(self.myAgent.getAID())
                                reply.setPerformative("refuse")
                                reply.setContent("( " + msg.getContent() +
                                                 "(unsuported-function " +
                                                 content.keys()[0] + "))")
                                self.myAgent.send(reply)

                                return -1

                        elif msg.getLanguage().lower() == "rdf":
                            # Content in RDF
                            co = msg.getContentObject()
                            content = msg.getContent()
                            ACLtemplate = Behaviour.ACLTemplate()
                            ACLtemplate.setConversationId(
                                msg.getConversationId())
                            ACLtemplate.setSender(msg.getSender())
                            template = (Behaviour.MessageTemplate(ACLtemplate))

                            if "fipa:action" in co.keys(
                            ) and "fipa:act" in co["fipa:action"].keys():
                                self.myAgent.DEBUG(
                                    "AMS: " +
                                    str(co["fipa:action"]["fipa:act"]) +
                                    " request. " + str(co.asRDFXML()), "info",
                                    "ams")
                                if co["fipa:action"]["fipa:act"] in [
                                        "register", "deregister"
                                ]:
                                    self.myAgent.addBehaviour(
                                        AMS.RegisterBehaviour(msg, content),
                                        template)
                                elif co["fipa:action"][
                                        "fipa:act"] == "get-description":
                                    self.myAgent.addBehaviour(
                                        AMS.PlatformBehaviour(msg, content),
                                        template)
                                elif co["fipa:action"]["fipa:act"] == "search":
                                    self.myAgent.addBehaviour(
                                        AMS.SearchBehaviour(msg, content),
                                        template)
                                elif co["fipa:action"]["fipa:act"] == "modify":
                                    self.myAgent.addBehaviour(
                                        AMS.ModifyBehaviour(msg, content),
                                        template)
                            else:
                                reply = msg.createReply()
                                reply.setSender(self.myAgent.getAID())
                                reply.setPerformative("refuse")
                                co["unsuported-function"] = "true"
                                reply.setContentObject(co)
                                self.myAgent.send(reply)
                                return -1

                        else:
                            error = "(unsupported-language " + msg.getLanguage(
                            ) + ")"
                    else:
                        error = "(unsupported-ontology " + msg.getOntology(
                        ) + ")"

                # By adding 'not-understood' to the following list of unsupported acts, we prevent an
                # infinite loop of counter-answers between the AMS and the registering agents
                elif msg.getPerformative().lower() not in [
                        'failure', 'refuse', 'not-understood'
                ]:
                    error = "(unsupported-act " + msg.getPerformative() + ")"
                if error:
                    reply = msg.createReply()
                    reply.setSender(self.myAgent.getAID())
                    reply.setPerformative("not-understood")
                    reply.setContent("( " + msg.getContent() + error + ")")
                    self.myAgent.send(reply)
                    return -1

            return 1