Browse Source

Removed OptionsAccumulator.h and merged it into Settings.h

Implemented some helper functions and convenience accessors


Former-commit-id: b2d91343af
main
PBerger 12 years ago
parent
commit
d5a9656cac
  1. 1
      src/settings/Argument.h
  2. 4
      src/settings/Option.h
  3. 48
      src/settings/OptionsAccumulator.cpp
  4. 120
      src/settings/OptionsAccumulator.h
  5. 105
      src/settings/Settings.cpp
  6. 143
      src/settings/Settings.h
  7. 18
      src/storm.cpp

1
src/settings/Argument.h

@ -21,6 +21,7 @@
namespace storm {
namespace settings {
template<typename T>
class Argument : public ArgumentBase {
public:

4
src/settings/Option.h

@ -28,8 +28,12 @@
namespace storm {
namespace settings {
class Settings;
class Option {
public:
friend class storm::settings::Settings;
/*
std::string longName;
std::string shortName;

48
src/settings/OptionsAccumulator.cpp

@ -1,48 +0,0 @@
#include "src/settings/OptionsAccumulator.h"
/*!
* The map holding the information regarding registered options and their types
*/
//std::unordered_map<std::string, std::shared_ptr<Option>> options;
/*!
* The map holding the information regarding registered options and their short names
*/
//std::unordered_map<std::string, std::string> shortNames;
storm::settings::OptionsAccumulator& storm::settings::OptionsAccumulator::addOption(Option* option) {
// For automatic management of option's lifetime
std::shared_ptr<Option> optionPtr(option);
std::string lowerLongName = storm::utility::StringHelper::stringToLower(option->getLongName());
std::string lowerShortName = storm::utility::StringHelper::stringToLower(option->getShortName());
auto longNameIterator = this->options.find(lowerLongName);
auto shortNameIterator = this->shortNames.find(lowerShortName);
if (longNameIterator == this->options.end()) {
// Not found
if (!(shortNameIterator == this->shortNames.end())) {
// There exists an option which uses the same shortname
// LOG
throw storm::exceptions::OptionUnificationException() << "Error: The Option \"" << shortNameIterator->second << "\" from Module \"" << this->options.find(shortNameIterator->second)->second.get()->getModuleName() << "\" uses the same ShortName as the Option \"" << option->getLongName() << "\" from Module \"" << option->getModuleName() << "\"!";
}
// Copy Shared_ptr
this->options.insert(std::make_pair(lowerLongName, std::shared_ptr<Option>(optionPtr)));
this->optionPointers.push_back(std::shared_ptr<Option>(optionPtr));
this->shortNames.insert(std::make_pair(lowerShortName, lowerLongName));
} else {
// This will fail if the shortNames are not identical, so no additional checks here.
longNameIterator->second.get()->unify(*option);
}
return *this;
}
void storm::settings::OptionsAccumulator::join(storm::settings::OptionsAccumulator const& rhs) {
for (auto it = rhs.options.begin(); it != rhs.options.end(); ++it) {
// Clone all Options
this->addOption(it->second.get()->clone());
}
}

120
src/settings/OptionsAccumulator.h

@ -1,120 +0,0 @@
/*
* OptionsAccumulator.h
*
* Created on: 22.08.2013
* Author: Philipp Berger
*/
#ifndef STORM_SETTINGS_OPTIONSACCUMULATOR_H_
#define STORM_SETTINGS_OPTIONSACCUMULATOR_H_
#include <iostream>
#include <string>
#include <functional>
#include <unordered_map>
#include <algorithm>
#include <cstdint>
#include <vector>
#include <memory>
#include "src/settings/Option.h"
namespace storm {
namespace settings {
class Settings;
class OptionsAccumulator {
public:
OptionsAccumulator() {}
~OptionsAccumulator() {
//this->shortNames.clear();
//this->options.clear();
}
OptionsAccumulator& addOption(Option* option);
void join(OptionsAccumulator const& rhs);
friend class storm::settings::Settings;
private:
/*!
* The map holding the information regarding registered options and their types
*/
std::unordered_map<std::string, std::shared_ptr<Option>> options;
/*!
* The vector holding a pointer to all options
*/
std::vector<std::shared_ptr<Option>> optionPointers;
/*!
* The map holding the information regarding registered options and their short names
*/
std::unordered_map<std::string, std::string> shortNames;
/*!
* Returns true IFF this accumulator contains an option with the specified longName.
*/
bool containsLongName(std::string const& longName) {
return (this->options.find(storm::utility::StringHelper::stringToLower(longName)) != this->options.end());
}
/*!
* Returns true IFF this accumulator contains an option with the specified shortName.
*/
bool containsShortName(std::string const& shortName) {
return (this->shortNames.find(storm::utility::StringHelper::stringToLower(shortName)) != this->shortNames.end());
}
/*!
* Returns a reference to the Option with the specified longName.
* Throws an Exception of Type InvalidArgumentException if there is no such Option.
*/
Option& getByLongName(std::string const& longName) {
auto longNameIterator = this->options.find(storm::utility::StringHelper::stringToLower(longName));
if (longNameIterator == this->options.end()) {
throw storm::exceptions::IllegalArgumentException() << "This Accumulator does not contain an Option named \"" << longName << "\"!";
}
return *longNameIterator->second.get();
}
/*!
* Returns a pointer to the Option with the specified longName.
* Throws an Exception of Type InvalidArgumentException if there is no such Option.
*/
Option* getPtrByLongName(std::string const& longName) {
auto longNameIterator = this->options.find(storm::utility::StringHelper::stringToLower(longName));
if (longNameIterator == this->options.end()) {
throw storm::exceptions::IllegalArgumentException() << "This Accumulator does not contain an Option named \"" << longName << "\"!";
}
return longNameIterator->second.get();
}
/*!
* Returns a reference to the Option with the specified shortName.
* Throws an Exception of Type InvalidArgumentException if there is no such Option.
*/
Option& getByShortName(std::string const& shortName) {
auto shortNameIterator = this->shortNames.find(storm::utility::StringHelper::stringToLower(shortName));
if (shortNameIterator == this->shortNames.end()) {
throw storm::exceptions::IllegalArgumentException() << "This Accumulator does not contain an Option with ShortName \"" << shortName << "\"!";
}
return *(this->options.find(shortNameIterator->second)->second.get());
}
/*!
* Returns a pointer to the Option with the specified shortName.
* Throws an Exception of Type InvalidArgumentException if there is no such Option.
*/
Option* getPtrByShortName(std::string const& shortName) {
auto shortNameIterator = this->shortNames.find(storm::utility::StringHelper::stringToLower(shortName));
if (shortNameIterator == this->shortNames.end()) {
throw storm::exceptions::IllegalArgumentException() << "This Accumulator does not contain an Option with ShortName \"" << shortName << "\"!";
}
return this->options.find(shortNameIterator->second)->second.get();
}
};
}
}
#endif // STORM_SETTINGS_OPTIONSACCUMULATOR_H_

105
src/settings/Settings.cpp

@ -44,7 +44,7 @@ storm::settings::stringPair_t storm::settings::Settings::splitOptionString(std::
void storm::settings::Settings::handleAssignment(std::string const& longOptionName, std::vector<std::string> arguments) {
std::string optionName = storm::utility::StringHelper::stringToLower(longOptionName);
Option* option = this->optionsAccumulator->getPtrByLongName(optionName);
Option* option = this->getPtrByLongName(optionName);
// Mark as Set
option->setHasOptionBeenSet();
@ -135,21 +135,21 @@ void storm::settings::Settings::parseCommandLine(int const argc, char const * co
if (nextOption.at(0) == '-' && nextOption.at(1) != '-') {
// Short Option
std::string nextShortOptionName = storm::utility::StringHelper::stringToLower(nextOption.substr(1, nextOption.size() - 1));
if (!this->optionsAccumulator->containsShortName(nextShortOptionName)) {
if (!this->containsShortName(nextShortOptionName)) {
// LOG
throw storm::exceptions::OptionParserException() << "Found an unknown ShortName for an Option: \"" << nextShortOptionName << "\".";
} else {
longOptionName = this->optionsAccumulator->getByShortName(nextShortOptionName).getLongName();
longOptionName = this->getByShortName(nextShortOptionName).getLongName();
optionActive = true;
}
} else {
// Long Option
std::string nextLongOptionName = storm::utility::StringHelper::stringToLower(nextOption.substr(2, nextOption.size() - 2));
if (!this->optionsAccumulator->containsLongName(nextLongOptionName)) {
if (!this->containsLongName(nextLongOptionName)) {
// LOG
throw storm::exceptions::OptionParserException() << "Found an unknown LongName for an Option: \"" << nextLongOptionName << "\".";
} else {
longOptionName = this->optionsAccumulator->getByLongName(nextLongOptionName).getLongName();
longOptionName = this->getByLongName(nextLongOptionName).getLongName();
optionActive = true;
}
}
@ -163,7 +163,7 @@ void storm::settings::Settings::parseCommandLine(int const argc, char const * co
}
}
for (auto it = this->optionsAccumulator->options.cbegin(); it != this->optionsAccumulator->options.cend(); ++it) {
for (auto it = this->options.cbegin(); it != this->options.cend(); ++it) {
if (!it->second.get()->getHasOptionBeenSet()) {
if (it->second.get()->getIsRequired()) {
throw storm::exceptions::OptionParserException() << "Option \"" << it->second.get()->getLongName() << "\" is marked as required, but was not set!";
@ -179,7 +179,7 @@ void storm::settings::Settings::parseCommandLine(int const argc, char const * co
bool storm::settings::Settings::registerNewModule(ModuleRegistrationFunction_t registrationFunction) {
Settings* myInstance = Settings::getInstance();
return registrationFunction(myInstance->optionsAccumulator);
return registrationFunction(myInstance);
}
storm::settings::Settings* storm::settings::Settings::getInstance() {
@ -190,6 +190,93 @@ storm::settings::Settings* storm::settings::Settings::getInstance() {
return &pInstance;
}
void storm::settings::Settings::addOptions(OptionsAccumulator const& options) {
this->optionsAccumulator->join(options);
storm::settings::Settings& storm::settings::Settings::addOption(Option* option) {
// For automatic management of option's lifetime
std::shared_ptr<Option> optionPtr(option);
std::string lowerLongName = storm::utility::StringHelper::stringToLower(option->getLongName());
std::string lowerShortName = storm::utility::StringHelper::stringToLower(option->getShortName());
auto longNameIterator = this->options.find(lowerLongName);
auto shortNameIterator = this->shortNames.find(lowerShortName);
if (longNameIterator == this->options.end()) {
// Not found
if (!(shortNameIterator == this->shortNames.end())) {
// There exists an option which uses the same shortname
// LOG
throw storm::exceptions::OptionUnificationException() << "Error: The Option \"" << shortNameIterator->second << "\" from Module \"" << this->options.find(shortNameIterator->second)->second.get()->getModuleName() << "\" uses the same ShortName as the Option \"" << option->getLongName() << "\" from Module \"" << option->getModuleName() << "\"!";
}
// Copy Shared_ptr
this->options.insert(std::make_pair(lowerLongName, std::shared_ptr<Option>(optionPtr)));
this->optionPointers.push_back(std::shared_ptr<Option>(optionPtr));
this->shortNames.insert(std::make_pair(lowerShortName, lowerLongName));
} else {
// This will fail if the shortNames are not identical, so no additional checks here.
longNameIterator->second.get()->unify(*option);
}
return *this;
}
std::string storm::settings::Settings::getHelpText() const {
// Copy all option names into a vector and sort it
std::vector<std::string> optionNames;
optionNames.reserve(this->options.size());
size_t longNameMaxSize = 0;
size_t shortNameMaxSize = 0;
size_t argumentNameMaxSize = 0;
// Get the maximum size of the long and short Names and copy the long names for sorting
std::for_each(this->options.cbegin(), this->options.cend(), [&] (std::pair<std::string, std::shared_ptr<storm::settings::Option>> const& it) -> void {
longNameMaxSize = std::max(longNameMaxSize, it.first.size());
shortNameMaxSize = std::max(shortNameMaxSize, it.second.get()->getShortName().size());
optionNames.push_back(it.first);
std::for_each(it.second.get()->arguments.cbegin(), it.second.get()->arguments.cend(), [&] (std::shared_ptr<ArgumentBase> const& arg) -> void {
argumentNameMaxSize = std::max(argumentNameMaxSize, arg.get()->getArgumentName().size());
});
});
// Sort the long names
std::sort(optionNames.begin(), optionNames.end(), [] (std::string const& a, std::string const& b) -> bool { return a.compare(b) < 0; });
std::stringstream ss;
/*
Layout:
--longName -shortName Description
ArgumentName (ArgumentType) ArgumentDescription
*/
const std::string delimiter = " ";
for (auto it = optionNames.cbegin(); it != optionNames.cend(); ++it) {
Option const& o = this->getByLongName(*it);
std::string const& longName = o.getLongName();
ss << delimiter << "--" << longName;
// Fill up the remaining space after the long Name
for (uint_fast64_t i = longName.size(); i < longNameMaxSize; ++i) {
ss << " ";
}
std::string const& shortName = o.getShortName();
ss << delimiter << "-" << shortName;
// Fill up the remaining space after the short Name
for (uint_fast64_t i = shortName.size(); i < shortNameMaxSize; ++i) {
ss << " ";
}
ss << delimiter << o.getDescription() << std::endl;
for (auto i = 0; i < o.getArgumentCount(); ++i) {
ArgumentBase const& a = o.getArgument(i);
std::string const& argumentName = a.getArgumentName();
ss << delimiter << delimiter << "Arg " << (i+1) << ": " << argumentName;
// Fill up the remaining space after the argument Name
for (uint_fast64_t i = argumentName.size(); i < argumentNameMaxSize; ++i) {
ss << " ";
}
ss << "(" << ArgumentTypeHelper::toString(a.getArgumentType()) << ")" << delimiter << a.getArgumentDescription() << std::endl;
}
}
return ss.str();
}

143
src/settings/Settings.h

@ -33,7 +33,7 @@ namespace storm {
namespace settings {
class Settings;
typedef std::function<bool (OptionsAccumulator*)> ModuleRegistrationFunction_t;
typedef std::function<bool (Settings*)> ModuleRegistrationFunction_t;
typedef bool (*stringValidationFunction_t)(const std::string);
typedef bool (*integerValidationFunction_t)(const int_fast64_t);
@ -44,14 +44,6 @@ namespace settings {
typedef std::pair<std::string, std::string> stringPair_t;
typedef std::pair<bool, std::string> fromStringAssignmentResult_t;
/*
typedef std::function<bool (std::string const)> stringValidationFunction_t;
typedef std::function<bool (int_fast64_t const)> integerValidationFunction_t;
typedef std::function<bool (uint_fast64_t const)> unsignedIntegerValidationFunction_t;
typedef std::function<bool (double const)> doubleValidationFunction_t;
typedef std::function<bool (bool const)> booleanValidationFunction_t;
*/
class Destroyer;
/*!
@ -76,46 +68,69 @@ namespace settings {
static bool registerNewModule(ModuleRegistrationFunction_t registrationFunction);
/*!
* Parsing
* This parses the command line of the application and matches it to all prior registered options
* @throws OptionParserException
*/
static void parse(int const argc, char const * const argv[]);
void addOptions(OptionsAccumulator const& options);
std::vector<std::shared_ptr<Option>> const& getOptions() const {
return this->optionsAccumulator->optionPointers;
return this->optionPointers;
}
// COPY INTERFACE OF OPTIONSACCUMULATOR
// PUBLIC INTERFACE OF OPTIONSACCUMULATOR (now internal)
/*!
* Returns true IFF an option with the specified longName exists.
*/
bool containsOptionByLongName(std::string const& longName) {
return this->optionsAccumulator->containsLongName(longName);
bool containsOptionByLongName(std::string const& longName) const {
return this->containsLongName(longName);
}
/*!
* Returns true IFF an option with the specified shortName exists.
*/
bool containsOptionByShortName(std::string const& shortName) {
return this->optionsAccumulator->containsLongName(shortName);
bool containsOptionByShortName(std::string const& shortName) const {
return this->containsLongName(shortName);
}
/*!
* Returns a reference to the Option with the specified longName.
* Throws an Exception of Type IllegalArgumentException if there is no such Option.
*/
Option const& getOptionByLongName(std::string const& longName) {
return this->optionsAccumulator->getByLongName(longName);
Option const& getOptionByLongName(std::string const& longName) const {
return this->getByLongName(longName);
}
/*!
* Returns a reference to the Option with the specified shortName.
* Throws an Exception of Type IllegalArgumentException if there is no such Option.
*/
Option const& getOptionByShortName(std::string const& shortName) {
return this->optionsAccumulator->getByShortName(shortName);
Option const& getOptionByShortName(std::string const& shortName) const {
return this->getByShortName(shortName);
}
/*!
* Adds the given option to the set of known options.
* Unifying with existing options is done automatically.
* Ownership of the Option is handed over when calling this function!
* Returns a reference to the settings instance
* @throws OptionUnificationException
*/
Settings& addOption(Option* option);
/*!
* Returns true iff there is an Option with the specified longName and it has been set
* @return bool true if the option exists and has been set
* @throws InvalidArgumentException
*/
bool isSet(std::string const& longName) const {
return this->getByLongName(longName).getHasOptionBeenSet();
}
/*
* This generated a list of all registered options and their arguments together with descriptions and defaults.
* @return A std::string containing the help text, delimited by \n
*/
std::string getHelpText() const;
static Settings* getInstance();
friend class Destroyer;
@ -127,8 +142,8 @@ namespace settings {
* an instance manually, one should always use the
* newInstance() method.
*/
Settings(): optionsAccumulator(nullptr) {
this->optionsAccumulator = new OptionsAccumulator();
Settings() {
//
}
/*!
@ -138,16 +153,25 @@ namespace settings {
* The object is automatically destroyed when the program terminates by the destroyer.
*/
virtual ~Settings() {
delete this->optionsAccumulator;
this->instance = nullptr;
}
void parseCommandLine(int const argc, char const * const argv[]);
/*!
* @brief The registered options
*/
OptionsAccumulator* optionsAccumulator;
* The map holding the information regarding registered options and their types
*/
std::unordered_map<std::string, std::shared_ptr<Option>> options;
/*!
* The vector holding a pointer to all options
*/
std::vector<std::shared_ptr<Option>> optionPointers;
/*!
* The map holding the information regarding registered options and their short names
*/
std::unordered_map<std::string, std::string> shortNames;
/*!
* @brief actual instance of this class.
@ -166,6 +190,69 @@ namespace settings {
std::vector<std::string> argvToStringArray(int const argc, char const * const argv[]);
std::vector<bool> scanForOptions(std::vector<std::string> const& arguments);
bool checkArgumentSyntaxForOption(std::string const& argvString);
/*!
* Returns true IFF this accumulator contains an option with the specified longName.
*/
bool containsLongName(std::string const& longName) const {
return (this->options.find(storm::utility::StringHelper::stringToLower(longName)) != this->options.end());
}
/*!
* Returns true IFF this accumulator contains an option with the specified shortName.
*/
bool containsShortName(std::string const& shortName) const {
return (this->shortNames.find(storm::utility::StringHelper::stringToLower(shortName)) != this->shortNames.end());
}
/*!
* Returns a reference to the Option with the specified longName.
* Throws an Exception of Type InvalidArgumentException if there is no such Option.
*/
Option& getByLongName(std::string const& longName) const {
auto longNameIterator = this->options.find(storm::utility::StringHelper::stringToLower(longName));
if (longNameIterator == this->options.end()) {
throw storm::exceptions::IllegalArgumentException() << "This Accumulator does not contain an Option named \"" << longName << "\"!";
}
return *longNameIterator->second.get();
}
/*!
* Returns a pointer to the Option with the specified longName.
* Throws an Exception of Type InvalidArgumentException if there is no such Option.
* @throws InvalidArgumentException
*/
Option* getPtrByLongName(std::string const& longName) const {
auto longNameIterator = this->options.find(storm::utility::StringHelper::stringToLower(longName));
if (longNameIterator == this->options.end()) {
throw storm::exceptions::IllegalArgumentException() << "This Accumulator does not contain an Option named \"" << longName << "\"!";
}
return longNameIterator->second.get();
}
/*!
* Returns a reference to the Option with the specified shortName.
* Throws an Exception of Type InvalidArgumentException if there is no such Option.
*/
Option& getByShortName(std::string const& shortName) const {
auto shortNameIterator = this->shortNames.find(storm::utility::StringHelper::stringToLower(shortName));
if (shortNameIterator == this->shortNames.end()) {
throw storm::exceptions::IllegalArgumentException() << "This Accumulator does not contain an Option with ShortName \"" << shortName << "\"!";
}
return *(this->options.find(shortNameIterator->second)->second.get());
}
/*!
* Returns a pointer to the Option with the specified shortName.
* Throws an Exception of Type InvalidArgumentException if there is no such Option.
*/
Option* getPtrByShortName(std::string const& shortName) const {
auto shortNameIterator = this->shortNames.find(storm::utility::StringHelper::stringToLower(shortName));
if (shortNameIterator == this->shortNames.end()) {
throw storm::exceptions::IllegalArgumentException() << "This Accumulator does not contain an Option with ShortName \"" << shortName << "\"!";
}
return this->options.find(shortNameIterator->second)->second.get();
}
};
/*!

18
src/storm.cpp

@ -111,7 +111,7 @@ void initializeLogger() {
*/
void setUpFileLogging() {
storm::settings::Settings* s = storm::settings::Settings::getInstance();
log4cplus::SharedAppenderPtr fileLogAppender(new log4cplus::FileAppender(s->getString("logfile")));
log4cplus::SharedAppenderPtr fileLogAppender(new log4cplus::FileAppender(s->getOptionByLongName("logfile").getArgument(0).getValueAsString()));
fileLogAppender->setName("mainFileAppender");
fileLogAppender->setLayout(std::auto_ptr<log4cplus::Layout>(new log4cplus::PatternLayout("%-5p - %D{%H:%M:%S} (%r ms) - %F:%L: %m%n")));
logger.addAppender(fileLogAppender);
@ -144,31 +144,31 @@ void printHeader(const int argc, const char* argv[]) {
bool parseOptions(const int argc, const char* argv[]) {
storm::settings::Settings* s = nullptr;
try {
s = storm::settings::newInstance(argc, argv, nullptr);
} catch (storm::exceptions::InvalidSettingsException& e) {
storm::settings::Settings::parse(argc, argv);
} catch (storm::exceptions::OptionParserException& e) {
std::cout << "Could not recover from settings error: " << e.what() << "." << std::endl;
std::cout << std::endl << storm::settings::help;
std::cout << std::endl << storm::settings::Settings::getInstance()->getHelpText();
return false;
}
if (s->isSet("help")) {
std::cout << storm::settings::help;
std::cout << storm::settings::Settings::getInstance()->getHelpText();
return false;
}
if (s->isSet("verbose")) {
logger.getAppender("mainConsoleAppender")->setThreshold(log4cplus::INFO_LOG_LEVEL);
LOG4CPLUS_INFO(logger, "Enable verbose mode, log output gets printed to console.");
LOG4CPLUS_INFO(logger, "Enabled verbose mode, log output gets printed to console.");
}
if (s->isSet("debug")) {
logger.setLogLevel(log4cplus::DEBUG_LOG_LEVEL);
logger.getAppender("mainConsoleAppender")->setThreshold(log4cplus::DEBUG_LOG_LEVEL);
LOG4CPLUS_INFO(logger, "Enable very verbose mode, log output gets printed to console.");
LOG4CPLUS_INFO(logger, "Enabled very verbose mode, log output gets printed to console.");
}
if (s->isSet("trace")) {
logger.setLogLevel(log4cplus::TRACE_LOG_LEVEL);
logger.getAppender("mainConsoleAppender")->setThreshold(log4cplus::TRACE_LOG_LEVEL);
LOG4CPLUS_INFO(logger, "Enable trace mode, log output gets printed to console.");
LOG4CPLUS_INFO(logger, "Enabled trace mode, log output gets printed to console.");
}
if (s->isSet("logfile")) {
setUpFileLogging();
@ -199,7 +199,7 @@ void cleanUp() {
*/
storm::modelchecker::prctl::AbstractModelChecker<double>* createPrctlModelChecker(storm::models::Dtmc<double>& dtmc) {
// Create the appropriate model checker.
storm::settings::Settings* s = storm::settings::instance();
storm::settings::Settings* s = storm::settings::Settings::getInstance();
if (s->getString("matrixlib") == "gmm++") {
return new storm::modelchecker::prctl::SparseDtmcPrctlModelChecker<double>(dtmc, new storm::solver::GmmxxLinearEquationSolver<double>());
}

Loading…
Cancel
Save