Storm 1.13.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SettingsManager.cpp
Go to the documentation of this file.
2
3#include <boost/algorithm/string.hpp>
4#include <boost/io/ios_state.hpp>
5#include <cstring>
6#include <fstream>
7#include <iomanip>
8#include <mutex>
9#include <regex>
10#include <set>
11
14#include "storm/io/file.h"
48
49namespace storm {
50namespace settings {
51
52SettingsManager::SettingsManager() : modules(), longNameToOptions(), shortNameToOptions(), moduleOptions() {}
53
54SettingsManager::~SettingsManager() {
55 // Intentionally left empty.
56}
57
59 static SettingsManager settingsManager;
60 return settingsManager;
61}
62
63void SettingsManager::setName(std::string const& name, std::string const& executableName) {
64 this->name = name;
65 this->executableName = executableName;
66}
67
68void SettingsManager::setFromCommandLine(int const argc, char const* const argv[]) {
69 // We convert the arguments to a vector of strings and strip off the first element since it refers to the
70 // name of the program.
71 std::vector<std::string> argumentVector(argc - 1);
72 for (int i = 1; i < argc; ++i) {
73 argumentVector[i - 1] = std::string(argv[i]);
74 }
75
76 this->setFromExplodedString(argumentVector);
77}
78
79void SettingsManager::setFromString(std::string const& commandLineString) {
80 if (commandLineString.empty()) {
81 this->setFromExplodedString({});
82 } else {
83 std::vector<std::string> argumentVector;
84 boost::split(argumentVector, commandLineString, boost::is_any_of("\t "));
85 this->setFromExplodedString(argumentVector);
86 }
87}
88
89void SettingsManager::handleUnknownOption(std::string const& optionName, bool isShort) const {
90 std::string optionNameWithDashes = (isShort ? "-" : "--") + optionName;
91 storm::utility::string::SimilarStrings similarStrings(optionNameWithDashes, 0.6, false);
92 std::map<std::string, std::vector<std::string>> similarOptionNames;
93 for (auto const& longOption : longNameToOptions) {
94 if (similarStrings.add("--" + longOption.first)) {
95 similarOptionNames["--" + longOption.first].push_back(longOption.first);
96 }
97 }
98 for (auto const& shortOption : shortNameToOptions) {
99 if (similarStrings.add("-" + shortOption.first)) {
100 for (auto const& option : shortOption.second) {
101 similarOptionNames["-" + shortOption.first].push_back(option->getLongName());
102 }
103 }
104 }
105 std::string errorMessage = "Unknown option '" + optionNameWithDashes + "'.";
106 if (!similarOptionNames.empty()) {
107 errorMessage += " " + similarStrings.toDidYouMeanString() + "\n\n";
108 std::vector<std::string> sortedSimilarOptionNames;
109 auto similarStringsList = similarStrings.toList();
110 for (auto const& s : similarStringsList) {
111 for (auto const& longOptionName : similarOptionNames.at(s)) {
112 sortedSimilarOptionNames.push_back(longOptionName);
113 }
114 }
115 errorMessage += getHelpForSelection({}, sortedSimilarOptionNames, "", "##### Suggested options:");
116 }
117 STORM_LOG_THROW(false, storm::exceptions::OptionParserException, errorMessage);
118}
119
120void SettingsManager::setFromExplodedString(std::vector<std::string> const& commandLineArguments) {
121 // In order to assign the parsed arguments to an option, we need to keep track of the "active" option's name.
122 bool optionActive = false;
123 bool activeOptionIsShortName = false;
124 std::string activeOptionName = "";
125 std::vector<std::string> argumentCache;
126
127 // Walk through all arguments.
128 for (uint_fast64_t i = 0; i < commandLineArguments.size(); ++i) {
129 std::string const& currentArgument = commandLineArguments[i];
130
131 // Check if the given argument is a new option or belongs to a previously given option.
132 if (!currentArgument.empty() && currentArgument.at(0) == '-') {
133 if (optionActive) {
134 // At this point we know that a new option is about to come. Hence, we need to assign the current
135 // cache content to the option that was active until now.
136 setOptionsArguments(activeOptionName, activeOptionIsShortName ? this->shortNameToOptions : this->longNameToOptions, argumentCache);
137
138 // After the assignment, the argument cache needs to be cleared.
139 argumentCache.clear();
140 } else {
141 optionActive = true;
142 }
143
144 if (currentArgument.at(1) == '-') {
145 // In this case, the argument has to be the long name of an option. Try to get all options that
146 // match the long name.
147 std::string optionName = currentArgument.substr(2);
148 auto optionIterator = this->longNameToOptions.find(optionName);
149 if (optionIterator == this->longNameToOptions.end()) {
150 handleUnknownOption(optionName, false);
151 }
152 activeOptionIsShortName = false;
153 activeOptionName = optionName;
154 } else {
155 // In this case, the argument has to be the short name of an option. Try to get all options that
156 // match the short name.
157 std::string optionName = currentArgument.substr(1);
158 auto optionIterator = this->shortNameToOptions.find(optionName);
159 if (optionIterator == this->shortNameToOptions.end()) {
160 handleUnknownOption(optionName, true);
161 }
162 activeOptionIsShortName = true;
163 activeOptionName = optionName;
164 }
165 } else if (optionActive) {
166 // Add the current argument to the list of arguments for the currently active options.
167 argumentCache.push_back(currentArgument);
168 } else {
169 STORM_LOG_THROW(false, storm::exceptions::OptionParserException,
170 "Found stray argument '" << currentArgument << "' that is not preceeded by a matching option.");
171 }
172 }
173
174 // If an option is still active at this point, we need to set it.
175 if (optionActive) {
176 setOptionsArguments(activeOptionName, activeOptionIsShortName ? this->shortNameToOptions : this->longNameToOptions, argumentCache);
177 }
178
179 // Include the options from a possibly specified configuration file, but don't overwrite existing settings.
183 }
184
185 // Finally, check whether all modules are okay with the current settings.
186 this->finalizeAllModules();
187}
188
189void SettingsManager::setFromConfigurationFile(std::string const& configFilename) {
190 std::map<std::string, std::vector<std::string>> configurationFileSettings = parseConfigFile(configFilename);
191
192 for (auto const& optionArgumentsPair : configurationFileSettings) {
193 auto options = this->longNameToOptions.find(optionArgumentsPair.first);
194
195 // We don't need to check whether this option exists or not, because this is already checked when
196 // parsing the configuration file.
197
198 // Now go through all the matching options and set them according to the values.
199 for (auto option : options->second) {
200 if (option->getHasOptionBeenSet()) {
201 // If the option was already set from the command line, we issue a warning and ignore the
202 // settings from the configuration file.
203 STORM_LOG_WARN("The option '" << option->getLongName() << "' of module '" << option->getModuleName()
204 << "' has been set in the configuration file '" << configFilename
205 << "', but was overwritten on the command line.\n");
206 } else {
207 // If, however, the option has not been set yet, we try to assign values ot its arguments
208 // based on the argument strings.
209 setOptionArguments(optionArgumentsPair.first, option, optionArgumentsPair.second);
210 }
211 }
212 }
213 // Finally, check whether all modules are okay with the current settings.
214 this->finalizeAllModules();
215}
216
217void SettingsManager::printHelp(std::string const& filter) const {
218 STORM_PRINT("usage: " << executableName << " [options]\n\n");
219
220 if (filter == "frequent" || filter == "all") {
221 bool includeAdvanced = (filter == "all");
222 // Find longest option name.
223 uint_fast64_t maxLength = getPrintLengthOfLongestOption(includeAdvanced);
224
225 std::vector<std::string> invisibleModules;
226 uint64_t numHidden = 0;
227 for (auto const& moduleName : this->moduleNames) {
228 // Only print for visible modules.
229 if (hasModule(moduleName, true)) {
230 STORM_PRINT(getHelpForModule(moduleName, maxLength, includeAdvanced));
231 // collect 'hidden' options
232 if (!includeAdvanced) {
233 auto moduleIterator = moduleOptions.find(moduleName);
234 if (moduleIterator != this->moduleOptions.end()) {
235 bool allAdvanced = true;
236 for (auto const& option : moduleIterator->second) {
237 if (!option->getIsAdvanced()) {
238 allAdvanced = false;
239 } else {
240 ++numHidden;
241 }
242 }
243 if (!moduleIterator->second.empty() && allAdvanced) {
244 invisibleModules.push_back(moduleName);
245 }
246 }
247 }
248 }
249 }
250 if (!includeAdvanced) {
251 if (numHidden == 1) {
252 STORM_PRINT(numHidden << " hidden option.\n");
253 } else {
254 STORM_PRINT(numHidden << " hidden options.\n");
255 }
256 if (!invisibleModules.empty()) {
257 if (invisibleModules.size() == 1) {
258 STORM_PRINT(invisibleModules.size() << " hidden module (" << boost::join(invisibleModules, ", ") << ").\n");
259 } else {
260 STORM_PRINT(invisibleModules.size() << " hidden modules (" << boost::join(invisibleModules, ", ") << ").\n");
261 }
262 }
263 STORM_PRINT("\nType '" + executableName + " --help modulename' to display all options of a specific module.\n");
264 STORM_PRINT("Type '" + executableName + " --help all' to display a complete list of options.\n");
265 }
266 } else {
267 // Create a regular expression from the input hint.
268 std::regex hintRegex(filter, std::regex_constants::ECMAScript | std::regex_constants::icase);
269
270 // Try to match the regular expression against the known modules.
271 std::vector<std::string> matchingModuleNames;
272 for (auto const& moduleName : this->moduleNames) {
273 if (std::regex_search(moduleName, hintRegex)) {
274 if (hasModule(moduleName, true)) {
275 matchingModuleNames.push_back(moduleName);
276 }
277 }
278 }
279
280 // Try to match the regular expression against the known options.
281 std::vector<std::string> matchingOptionNames;
282 for (auto const& optionName : this->longOptionNames) {
283 if (std::regex_search(optionName, hintRegex)) {
284 matchingOptionNames.push_back(optionName);
285 }
286 }
287
288 std::string optionList = getHelpForSelection(matchingModuleNames, matchingOptionNames,
289 "Matching modules for filter '" + filter + "':", "Matching options for filter '" + filter + "':");
290 if (optionList.empty()) {
291 STORM_PRINT("Filter '" << filter << "' did not match any modules or options.\n");
292 } else {
293 STORM_PRINT(optionList);
294 }
295 }
296}
297
298std::string SettingsManager::getHelpForSelection(std::vector<std::string> const& selectedModuleNames, std::vector<std::string> const& selectedLongOptionNames,
299 std::string modulesHeader, std::string optionsHeader) const {
300 std::stringstream stream;
301
302 // Remember which options we printed, so we don't display options twice.
303 std::set<std::shared_ptr<Option>> printedOptions;
304
305 // Try to match the regular expression against the known modules.
306 uint_fast64_t maxLengthModules = 0;
307 for (auto const& moduleName : selectedModuleNames) {
308 maxLengthModules = std::max(maxLengthModules, getPrintLengthOfLongestOption(moduleName, true));
309 // Add all options of this module to the list of printed options so we don't print them twice.
310 auto optionIterator = this->moduleOptions.find(moduleName);
311 STORM_LOG_ASSERT(optionIterator != this->moduleOptions.end(), "Unable to find selected module " << moduleName << ".");
312 printedOptions.insert(optionIterator->second.begin(), optionIterator->second.end());
313 }
314
315 // Try to match the regular expression against the known options.
316 std::vector<std::shared_ptr<Option>> matchingOptions;
317 uint_fast64_t maxLengthOptions = 0;
318 for (auto const& optionName : selectedLongOptionNames) {
319 auto optionIterator = this->longNameToOptions.find(optionName);
320 STORM_LOG_ASSERT(optionIterator != this->longNameToOptions.end(), "Unable to find selected option " << optionName << ".");
321 for (auto const& option : optionIterator->second) {
322 // Only add the option if we have not already added it to the list of options that is going
323 // to be printed anyway.
324 if (printedOptions.find(option) == printedOptions.end()) {
325 maxLengthOptions = std::max(maxLengthOptions, option->getPrintLength());
326 matchingOptions.push_back(option);
327 printedOptions.insert(option);
328 }
329 }
330 }
331
332 // Print the matching modules.
333 uint_fast64_t maxLength = std::max(maxLengthModules, maxLengthOptions);
334 if (selectedModuleNames.size() > 0) {
335 if (modulesHeader != "") {
336 stream << modulesHeader << '\n';
337 }
338 for (auto const& matchingModuleName : selectedModuleNames) {
339 stream << getHelpForModule(matchingModuleName, maxLength, true);
340 }
341 }
342
343 // Print the matching options.
344 if (matchingOptions.size() > 0) {
345 if (optionsHeader != "") {
346 stream << optionsHeader << '\n';
347 }
348 for (auto const& option : matchingOptions) {
349 stream << std::setw(maxLength) << std::left << *option << '\n';
350 }
351 }
352 return stream.str();
353}
354
355std::string SettingsManager::getHelpForModule(std::string const& moduleName, uint_fast64_t maxLength, bool includeAdvanced) const {
356 auto moduleIterator = moduleOptions.find(moduleName);
357 if (moduleIterator == this->moduleOptions.end()) {
358 return "";
359 }
360 // STORM_LOG_THROW(moduleIterator != moduleOptions.end(), storm::exceptions::IllegalFunctionCallException, "Cannot print help for unknown module '" <<
361 // moduleName << "'.");
362
363 // Check whether there is at least one (enabled) option in this module
364 uint64_t numOfOptions = 0;
365 for (auto const& option : moduleIterator->second) {
366 if (includeAdvanced || !option->getIsAdvanced()) {
367 ++numOfOptions;
368 }
369 }
370
371 std::stringstream stream;
372 if (numOfOptions > 0) {
373 std::string displayedModuleName = "'" + moduleName + "'";
374 if (!includeAdvanced) {
375 displayedModuleName += " (" + std::to_string(numOfOptions) + "/" + std::to_string(moduleIterator->second.size()) + " shown)";
376 }
377 stream << "##### Module " << displayedModuleName << " " << std::string(std::min(maxLength, maxLength - displayedModuleName.length() - 14), '#') << '\n';
378
379 // Save the flags for std::cout so we can manipulate them and be sure they will be restored as soon as this
380 // stream goes out of scope.
381 boost::io::ios_flags_saver out(std::cout);
382
383 for (auto const& option : moduleIterator->second) {
384 if (includeAdvanced || !option->getIsAdvanced()) {
385 stream << std::setw(maxLength) << std::left << *option << '\n';
386 }
387 }
388 stream << '\n';
389 }
390 return stream.str();
391}
392
393uint_fast64_t SettingsManager::getPrintLengthOfLongestOption(bool includeAdvanced) const {
394 uint_fast64_t length = 0;
395 for (auto const& moduleName : this->moduleNames) {
396 length = std::max(getPrintLengthOfLongestOption(moduleName, includeAdvanced), length);
397 }
398 return length;
399}
400
401uint_fast64_t SettingsManager::getPrintLengthOfLongestOption(std::string const& moduleName, bool includeAdvanced) const {
402 auto moduleIterator = modules.find(moduleName);
403 STORM_LOG_THROW(moduleIterator != modules.end(), storm::exceptions::IllegalFunctionCallException,
404 "Unable to retrieve option length of unknown module '" << moduleName << "'.");
405 return moduleIterator->second->getPrintLengthOfLongestOption(includeAdvanced);
406}
407
408void SettingsManager::addModule(std::unique_ptr<modules::ModuleSettings>&& moduleSettings, bool doRegister) {
409 auto moduleIterator = this->modules.find(moduleSettings->getModuleName());
410 STORM_LOG_THROW(moduleIterator == this->modules.end(), storm::exceptions::IllegalFunctionCallException,
411 "Unable to register module '" << moduleSettings->getModuleName() << "' because a module with the same name already exists.");
412
413 // Take over the module settings object.
414 std::string moduleName = moduleSettings->getModuleName();
415 this->moduleNames.push_back(moduleName);
416 this->modules.emplace(moduleSettings->getModuleName(), std::move(moduleSettings));
417 auto iterator = this->modules.find(moduleName);
418 std::unique_ptr<modules::ModuleSettings> const& settings = iterator->second;
419
420 if (doRegister) {
421 this->moduleOptions.emplace(moduleName, std::vector<std::shared_ptr<Option>>());
422 // Now register the options of the module.
423 for (auto const& option : settings->getOptions()) {
424 this->addOption(option);
425 }
426 }
427}
428
429void SettingsManager::addOption(std::shared_ptr<Option> const& option) {
430 // First, we register to which module the given option belongs.
431 auto moduleOptionIterator = this->moduleOptions.find(option->getModuleName());
432 STORM_LOG_THROW(moduleOptionIterator != this->moduleOptions.end(), storm::exceptions::IllegalFunctionCallException,
433 "Cannot add option for unknown module '" << option->getModuleName() << "'.");
434 moduleOptionIterator->second.emplace_back(option);
435
436 // Then, we add the option's name (and possibly short name) to the registered options. If a module prefix is
437 // not required for this option, we have to add both versions to our mappings, the prefixed one and the
438 // non-prefixed one.
439 if (!option->getRequiresModulePrefix()) {
440 bool isCompatible = this->isCompatible(option, option->getLongName(), this->longNameToOptions);
441 STORM_LOG_THROW(isCompatible, storm::exceptions::IllegalFunctionCallException,
442 "Unable to add option '" << option->getLongName() << "', because an option with the same name is incompatible with it.");
443 addOptionToMap(option->getLongName(), option, this->longNameToOptions);
444 }
445 // For the prefixed name, we don't need a compatibility check, because a module is not allowed to register the same option twice.
446 addOptionToMap(option->getModuleName() + ":" + option->getLongName(), option, this->longNameToOptions);
447 longOptionNames.push_back(option->getModuleName() + ":" + option->getLongName());
448
449 if (option->getHasShortName()) {
450 if (!option->getRequiresModulePrefix()) {
451 bool isCompatible = this->isCompatible(option, option->getShortName(), this->shortNameToOptions);
452 STORM_LOG_THROW(isCompatible, storm::exceptions::IllegalFunctionCallException,
453 "Unable to add option '" << option->getLongName() << "', because an option with the same name is incompatible with it.");
454 addOptionToMap(option->getShortName(), option, this->shortNameToOptions);
455 }
456 addOptionToMap(option->getModuleName() + ":" + option->getShortName(), option, this->shortNameToOptions);
457 }
458}
459
460bool SettingsManager::hasModule(std::string const& moduleName, bool checkHidden) const {
461 if (checkHidden) {
462 return this->moduleOptions.find(moduleName) != this->moduleOptions.end();
463 } else {
464 return this->modules.find(moduleName) != this->modules.end();
465 }
466}
467
468modules::ModuleSettings const& SettingsManager::getModule(std::string const& moduleName) const {
469 auto moduleIterator = this->modules.find(moduleName);
470 STORM_LOG_THROW(moduleIterator != this->modules.end(), storm::exceptions::IllegalFunctionCallException,
471 "Cannot retrieve unknown module '" << moduleName << "'.");
472 return *moduleIterator->second;
473}
474
476 auto moduleIterator = this->modules.find(moduleName);
477 STORM_LOG_THROW(moduleIterator != this->modules.end(), storm::exceptions::IllegalFunctionCallException,
478 "Cannot retrieve unknown module '" << moduleName << "'.");
479 return *moduleIterator->second;
480}
481
482bool SettingsManager::isCompatible(std::shared_ptr<Option> const& option, std::string const& optionName,
483 std::unordered_map<std::string, std::vector<std::shared_ptr<Option>>> const& optionMap) {
484 auto optionIterator = optionMap.find(optionName);
485 if (optionIterator != optionMap.end()) {
486 for (auto const& otherOption : optionIterator->second) {
487 bool locallyCompatible = option->isCompatibleWith(*otherOption);
488 if (!locallyCompatible) {
489 return false;
490 }
491 }
492 }
493 return true;
494}
495
496void SettingsManager::setOptionArguments(std::string const& optionName, std::shared_ptr<Option> option, std::vector<std::string> const& argumentCache) {
497 STORM_LOG_THROW(argumentCache.size() <= option->getArgumentCount(), storm::exceptions::OptionParserException,
498 "Too many arguments for option '" << optionName << "'.");
499 STORM_LOG_THROW(!option->getHasOptionBeenSet(), storm::exceptions::OptionParserException, "Option '" << optionName << "' is set multiple times.");
500
501 // Now set the provided argument values one by one.
502 for (uint_fast64_t i = 0; i < argumentCache.size(); ++i) {
503 ArgumentBase& argument = option->getArgument(i);
504 bool conversionOk = argument.setFromStringValue(argumentCache[i]);
505 STORM_LOG_THROW(conversionOk, storm::exceptions::OptionParserException,
506 "Value '" << argumentCache[i] << "' is invalid for argument <" << argument.getName() << "> of option:\n"
507 << *option);
508 }
509
510 // In case there are optional arguments that were not set, we set them to their default value.
511 for (uint_fast64_t i = argumentCache.size(); i < option->getArgumentCount(); ++i) {
512 ArgumentBase& argument = option->getArgument(i);
513 STORM_LOG_THROW(argument.getIsOptional(), storm::exceptions::OptionParserException,
514 "Non-optional argument <" << argument.getName() << "> of option:\n"
515 << *option);
516 argument.setFromDefaultValue();
517 }
518
519 option->setHasOptionBeenSet();
520 if (optionName != option->getLongName() && optionName != option->getShortName() && boost::starts_with(optionName, option->getModuleName())) {
521 option->setHasOptionBeenSetWithModulePrefix();
522 }
523}
524
525void SettingsManager::setOptionsArguments(std::string const& optionName, std::unordered_map<std::string, std::vector<std::shared_ptr<Option>>> const& optionMap,
526 std::vector<std::string> const& argumentCache) {
527 auto optionIterator = optionMap.find(optionName);
528 STORM_LOG_THROW(optionIterator != optionMap.end(), storm::exceptions::OptionParserException, "Unknown option '" << optionName << "'.");
529
530 // Iterate over all options and set the arguments.
531 for (auto& option : optionIterator->second) {
532 setOptionArguments(optionName, option, argumentCache);
533 }
534}
535
536void SettingsManager::addOptionToMap(std::string const& name, std::shared_ptr<Option> const& option,
537 std::unordered_map<std::string, std::vector<std::shared_ptr<Option>>>& optionMap) {
538 auto optionIterator = optionMap.find(name);
539 if (optionIterator == optionMap.end()) {
540 std::vector<std::shared_ptr<Option>> optionVector;
541 optionVector.push_back(option);
542 optionMap.emplace(name, optionVector);
543 } else {
544 optionIterator->second.push_back(option);
545 }
546}
547
548void SettingsManager::finalizeAllModules() {
549 for (auto const& nameModulePair : this->modules) {
550 nameModulePair.second->finalize();
551 nameModulePair.second->check();
552 }
553}
554
555std::map<std::string, std::vector<std::string>> SettingsManager::parseConfigFile(std::string const& filename) const {
556 std::map<std::string, std::vector<std::string>> result;
557
558 std::ifstream input;
559 storm::io::openFile(filename, input);
560
561 bool globalScope = true;
562 std::string activeModule = "";
563 uint_fast64_t lineNumber = 1;
564 for (std::string line; storm::io::getline(input, line); ++lineNumber) {
565 // If the first character of the line is a "[", we expect the settings of a new module to start and
566 // the line to be of the shape [<module>].
567 if (line.at(0) == '[') {
569 line.at(0) == '[' && line.find("]") == line.length() - 1 && line.find("[", 1) == line.npos, storm::exceptions::OptionParserException,
570 "Illegal module name header in configuration file '" << filename << " in line " << std::to_string(lineNumber)
571 << ". Expected [<module>] where <module> is a placeholder for a known module.");
572
573 // Extract the module name and check whether it's a legal one.
574 std::string moduleName = line.substr(1, line.length() - 2);
575 STORM_LOG_THROW(moduleName != "" && (moduleName == "global" || (this->modules.find(moduleName) != this->modules.end())),
576 storm::exceptions::OptionParserException,
577 "Module header in configuration file '" << filename << " in line " << std::to_string(lineNumber) << " refers to unknown module '"
578 << moduleName << ".");
579
580 // If the module name is "global", we unset the currently active module and treat all options to follow as unprefixed.
581 if (moduleName == "global") {
582 globalScope = true;
583 } else {
584 activeModule = moduleName;
585 globalScope = false;
586 }
587 } else {
588 // In this case, we expect the line to be of the shape o or o=a b c, where o is an option and a, b
589 // and c are the values that are supposed to be assigned to the arguments of the option.
590 std::size_t assignmentSignIndex = line.find("=");
591 bool containsAssignment = false;
592 if (assignmentSignIndex != line.npos) {
593 containsAssignment = true;
594 }
595
596 std::string optionName;
597 if (containsAssignment) {
598 optionName = line.substr(0, assignmentSignIndex);
599 } else {
600 optionName = line;
601 }
602
603 if (globalScope) {
604 STORM_LOG_THROW(this->longNameToOptions.find(optionName) != this->longNameToOptions.end(), storm::exceptions::OptionParserException,
605 "Option assignment in configuration file '" << filename << " in line " << lineNumber << " refers to unknown option '"
606 << optionName << "'.");
607 } else {
608 STORM_LOG_THROW(this->longNameToOptions.find(activeModule + ":" + optionName) != this->longNameToOptions.end(),
609 storm::exceptions::OptionParserException,
610 "Option assignment in configuration file '" << filename << " in line " << lineNumber << " refers to unknown option '"
611 << activeModule << ":" << optionName << "'.");
612 }
613
614 std::string fullOptionName = (!globalScope ? activeModule + ":" : "") + optionName;
615 STORM_LOG_WARN_COND(result.find(fullOptionName) == result.end(), "Option '" << fullOptionName << "' is set in line " << lineNumber
616 << " of configuration file " << filename
617 << ", but has been set before.");
618
619 // If the current line is an assignment, split the right-hand side of the assignment into parts
620 // enclosed by quotation marks.
621 if (containsAssignment) {
622 std::string assignedValues = line.substr(assignmentSignIndex + 1);
623 std::vector<std::string> argumentCache;
624
625 // As horrible as it may look, this regular expression matches either a quoted string (possibly
626 // containing escaped quotes) or a simple word (without whitespaces and quotes).
627 std::regex argumentRegex("\"(([^\\\\\"]|((\\\\\\\\)*\\\\\")|\\\\[^\"])*)\"|(([^ \\\\\"]|((\\\\\\\\)*\\\\\")|\\\\[^\"])+)");
628 boost::algorithm::trim_left(assignedValues);
629
630 while (!assignedValues.empty()) {
631 std::smatch match;
632 bool hasMatch = std::regex_search(assignedValues, match, argumentRegex);
633
634 // If the input could not be matched, we have a parsing error.
636 hasMatch, storm::exceptions::OptionParserException,
637 "Parsing error in configuration file '" << filename << "' in line " << lineNumber << ". Unexpected input '" << assignedValues << "'.");
638
639 // Extract the matched argument and cut off the quotation marks if necessary.
640 std::string matchedArgument = std::string(match[0].first, match[0].second);
641 if (matchedArgument.at(0) == '"') {
642 matchedArgument = matchedArgument.substr(1, matchedArgument.length() - 2);
643 }
644 argumentCache.push_back(matchedArgument);
645
646 assignedValues = assignedValues.substr(match.length());
647 boost::algorithm::trim_left(assignedValues);
648 }
649
650 // After successfully parsing the argument values, we store them in the result map.
651 result.emplace(fullOptionName, argumentCache);
652 } else {
653 // In this case, we can just insert the option to indicate it should be set (without arguments).
654 result.emplace(fullOptionName, std::vector<std::string>());
655 }
656 }
657 }
658
660 return result;
661}
662
666
670
674
678
679void initializeAll(std::string const& name, std::string const& executableName) {
680 storm::settings::mutableManager().setName(name, executableName);
681
682 // Register all known settings modules.
713}
714
715} // namespace settings
716} // namespace storm
Provides the central API for the registration of command line options and parsing the options from th...
void setFromCommandLine(int const argc, char const *const argv[])
This function parses the given command line arguments and sets all registered options accordingly.
std::string getHelpForModule(std::string const &moduleName, uint_fast64_t maxLength=30, bool includeAdvanced=true) const
This function prints a help message for the specified module to the standard output.
void setFromExplodedString(std::vector< std::string > const &commandLineArguments)
This function parses the given command line arguments (represented by several strings) and sets all r...
void setFromString(std::string const &commandLineString)
This function parses the given command line arguments (represented by one big string) and sets all re...
void setFromConfigurationFile(std::string const &configFilename)
This function parses the given file and sets all registered options accordingly.
void addModule(std::unique_ptr< modules::ModuleSettings > &&moduleSettings, bool doRegister=true)
Adds a new module with the given name.
void printHelp(std::string const &filter="frequent") const
This function prints a help message to the standard output.
void handleUnknownOption(std::string const &optionName, bool isShort) const
Throws an exception with a nice error message indicating similar valid option names.
void setName(std::string const &name, std::string const &executableName)
Sets the name of the tool.
SettingsManager(SettingsManager const &)=delete
modules::ModuleSettings const & getModule(std::string const &moduleName) const
Retrieves the settings of the module with the given name.
bool hasModule(std::string const &moduleName, bool checkHidden=false) const
Checks whether the module with the given name exists.
static SettingsManager & manager()
Retrieves the only existing instance of a settings manager.
This class represents the settings for the abstraction procedures.
This is the base class of the settings for a particular module.
bool add(std::string const &string)
Adds the given string to the set of similar strings (if it is similar).
Definition string.cpp:18
std::string toDidYouMeanString() const
Returns a "Did you mean abc?" string.
Definition string.cpp:34
std::vector< std::string > toList() const
Gets a list of all added strings that are similar to the reference string.
Definition string.cpp:26
#define STORM_LOG_WARN(message)
Definition logging.h:25
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_WARN_COND(cond, message)
Definition macros.h:38
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
#define STORM_PRINT(message)
Define the macros that print information and optionally also log it.
Definition macros.h:62
std::basic_istream< CharT, Traits > & getline(std::basic_istream< CharT, Traits > &input, std::basic_string< CharT, Traits, Allocator > &str)
Overloaded getline function which handles different types of newline ( and \r).
Definition file.h:80
void closeFile(std::ofstream &stream)
Close the given file after writing.
Definition file.h:47
void openFile(std::string const &filepath, std::ofstream &filestream, bool append=false, bool silent=false)
Open the given file for writing.
Definition file.h:18
storm::settings::modules::BuildSettings & mutableBuildSettings()
Retrieves the build settings in a mutable form.
storm::settings::modules::AbstractionSettings & mutableAbstractionSettings()
Retrieves the abstraction settings in a mutable form.
bool hasModule()
Returns true if the given module is registered.
SettingsType const & getModule()
Get module.
void addModule(bool doRegister=true)
Add new module to use for the settings.
void initializeAll(std::string const &name, std::string const &executableName)
Initialize the settings manager with all available modules.
SettingsManager const & manager()
Retrieves the settings manager.
SettingsManager & mutableManager()
Retrieves the settings manager.