コード例 #1
0
ファイル: handlers.py プロジェクト: ejfitzgerald/agents-aea
    def _update_ownership_and_preferences(
        self, tac_msg: TacMessage, tac_dialogue: TacDialogue
    ) -> None:
        """
        Update ownership and preferences.

        :param tac_msg: the game data
        :param tac_dialogue: the tac dialogue
        :return: None
        """
        state_update_dialogues = cast(
            StateUpdateDialogues, self.context.state_update_dialogues
        )
        state_update_msg = StateUpdateMessage(
            performative=StateUpdateMessage.Performative.INITIALIZE,
            dialogue_reference=state_update_dialogues.new_self_initiated_dialogue_reference(),
            amount_by_currency_id=tac_msg.amount_by_currency_id,
            quantities_by_good_id=tac_msg.quantities_by_good_id,
            exchange_params_by_currency_id=tac_msg.exchange_params_by_currency_id,
            utility_params_by_good_id=tac_msg.utility_params_by_good_id,
        )
        self.context.shared_state["fee_by_currency_id"] = tac_msg.fee_by_currency_id
        state_update_msg.counterparty = "decision_maker"
        state_update_dialogue = cast(
            Optional[StateUpdateDialogue],
            state_update_dialogues.update(state_update_msg),
        )
        assert state_update_dialogue is not None, "StateUpdateDialogue not created."
        game = cast(Game, self.context.game)
        game.state_update_dialogue = state_update_dialogue
        self.context.decision_maker_message_queue.put_nowait(state_update_msg)
コード例 #2
0
ファイル: handlers.py プロジェクト: ejfitzgerald/agents-aea
    def _on_transaction_confirmed(
        self, tac_msg: TacMessage, tac_dialogue: TacDialogue
    ) -> None:
        """
        Handle 'on transaction confirmed' event emitted by the controller.

        :param tac_msg: the TacMessage.
        :param tac_dialogue: the tac dialogue
        :return: None
        """
        game = cast(Game, self.context.game)
        if game.phase.value != Phase.GAME.value:
            self.context.logger.warning(
                "we do not expect a tranasaction in game phase={}".format(
                    game.phase.value
                )
            )
            return

        self.context.logger.info(
            "received transaction confirmation from the controller: transaction_id={}".format(
                tac_msg.transaction_id
            )
        )
        state_update_dialogue = game.state_update_dialogue
        last_msg = state_update_dialogue.last_message
        assert last_msg is not None, "Could not retrieve last message."
        state_update_msg = StateUpdateMessage(
            performative=StateUpdateMessage.Performative.APPLY,
            dialogue_reference=state_update_dialogue.dialogue_label.dialogue_reference,
            message_id=last_msg.message_id + 1,
            target=last_msg.message_id,
            amount_by_currency_id=tac_msg.amount_by_currency_id,
            quantities_by_good_id=tac_msg.quantities_by_good_id,
        )
        state_update_msg.counterparty = "decision_maker"
        state_update_dialogue.update(state_update_msg)
        self.context.decision_maker_message_queue.put_nowait(state_update_msg)
        if "confirmed_tx_ids" not in self.context.shared_state.keys():
            self.context.shared_state["confirmed_tx_ids"] = []
        self.context.shared_state["confirmed_tx_ids"].append(tac_msg.transaction_id)
コード例 #3
0
    def test_decision_maker_handle_state_update_initialize_and_apply(self):
        """Test the handle method for a stateUpdate message with Initialize and Apply performative."""
        good_holdings = {"good_id": 2}
        currency_holdings = {"FET": 100}
        utility_params = {"good_id": 20.0}
        exchange_params = {"FET": 10.0}
        currency_deltas = {"FET": -10}
        good_deltas = {"good_id": 1}

        state_update_dialogues = StateUpdateDialogues("agent")
        state_update_message_1 = StateUpdateMessage(
            performative=StateUpdateMessage.Performative.INITIALIZE,
            dialogue_reference=state_update_dialogues.new_self_initiated_dialogue_reference(),
            amount_by_currency_id=currency_holdings,
            quantities_by_good_id=good_holdings,
            exchange_params_by_currency_id=exchange_params,
            utility_params_by_good_id=utility_params,
        )
        state_update_message_1.counterparty = "decision_maker"
        state_update_dialogue = cast(
            Optional[StateUpdateDialogue],
            state_update_dialogues.update(state_update_message_1),
        )
        assert state_update_dialogue is not None, "StateUpdateDialogue not created"
        self.decision_maker.handle(state_update_message_1)
        assert (
            self.decision_maker_handler.context.ownership_state.amount_by_currency_id
            is not None
        )
        assert (
            self.decision_maker_handler.context.ownership_state.quantities_by_good_id
            is not None
        )
        assert (
            self.decision_maker_handler.context.preferences.exchange_params_by_currency_id
            is not None
        )
        assert (
            self.decision_maker_handler.context.preferences.utility_params_by_good_id
            is not None
        )

        state_update_message_2 = StateUpdateMessage(
            performative=StateUpdateMessage.Performative.APPLY,
            dialogue_reference=state_update_dialogue.dialogue_label.dialogue_reference,
            message_id=state_update_message_1.message_id + 1,
            target=state_update_message_1.message_id,
            amount_by_currency_id=currency_deltas,
            quantities_by_good_id=good_deltas,
        )
        state_update_message_2.counterparty = "decision_maker"
        assert state_update_dialogue.update(state_update_message_2)
        self.decision_maker.handle(state_update_message_2)
        expected_amount_by_currency_id = {
            key: currency_holdings.get(key, 0) + currency_deltas.get(key, 0)
            for key in set(currency_holdings) | set(currency_deltas)
        }
        expected_quantities_by_good_id = {
            key: good_holdings.get(key, 0) + good_deltas.get(key, 0)
            for key in set(good_holdings) | set(good_deltas)
        }
        assert (
            self.decision_maker_handler.context.ownership_state.amount_by_currency_id
            == expected_amount_by_currency_id
        ), "The amount_by_currency_id must be equal with the expected amount."
        assert (
            self.decision_maker_handler.context.ownership_state.quantities_by_good_id
            == expected_quantities_by_good_id
        )