Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
DFT.h
Go to the documentation of this file.
1#pragma once
2
3#include <list>
4#include <map>
5#include <memory>
6#include <unordered_map>
7#include <vector>
8
9#include <boost/iterator/counting_iterator.hpp>
10
14#include "storm/utility/math.h"
15
21
22namespace storm::dft {
23
24// Forward declarations
25namespace builder {
26template<typename T>
27class DFTBuilder;
28} // namespace builder
29
30namespace utility {
31class RelevantEvents;
32} // namespace utility
33
34namespace storage {
35
36template<typename ValueType>
39 std::shared_ptr<storm::dft::storage::elements::DFTElement<ValueType>> const& b) const {
40 if (a->rank() == 0 && b->rank() == 0) {
41 return a->isConstant();
42 } else {
43 return a->rank() < b->rank();
44 }
45 }
46};
47
51template<typename ValueType>
52class DFT {
53 using DFTElementPointer = std::shared_ptr<storm::dft::storage::elements::DFTElement<ValueType>>;
54 using DFTElementCPointer = std::shared_ptr<storm::dft::storage::elements::DFTElement<ValueType> const>;
55 using DFTElementVector = std::vector<DFTElementPointer>;
56 using DFTGatePointer = std::shared_ptr<storm::dft::storage::elements::DFTGate<ValueType>>;
57 using DFTGateVector = std::vector<DFTGatePointer>;
58 using DFTStatePointer = std::shared_ptr<storm::dft::storage::DFTState<ValueType>>;
59
60 private:
61 DFTElementVector mElements;
62 size_t mNrOfBEs;
63 size_t mNrOfSpares;
64 size_t mNrRepresentatives;
65 size_t mTopLevelIndex;
66 size_t mStateVectorSize;
67 size_t mMaxSpareChildCount;
68 std::map<size_t, storm::dft::storage::DftModule> mModules;
69 std::vector<size_t> mBEs;
70 std::vector<size_t> mDependencies;
71 std::map<size_t, size_t> mRepresentants; // id element -> id representative
72 std::map<size_t, DFTLayoutInfo> mLayoutInfo;
73 mutable std::vector<size_t> mRelevantEvents;
74 std::map<size_t, bool> mDependencyInConflict;
75
76 public:
77 DFT(DFTElementVector const& elements, DFTElementPointer const& tle);
78
80
81 size_t generateStateInfo(DFTStateGenerationInfo& generationInfo, size_t id, storm::storage::BitVector& visited, size_t stateIndex) const;
82
83 size_t performStateGenerationInfoDFS(DFTStateGenerationInfo& generationInfo, std::queue<size_t>& visitQueue, storm::storage::BitVector& visited,
84 size_t stateIndex) const;
85
87
88 void copyElements(std::vector<size_t> elements, storm::dft::builder::DFTBuilder<ValueType> builder) const;
89
90 size_t stateBitVectorSize() const {
91 // Ensure multiple of 64
92 return (mStateVectorSize / 64 + (mStateVectorSize % 64 != 0)) * 64;
93 }
94
95 size_t nrElements() const {
96 return mElements.size();
97 }
98
99 size_t nrBasicElements() const {
100 return mNrOfBEs;
101 }
102
103 size_t nrDynamicElements() const;
104
105 size_t nrStaticElements() const;
106
107 size_t getTopLevelIndex() const {
108 return mTopLevelIndex;
109 }
110
114
115 size_t getMaxSpareChildCount() const {
116 return mMaxSpareChildCount;
117 }
118
119 std::vector<size_t> getSpareIndices() const {
120 std::vector<size_t> indices;
121 for (auto const& elem : mElements) {
122 if (elem->isSpareGate()) {
123 indices.push_back(elem->id());
124 }
125 }
126 return indices;
127 }
128
129 storm::dft::storage::DftModule const& module(size_t representativeId) const {
130 STORM_LOG_ASSERT(mModules.count(representativeId) > 0, "Representative not found.");
131 return mModules.at(representativeId);
132 }
133
134 std::vector<storm::dft::storage::DftModule> getSpareModules() const {
135 std::vector<storm::dft::storage::DftModule> spareModules;
136 for (auto const& pair : mModules) {
137 if (pair.first != mTopLevelIndex) {
138 spareModules.push_back(pair.second);
139 }
140 }
141 return spareModules;
142 }
143
144 bool isDependencyInConflict(size_t id) const {
145 STORM_LOG_ASSERT(isDependency(id), "Not a dependency.");
146 return mDependencyInConflict.at(id);
147 }
148
150 STORM_LOG_ASSERT(isDependency(id), "Not a dependency.");
151 mDependencyInConflict.at(id) = false;
152 }
153
154 std::vector<size_t> const& getDependencies() const {
155 return mDependencies;
156 }
157
158 std::vector<size_t> nonColdBEs() const {
159 std::vector<size_t> result;
160 for (DFTElementPointer elem : mElements) {
161 if (elem->isBasicElement()) {
162 std::shared_ptr<storm::dft::storage::elements::DFTBE<ValueType>> be =
163 std::static_pointer_cast<storm::dft::storage::elements::DFTBE<ValueType>>(elem);
164 if (be->canFail()) {
165 switch (be->beType()) {
167 result.push_back(be->id());
168 break;
170 auto beExp = std::static_pointer_cast<storm::dft::storage::elements::BEExponential<ValueType>>(be);
171 if (!beExp->isColdBasicElement()) {
172 result.push_back(be->id());
173 }
174 break;
175 }
176 default:
177 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "BE type '" << be->beType() << "' is not supported.");
178 }
179 }
180 }
181 }
182 return result;
183 }
184
189 DFTElementCPointer getElement(size_t index) const {
190 STORM_LOG_ASSERT(index < nrElements(), "Index invalid.");
191 return mElements[index];
192 }
193
194 bool isBasicElement(size_t index) const {
195 return getElement(index)->isBasicElement();
196 }
197
198 bool isGate(size_t index) const {
199 return getElement(index)->isGate();
200 }
201
202 bool isDependency(size_t index) const {
203 return getElement(index)->isDependency();
204 }
205
206 bool isRestriction(size_t index) const {
207 return getElement(index)->isRestriction();
208 }
209
210 std::shared_ptr<storm::dft::storage::elements::DFTBE<ValueType> const> getBasicElement(size_t index) const {
211 STORM_LOG_ASSERT(isBasicElement(index), "Element is no BE.");
212 return std::static_pointer_cast<storm::dft::storage::elements::DFTBE<ValueType> const>(mElements[index]);
213 }
214
215 DFTElementCPointer getTopLevelElement() const {
217 }
218
219 std::shared_ptr<storm::dft::storage::elements::DFTGate<ValueType> const> getGate(size_t index) const {
220 STORM_LOG_ASSERT(isGate(index), "Element is no gate.");
221 return std::static_pointer_cast<storm::dft::storage::elements::DFTGate<ValueType> const>(mElements[index]);
222 }
223
224 std::shared_ptr<storm::dft::storage::elements::DFTDependency<ValueType> const> getDependency(size_t index) const {
225 STORM_LOG_ASSERT(isDependency(index), "Element is no dependency.");
226 return std::static_pointer_cast<storm::dft::storage::elements::DFTDependency<ValueType> const>(mElements[index]);
227 }
228
229 std::shared_ptr<storm::dft::storage::elements::DFTRestriction<ValueType> const> getRestriction(size_t index) const {
230 STORM_LOG_ASSERT(isRestriction(index), "Element is no restriction.");
231 return std::static_pointer_cast<storm::dft::storage::elements::DFTRestriction<ValueType> const>(mElements[index]);
232 }
233
240 void setBEOrder(std::vector<size_t> const& bes) {
241 STORM_LOG_ASSERT(bes.size() == nrBasicElements(), "BEs must be the same size.");
242 STORM_LOG_ASSERT(std::all_of(bes.begin(), bes.end(), [this](size_t id) { return this->getElement(id)->isBasicElement(); }),
243 "All elements must be BEs.");
244 this->mBEs = bes;
245 }
246
252 std::vector<std::shared_ptr<storm::dft::storage::elements::DFTBE<ValueType> const>> getBasicElements() const {
253 std::vector<std::shared_ptr<storm::dft::storage::elements::DFTBE<ValueType> const>> elements;
254 for (size_t id : mBEs) {
255 auto element = getElement(id);
256 STORM_LOG_ASSERT(element->isBasicElement(), "Element is not a BE.");
257 elements.push_back(std::static_pointer_cast<storm::dft::storage::elements::DFTBE<ValueType> const>(element));
258 }
259 return elements;
260 }
261
262 bool canHaveNondeterminism() const;
263
264 uint64_t maxRank() const;
265
266 std::vector<DFT<ValueType>> topModularisation() const;
267
268 bool isRepresentative(size_t id) const {
269 for (auto const& parent : getElement(id)->parents()) {
270 if (parent->isSpareGate()) {
271 return true;
272 }
273 }
274 return false;
275 }
276
277 bool hasRepresentant(size_t id) const {
278 return mRepresentants.find(id) != mRepresentants.end();
279 }
280
281 size_t getRepresentant(size_t id) const {
282 STORM_LOG_ASSERT(hasRepresentant(id), "Element has no representant.");
283 return mRepresentants.find(id)->second;
284 }
285
286 bool hasFailed(DFTStatePointer const& state) const {
287 return state->hasFailed(mTopLevelIndex);
288 }
289
290 bool hasFailed(storm::storage::BitVector const& state, DFTStateGenerationInfo const& stateGenerationInfo) const {
291 return storm::dft::storage::DFTState<ValueType>::hasFailed(state, stateGenerationInfo.getStateIndex(mTopLevelIndex));
292 }
293
294 bool isFailsafe(DFTStatePointer const& state) const {
295 return state->isFailsafe(mTopLevelIndex);
296 }
297
298 bool isFailsafe(storm::storage::BitVector const& state, DFTStateGenerationInfo const& stateGenerationInfo) const {
299 return storm::dft::storage::DFTState<ValueType>::isFailsafe(state, stateGenerationInfo.getStateIndex(mTopLevelIndex));
300 }
301
311 size_t uses(storm::storage::BitVector const& state, DFTStateGenerationInfo const& stateGenerationInfo, size_t id) const {
312 size_t nrUsedChild = storm::dft::storage::DFTState<ValueType>::usesIndex(state, stateGenerationInfo, id);
313 if (nrUsedChild == getMaxSpareChildCount()) {
314 return id;
315 } else {
316 return getChild(id, nrUsedChild);
317 }
318 }
319
320 size_t getChild(size_t spareId, size_t nrUsedChild) const;
321
322 size_t getNrChild(size_t spareId, size_t childId) const;
323
324 std::string getElementsString() const;
325
326 std::string getInfoString() const;
327
328 std::string getModulesString() const;
329
330 std::string getElementsWithStateString(DFTStatePointer const& state) const;
331
332 std::string getStateString(DFTStatePointer const& state) const;
333
334 std::string getStateString(storm::storage::BitVector const& status, DFTStateGenerationInfo const& stateGenerationInfo, size_t id) const;
335
336 std::vector<size_t> immediateFailureCauses(size_t index) const;
337
338 std::vector<size_t> findModularisationRewrite() const;
339
340 void setElementLayoutInfo(size_t id, DFTLayoutInfo const& layoutInfo) {
341 mLayoutInfo[id] = layoutInfo;
342 }
343
344 DFTLayoutInfo const& getElementLayoutInfo(size_t id) const {
345 return mLayoutInfo.at(id);
346 }
347
348 void writeStatsToStream(std::ostream& stream) const;
349
354 std::set<size_t> getAllIds() const;
355
361 bool existsName(std::string const& name) const;
362
368 size_t getIndex(std::string const& name) const;
369
374 std::vector<size_t> const& getRelevantEvents() const;
375
381 void setRelevantEvents(storm::dft::utility::RelevantEvents const& relevantEvents, bool const allowDCForRelevant) const;
382
387 std::string getRelevantEventsString() const;
388
389 private:
390 bool elementIndicesCorrect() const {
391 for (size_t i = 0; i < mElements.size(); ++i) {
392 if (mElements[i]->id() != i)
393 return false;
394 }
395 return true;
396 }
397};
398
404std::set<storm::RationalFunctionVariable> getParameters(DFT<storm::RationalFunction> const& dft);
405
406} // namespace storage
407} // namespace storm::dft
Represents a Dynamic Fault Tree.
Definition DFT.h:52
size_t nrBasicElements() const
Definition DFT.h:99
bool hasFailed(DFTStatePointer const &state) const
Definition DFT.h:286
std::vector< size_t > nonColdBEs() const
Definition DFT.h:158
size_t getTopLevelIndex() const
Definition DFT.h:107
bool isRepresentative(size_t id) const
Definition DFT.h:268
void setBEOrder(std::vector< size_t > const &bes)
Set order of BEs.
Definition DFT.h:240
std::string getRelevantEventsString() const
Get a string containing the list of all relevant events.
Definition DFT.cpp:716
std::shared_ptr< storm::dft::storage::elements::DFTDependency< ValueType > const > getDependency(size_t index) const
Definition DFT.h:224
void setRelevantEvents(storm::dft::utility::RelevantEvents const &relevantEvents, bool const allowDCForRelevant) const
Set the relevance flag for all elements according to the given relevant events.
Definition DFT.cpp:690
std::shared_ptr< storm::dft::storage::elements::DFTBE< ValueType > const > getBasicElement(size_t index) const
Definition DFT.h:210
size_t nrDynamicElements() const
Definition DFT.cpp:459
size_t getNrChild(size_t spareId, size_t childId) const
Definition DFT.cpp:609
DFTLayoutInfo const & getElementLayoutInfo(size_t id) const
Definition DFT.h:344
std::string getModulesString() const
Definition DFT.cpp:527
std::set< size_t > getAllIds() const
Get Ids of all elements.
Definition DFT.cpp:669
bool hasRepresentant(size_t id) const
Definition DFT.h:277
std::vector< size_t > findModularisationRewrite() const
Definition DFT.cpp:637
void setDependencyNotInConflict(size_t id)
Definition DFT.h:149
DFTElementCPointer getElement(size_t index) const
Get a pointer to an element in the DFT.
Definition DFT.h:189
void setElementLayoutInfo(size_t id, DFTLayoutInfo const &layoutInfo)
Definition DFT.h:340
void writeStatsToStream(std::ostream &stream) const
Definition DFT.cpp:731
storm::dft::storage::elements::DFTElementType getTopLevelType() const
Definition DFT.h:111
std::string getElementsWithStateString(DFTStatePointer const &state) const
Definition DFT.cpp:536
DFTElementCPointer getTopLevelElement() const
Definition DFT.h:215
std::vector< size_t > const & getDependencies() const
Definition DFT.h:154
bool existsName(std::string const &name) const
Check whether an element with the given name exists.
Definition DFT.cpp:678
size_t nrElements() const
Definition DFT.h:95
std::shared_ptr< storm::dft::storage::elements::DFTGate< ValueType > const > getGate(size_t index) const
Definition DFT.h:219
uint64_t maxRank() const
Definition DFT.cpp:362
std::string getStateString(DFTStatePointer const &state) const
Definition DFT.cpp:559
size_t stateBitVectorSize() const
Definition DFT.h:90
size_t uses(storm::storage::BitVector const &state, DFTStateGenerationInfo const &stateGenerationInfo, size_t id) const
Return id of used child for a given spare gate.
Definition DFT.h:311
std::string getInfoString() const
Definition DFT.cpp:520
bool isDependencyInConflict(size_t id) const
Definition DFT.h:144
size_t getRepresentant(size_t id) const
Definition DFT.h:281
size_t generateStateInfo(DFTStateGenerationInfo &generationInfo, size_t id, storm::storage::BitVector &visited, size_t stateIndex) const
Definition DFT.cpp:276
bool isDependency(size_t index) const
Definition DFT.h:202
DFTStateGenerationInfo buildStateGenerationInfo(storm::dft::storage::DftSymmetries const &symmetries) const
Definition DFT.cpp:105
bool isGate(size_t index) const
Definition DFT.h:198
std::shared_ptr< storm::dft::storage::elements::DFTRestriction< ValueType > const > getRestriction(size_t index) const
Definition DFT.h:229
size_t nrStaticElements() const
Definition DFT.cpp:485
bool hasFailed(storm::storage::BitVector const &state, DFTStateGenerationInfo const &stateGenerationInfo) const
Definition DFT.h:290
std::vector< size_t > const & getRelevantEvents() const
Get all relevant events.
Definition DFT.cpp:711
std::vector< std::shared_ptr< storm::dft::storage::elements::DFTBE< ValueType > const > > getBasicElements() const
Return list of basic elements.
Definition DFT.h:252
size_t getIndex(std::string const &name) const
Get id for the given element name.
Definition DFT.cpp:683
std::vector< size_t > getSpareIndices() const
Definition DFT.h:119
bool isRestriction(size_t index) const
Definition DFT.h:206
size_t performStateGenerationInfoDFS(DFTStateGenerationInfo &generationInfo, std::queue< size_t > &visitQueue, storm::storage::BitVector &visited, size_t stateIndex) const
Definition DFT.cpp:298
bool isFailsafe(storm::storage::BitVector const &state, DFTStateGenerationInfo const &stateGenerationInfo) const
Definition DFT.h:298
void copyElements(std::vector< size_t > elements, storm::dft::builder::DFTBuilder< ValueType > builder) const
bool canHaveNondeterminism() const
Definition DFT.cpp:632
size_t getMaxSpareChildCount() const
Definition DFT.h:115
std::vector< size_t > immediateFailureCauses(size_t index) const
Definition DFT.cpp:622
size_t getChild(size_t spareId, size_t nrUsedChild) const
Definition DFT.cpp:603
std::vector< storm::dft::storage::DftModule > getSpareModules() const
Definition DFT.h:134
std::string getElementsString() const
Definition DFT.cpp:511
storm::dft::storage::DftModule const & module(size_t representativeId) const
Definition DFT.h:129
std::vector< DFT< ValueType > > topModularisation() const
Definition DFT.cpp:320
DFT< ValueType > optimize() const
Definition DFT.cpp:373
bool isFailsafe(DFTStatePointer const &state) const
Definition DFT.h:294
bool isBasicElement(size_t index) const
Definition DFT.h:194
bool hasFailed(size_t id) const
Definition DFTState.cpp:155
bool isFailsafe(size_t id) const
Definition DFTState.cpp:165
static uint_fast64_t usesIndex(storm::storage::BitVector const &state, DFTStateGenerationInfo const &stateGenerationInfo, size_t id)
Returns the index of the used child for a spare gate.
Definition DFTState.cpp:441
Represents a module/subtree in a DFT.
Definition DftModule.h:18
Abstract base class for basic events (BEs) in DFTs.
Definition DFTBE.h:14
Abstract base class for DFT elements.
Definition DFTElement.h:39
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:18
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
DFTElementType
Element types in a DFT.
std::set< storm::RationalFunctionVariable > getParameters(DFT< storm::RationalFunction > const &dft)
Get all rate/probability parameters occurring in the DFT.
Definition DFT.cpp:825
bool operator()(std::shared_ptr< storm::dft::storage::elements::DFTElement< ValueType > > const &a, std::shared_ptr< storm::dft::storage::elements::DFTElement< ValueType > > const &b) const
Definition DFT.h:38