Fast map matching  0.1.0
ubodt_gen_app_config.cpp
1 #include "mm/fmm/ubodt_gen_app_config.hpp"
2 #include "util/util.hpp"
3 #include "util/debug.hpp"
4 
5 using namespace FMM;
6 using namespace FMM::CORE;
7 using namespace FMM::NETWORK;
8 using namespace FMM::MM;
9 using namespace FMM::CONFIG;
10 UBODTGenAppConfig::UBODTGenAppConfig(int argc, char **argv) {
11  spdlog::set_pattern("[%^%l%$][%s:%-3#] %v");
12  if (argc==2) {
13  std::string configfile(argv[1]);
14  if (UTIL::check_file_extension(configfile,"xml,XML"))
15  load_xml(configfile);
16  else {
17  load_arg(argc,argv);
18  }
19  } else {
20  load_arg(argc,argv);
21  }
22  spdlog::set_level((spdlog::level::level_enum) log_level);
23  if (!help_specified)
24  print();
25 }
26 
27 void UBODTGenAppConfig::load_xml(const std::string &file) {
28  SPDLOG_INFO("Read configuration from xml file: {}",file);
29  // Create empty property tree object
30  boost::property_tree::ptree tree;
31  boost::property_tree::read_xml(file, tree);
32  network_config = NetworkConfig::load_from_xml(tree);
33  delta = tree.get("config.parameters.delta", 3000.0);
34  result_file = tree.get<std::string>("config.output.file");
35  // 0-trace,1-debug,2-info,3-warn,4-err,5-critical,6-off
36  log_level = tree.get("config.other.log_level", 2);
37  use_omp = !(!tree.get_child_optional("config.other.use_omp"));
38  SPDLOG_INFO("Read configuration from xml file done");
39 }
40 
41 void UBODTGenAppConfig::load_arg(int argc, char **argv) {
42  SPDLOG_INFO("Start reading ubodt configuration from arguments");
43  cxxopts::Options options("config",
44  "Configuration parser of ubodt_gen");
45  options.add_options()
46  ("network", "Network file name",
47  cxxopts::value<std::string>()->default_value(""))
48  ("network_id", "Network id name",
49  cxxopts::value<std::string>()->default_value("id"))
50  ("source", "Network source name",
51  cxxopts::value<std::string>()->default_value("source"))
52  ("target", "Network target name",
53  cxxopts::value<std::string>()->default_value("target"))
54  ("delta", "Upperbound distance",
55  cxxopts::value<double>()->default_value("3000.0"))
56  ("o,output", "Output file name",
57  cxxopts::value<std::string>()->default_value(""))
58  ("l,log_level", "Log level", cxxopts::value<int>()->default_value("2"))
59  ("h,help", "Help information")
60  ("use_omp","Use parallel computing if specified")
61  ("projected","Data is projected or not");
62  if (argc==1) {
63  help_specified = true;
64  return;
65  }
66  auto result = options.parse(argc, argv);
67  // Output
68  result_file = result["output"].as<std::string>();
69  network_config = NetworkConfig::load_from_arg(result);
70  log_level = result["log_level"].as<int>();
71  delta = result["delta"].as<double>();
72  use_omp = result.count("use_omp")>0;
73  if (result.count("help")>0) {
74  help_specified = true;
75  }
76  SPDLOG_INFO("Finish with reading ubodt arg configuration");
77 }
78 
79 void UBODTGenAppConfig::print() const {
80  SPDLOG_INFO("---- Print configuration ----");
81  network_config.print();
82  SPDLOG_INFO("Delta {}",delta);
83  SPDLOG_INFO("Output file {}",result_file);
84  SPDLOG_INFO("Log level {}",UTIL::LOG_LEVESLS[log_level]);
85  SPDLOG_INFO("Use omp {}",(use_omp ? "true" : "false"));
86  SPDLOG_INFO("---- Print configuration done ----");
87 }
88 
89 void UBODTGenAppConfig::print_help() {
90  std::cout << "ubodt_gen argument lists:\n";
91  std::cout << "--network (required) <string>: Network file name\n";
92  std::cout << "--output (required) <string>: Output file name\n";
93  std::cout << "--id (optional) <string>: Network id name (id)\n";
94  std::cout << "--source (optional) <string>: Network source name (source)\n";
95  std::cout << "--target (optional) <string>: Network target name (target)\n";
96  std::cout << "--delta (optional) <double>: upperbound (3000.0)\n";
97  std::cout << "--log_level (optional) <int>: log level (2)\n";
98  std::cout << "--use_omp: use OpenMP or not\n";
99  std::cout << "-h/--help: help information\n";
100  std::cout << "For xml configuration, check example folder\n";
101 }
102 
103 bool UBODTGenAppConfig::validate() const {
104  SPDLOG_INFO("Validating configuration for UBODT construction");
105  if (!network_config.validate()) {
106  return false;
107  }
108  if (UTIL::file_exists(result_file)) {
109  SPDLOG_WARN("Overwrite result file {}", result_file);
110  }
111  std::string output_folder = UTIL::get_file_directory(result_file);
112  if (!UTIL::folder_exist(output_folder)) {
113  SPDLOG_CRITICAL("Output folder {} not exists", output_folder);
114  return false;
115  }
116  if (log_level < 0 || log_level > UTIL::LOG_LEVESLS.size()) {
117  SPDLOG_CRITICAL("Invalid log_level {}, which should be 0 - 6", log_level);
118  SPDLOG_INFO("0-trace,1-debug,2-info,3-warn,4-err,5-critical,6-off");
119  return false;
120  }
121  if (delta <= 0) {
122  SPDLOG_CRITICAL("Delta {} should be positive");
123  return false;
124  }
125  SPDLOG_INFO("Validating done.");
126  return true;
127 }
128 
129 bool UBODTGenAppConfig::is_binary_output() const {
130  if (UTIL::check_file_extension(result_file,"bin")) {
131  return true;
132  }
133  return false;
134 }
FMM::UTIL::LOG_LEVESLS
static const std::vector< std::string > LOG_LEVESLS
Log level strings for printing the log level information.
Definition: debug.hpp:28
FMM::UTIL::check_file_extension
bool check_file_extension(const std::string &filename, const std::string &extension_list_str)
Check if the filename has an extension in the list.
Definition: util.cpp:105
FMM::CONFIG
Classes related with configuration.
Definition: gps_config.hpp:22
FMM
Fast map matching.
Definition: geom_algorithm.hpp:17
FMM::UTIL::folder_exist
bool folder_exist(const std::string &folder_name)
Check if folder exists or not.
Definition: util.cpp:120
FMM::UTIL::get_file_directory
std::string get_file_directory(const std::string &fn)
Get folder path from file path.
Definition: util.cpp:129
FMM::CORE
Core data types.
Definition: geometry.hpp:22
FMM::MM
Class related with map matching.
Definition: composite_graph.hpp:18
FMM::NETWORK
Classes related with network and graph.
Definition: graph.hpp:21
FMM::UTIL::file_exists
bool file_exists(const char *filename)
Check if file exist or not.
Definition: util.cpp:61