Ejemplo n.º 1
0
    def simulation_report(self):
        Logger.normal('Generate simulation report.')
        response = {'status': 0, 'message': ''}

        try:
            report = self.copycat.simulation_report()

            if isinstance(report, str):
                Logger.error('Error to generate simulation report.')
                response['message'] = 'Error to generate simulation report.'
            
            else:
                response['status'] = 1
                response['report'] = report

        except Exception as e:
            response['message'] = str(e)

        return response
Ejemplo n.º 2
0
    def save_logs(self):
        """Write all the saved logs to a file on the root of the project, the file will be inside a folder structure
        based on the date and time the simulation ran."""

        Logger.normal('Save the logs.')

        year, month, day, hour, minute, config_file, logs = self.copycat.get_logs()
        path = pathlib.Path(__file__).parents[3] / str(year) / str(month) / str(day) / str(config_file)

        os.makedirs(str(path.absolute()), exist_ok=True)

        hour = '{:0>2d}'.format(hour)
        minute = '{:0>2d}'.format(minute)

        for log in logs:
            delivered_items = []
            for item_log in logs[log]['environment']['delivered_items']:
                delivered_items.extend(item_log['items'])
            json_items = self.jsonify_delivered_items(delivered_items)

            json_agents = self.jsonify_agents(logs[log]['agents']['agents'])
            json_assets = self.jsonify_assets(logs[log]['assets']['assets'])
            json_active_agents = self.jsonify_agents(logs[log]['agents']['active_agents'])
            json_active_assets = self.jsonify_assets(logs[log]['assets']['active_assets'])
            json_action_token_by_step = self.jsonify_action_token_by_step(logs[log]['actions']['action_token_by_step'])
            json_acts_by_step = self.jsonify_amount_of_actions_by_step(logs[log]['actions']['amount_of_actions_by_step'])
            json_actions_by_step = self.jsonify_actions_by_step(logs[log]['actions']['actions_by_step'])

            logs[log]['environment']['delivered_items'] = json_items
            logs[log]['agents']['agents'] = json_agents
            logs[log]['assets']['assets'] = json_assets
            logs[log]['agents']['active_agents'] = json_active_agents
            logs[log]['assets']['active_assets'] = json_active_assets
            logs[log]['actions']['action_token_by_step'] = json_action_token_by_step
            logs[log]['actions']['amount_of_actions_by_step'] = json_acts_by_step
            logs[log]['actions']['actions_by_step'] = json_actions_by_step

            map_log = re.sub('([\w\s\d]+?\\\\)|([\w\s\d]+?/)|(\.\w+)', '', log)

            with open(str((path / f'LOG FILE {map_log} at {hour}h {minute}min.txt').absolute()), 'w') as file:
                file.write(json.dumps(logs[log], sort_keys=False, indent=4))
                file.write('\n\n' + '=' * 120 + '\n\n')
Ejemplo n.º 3
0
    def match_report(self):
        Logger.normal('Generate match report.')
        response = {'status': 0, 'message': ''}
        report = {'status': 0, 'message': ''}

        try:
            match_report = self.copycat.match_report()

            report['status'] = 1
            report['report'] = match_report

            response['report'] = report

        except Exception as e:
            response['message'] = str(e)
            report['message'] = str(e)

            response['report'] = report

        return response
Ejemplo n.º 4
0
    def restart(self):
        """Restart the simulation and returns a JSON response.

        All the agents, social assets and events are converted, the copycat class prevents from the formatter changing
        the objects inside the engine.

        :return dict: Dictionary with status representing if any errors were found, the list of agents and social assets,
        the event with the flood, victims, photos and water samples and a general message."""

        Logger.normal('Try to restart the simulation.')

        try:
            agents, step, current_step, new_map_percepts, report, assets_tokens = self.copycat.restart()
            message = 'Simulation restarted.'

            json_agents_init = [{'agent': self.jsonify_agent(agent)} for agent in agents]

            json_agents = self.jsonify_agents(agents)
            json_actors = [{'agent': agent, 'message': message} for agent in json_agents]
            environment = {'events': self.jsonify_events(step), 'step': current_step}

            percepts = {'status': 1, 'actors': json_actors, 'environment': environment, 'message': 'Simulation restarted.'}
            initial_percepts = {'status': 1, 'agents': json_agents_init, 'map_percepts': new_map_percepts, 'message': ''}
            report_response = {'status': 1, 'report': report, 'message': ''}

            Logger.normal('Simulation restarted.')

            return {'status': 1, 'initial_percepts': initial_percepts, 'assets_tokens': assets_tokens,
                    'report': report_response, 'percepts': percepts, 'message': message}

        except Exception as e:
            Logger.critical(f'Error to restart the simulation, Error: {str(e)}.')

            return {'status': 0, 'message': f'An error occurred during restart: "{str(e)}"'}
Ejemplo n.º 5
0
    def start(self):
        """Start the simulation and returns a JSON response.

        All the agents, social assets and events are converted, the copycat class prevents from the formatter changing
        the objects inside the engine.

        :return dict: Dictionary with status representing if any errors were found, the list of agents and social assets,
        the event with the flood, victims, photos and water samples and a general message."""

        Logger.normal('Try to start the simulation.')

        try:
            response = self.copycat.start()
            message = 'Simulation started.'

            json_agents = self.jsonify_agents(response[0])
            json_actors = [{'agent': agent, 'message': message} for agent in [*json_agents]]
            environment = {'events': self.jsonify_events(response[1]), 'step': response[2]}
            map_percepts = response[3]

            Logger.normal(message)

            return {'status': 1, 'actors': json_actors, 'environment': environment,
                    'map_percepts': map_percepts, 'message': message}

        except Exception as e:
            Logger.error(f'Unknown error: {str(e)}.')

            return {'status': 0, 'message': f'An error occurred during restart: "{str(e)}"'}
Ejemplo n.º 6
0
    def finish_social_asset_connections(self, tokens):
        Logger.normal('Finishing social assets connections.')

        try:
            response = self.copycat.finish_social_asset_connections(tokens)
            json_actors = []
            if response is not None:
                for agent in response:
                    json_actors.append({'asset': self.jsonify_asset(agent), 'message': 'Social asset connected'})

            Logger.normal('Social assets connection finished.')

            return {'status': 1, 'actors': json_actors, 'message': 'Finish social asset connections'}

        except Exception as e:
            Logger.error(f'Unknown error {str(e)}.')

            return {'status': 0, 'message': f'An error occurred during connection: {str(e)}.'}