Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ArgumentValidators.cpp
Go to the documentation of this file.
2
3#include <boost/algorithm/string/join.hpp>
4
5#include <sys/stat.h>
6
11#include "storm/io/file.h"
14
15namespace storm {
16namespace settings {
17
18template<typename ValueType>
19RangeArgumentValidator<ValueType>::RangeArgumentValidator(boost::optional<ValueType> const& lower, boost::optional<ValueType> const& upper, bool lowerIncluded,
20 bool upperIncluded)
21 : lower(lower), upper(upper), lowerIncluded(lowerIncluded), upperIncluded(upperIncluded) {
22 // Intentionally left empty.
23}
24
25template<typename ValueType>
26bool RangeArgumentValidator<ValueType>::isValid(ValueType const& value) {
27 bool result = true;
28 if (lower) {
29 if (lowerIncluded) {
30 result &= value >= lower.get();
31 } else {
32 result &= value > lower.get();
33 }
34 }
35 if (upper) {
36 if (upperIncluded) {
37 result &= value <= upper.get();
38 } else {
39 result &= value < upper.get();
40 }
41 }
42 return result;
43}
44
45template<typename ValueType>
47 std::stringstream stream;
48 stream << "in ";
49 if (lower) {
50 if (lowerIncluded) {
51 stream << "[";
52 } else {
53 stream << "(";
54 }
55 stream << lower.get();
56 } else {
57 stream << "(-inf";
58 }
59 stream << ", ";
60 if (upper) {
61 stream << upper.get();
62 if (upperIncluded) {
63 stream << "]";
64 } else {
65 stream << ")";
66 }
67 } else {
68 stream << "+inf)";
69 }
70
71 return stream.str();
72}
73
75 // Intentionally left empty.
76}
77
78bool FileValidator::isValid(std::string const& filename) {
79 if (mode == Mode::Exists) {
80 // First check existence as ifstream::good apparently also returns true for directories.
81 struct stat info;
82 stat(filename.c_str(), &info);
83 STORM_LOG_THROW(info.st_mode & S_IFREG, storm::exceptions::IllegalArgumentValueException,
84 "Unable to read from non-existing file '" << filename << "'.");
85
86 // Now that we know it's a file, we can check its readability.
87 STORM_LOG_THROW(storm::io::fileExistsAndIsReadable(filename), storm::exceptions::IllegalArgumentValueException,
88 "Unable to read from file '" << filename << "'.");
89
90 return true;
91 } else if (mode == Mode::Writable) {
92 std::ofstream filestream;
93 storm::io::openFile(filename, filestream, false, true);
94 STORM_LOG_THROW(filestream.is_open(), storm::exceptions::IllegalArgumentValueException, "Could not open file '" << filename << "' for writing.");
96 std::remove(filename.c_str());
97 return true;
98 }
99 return false;
100}
101
102std::string FileValidator::toString() const {
103 if (mode == Mode::Exists) {
104 return "existing file";
105 } else {
106 return "writable file";
107 }
108}
109
110MultipleChoiceValidator::MultipleChoiceValidator(std::vector<std::string> const& legalValues) : legalValues(legalValues) {
111 // Intentionally left empty.
112}
113
114bool MultipleChoiceValidator::isValid(std::string const& value) {
115 for (auto const& legalValue : legalValues) {
116 if (legalValue == value) {
117 return true;
118 }
119 }
120 return false;
121}
122
124 return "in {" + boost::join(legalValues, ", ") + "}";
125}
126
127std::shared_ptr<ArgumentValidator<int64_t>> ArgumentValidatorFactory::createIntegerRangeValidatorExcluding(int_fast64_t lowerBound, int_fast64_t upperBound) {
128 return createRangeValidatorExcluding<int64_t>(lowerBound, upperBound);
129}
130
131std::shared_ptr<ArgumentValidator<uint64_t>> ArgumentValidatorFactory::createUnsignedRangeValidatorExcluding(uint64_t lowerBound, uint64_t upperBound) {
132 return createRangeValidatorExcluding<uint64_t>(lowerBound, upperBound);
133}
134
135std::shared_ptr<ArgumentValidator<uint64_t>> ArgumentValidatorFactory::createUnsignedRangeValidatorIncluding(uint64_t lowerBound, uint64_t upperBound) {
136 return createRangeValidatorIncluding<uint64_t>(lowerBound, upperBound);
137}
138
139std::shared_ptr<ArgumentValidator<double>> ArgumentValidatorFactory::createDoubleRangeValidatorExcluding(double lowerBound, double upperBound) {
140 return createRangeValidatorExcluding<double>(lowerBound, upperBound);
141}
142
143std::shared_ptr<ArgumentValidator<double>> ArgumentValidatorFactory::createDoubleRangeValidatorIncluding(double lowerBound, double upperBound) {
144 return createRangeValidatorIncluding<double>(lowerBound, upperBound);
145}
146
147std::shared_ptr<ArgumentValidator<int64_t>> ArgumentValidatorFactory::createIntegerGreaterValidator(int_fast64_t lowerBound) {
148 return createGreaterValidator<int64_t>(lowerBound, false);
149}
150
151std::shared_ptr<ArgumentValidator<uint64_t>> ArgumentValidatorFactory::createUnsignedGreaterValidator(uint64_t lowerBound) {
152 return createGreaterValidator<uint64_t>(lowerBound, false);
153}
154
155std::shared_ptr<ArgumentValidator<double>> ArgumentValidatorFactory::createDoubleGreaterValidator(double lowerBound) {
156 return createGreaterValidator<double>(lowerBound, false);
157}
158
159std::shared_ptr<ArgumentValidator<int64_t>> ArgumentValidatorFactory::createIntegerGreaterEqualValidator(int_fast64_t lowerBound) {
160 return createGreaterValidator<int64_t>(lowerBound, true);
161}
162
163std::shared_ptr<ArgumentValidator<uint64_t>> ArgumentValidatorFactory::createUnsignedGreaterEqualValidator(uint64_t lowerBound) {
164 return createGreaterValidator<uint64_t>(lowerBound, true);
165}
166
167std::shared_ptr<ArgumentValidator<double>> ArgumentValidatorFactory::createDoubleGreaterEqualValidator(double lowerBound) {
168 return createGreaterValidator<double>(lowerBound, true);
169}
170
171std::shared_ptr<ArgumentValidator<std::string>> ArgumentValidatorFactory::createExistingFileValidator() {
172 return std::make_unique<FileValidator>(FileValidator::Mode::Exists);
173}
174
175std::shared_ptr<ArgumentValidator<std::string>> ArgumentValidatorFactory::createWritableFileValidator() {
176 return std::make_unique<FileValidator>(FileValidator::Mode::Writable);
177}
178
179std::shared_ptr<ArgumentValidator<std::string>> ArgumentValidatorFactory::createMultipleChoiceValidator(std::vector<std::string> const& choices) {
180 return std::make_unique<MultipleChoiceValidator>(choices);
181}
182
183template<typename ValueType>
184std::shared_ptr<ArgumentValidator<ValueType>> ArgumentValidatorFactory::createRangeValidatorExcluding(ValueType lowerBound, ValueType upperBound) {
185 return std::make_unique<RangeArgumentValidator<ValueType>>(lowerBound, upperBound, false, false);
186}
187
188template<typename ValueType>
189std::shared_ptr<ArgumentValidator<ValueType>> ArgumentValidatorFactory::createRangeValidatorIncluding(ValueType lowerBound, ValueType upperBound) {
190 return std::make_unique<RangeArgumentValidator<ValueType>>(lowerBound, upperBound, true, true);
191}
192
193template<typename ValueType>
194std::shared_ptr<ArgumentValidator<ValueType>> ArgumentValidatorFactory::createGreaterValidator(ValueType lowerBound, bool equalAllowed) {
195 return std::make_unique<RangeArgumentValidator<ValueType>>(lowerBound, boost::none, equalAllowed, false);
196}
197
198} // namespace settings
199} // namespace storm
static std::shared_ptr< ArgumentValidator< uint64_t > > createUnsignedRangeValidatorIncluding(uint64_t lowerBound, uint64_t upperBound)
static std::shared_ptr< ArgumentValidator< uint64_t > > createUnsignedRangeValidatorExcluding(uint64_t lowerBound, uint64_t upperBound)
static std::shared_ptr< ArgumentValidator< double > > createDoubleRangeValidatorExcluding(double lowerBound, double upperBound)
static std::shared_ptr< ArgumentValidator< double > > createDoubleGreaterValidator(double lowerBound)
static std::shared_ptr< ArgumentValidator< uint64_t > > createUnsignedGreaterValidator(uint64_t lowerBound)
static std::shared_ptr< ArgumentValidator< int64_t > > createIntegerGreaterEqualValidator(int_fast64_t lowerBound)
static std::shared_ptr< ArgumentValidator< int64_t > > createIntegerGreaterValidator(int_fast64_t lowerBound)
static std::shared_ptr< ArgumentValidator< double > > createDoubleGreaterEqualValidator(double lowerBound)
static std::shared_ptr< ArgumentValidator< uint64_t > > createUnsignedGreaterEqualValidator(uint64_t lowerBound)
static std::shared_ptr< ArgumentValidator< std::string > > createMultipleChoiceValidator(std::vector< std::string > const &choices)
static std::shared_ptr< ArgumentValidator< std::string > > createExistingFileValidator()
static std::shared_ptr< ArgumentValidator< int64_t > > createIntegerRangeValidatorExcluding(int_fast64_t lowerBound, int_fast64_t upperBound)
static std::shared_ptr< ArgumentValidator< double > > createDoubleRangeValidatorIncluding(double lowerBound, double upperBound)
static std::shared_ptr< ArgumentValidator< std::string > > createWritableFileValidator()
virtual std::string toString() const override
Retrieves a string representation of the valid values.
virtual bool isValid(std::string const &value) override
Checks whether the argument passes the validation.
MultipleChoiceValidator(std::vector< std::string > const &legalValues)
virtual bool isValid(std::string const &value) override
Checks whether the argument passes the validation.
virtual std::string toString() const override
Retrieves a string representation of the valid values.
RangeArgumentValidator(boost::optional< ValueType > const &lower, boost::optional< ValueType > const &upper, bool lowerIncluded, bool upperIncluded)
virtual bool isValid(ValueType const &value) override
Checks whether the argument passes the validation.
virtual std::string toString() const override
Retrieves a string representation of the valid values.
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
void closeFile(std::ofstream &stream)
Close the given file after writing.
Definition file.h:47
bool fileExistsAndIsReadable(std::string const &filename)
Tests whether the given file exists and is readable.
Definition file.h:66
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
SettingsType const & getModule()
Get module.
LabParser.cpp.
Definition cli.cpp:18