Storm 1.13.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ExplicitQualitativeCheckResult.cpp
Go to the documentation of this file.
4
6
10
11namespace storm {
12namespace modelchecker {
13
14template<typename ValueType>
16 // Intentionally left empty.
17}
18
19template<typename ValueType>
21 // Intentionally left empty.
22}
23
24template<typename ValueType>
26 // Intentionally left empty.
27}
28
29template<typename ValueType>
31 boost::get<map_type>(truthValues)[state] = value;
32}
33
34template<typename ValueType>
36 // Intentionally left empty.
37}
38
39template<typename ValueType>
41 // Intentionally left empty.
42}
43
44template<typename ValueType>
45ExplicitQualitativeCheckResult<ValueType>::ExplicitQualitativeCheckResult(boost::variant<vector_type, map_type> const& truthValues,
46 std::optional<std::shared_ptr<storm::storage::Scheduler<ValueType>>> scheduler)
47 : truthValues(truthValues), scheduler(scheduler) {
48 // Intentionally left empty.
49}
50
51template<typename ValueType>
52ExplicitQualitativeCheckResult<ValueType>::ExplicitQualitativeCheckResult(boost::variant<vector_type, map_type>&& truthValues,
53 std::optional<std::shared_ptr<storm::storage::Scheduler<ValueType>>> scheduler)
54 : truthValues(std::move(truthValues)), scheduler(scheduler) {
55 // Intentionally left empty.
56}
57
58template<typename ValueType>
59std::unique_ptr<CheckResult> ExplicitQualitativeCheckResult<ValueType>::clone() const {
60 return std::make_unique<ExplicitQualitativeCheckResult<ValueType>>(this->truthValues);
61}
62
63template<typename ValueType>
64void ExplicitQualitativeCheckResult<ValueType>::performLogicalOperation(ExplicitQualitativeCheckResult<ValueType>& first, QualitativeCheckResult const& second,
65 bool logicalAnd) {
66 STORM_LOG_THROW(second.isExplicitQualitativeCheckResult(), storm::exceptions::InvalidOperationException,
67 "Cannot perform logical 'and' on check results of incompatible type.");
68 STORM_LOG_THROW(first.isResultForAllStates() == second.isResultForAllStates(), storm::exceptions::InvalidOperationException,
69 "Cannot perform logical 'and' on check results of incompatible type.");
70 ExplicitQualitativeCheckResult<ValueType> const& secondCheckResult = static_cast<ExplicitQualitativeCheckResult<ValueType> const&>(second);
71 if (first.isResultForAllStates()) {
72 if (logicalAnd) {
73 boost::get<vector_type>(first.truthValues) &= boost::get<vector_type>(secondCheckResult.truthValues);
74 } else {
75 boost::get<vector_type>(first.truthValues) |= boost::get<vector_type>(secondCheckResult.truthValues);
76 }
77 } else {
78 std::function<bool(bool, bool)> function = logicalAnd ? std::function<bool(bool, bool)>([](bool a, bool b) { return a && b; })
79 : std::function<bool(bool, bool)>([](bool a, bool b) { return a || b; });
80
81 map_type& map1 = boost::get<map_type>(first.truthValues);
82 map_type const& map2 = boost::get<map_type>(secondCheckResult.truthValues);
83 for (auto& element1 : map1) {
84 auto const& keyValuePair = map2.find(element1.first);
85 STORM_LOG_THROW(keyValuePair != map2.end(), storm::exceptions::InvalidOperationException,
86 "Cannot perform logical 'and' on check results of incompatible type.");
87 element1.second = function(element1.second, keyValuePair->second);
88 }
89
90 // Double-check that there are no entries in map2 that the current result does not have.
91 for (auto const& element2 : map2) {
92 auto const& keyValuePair = map1.find(element2.first);
93 STORM_LOG_THROW(keyValuePair != map1.end(), storm::exceptions::InvalidOperationException,
94 "Cannot perform logical 'and' on check results of incompatible type.");
95 }
96 }
97}
98
99template<typename ValueType>
101 performLogicalOperation(*this, other, true);
102 return *this;
103}
104
105template<typename ValueType>
107 performLogicalOperation(*this, other, false);
108 return *this;
109}
110
111template<typename ValueType>
113 if (this->isResultForAllStates()) {
114 return !boost::get<vector_type>(truthValues).empty();
115 } else {
116 for (auto& element : boost::get<map_type>(truthValues)) {
117 if (element.second) {
118 return true;
119 }
120 }
121 return false;
122 }
123}
124
125template<typename ValueType>
127 if (this->isResultForAllStates()) {
128 return boost::get<vector_type>(truthValues).full();
129 } else {
130 for (auto& element : boost::get<map_type>(truthValues)) {
131 if (!element.second) {
132 return false;
133 }
134 }
135 return true;
136 }
137}
138
139template<typename ValueType>
141 if (this->isResultForAllStates()) {
142 return boost::get<vector_type>(truthValues).getNumberOfSetBits();
143 } else {
144 uint64_t result = 0;
145 for (auto& element : boost::get<map_type>(truthValues)) {
146 if (element.second) {
147 ++result;
148 }
149 }
150 return result;
151 }
152}
153
154template<typename ValueType>
156 if (this->isResultForAllStates()) {
157 return boost::get<vector_type>(truthValues).get(state);
158 } else {
159 map_type const& map = boost::get<map_type>(truthValues);
160 auto const& keyValuePair = map.find(state);
161 STORM_LOG_THROW(keyValuePair != map.end(), storm::exceptions::InvalidOperationException, "Unknown key '" << state << "'.");
162 return keyValuePair->second;
163 }
164}
165
166template<typename ValueType>
168 return boost::get<vector_type>(truthValues);
169}
170
171template<typename ValueType>
173 return boost::get<map_type>(truthValues);
174}
175
176template<typename ValueType>
178 if (this->isResultForAllStates()) {
179 boost::get<vector_type>(truthValues).complement();
180 } else {
181 for (auto& element : boost::get<map_type>(truthValues)) {
182 element.second = !element.second;
183 }
184 }
185}
186
187template<typename ValueType>
189 return true;
190}
191
192template<typename ValueType>
194 return truthValues.which() == 0;
195}
196
197template<typename ValueType>
201
202template<typename ValueType>
203std::ostream& ExplicitQualitativeCheckResult<ValueType>::writeToStream(std::ostream& out) const {
204 if (this->isResultForAllStates()) {
205 vector_type const& vector = boost::get<vector_type>(truthValues);
206 bool allTrue = vector.full();
207 bool allFalse = !allTrue && vector.empty();
208 if (allTrue) {
209 out << "{true}";
210 } else if (allFalse) {
211 out << "{false}";
212 } else {
213 out << "{true, false}";
214 }
215 } else {
216 std::ios::fmtflags oldflags(std::cout.flags());
217 out << std::boolalpha;
218
219 map_type const& map = boost::get<map_type>(truthValues);
220 if (map.size() == 1) {
221 out << map.begin()->second;
222 } else {
223 bool allTrue = true;
224 bool allFalse = true;
225 for (auto const& entry : map) {
226 if (entry.second) {
227 allFalse = false;
228 } else {
229 allTrue = false;
230 }
231 }
232 if (allTrue) {
233 out << "{true}";
234 } else if (allFalse) {
235 out << "{false}";
236 } else {
237 out << "{true, false}";
238 }
239 }
240
241 std::cout.flags(oldflags);
242 }
243 return out;
244}
245
246template<typename ValueType>
248 STORM_LOG_THROW(filter.isExplicitQualitativeCheckResult(), storm::exceptions::InvalidOperationException,
249 "Cannot filter explicit check result with non-explicit filter.");
250 STORM_LOG_THROW(filter.isResultForAllStates(), storm::exceptions::InvalidOperationException, "Cannot filter check result with non-complete filter.");
252 vector_type const& filterTruthValues = explicitFilter.getTruthValuesVector();
253
254 if (this->isResultForAllStates()) {
255 map_type newMap;
256 for (auto element : filterTruthValues) {
257 newMap.emplace(element, this->getTruthValuesVector().get(element));
258 }
259 this->truthValues = newMap;
260 } else {
261 map_type const& map = boost::get<map_type>(truthValues);
262
263 map_type newMap;
264 for (auto const& element : map) {
265 if (filterTruthValues.get(element.first)) {
266 newMap.insert(element);
267 }
268 }
269
270 STORM_LOG_THROW(newMap.size() == filterTruthValues.getNumberOfSetBits(), storm::exceptions::InvalidOperationException,
271 "The check result fails to contain some results referred to by the filter.");
272
273 this->truthValues = newMap;
274 }
275}
276
277template<typename ValueType>
279 return static_cast<bool>(scheduler);
280}
281
282template<typename ValueType>
284 this->scheduler = std::move(scheduler);
285}
286
287template<typename ValueType>
289 STORM_LOG_THROW(this->hasScheduler(), storm::exceptions::InvalidOperationException, "Unable to retrieve non-existing scheduler.");
290 return *scheduler.value();
291}
292
293template<typename ValueType>
295 STORM_LOG_THROW(this->hasScheduler(), storm::exceptions::InvalidOperationException, "Unable to retrieve non-existing scheduler.");
296 return *scheduler.value();
297}
298
299template<typename JsonRationalType>
300void insertJsonEntry(storm::json<JsonRationalType>& json, uint64_t const& id, bool value,
301 std::optional<storm::storage::sparse::StateValuations> const& stateValuations = std::nullopt,
302 std::optional<storm::models::sparse::StateLabeling> const& stateLabels = std::nullopt) {
304 if (stateValuations) {
305 entry["s"] = stateValuations->template toJson<JsonRationalType>(id);
306 } else {
307 entry["s"] = id;
308 }
309 entry["v"] = value;
310 if (stateLabels) {
311 auto labs = stateLabels->getLabelsOfState(id);
312 entry["l"] = labs;
313 }
314 json.push_back(std::move(entry));
315}
316
317template<typename ValueType>
318template<typename JsonRationalType>
319storm::json<JsonRationalType> ExplicitQualitativeCheckResult<ValueType>::toJson(std::optional<storm::storage::sparse::StateValuations> const& stateValuations,
320 std::optional<storm::models::sparse::StateLabeling> const& stateLabels) const {
322 if (this->isResultForAllStates()) {
323 vector_type const& valuesAsVector = boost::get<vector_type>(truthValues);
324 for (uint64_t state = 0; state < valuesAsVector.size(); ++state) {
325 insertJsonEntry(result, state, valuesAsVector.get(state), stateValuations, stateLabels);
326 }
327 } else {
328 map_type const& valuesAsMap = boost::get<map_type>(truthValues);
329 for (auto const& stateValue : valuesAsMap) {
330 insertJsonEntry(result, stateValue.first, stateValue.second, stateValuations, stateLabels);
331 }
332 }
333 return result;
334}
335
336// Explicit template instantiations
338template storm::json<double> ExplicitQualitativeCheckResult<double>::toJson<double>(std::optional<storm::storage::sparse::StateValuations> const&,
339 std::optional<storm::models::sparse::StateLabeling> const&) const;
340
342 std::optional<storm::storage::sparse::StateValuations> const&, std::optional<storm::models::sparse::StateLabeling> const&) const;
343
346 std::optional<storm::storage::sparse::StateValuations> const&, std::optional<storm::models::sparse::StateLabeling> const&) const;
348 std::optional<storm::storage::sparse::StateValuations> const&, std::optional<storm::models::sparse::StateLabeling> const&) const;
349
352 std::optional<storm::storage::sparse::StateValuations> const&, std::optional<storm::models::sparse::StateLabeling> const&) const;
354 std::optional<storm::storage::sparse::StateValuations> const&, std::optional<storm::models::sparse::StateLabeling> const&) const;
355
357template storm::json<double> ExplicitQualitativeCheckResult<storm::Interval>::toJson<double>(std::optional<storm::storage::sparse::StateValuations> const&,
358 std::optional<storm::models::sparse::StateLabeling> const&) const;
360 std::optional<storm::storage::sparse::StateValuations> const&, std::optional<storm::models::sparse::StateLabeling> const&) const;
361
362} // namespace modelchecker
363} // namespace storm
ExplicitQualitativeCheckResult< ValueType > & asExplicitQualitativeCheckResult()
virtual bool isResultForAllStates() const
virtual bool isExplicitQualitativeCheckResult() const
storm::json< JsonRationalType > toJson(std::optional< storm::storage::sparse::StateValuations > const &stateValuations=std::nullopt, std::optional< storm::models::sparse::StateLabeling > const &stateLabels=std::nullopt) const
storm::storage::Scheduler< ValueType > const & getScheduler() const
std::map< storm::storage::sparse::state_type, bool > map_type
virtual void filter(QualitativeCheckResult const &filter) override
Filters the current result wrt.
bool operator[](storm::storage::sparse::state_type index) const
virtual QualitativeCheckResult & operator&=(QualitativeCheckResult const &other) override
virtual std::unique_ptr< CheckResult > clone() const override
virtual QualitativeCheckResult & operator|=(QualitativeCheckResult const &other) override
void setScheduler(std::unique_ptr< storm::storage::Scheduler< ValueType > > &&scheduler)
virtual std::ostream & writeToStream(std::ostream &out) const override
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
bool full() const
Retrieves whether all bits are set in this bit vector.
bool empty() const
Retrieves whether no bits are set to true in this bit vector.
uint64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
size_t size() const
Retrieves the number of bits this bit vector can store.
bool get(uint64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
This class defines which action is chosen in a particular state of a non-deterministic model.
Definition Scheduler.h:18
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
void insertJsonEntry(storm::json< JsonRationalType > &json, uint64_t const &id, bool value, std::optional< storm::storage::sparse::StateValuations > const &stateValuations=std::nullopt, std::optional< storm::models::sparse::StateLabeling > const &stateLabels=std::nullopt)
nlohmann::basic_json< std::map, std::vector, std::string, bool, int64_t, uint64_t, ValueType > json
Definition JsonForward.h:10