def __init__(self, agent, message=None, is_initiator=True): """Initialize method """ super(FipaSubscribeProtocol, self).__init__(agent) self.is_initiator = is_initiator self.message = message self.subscribers = set() self.filter_protocol = Filter() self.filter_protocol.set_protocol(ACLMessage.FIPA_SUBSCRIBE_PROTOCOL) self.filter_subscribe = Filter() self.filter_subscribe.set_performative(ACLMessage.SUBSCRIBE) self.filter_agree = Filter() self.filter_agree.set_performative(ACLMessage.AGREE) self.filter_inform = Filter() self.filter_inform.set_performative(ACLMessage.INFORM) self.filter_refuse = Filter() self.filter_refuse.set_performative(ACLMessage.REFUSE) self.filter_cancel = Filter() self.filter_cancel.set_performative(ACLMessage.CANCEL) self.filter_failure = Filter() self.filter_failure.set_performative(ACLMessage.FAILURE) if self.message is not None: self.filter_conversation_id = Filter() self.filter_conversation_id.set_conversation_id( self.message.conversation_id)
def action(self): # Setting a filter f = Filter() f.set_performative(ACLMessage.INFORM) # Accept only INFORM messages message = self.read() if f.filter(message): # Filtering the message display(self.agent, 'I received this list: \n%s' % message.content)
def main(): # para troca de mensagens esparsas entre leiloeiro e compradores f = Filter() f.performative = ACLMessage.INFORM agents = list() port = int(argv[1]) logger = Logger() # criando objeto a ser leiloado objeto = ObjetoLeiloado('Vaso Antigo', 40) # criando agente leiloeiro agente_leiloeiro = AgenteLeiloeiro( AID(name=f'leiloeiro@localhost:{port}'),f, objeto, logger) agents.append(agente_leiloeiro) port += 1 numero_de_compradores=3 for i in range(numero_de_compradores): # criando agentes compradores agent_dinheiro = randint(100,1000) agente_comprador = AgenteComprador(AID(name=f'comprador_{i}@localhost:{port+i}'), f, logger,agent_dinheiro) agents.append(agente_comprador) start_loop(agents)
def __init__(self, agent, message=None, is_initiator=True): """ método de inicialização ----------------------- """ super(FipaSubscribeProtocol, self).__init__(agent) self.is_initiator = is_initiator self.message = message self.subscribers = set() self.filter_protocol = Filter() self.filter_protocol.set_protocol(ACLMessage.FIPA_SUBSCRIBE_PROTOCOL) self.filter_subscribe = Filter() self.filter_subscribe.set_performative(ACLMessage.SUBSCRIBE) self.filter_agree = Filter() self.filter_agree.set_performative(ACLMessage.AGREE) self.filter_inform = Filter() self.filter_inform.set_performative(ACLMessage.INFORM) self.filter_refuse = Filter() self.filter_refuse.set_performative(ACLMessage.REFUSE) self.filter_cancel = Filter() self.filter_cancel.set_performative(ACLMessage.CANCEL) self.filter_failure = Filter() self.filter_failure.set_performative(ACLMessage.FAILURE)
def __init__(self, agent, message=None, is_initiator=True): """ Inicializes the class that implements FipaContractNetProtocol protocol. :param agent: instance of the agent that will execute the protocol's established behaviours. :param message: message to be sent by the agent when is_initiator parameter is true. :param is_initiator: boolean type parameter that specifies if the protocol instance will act as Initiator or Participant. """ super(FipaContractNetProtocol, self).__init__(agent) self.is_initiator = is_initiator self.received_qty = 0 self.proposes = [] self.message = message self.filter_protocol = Filter() self.filter_protocol.set_protocol( ACLMessage.FIPA_CONTRACT_NET_PROTOCOL) self.filter_cfp = Filter() self.filter_cfp.set_performative(ACLMessage.CFP) self.filter_refuse = Filter() self.filter_refuse.set_performative(ACLMessage.REFUSE) self.filter_propose = Filter() self.filter_propose.set_performative(ACLMessage.PROPOSE) self.filter_accept_propose = Filter() self.filter_accept_propose.set_performative(ACLMessage.ACCEPT_PROPOSAL) self.filter_reject_propose = Filter() self.filter_reject_propose.set_performative(ACLMessage.REJECT_PROPOSAL) self.filter_failure = Filter() self.filter_failure.set_performative(ACLMessage.FAILURE) self.filter_inform = Filter() self.filter_inform.set_performative(ACLMessage.INFORM) if self.message is not None: self.filter_conversation_id = Filter() self.filter_conversation_id.set_conversation_id( self.message.conversation_id)
def action(self): # Setting a filter f = Filter() # The object 'f' is the 'model' of message you want to receive f.set_ontology(self.agent.section ) # The filter only accept agent's section messages if self.agent.has_messages(): message = self.agent.read(f) if message != None: # Filtering the message display(self.agent, 'I received this list: \n%s' % message.content)
def action(self): f = Filter() f.set_performative(ACLMessage.REQUEST) f.set_ontology('foods') message = self.read() if f.filter(message): # If the message satisfies the filter reply = message.create_reply() reply.set_content('meat\nchicken\ncookies\nice cream\nbread') reply.set_performative(ACLMessage.INFORM) self.agent.send(reply) display(self.agent, 'Food list sent to %s.' % message.sender.getLocalName())
def __init__(self, agent, message=None, is_initiator=True): """Inicializa a classe que implementa o protocolo FipaRequestProtocol :param agent: instância do agente que executara os comportamentos estebelecidos pelo protocolo :param message: mensagem a ser enviada pelo agente quando o parâmetro is_initiator for verdadeiro :param is_initiator: parâmetro do tipo booleano que indica se esta instancia do protocolo agirá como Initiator ou como participante """ super(FipaRequestProtocol, self).__init__(agent) self.is_initiator = is_initiator self.message = message self.filter_protocol = Filter() self.filter_protocol.set_protocol(ACLMessage.FIPA_REQUEST_PROTOCOL) self.filter_Request = Filter() self.filter_Request.set_performative(ACLMessage.REQUEST) self.filter_refuse = Filter() self.filter_refuse.set_performative(ACLMessage.REFUSE) self.filter_Agree = Filter() self.filter_Agree.set_performative(ACLMessage.AGREE) self.filter_failure = Filter() self.filter_failure.set_performative(ACLMessage.FAILURE) self.filter_inform = Filter() self.filter_inform.set_performative(ACLMessage.INFORM)
def __init__(self, agent, message=None, is_initiator=True): """Inicializes the class that implements FipaRequestProtocol protocol :param agent: instance of the agent that will execute the protocol's established behaviours. :param message: message to be sent by the agent when is_initiator parameter is true. :param is_initiator: boolean type parameter that specifies if the protocol instance will act as Initiator or Participant. """ super(FipaRequestProtocol, self).__init__(agent, message, is_initiator) self.filter_protocol = Filter() self.filter_protocol.set_protocol(ACLMessage.FIPA_REQUEST_PROTOCOL) self.filter_Request = Filter() self.filter_Request.set_performative(ACLMessage.REQUEST) self.filter_refuse = Filter() self.filter_refuse.set_performative(ACLMessage.REFUSE) self.filter_Agree = Filter() self.filter_Agree.set_performative(ACLMessage.AGREE) self.filter_failure = Filter() self.filter_failure.set_performative(ACLMessage.FAILURE) self.filter_inform = Filter() self.filter_inform.set_performative(ACLMessage.INFORM)
def __init__(self, agent, message=None, is_initiator=True): """ Inicializa a classe que implementa o protocolo FipaContractNetProtocol Parâmetros ---------- agent : instância do agente que executara os comportamentos estebelecidos pelo protocolo message : mensagem a ser enviada pelo agente quando o parâmetro is_initiator for verdadeiro is_initiator : parâmetro do tipo booleano que indica se esta instancia do protocolo agirá como Initiator ou como participante """ super(FipaContractNetProtocol, self).__init__(agent) self.is_initiator = is_initiator self.received_qtd = 0 self.proposes = [] self.message = message self.filter_protocol = Filter() self.filter_protocol.set_protocol( ACLMessage.FIPA_CONTRACT_NET_PROTOCOL) self.filter_cfp = Filter() self.filter_cfp.set_performative(ACLMessage.CFP) self.filter_refuse = Filter() self.filter_refuse.set_performative(ACLMessage.REFUSE) self.filter_propose = Filter() self.filter_propose.set_performative(ACLMessage.PROPOSE) self.filter_accept_propose = Filter() self.filter_accept_propose.set_performative(ACLMessage.ACCEPT_PROPOSAL) self.filter_reject_propose = Filter() self.filter_reject_propose.set_performative(ACLMessage.REJECT_PROPOSAL) self.filter_failure = Filter() self.filter_failure.set_performative(ACLMessage.FAILURE) self.filter_inform = Filter() self.filter_inform.set_performative(ACLMessage.INFORM)
def action(self): f = Filter() f.set_performative(ACLMessage.REQUEST) f.set_ontology('office') message = self.read() if f.filter(message): reply = message.create_reply() reply.set_content('pen\nclips\nscissors\npaper\npencil') reply.set_performative(ACLMessage.INFORM) self.agent.send(reply) display( self.agent, 'Office material list sent to %s.' % message.sender.getLocalName())
def __init__(self, agent, message, is_initiator): """Inicializes the class that implements a FipaProtocol :param agent: instance of the agent that will execute the protocol's established behaviours. :param message: message to be sent by the agent when is_initiator parameter is true. :param is_initiator: boolean type parameter that specifies if the protocol instance will act as Initiator or Participant. """ super(FipaProtocol, self).__init__(agent) self.is_initiator = is_initiator self.message = message self.filter_not_undestood = Filter() self.filter_not_undestood.set_performative(ACLMessage.NOT_UNDERSTOOD) if self.message is not None: self.filter_conversation_id = Filter() self.filter_conversation_id.set_conversation_id( self.message.conversation_id)
def action(self): f = Filter() f.set_performative(ACLMessage.REQUEST) message = self.read() if f.filter(message): reply = message.create_reply() contact = self.search(message.content) if contact != None: # If the searched contact exists reply.set_performative(ACLMessage.INFORM) # Here we will serialize the object to send reply.set_content(pickle.dumps(contact)) else: # If the searched contact doesn't exist reply.set_performative(ACLMessage.FAILURE) reply.set_content(pickle.dumps({'error': 404, 'description': 'Contact not found.'})) self.agent.send(reply)
def action(self): # Setting a filter f = Filter() f.set_performative(ACLMessage.REQUEST) # Accept only REQUEST messages f.set_ontology('fruits') # Reveiving a message and filtering it message = self.read() if f.filter(message): # If the message satisfies the filter reply = message.create_reply() reply.set_content( 'apple\nbanana\ncocoa\ncoconuts\ngrape\norange\nstrawberry') reply.set_performative(ACLMessage.INFORM) self.agent.send(reply) display(self.agent, 'Fruit list sent to %s.' % message.sender.getLocalName())
def action(self): # Setting the filter f = Filter() f.set_performative(ACLMessage.REQUEST) f.set_ontology('foods') # Filtering the received message based on 'f' filter if self.agent.has_messages( ): # If the message satisfies the filter 'f' message = self.agent.read(f) if message != None: reply = message.create_reply() # Creates a reply to the sender reply = message.create_reply() reply.set_content('meat\nchicken\ncookies\nice cream\nbread') reply.set_performative(ACLMessage.INFORM) self.send(reply) display(self.agent, 'Food list sent to %s.' % message.sender.getName())
def action(self): # Setting the filter f = Filter() f.set_performative(ACLMessage.REQUEST) f.set_ontology('office') # Filtering the received message based on 'f' filter if self.agent.has_messages( ): # If the message satisfies the filter 'f' message = self.agent.read(f) if message != None: reply = message.create_reply() # Creates a reply to the sender reply = message.create_reply() reply.set_content('pen\nclips\nscissors\npaper\npencil') reply.set_performative(ACLMessage.INFORM) self.send(reply) display( self.agent, 'Office material list sent to %s.' % message.sender.getName())
def action(self): message = self.read() # Deserializing the received information contact = pickle.loads(message.content) # Filtering the message f = Filter() f.set_performative(ACLMessage.INFORM) if f.filter(message): template = 'Contact info: \nName: {name}\nPhone: {phone}\nE-mail: {email}' # Showing the deserialized data display( self.agent, template.format(name=contact['name'], phone=contact['phone'], email=contact['email'])) elif message.performative == ACLMessage.FAILURE: # Showing the failure information display( self.agent, '{id}: {desc}'.format(id=contact['error'], desc=contact['description']))
def action(self): # Setting the filter f = Filter() # The object 'f' is the 'model' of message you want to receive f.set_performative(ACLMessage.REQUEST) # Only accept REQUEST messages f.set_ontology( 'fruits') # Only accept messagens with 'fruits' in ontology field # Filtering the received message based on 'f' filter if self.agent.has_messages( ): # If the message satisfies the filter 'f' message = self.agent.read(f) if message != None: reply = message.create_reply() # Creates a reply to the sender reply.set_content( 'apple\nbanana\ncocoa\ncoconuts\ngrape\norange\nstrawberry' ) reply.set_performative(ACLMessage.INFORM) self.send(reply) # Sends the reply to the sender display(self.agent, 'Fruit list sent to %s.' % message.sender.getName())
def action(self): f = Filter() f.set_performative(ACLMessage.REQUEST) message = self.read() if f.filter(message): reply = message.create_reply() # Creates a reply to sender contact = self.search(message.content) if contact != None: # If the searched contact exists # Setting the performative of the reply to INFORM reply.set_performative(ACLMessage.INFORM) # Here we will serialize the object to send reply.set_content(pickle.dumps(contact)) else: # If the searched contact doesn't exist # Setting the performative of the reply to FAILURE reply.set_performative(ACLMessage.FAILURE) # Adding the reason of the FAILURE (using a dict) reply.set_content( pickle.dumps({ 'error': 404, 'description': 'Contact not found.' })) self.send(reply)