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