Skip to content

Commit

Permalink
Use specific SettingsError instead of InvalidArg
Browse files Browse the repository at this point in the history
For the analysis settings,
it is more specific to use custom exception class.

Issue #219
  • Loading branch information
rakhimov committed Oct 15, 2017
1 parent 791fa51 commit a5af9c0
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void Config::GatherOptions(const xml::Element& root) {
} else if (name == "limits") {
SetLimits(option_group);
}
} catch (InvalidArgument& err) {
} catch (SettingsError& err) {
err << boost::errinfo_at_line(option_group.line());
throw;
}
Expand All @@ -118,7 +118,7 @@ void Config::GatherOptions(const xml::Element& root) {
options_element->child("analysis")) {
try {
SetAnalysis(*analysis_group);
} catch (InvalidArgument& err) {
} catch (SettingsError& err) {
err << boost::errinfo_at_line(analysis_group->line());
throw;
}
Expand Down
2 changes: 1 addition & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Config {
/// @param[in] config_file The path to an XML file with configurations.
///
/// @throws ValidationError The configurations have problems.
/// @throws InvalidArgument Settings values contain errors.
/// @throws SettingsError Settings values contain errors.
/// @throws IOError The file is not accessible.
explicit Config(const std::string& config_file);

Expand Down
5 changes: 5 additions & 0 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class Error : virtual public std::exception, virtual public boost::exception {
std::string thrown_; ///< The message to throw with the prefix.
};

/// The error in analysis settings.
struct SettingsError : public Error {
using Error::Error;
};

/// For validating input parameters or user arguments.
struct ValidationError : public Error {
using Error::Error;
Expand Down
2 changes: 1 addition & 1 deletion src/scram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ int ParseArguments(int argc, char* argv[], po::variables_map* vm) {
/// @param[in] vm Variables map of program options.
/// @param[in,out] settings Pre-configured or default settings.
///
/// @throws InvalidArgument The indication of an error in arguments.
/// @throws SettingsError The indication of an error in arguments.
/// @throws std::exception vm does not contain a required option.
/// At least defaults are expected.
void ConstructSettings(const po::variables_map& vm,
Expand Down
36 changes: 18 additions & 18 deletions src/settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ Settings& Settings::algorithm(Algorithm value) noexcept {
Settings& Settings::algorithm(boost::string_ref value) {
auto it = boost::find(kAlgorithmToString, value);
if (it == std::end(kAlgorithmToString))
throw InvalidArgument("The qualitative analysis algorithm '" +
value.to_string() + "' is not recognized.");
throw SettingsError("The qualitative analysis algorithm '" +
value.to_string() + "' is not recognized.");
return algorithm(
static_cast<Algorithm>(std::distance(kAlgorithmToString, it)));
}

Settings& Settings::approximation(Approximation value) {
if (value != Approximation::kNone && prime_implicants_)
throw InvalidArgument(
throw SettingsError(
"Prime implicants require no quantitative approximation.");
approximation_ = value;
return *this;
Expand All @@ -62,15 +62,15 @@ Settings& Settings::approximation(Approximation value) {
Settings& Settings::approximation(boost::string_ref value) {
auto it = boost::find(kApproximationToString, value);
if (it == std::end(kApproximationToString))
throw InvalidArgument("The probability approximation '" +
value.to_string() + "'is not recognized.");
throw SettingsError("The probability approximation '" +
value.to_string() + "'is not recognized.");
return approximation(
static_cast<Approximation>(std::distance(kApproximationToString, it)));
}

Settings& Settings::prime_implicants(bool flag) {
if (flag && algorithm_ != Algorithm::kBdd)
throw InvalidArgument("Prime implicants can only be calculated with BDD");
throw SettingsError("Prime implicants can only be calculated with BDD");

prime_implicants_ = flag;
if (prime_implicants_)
Expand All @@ -80,73 +80,73 @@ Settings& Settings::prime_implicants(bool flag) {

Settings& Settings::limit_order(int order) {
if (order < 0)
throw InvalidArgument("The limit on the order of products "
"cannot be less than 0.");
throw SettingsError("The limit on the order of products "
"cannot be less than 0.");
limit_order_ = order;
return *this;
}

Settings& Settings::cut_off(double prob) {
if (prob < 0 || prob > 1)
throw InvalidArgument("The cut-off probability cannot be negative or"
" more than 1.");
throw SettingsError("The cut-off probability cannot be negative or"
" more than 1.");
cut_off_ = prob;
return *this;
}

Settings& Settings::num_trials(int n) {
if (n < 1)
throw InvalidArgument("The number of trials cannot be less than 1.");
throw SettingsError("The number of trials cannot be less than 1.");

num_trials_ = n;
return *this;
}

Settings& Settings::num_quantiles(int n) {
if (n < 1)
throw InvalidArgument("The number of quantiles cannot be less than 1.");
throw SettingsError("The number of quantiles cannot be less than 1.");

num_quantiles_ = n;
return *this;
}

Settings& Settings::num_bins(int n) {
if (n < 1)
throw InvalidArgument("The number of bins cannot be less than 1.");
throw SettingsError("The number of bins cannot be less than 1.");

num_bins_ = n;
return *this;
}

Settings& Settings::seed(int s) {
if (s < 0)
throw InvalidArgument("The seed for PRNG cannot be negative.");
throw SettingsError("The seed for PRNG cannot be negative.");

seed_ = s;
return *this;
}

Settings& Settings::mission_time(double time) {
if (time < 0)
throw InvalidArgument("The mission time cannot be negative.");
throw SettingsError("The mission time cannot be negative.");

mission_time_ = time;
return *this;
}

Settings& Settings::time_step(double time) {
if (time < 0)
throw InvalidArgument("The time step cannot be negative.");
throw SettingsError("The time step cannot be negative.");
if (!time && safety_integrity_levels_)
throw InvalidArgument("The time step cannot be disabled for the SIL");
throw SettingsError("The time step cannot be disabled for the SIL");

time_step_ = time;
return *this;
}

Settings& Settings::safety_integrity_levels(bool flag) {
if (flag && !time_step_)
throw InvalidArgument("The time step is not set for the SIL calculations.");
throw SettingsError("The time step is not set for the SIL calculations.");

safety_integrity_levels_ = flag;
if (safety_integrity_levels_)
Expand Down
26 changes: 13 additions & 13 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Settings {
///
/// @param[in] value The string representation of the algorithm.
///
/// @throws InvalidArgument The algorithm is not recognized.
/// @throws SettingsError The algorithm is not recognized.
///
/// @returns Reference to this object.
Settings& algorithm(boost::string_ref value);
Expand All @@ -92,7 +92,7 @@ class Settings {
///
/// @returns Reference to this object.
///
/// @throws InvalidArgument The approximation is not recognized
/// @throws SettingsError The approximation is not recognized
/// or inappropriate for analysis.
/// @{
Settings& approximation(Approximation value);
Expand All @@ -113,7 +113,7 @@ class Settings {
///
/// @returns Reference to this object.
///
/// @throws InvalidArgument The request is not relevant to the algorithm.
/// @throws SettingsError The request is not relevant to the algorithm.
Settings& prime_implicants(bool flag);

/// @returns The limit on the size of products.
Expand All @@ -125,7 +125,7 @@ class Settings {
///
/// @returns Reference to this object.
///
/// @throws InvalidArgument The number is less than 0.
/// @throws SettingsError The number is less than 0.
Settings& limit_order(int order);

/// @returns The minimum required probability for products.
Expand All @@ -138,7 +138,7 @@ class Settings {
///
/// @returns Reference to this object.
///
/// @throws InvalidArgument The probability is not in the [0, 1] range.
/// @throws SettingsError The probability is not in the [0, 1] range.
Settings& cut_off(double prob);

/// @returns The number of trials for Monte-Carlo simulations.
Expand All @@ -150,7 +150,7 @@ class Settings {
///
/// @returns Reference to this object.
///
/// @throws InvalidArgument The number is less than 1.
/// @throws SettingsError The number is less than 1.
Settings& num_trials(int n);

/// @returns The number of quantiles for distributions.
Expand All @@ -162,7 +162,7 @@ class Settings {
///
/// @returns Reference to this object.
///
/// @throws InvalidArgument The number is less than 1.
/// @throws SettingsError The number is less than 1.
Settings& num_quantiles(int n);

/// @returns The number of bins for histograms.
Expand All @@ -174,7 +174,7 @@ class Settings {
///
/// @returns Reference to this object.
///
/// @throws InvalidArgument The number is less than 1.
/// @throws SettingsError The number is less than 1.
Settings& num_bins(int n);

/// @returns The seed of the pseudo-random number generator.
Expand All @@ -186,7 +186,7 @@ class Settings {
///
/// @returns Reference to this object.
///
/// @throws InvalidArgument The number is negative.
/// @throws SettingsError The number is negative.
Settings& seed(int s);

/// @returns The length time of the system under risk.
Expand All @@ -198,7 +198,7 @@ class Settings {
///
/// @returns Reference to this object.
///
/// @throws InvalidArgument The time value is negative.
/// @throws SettingsError The time value is negative.
Settings& mission_time(double time);

/// @returns The time step in hours for probability analyses.
Expand All @@ -212,8 +212,8 @@ class Settings {
///
/// @returns Reference to this object.
///
/// @throws InvalidArgument The time value is negative.
/// @throws InvalidArgument The time step is being disabled (value 0)
/// @throws SettingsError The time value is negative.
/// @throws SettingsError The time step is being disabled (value 0)
/// while the SIL metrics are requested.
Settings& time_step(double time);

Expand Down Expand Up @@ -246,7 +246,7 @@ class Settings {
///
/// @returns Reference to this object.
///
/// @throws InvalidArgument The flag is True, but no time-step is set.
/// @throws SettingsError The flag is True, but no time-step is set.
Settings& safety_integrity_levels(bool flag);

/// @returns true if importance analysis is requested.
Expand Down
38 changes: 19 additions & 19 deletions tests/settings_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,35 @@ namespace test {
TEST(SettingsTest, IncorrectSetup) {
Settings s;
// Incorrect algorithm.
EXPECT_THROW(s.algorithm("the-best"), InvalidArgument);
EXPECT_THROW(s.algorithm("the-best"), SettingsError);
// Incorrect approximation argument.
EXPECT_THROW(s.approximation("approx"), InvalidArgument);
EXPECT_THROW(s.approximation("approx"), SettingsError);
// Incorrect limit order for products.
EXPECT_THROW(s.limit_order(-1), InvalidArgument);
EXPECT_THROW(s.limit_order(-1), SettingsError);
// Incorrect cut-off probability.
EXPECT_THROW(s.cut_off(-1), InvalidArgument);
EXPECT_THROW(s.cut_off(10), InvalidArgument);
EXPECT_THROW(s.cut_off(-1), SettingsError);
EXPECT_THROW(s.cut_off(10), SettingsError);
// Incorrect number of trials.
EXPECT_THROW(s.num_trials(-10), InvalidArgument);
EXPECT_THROW(s.num_trials(0), InvalidArgument);
EXPECT_THROW(s.num_trials(-10), SettingsError);
EXPECT_THROW(s.num_trials(0), SettingsError);
// Incorrect number of quantiles.
EXPECT_THROW(s.num_quantiles(-10), InvalidArgument);
EXPECT_THROW(s.num_quantiles(0), InvalidArgument);
EXPECT_THROW(s.num_quantiles(-10), SettingsError);
EXPECT_THROW(s.num_quantiles(0), SettingsError);
// Incorrect number of bins.
EXPECT_THROW(s.num_bins(-10), InvalidArgument);
EXPECT_THROW(s.num_bins(0), InvalidArgument);
EXPECT_THROW(s.num_bins(-10), SettingsError);
EXPECT_THROW(s.num_bins(0), SettingsError);
// Incorrect seed.
EXPECT_THROW(s.seed(-1), InvalidArgument);
EXPECT_THROW(s.seed(-1), SettingsError);
// Incorrect mission time.
EXPECT_THROW(s.mission_time(-10), InvalidArgument);
EXPECT_THROW(s.mission_time(-10), SettingsError);
// Incorrect time step.
EXPECT_THROW(s.time_step(-1), InvalidArgument);
EXPECT_THROW(s.time_step(-1), SettingsError);
// The time step is not set for the SIL calculations.
EXPECT_THROW(s.safety_integrity_levels(true), InvalidArgument);
EXPECT_THROW(s.safety_integrity_levels(true), SettingsError);
// Disable time step while the SIL is requested.
EXPECT_NO_THROW(s.time_step(1));
EXPECT_NO_THROW(s.safety_integrity_levels(true));
EXPECT_THROW(s.time_step(0), InvalidArgument);
EXPECT_THROW(s.time_step(0), SettingsError);
}

TEST(SettingsTest, CorrectSetup) {
Expand Down Expand Up @@ -114,14 +114,14 @@ TEST(SettingsTest, SetupForPrimeImplicants) {
Settings s;
// Incorrect request for prime implicants.
EXPECT_NO_THROW(s.algorithm("mocus"));
EXPECT_THROW(s.prime_implicants(true), InvalidArgument);
EXPECT_THROW(s.prime_implicants(true), SettingsError);
// Correct request for prime implicants.
ASSERT_NO_THROW(s.algorithm("bdd"));
ASSERT_NO_THROW(s.prime_implicants(true));
// Prime implicants with quantitative approximations.
EXPECT_NO_THROW(s.approximation("none"));
EXPECT_THROW(s.approximation("rare-event"), InvalidArgument);
EXPECT_THROW(s.approximation("mcub"), InvalidArgument);
EXPECT_THROW(s.approximation("rare-event"), SettingsError);
EXPECT_THROW(s.approximation("mcub"), SettingsError);
}

} // namespace test
Expand Down

0 comments on commit a5af9c0

Please sign in to comment.