Beispiel #1
0
    def agent__get_account_num_transactions(self, args):
        import os
        from sample_agent import Agent
        from sample_config import Config  # needed for the bankDirectory

        text = "This test checks agent.get_account_num_transactions \n"
        text = text + "  The purpose of this method is to count the numbers of transaction for   \n"
        text = text + "  accounts firms hold. Our standard frm has 3 transactions by default. \n"
        text = text + "  But we double count as we have only one agent, so you should see 6. \n"
        self.print_info(text)
        #
        # INITIALIZATION
        #
        environment_directory = str(args[0])
        identifier = str(args[1])
        log_directory = str(args[2])

        # Configure logging parameters so we get output while the program runs
        logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S',
                            filename=log_directory + identifier + ".log", level=logging.INFO)
        logging.info('START logging for test agent__get_account_num_transactions in run: %s',
                     environment_directory + identifier + ".xml")

        # Construct firm filename
        config = Config()
        list_env = os.listdir(environment_directory)
        config.read_xml_config_file(environment_directory + list_env[0])
        # get the firm_directory from the environment
        firm_directory = config.firm_directory
        # and loop over all firms in the directory
        listing = os.listdir(firm_directory)
        firmFilename = firm_directory + listing[0]

        # generate an agent
        agent = Agent("baseagent id", {"test": "parameters"}, {"test": "variables"})
        agent.identifier = "test_agent"
        config.agents = []
        config.agents.append(agent)
        agent.get_parameters_from_file(firmFilename, config)

        from sample_transaction import Transaction
        amount = 150.0
        transaction = Transaction()
        transaction.this_transaction("loans", "", agent.identifier, agent.identifier,
                                     amount,  0.0,  0, -1)
        # environment.firms[0] is only for testing purposes DO NOT USE IN PRODUCTION
        transaction.add_transaction(config)
        amount = 150.0
        transaction = Transaction()
        transaction.this_transaction("cash", "", agent.identifier, agent.identifier,
                                     amount,  0.0,  0, -1)
        # environment.firms[0] is only for testing purposes DO NOT USE IN PRODUCTION
        transaction.add_transaction(config)
        amount = 200.0
        transaction = Transaction()
        transaction.this_transaction("goods", "", agent.identifier, agent.identifier,
                                     amount,  0.0,  0, -1)
        # environment.firms[0] is only for testing purposes DO NOT USE IN PRODUCTION
        transaction.add_transaction(config)
        #
        # TESTING
        #

        num_transactions = 0.0          # counting all types in account together
        print(agent)
        # and checking if the number of transaction
        # is increasing by one
        for type in ["loans",  "cash",  "goods"]:
                        if type == "loans":
                                num_transactions = num_transactions + agent.get_account_num_transactions(type)
                                print("L = " + str(num_transactions))
                        if type == "cash":
                                num_transactions = num_transactions + agent.get_account_num_transactions(type)
                                print("L+M = " + str(num_transactions))
                        if type == "goods":
                                num_transactions = num_transactions + agent.get_account_num_transactions(type)
                                print("L+M+G = " + str(num_transactions))