<commit_before>/*
This file is part of Groho, a simulator for inter-planetary travel and warfare.
Copyright (c) 2017, 2018 by <NAME>. Some rights reserved, see LICENSE

Entry point function for command line program
*/

#include <functional>
#include <iostream>
#include <signal.h>
#include <stdlib.h>
#include <thread>

#include "CLI11.hpp"

#ifndef NOGUI
#include "app.hpp"
#endif

#include "configuration.hpp"
#include "simulator.hpp"

#define LOGURU_IMPLEMENTATION 1
#define LOGURU_WITH_STREAMS 1
#include "loguru.hpp"

volatile sig_atomic_t keep_running = true;

void ctrl_c_pressed(int) { keep_running = false; }

void print_license()
{
    std::cout << R"(  
    Groho 18.11: A simulator for inter-planetary travel and warfare
    Copyright (c) 2017, 2018 by <NAME>. 
    Released under the MIT License. Some rights reserved
    
)";
}

struct Options {
    std::string scenario_file;
    std::string annotation_file;
    bool        non_interactive = false;
};

Options parse_arguments(int argc, char* argv[])
{
    Options opt;
    bool    show_actions_and_exit;

    CLI::App cli_options{
        "Groho: A simulator for inter-planetary travel and warfare"
    };
    cli_options.add_flag(
        "--no-gui", opt.non_interactive, "Run in non-interactive mode");
    cli_options.add_flag(
        "--actions", show_actions_and_exit, "Show action list");
    cli_options.add_option("scenariofile", opt.scenario_file, "Scenario file")
        ->check(CLI::ExistingFile);
    cli_options
        .add_option("annotationfile", opt.annotation_file, "Annotation file")
        ->check(CLI::ExistingFile);

    try {
        cli_options.parse(argc, argv);
    } catch (const CLI::ParseError& e) {
        exit(cli_options.exit(e));
    }

    if (show_actions_and_exit) {
        std::cout << sim::list_available_actions();
        exit(0);
    }

    if (opt.scenario_file == "") {
        std::cout << cli_options.help("", CLI::AppFormatMode::All);
        exit(0);
    }

    return opt;
}

// Periodically monitor scenario file and restart simulator as needed
void simulator_loop(
    sim::Simulator& simulator, Options options, unsigned int interval_ms)
{
    sim::Configuration config = sim::parse_configuration(options.scenario_file);
    simulator.restart_with(config);

    time_t last_mod_time = config.latest_modification();
    while (keep_running) {
        if (last_mod_time < config.latest_modification()) {

            LOG_S(INFO) << "Reloading scenario files";

            config = sim::parse_configuration(options.scenario_file);

            // if (new_scenario != scenario) {
            if (true) {
                LOG_S(INFO) << "Scenario file changed";
                simulator.restart_with(config);
                last_mod_time = config.latest_modification();
            }
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms));
    }
    simulator.stop();
}

int main(int argc, char* argv[])
{
    print_license();

    Options opt = parse_arguments(argc, argv);

    loguru::init(argc, argv);

    // This has to come after loguru::init
    signal(SIGINT, ctrl_c_pressed);

    sim::Simulator simulator;

    unsigned int interval_ms = 500;

    auto simulator_thread
        = std::thread(simulator_loop, std::ref(simulator), opt, interval_ms);

    int ret_val = 0;

#ifndef NOGUI
    if (!opt.non_interactive) {
        sim::GrohoApp app({ argc, argv }, simulator);
        ret_val      = app.exec();
        keep_running = false;
    }
#endif

    if (simulator_thread.joinable()) {
        simulator_thread.join();
    }

    return ret_val;
}<commit_msg>Main reconstituted<commit_after>/*
This file is part of Groho, a simulator for inter-planetary travel and warfare.
Copyright (c) 2017-2020 by <NAME>. Some rights reserved, see
LICENSE

Entry point function for command line program
*/

#include <chrono>
#include <filesystem>
#include <iostream>
#include <thread>

#include <signal.h>
#include <stdlib.h>

#include "CLI11.hpp"

#include "simulator.hpp"

#define LOGURU_IMPLEMENTATION 1
#define LOGURU_WITH_STREAMS 1
#include "loguru.hpp"

void print_license()
{
    std::cout << R"(  
    Groho 20.02: A simulator for inter-planetary travel and warfare
    Copyright (c) 2017 - 2020 by <NAME>. 
    Released under the MIT License. Some rights reserved.
    
)";
}

struct Options {
    std::string scenario_file;
    std::string output_folder;
};

Options parse_arguments(int argc, char* argv[])
{
    bool        show_actions_and_exit;
    std::string scenario_file, output_folder;

    CLI::App cli_options{ "Groho: A simulator for inter-planetary travel" };
    cli_options.add_flag(
        "--actions", show_actions_and_exit, "Show action list");
    cli_options.add_option("scenariofile", scenario_file, "Scenario file");
    cli_options.add_option("outputfolder", output_folder, "Output folder");

    try {
        cli_options.parse(argc, argv);
    } catch (const CLI::ParseError& e) {
        exit(cli_options.exit(e));
    }

    if (show_actions_and_exit) {
        std::cout << groho::Simulator::list_available_actions();
        exit(0);
    }

    if (scenario_file == "" | output_folder == "") {
        std::cout << cli_options.help("", CLI::AppFormatMode::All);
        exit(0);
    }

    return Options{ scenario_file, output_folder };
}

volatile sig_atomic_t keep_running = true;

void ctrl_c_pressed(int) { keep_running = false; }

int main(int argc, char* argv[])
{
    print_license();

    Options opt = parse_arguments(argc, argv);

    loguru::init(argc, argv);

    // This has to come after loguru::init
    signal(SIGINT, ctrl_c_pressed);

    using namespace std::chrono_literals;
    auto poll_interval_ms = 500ms;

    groho::Simulator simulator(opt.scenario_file, opt.output_folder);

    while (keep_running && simulator.ok()) {
        if (simulator.scenario_changed()) {
            simulator.restart();
        }
        std::this_thread::sleep_for(poll_interval_ms);
    }

    simulator.exit();

    return 0;
}
{'fraction_non_alphanumeric': 0.11998008629273149, 'fraction_numerical': 0.009956853634251576, 'mean_word_length': 3.2653927813163484, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1489874', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1883, 'n_words': 465}