Skip to content

Commit

Permalink
Move p() and order() into Product
Browse files Browse the repository at this point in the history
These member functions
replace GetOrder and CalculateProbability non-member functions.

Issue #159
  • Loading branch information
rakhimov committed Feb 12, 2017
1 parent 95e5893 commit 51c6b83
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 30 deletions.
8 changes: 2 additions & 6 deletions src/fault_tree_analysis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,14 @@ void Print(const ProductContainer& products) {
std::cerr << std::flush;
}

double CalculateProbability(const Product& product) {
double Product::p() const {
double p = 1;
for (const Literal& literal : product) {
for (const Literal& literal : *this) {
p *= literal.complement ? 1 - literal.event.p() : literal.event.p();
}
return p;
}

int GetOrder(const Product& product) {
return product.empty() ? 1 : product.size();
}

FaultTreeAnalysis::FaultTreeAnalysis(const mef::Gate& root,
const Settings& settings)
: Analysis(settings),
Expand Down
28 changes: 10 additions & 18 deletions src/fault_tree_analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ class Product {
/// @returns The number of literals in the product.
int size() const { return data_.size(); }

/// @returns The order of the product.
///
/// @note An empty set indicates the Base/Unity set.
int order() const { return empty() ? 1 : size(); }

/// @returns The product of the literal probabilities.
///
/// @pre Events are initialized with expressions.
double p() const;

/// @returns A read proxy iterator that points to the first element.
auto begin() const {
return boost::make_transform_iterator(data_.begin(),
Expand Down Expand Up @@ -167,24 +177,6 @@ class ProductContainer {
/// @param[in] products Valid, unique collection of analysis results.
void Print(const ProductContainer& products);

/// Helper function to compute a Boolean product probability.
///
/// @param[in] product A set of literals.
///
/// @returns Product of probabilities of the literals.
///
/// @pre Events are initialized with expressions.
double CalculateProbability(const Product& product);

/// Helper function to determine order of a Boolean product.
///
/// @param[in] product A set of literals.
///
/// @returns The order of the product.
///
/// @note An empty set is assumed to indicate the Base/Unity set.
int GetOrder(const Product& product);

/// Fault tree analysis functionality.
/// The analysis must be done on
/// a validated and fully initialized fault trees.
Expand Down
6 changes: 3 additions & 3 deletions src/reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,13 @@ void Reporter::ReportResults(const std::string& ft_name,
double sum = 0; // Sum of probabilities for contribution calculations.
if (prob_analysis) {
for (const core::Product& product_set : fta.products())
sum += CalculateProbability(product_set);
sum += product_set.p();
}
for (const core::Product& product_set : fta.products()) {
XmlStreamElement product = sum_of_products.AddChild("product");
product.SetAttribute("order", GetOrder(product_set));
product.SetAttribute("order", product_set.order());
if (prob_analysis) {
double prob = CalculateProbability(product_set);
double prob = product_set.p();
product.SetAttribute("probability", prob);
product.SetAttribute("contribution", prob / sum);
}
Expand Down
5 changes: 2 additions & 3 deletions tests/risk_analysis_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ std::vector<int> RiskAnalysisTest::ProductDistribution() {
const FaultTreeAnalysis* fta =
analysis->fault_tree_analyses().begin()->second.get();
for (const Product& product : fta->products()) {
distr[GetOrder(product) - 1]++;
distr[product.order() - 1]++;
}
while (!distr.empty() && !distr.back())
distr.pop_back();
Expand All @@ -110,8 +110,7 @@ RiskAnalysisTest::product_probability() {
const FaultTreeAnalysis* fta =
analysis->fault_tree_analyses().begin()->second.get();
for (const Product& product : fta->products()) {
result_.product_probability.emplace(Convert(product),
CalculateProbability(product));
result_.product_probability.emplace(Convert(product), product.p());
}
}
return result_.product_probability;
Expand Down

0 comments on commit 51c6b83

Please sign in to comment.