You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 lines
14 KiB

  1. #ifndef STORM_SETTINGS_SETTINGSMANAGER_H_
  2. #define STORM_SETTINGS_SETTINGSMANAGER_H_
  3. #include <iostream>
  4. #include <sstream>
  5. #include <list>
  6. #include <set>
  7. #include <utility>
  8. #include <functional>
  9. #include <unordered_map>
  10. #include <map>
  11. #include <vector>
  12. #include <memory>
  13. #include "src/settings/Option.h"
  14. #include "src/settings/OptionBuilder.h"
  15. #include "src/settings/ArgumentBase.h"
  16. #include "src/settings/Argument.h"
  17. #include "src/settings/ArgumentBuilder.h"
  18. #include "src/settings/ArgumentType.h"
  19. #include "src/settings/modules/ModuleSettings.h"
  20. #include "src/settings/modules/GeneralSettings.h"
  21. #include "src/settings/modules/DebugSettings.h"
  22. #include "src/settings/modules/CounterexampleGeneratorSettings.h"
  23. #include "src/settings/modules/CuddSettings.h"
  24. #include "src/settings/modules/GmmxxEquationSolverSettings.h"
  25. #include "src/settings/modules/NativeEquationSolverSettings.h"
  26. #include "src/settings/modules/BisimulationSettings.h"
  27. #include "src/settings/modules/GlpkSettings.h"
  28. #include "src/settings/modules/GurobiSettings.h"
  29. #include "src/settings/modules/ParametricSettings.h"
  30. #include "src/utility/macros.h"
  31. #include "src/exceptions/OptionParserException.h"
  32. namespace storm {
  33. namespace settings {
  34. /*!
  35. * Provides the central API for the registration of command line options and parsing the options from the
  36. * command line. Since this class is a singleton, the only instance is accessible via a call to the manager()
  37. * function.
  38. */
  39. class SettingsManager {
  40. public:
  41. /*!
  42. * This function parses the given command line arguments and sets all registered options accordingly. If the
  43. * command line cannot be matched using the known options, an exception is thrown.
  44. *
  45. * @param argc The number of command line arguments.
  46. * @param argv The command line arguments.
  47. */
  48. void setFromCommandLine(int const argc, char const * const argv[]);
  49. /*!
  50. * This function parses the given command line arguments (represented by one big string) and sets all
  51. * registered options accordingly. If the command line cannot be matched using the known options, an
  52. * exception is thrown.
  53. *
  54. * @param commandLineString The command line arguments as one string.
  55. */
  56. void setFromString(std::string const& commandLineString);
  57. /*!
  58. * This function parses the given command line arguments (represented by several strings) and sets all
  59. * registered options accordingly. If the command line cannot be matched using the known options, an
  60. * exception is thrown.
  61. *
  62. * @param commandLineArguments The command line arguments.
  63. */
  64. void setFromExplodedString(std::vector<std::string> const& commandLineArguments);
  65. /*!
  66. * This function parses the given file and sets all registered options accordingly. If the settings cannot
  67. * be matched using the known options, an exception is thrown.
  68. */
  69. void setFromConfigurationFile(std::string const& configFilename);
  70. /*!
  71. * This function prints a help message to the standard output. Optionally, a string can be given as a hint.
  72. * If it is 'all', the complete help is shown. Otherwise, the string is interpreted as a regular expression
  73. * and matched against the known modules and options to restrict the help output.
  74. *
  75. * @param hint A regular expression to restrict the help output or "all" for the full help text.
  76. */
  77. void printHelp(std::string const& hint = "all") const;
  78. /*!
  79. * This function prints a help message for the specified module to the standard output.
  80. *
  81. * @param moduleName The name of the module for which to show the help.
  82. * @param maxLength The maximal length of an option name (necessary for proper alignment).
  83. */
  84. void printHelpForModule(std::string const& moduleName, uint_fast64_t maxLength = 30) const;
  85. /*!
  86. * This function prints the version string to the command line.
  87. */
  88. void printVersion() const;
  89. /*!
  90. * Retrieves the only existing instance of a settings manager.
  91. *
  92. * @return The only existing instance of a settings manager
  93. */
  94. static SettingsManager& manager();
  95. /*!
  96. * Adds a new module with the given name. If the module could not be successfully added, an exception is
  97. * thrown.
  98. *
  99. * @param moduleSettings The settings of the module to add.
  100. */
  101. void addModule(std::unique_ptr<modules::ModuleSettings>&& moduleSettings);
  102. /*!
  103. * Retrieves the settings of the module with the given name.
  104. *
  105. * @param moduleName The name of the module for which to retrieve the settings.
  106. * @return An object that provides access to the settings of the module.
  107. */
  108. modules::ModuleSettings const& getModule(std::string const& moduleName) const;
  109. /*!
  110. * Retrieves the settings of the module with the given name.
  111. *
  112. * @param moduleName The name of the module for which to retrieve the settings.
  113. * @return An object that provides access to the settings of the module.
  114. */
  115. modules::ModuleSettings& getModule(std::string const& moduleName);
  116. private:
  117. /*!
  118. * Constructs a new manager. This constructor is private to forbid instantiation of this class. The only
  119. * way to create a new instance is by calling the static manager() method.
  120. */
  121. SettingsManager();
  122. /*!
  123. * This destructor is private, since we need to forbid explicit destruction of the manager.
  124. */
  125. virtual ~SettingsManager();
  126. // The registered modules.
  127. std::vector<std::string> moduleNames;
  128. std::unordered_map<std::string, std::unique_ptr<modules::ModuleSettings>> modules;
  129. // Mappings from all known option names to the options that match it. All options for one option name need
  130. // to be compatible in the sense that calling isCompatible(...) pairwise on all options must always return true.
  131. std::unordered_map<std::string, std::vector<std::shared_ptr<Option>>> longNameToOptions;
  132. std::unordered_map<std::string, std::vector<std::shared_ptr<Option>>> shortNameToOptions;
  133. // A mapping of module names to the corresponding options.
  134. std::unordered_map<std::string, std::vector<std::shared_ptr<Option>>> moduleOptions;
  135. // A list of long option names to keep the order in which they were registered. This is, for example, used
  136. // to match the regular expression given to the help option against the option names.
  137. std::vector<std::string> longOptionNames;
  138. /*!
  139. * Adds the given option to the known options.
  140. *
  141. * @param option The option to add.
  142. */
  143. void addOption(std::shared_ptr<Option> const& option);
  144. /*!
  145. * Sets the arguments of the given option from the provided strings.
  146. *
  147. * @param optionName The name of the option. This is only used for error output.
  148. * @param option The option for which to set the arguments.
  149. * @param argumentCache The arguments of the option as string values.
  150. */
  151. static void setOptionArguments(std::string const& optionName, std::shared_ptr<Option> option, std::vector<std::string> const& argumentCache);
  152. /*!
  153. * Sets the arguments of the options matching the given name from the provided strings.
  154. *
  155. * @param optionName The name of the options for which to set the arguments.
  156. * @param optionMap The mapping from option names to options.
  157. * @param argumentCache The arguments of the option as string values.
  158. */
  159. static void setOptionsArguments(std::string const& optionName, std::unordered_map<std::string, std::vector<std::shared_ptr<Option>>> const& optionMap, std::vector<std::string> const& argumentCache);
  160. /*!
  161. * Checks whether the given option is compatible with all options with the given name in the given mapping.
  162. */
  163. static bool isCompatible(std::shared_ptr<Option> const& option, std::string const& optionName, std::unordered_map<std::string, std::vector<std::shared_ptr<Option>>> const& optionMap);
  164. /*!
  165. * Inserts the given option to the options with the given name in the given map.
  166. *
  167. * @param name The name of the option.
  168. * @param option The option to add.
  169. * @param optionMap The map into which the option is to be inserted.
  170. */
  171. static void addOptionToMap(std::string const& name, std::shared_ptr<Option> const& option, std::unordered_map<std::string, std::vector<std::shared_ptr<Option>>>& optionMap);
  172. /*!
  173. * Checks all modules for consistency by calling their respective check method.
  174. */
  175. void checkAllModules() const;
  176. /*!
  177. * Retrieves the (print) length of the longest option of all modules.
  178. *
  179. * @return The length of the longest option.
  180. */
  181. uint_fast64_t getPrintLengthOfLongestOption() const;
  182. /*!
  183. * Retrieves the (print) length of the longest option in the given module, so we can align the options.
  184. *
  185. * @param moduleName The module name for which to retrieve the length of the longest option.
  186. * @return The length of the longest option name.
  187. */
  188. uint_fast64_t getPrintLengthOfLongestOption(std::string const& moduleName) const;
  189. /*!
  190. * Parses the given file and stores the settings in the returned map.
  191. *
  192. * @param filename The name of the file that is to be scanned for settings.
  193. * @return A mapping of option names to the argument values (represented as strings).
  194. */
  195. std::map<std::string, std::vector<std::string>> parseConfigFile(std::string const& filename) const;
  196. };
  197. /*!
  198. * Retrieves the settings manager.
  199. *
  200. * @return The only settings manager.
  201. */
  202. SettingsManager const& manager();
  203. /*!
  204. * Retrieves the settings manager.
  205. *
  206. * @return The only settings manager.
  207. */
  208. SettingsManager& mutableManager();
  209. /*!
  210. * Retrieves the general settings.
  211. *
  212. * @return An object that allows accessing the general settings.
  213. */
  214. storm::settings::modules::GeneralSettings const& generalSettings();
  215. /*!
  216. * Retrieves the general settings in a mutable form. This is only meant to be used for debug purposes or very
  217. * rare cases where it is necessary.
  218. *
  219. * @return An object that allows accessing and modifying the general settings.
  220. */
  221. storm::settings::modules::GeneralSettings& mutableGeneralSettings();
  222. /*!
  223. * Retrieves the debug settings.
  224. *
  225. * @return An object that allows accessing the debug settings.
  226. */
  227. storm::settings::modules::DebugSettings const& debugSettings();
  228. /*!
  229. * Retrieves the counterexample generator settings.
  230. *
  231. * @return An object that allows accessing the counterexample generator settings.
  232. */
  233. storm::settings::modules::CounterexampleGeneratorSettings const& counterexampleGeneratorSettings();
  234. /*!
  235. * Retrieves the CUDD settings.
  236. *
  237. * @return An object that allows accessing the CUDD settings.
  238. */
  239. storm::settings::modules::CuddSettings const& cuddSettings();
  240. /*!
  241. * Retrieves the settings of the gmm++-based equation solver.
  242. *
  243. * @return An object that allows accessing the settings of the gmm++-based equation solver.
  244. */
  245. storm::settings::modules::GmmxxEquationSolverSettings const& gmmxxEquationSolverSettings();
  246. /*!
  247. * Retrieves the settings of the native equation solver.
  248. *
  249. * @return An object that allows accessing the settings of the native equation solver.
  250. */
  251. storm::settings::modules::NativeEquationSolverSettings const& nativeEquationSolverSettings();
  252. /*!
  253. * Retrieves the settings of the native equation solver.
  254. *
  255. * @return An object that allows accessing the settings of the native equation solver.
  256. */
  257. storm::settings::modules::BisimulationSettings const& bisimulationSettings();
  258. /*!
  259. * Retrieves the settings of glpk.
  260. *
  261. * @return An object that allows accessing the settings of glpk.
  262. */
  263. storm::settings::modules::GlpkSettings const& glpkSettings();
  264. /*!
  265. * Retrieves the settings of Gurobi.
  266. *
  267. * @return An object that allows accessing the settings of Gurobi.
  268. */
  269. storm::settings::modules::GurobiSettings const& gurobiSettings();
  270. /*!
  271. * Retrieves the settings for parametric model checking.
  272. *
  273. * @return An object that allows accessing the settings for parameteric model checking.
  274. */
  275. storm::settings::modules::ParametricSettings const& parametricSettings();
  276. } // namespace settings
  277. } // namespace storm
  278. #endif /* STORM_SETTINGS_SETTINGSMANAGER_H_ */