def _on_propose(self, propose: FipaMessage, dialogue: Dialogue) -> None: """ Handle a Propose. :param propose: the message containing the Propose :param dialogue: the dialogue :return: None """ new_msg_id = propose.message_id + 1 strategy = cast(Strategy, self.context.strategy) proposal_description = propose.proposal self.context.logger.debug("[{}]: on Propose as {}.".format( self.context.agent_name, dialogue.role)) transactions = cast(Transactions, self.context.transactions) transaction_msg = transactions.generate_transaction_message( TransactionMessage.Performative.PROPOSE_FOR_SIGNING, proposal_description, dialogue.dialogue_label, dialogue.is_seller, self.context.agent_address, ) if strategy.is_profitable_transaction(transaction_msg, is_seller=dialogue.is_seller): self.context.logger.info("[{}]: Accepting propose (as {}).".format( self.context.agent_name, dialogue.role)) transactions.add_locked_tx(transaction_msg, as_seller=dialogue.is_seller) transactions.add_pending_initial_acceptance( dialogue.dialogue_label, new_msg_id, transaction_msg) fipa_msg = FipaMessage( performative=FipaMessage.Performative.ACCEPT, message_id=new_msg_id, dialogue_reference=dialogue.dialogue_label.dialogue_reference, target=propose.message_id, ) else: self.context.logger.info("[{}]: Declining propose (as {})".format( self.context.agent_name, dialogue.role)) fipa_msg = FipaMessage( performative=FipaMessage.Performative.DECLINE, message_id=new_msg_id, dialogue_reference=dialogue.dialogue_label.dialogue_reference, target=propose.message_id, ) dialogues = cast(Dialogues, self.context.dialogues) dialogues.dialogue_stats.add_dialogue_endstate( Dialogue.EndState.DECLINED_PROPOSE, dialogue.is_self_initiated) dialogue.outgoing_extend(fipa_msg) self.context.outbox.put_message( to=dialogue.dialogue_label.dialogue_opponent_addr, sender=self.context.agent_address, protocol_id=FipaMessage.protocol_id, message=FipaSerializer().encode(fipa_msg), )
def _on_accept(self, accept: FipaMessage, dialogue: Dialogue) -> None: """ Handle an Accept. :param accept: the Accept message :param dialogue: the dialogue :return: None """ self.context.logger.debug( "[{}]: on_accept: msg_id={}, dialogue_reference={}, origin={}, target={}" .format( self.context.agent_name, accept.message_id, accept.dialogue_reference, dialogue.dialogue_label.dialogue_opponent_addr, accept.target, )) new_msg_id = accept.message_id + 1 transactions = cast(Transactions, self.context.transactions) transaction_msg = transactions.pop_pending_proposal( dialogue.dialogue_label, accept.target) strategy = cast(Strategy, self.context.strategy) if strategy.is_profitable_transaction(transaction_msg, is_seller=dialogue.is_seller): self.context.logger.info( "[{}]: locking the current state (as {}).".format( self.context.agent_name, dialogue.role)) transactions.add_locked_tx(transaction_msg, as_seller=dialogue.is_seller) self.context.decision_maker_message_queue.put(transaction_msg) else: self.context.logger.debug( "[{}]: decline the Accept (as {}).".format( self.context.agent_name, dialogue.role)) fipa_msg = FipaMessage( performative=FipaMessage.Performative.DECLINE, message_id=new_msg_id, dialogue_reference=dialogue.dialogue_label.dialogue_reference, target=accept.message_id, ) dialogue.outgoing_extend(fipa_msg) dialogues = cast(Dialogues, self.context.dialogues) dialogues.dialogue_stats.add_dialogue_endstate( Dialogue.EndState.DECLINED_ACCEPT, dialogue.is_self_initiated) self.context.outbox.put_message( to=dialogue.dialogue_label.dialogue_opponent_addr, sender=self.context.agent_address, protocol_id=FipaMessage.protocol_id, message=FipaSerializer().encode(fipa_msg), )
def _on_cfp(self, cfp: FipaMessage, dialogue: Dialogue) -> None: """ Handle a CFP. :param cfp: the fipa message containing the CFP :param dialogue: the dialogue :return: None """ new_msg_id = cfp.message_id + 1 query = cast(Query, cfp.query) strategy = cast(Strategy, self.context.strategy) proposal_description = strategy.get_proposal_for_query( query, dialogue.is_seller) if proposal_description is None: self.context.logger.debug("[{}]: sending to {} a Decline{}".format( self.context.agent_name, dialogue.dialogue_label.dialogue_opponent_addr[-5:], pprint.pformat({ "msg_id": new_msg_id, "dialogue_reference": cfp.dialogue_reference, "origin": dialogue.dialogue_label.dialogue_opponent_addr[-5:], "target": cfp.target, }), )) fipa_msg = FipaMessage( performative=FipaMessage.Performative.DECLINE, message_id=new_msg_id, dialogue_reference=dialogue.dialogue_label.dialogue_reference, target=cfp.message_id, ) dialogues = cast(Dialogues, self.context.dialogues) dialogues.dialogue_stats.add_dialogue_endstate( Dialogue.EndState.DECLINED_CFP, dialogue.is_self_initiated) else: transactions = cast(Transactions, self.context.transactions) transaction_msg = transactions.generate_transaction_message( TransactionMessage.Performative.PROPOSE_FOR_SIGNING, proposal_description, dialogue.dialogue_label, dialogue.is_seller, self.context.agent_address, ) transactions.add_pending_proposal(dialogue.dialogue_label, new_msg_id, transaction_msg) self.context.logger.info("[{}]: sending to {} a Propose{}".format( self.context.agent_name, dialogue.dialogue_label.dialogue_opponent_addr[-5:], pprint.pformat({ "msg_id": new_msg_id, "dialogue_reference": cfp.dialogue_reference, "origin": dialogue.dialogue_label.dialogue_opponent_addr[-5:], "target": cfp.message_id, "propose": proposal_description.values, }), )) fipa_msg = FipaMessage( performative=FipaMessage.Performative.PROPOSE, message_id=new_msg_id, dialogue_reference=dialogue.dialogue_label.dialogue_reference, target=cfp.message_id, proposal=[proposal_description], ) dialogue.outgoing_extend(fipa_msg) self.context.outbox.put_message( to=dialogue.dialogue_label.dialogue_opponent_addr, sender=self.context.agent_address, protocol_id=FipaMessage.protocol_id, message=FipaSerializer().encode(fipa_msg), )