Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Valuations.cpp
Go to the documentation of this file.
2
3#include <boost/algorithm/string/join.hpp>
4
9
10namespace storm::storage::sparse {
11
12Valuations::Valuations(ValuationClassDescription const umbValuationDescription, std::shared_ptr<storm::expressions::ExpressionManager const> const& manager,
13 uint64_t const numEntities)
14 : umbValuations(std::make_unique<ValuationsStorage>(umbValuationDescription, manager)) {
15 umbValuations->resize(numEntities);
16}
17
18Valuations::Valuations(ValuationsStorage&& umbValuations) : umbValuations(std::make_unique<ValuationsStorage>(std::move(umbValuations))) {
19 // Intentionally empty
20}
21
22// The type ValuationsStorage is incomplete (forward declared) in the header file and complete in this cpp file.
23// The member variable Valuations::umbValuations is of type std::unique_ptr<ValuationsStorage>.
24// To re-assign or destruct umbValuations, the type ValuationsStorage must be complete (because ValuationsStorage::~ValuationsStorage must be invoked).
25// We therefore must define the following destructors / constructors / assignment operators in the .cpp file, not the header file.
26Valuations::~Valuations() = default;
27Valuations::Valuations(Valuations&& other) = default;
29
31 if (other.umbValuations) {
32 // Create a deep copy
33 umbValuations = std::make_unique<ValuationsStorage>(*other.umbValuations);
34 }
35}
36
38 if (this != &other) {
39 if (other.umbValuations) {
40 // Create a deep copy
41 umbValuations = std::make_unique<ValuationsStorage>(*other.umbValuations);
42 } else {
43 umbValuations.reset();
44 }
45 }
46 return *this;
47}
48
50 return umbValuations->getManager();
51}
52
54 return *umbValuations;
55}
57 return *umbValuations;
58}
59
61 return umbValuations->size();
62}
63
64std::set<storm::expressions::Variable> Valuations::getAllVariables() const {
65 return umbValuations->getAllVariables();
66}
67
68bool Valuations::entityHasVariable(uint64_t entity, const storm::expressions::Variable& variable) const {
69 return umbValuations->entityHasVariable(entity, variable);
70}
71
72bool Valuations::getBooleanValue(uint64_t const entity, storm::expressions::Variable const& booleanVariable) const {
73 return umbValuations->readValue<bool>(entity, booleanVariable);
74}
75
76std::optional<bool> Valuations::getOptionalBooleanValue(uint64_t const entity, storm::expressions::Variable const& booleanVariable) const {
77 if (!entityHasVariable(entity, booleanVariable)) {
78 return std::nullopt;
79 }
80 std::optional<bool> result;
81 umbValuations->readCallback<std::nullopt_t, bool>(entity, booleanVariable, [&result](auto, auto const&, auto&& value) {
82 using T = std::remove_cvref_t<decltype(value)>;
83 if constexpr (std::is_same_v<T, bool>) {
84 result = value;
85 }
86 // std::nullopt_t means the optional variable's presence bit is unset → result stays std::nullopt
87 });
88 return result;
89}
90
91int64_t Valuations::getInt64Value(uint64_t const entity, storm::expressions::Variable const& integerVariable) const {
92 return umbValuations->readValue<int64_t>(entity, integerVariable);
93}
94
95std::optional<int64_t> Valuations::getOptionalInt64Value(uint64_t const entity, storm::expressions::Variable const& integerVariable) const {
96 if (!entityHasVariable(entity, integerVariable)) {
97 return std::nullopt;
98 }
99 std::optional<int64_t> result;
100 umbValuations->readCallback<std::nullopt_t, int64_t>(entity, integerVariable, [&result](auto, auto const&, auto&& value) {
101 using T = std::remove_cvref_t<decltype(value)>;
102 if constexpr (std::is_same_v<T, int64_t>) {
103 result = value;
104 }
105 });
106 return result;
107}
108
109double Valuations::getDoubleValue(uint64_t const entity, storm::expressions::Variable const& doubleVariable) const {
110 return umbValuations->readValue<double>(entity, doubleVariable);
111}
112
113std::optional<double> Valuations::getOptionalDoubleValue(uint64_t const entity, storm::expressions::Variable const& doubleVariable) const {
114 if (!entityHasVariable(entity, doubleVariable)) {
115 return std::nullopt;
116 }
117 std::optional<double> result;
118 umbValuations->readCallback<std::nullopt_t, double>(entity, doubleVariable, [&result](auto, auto const&, auto&& value) {
119 using T = std::remove_cvref_t<decltype(value)>;
120 if constexpr (std::is_same_v<T, double>) {
121 result = value;
122 }
123 });
124 return result;
125}
126
127storm::RationalNumber Valuations::getRationalValue(uint64_t const entity, storm::expressions::Variable const& rationalVariable) const {
128 return umbValuations->readValue<storm::RationalNumber>(entity, rationalVariable);
129}
130
131std::optional<storm::RationalNumber> Valuations::getOptionalRationalValue(uint64_t const entity, storm::expressions::Variable const& rationalVariable) const {
132 if (!entityHasVariable(entity, rationalVariable)) {
133 return std::nullopt;
134 }
135 std::optional<storm::RationalNumber> result;
136 umbValuations->readCallback<std::nullopt_t, storm::RationalNumber>(entity, rationalVariable, [&result](auto, auto const&, auto&& value) {
137 using T = std::remove_cvref_t<decltype(value)>;
138 if constexpr (std::is_same_v<T, storm::RationalNumber>) {
139 result = std::forward<decltype(value)>(value);
140 }
141 });
142 return result;
143}
144
145std::string Valuations::getStringValue(uint64_t const entity, storm::expressions::Variable const& stringVariable) const {
146 return umbValuations->readValue<std::string>(entity, stringVariable);
147}
148
149std::optional<std::string> Valuations::getOptionalStringValue(uint64_t const entity, storm::expressions::Variable const& stringVariable) const {
150 if (!entityHasVariable(entity, stringVariable)) {
151 return std::nullopt;
152 }
153 std::optional<std::string> result;
154 umbValuations->readCallback<std::nullopt_t, std::string>(entity, stringVariable, [&result](auto, auto const&, auto&& value) {
155 using T = std::remove_cvref_t<decltype(value)>;
156 if constexpr (std::is_same_v<T, std::string>) {
157 result = std::forward<decltype(value)>(value);
158 }
159 });
160 return result;
161}
162
163template<typename RationalValueType>
165 umbValuations->setValuesInEvaluator(entity, evaluator);
166}
167template void Valuations::setValuesInEvaluator(uint64_t entity, storm::expressions::ExpressionEvaluator<double>& evaluator) const;
169
171 STORM_LOG_ASSERT(booleanVariable.hasBooleanType(), "Variable " << booleanVariable.getName() << " is not of boolean type.");
173 umbValuations->readCallback<bool>(booleanVariable, [&result](auto const entity, auto, bool value) {
174 if (value) {
175 result.set(entity);
176 }
177 });
178 return result;
179}
180
181std::vector<int64_t> Valuations::getInt64Values(storm::expressions::Variable const& integerVariable) const {
182 STORM_LOG_ASSERT(integerVariable.hasIntegerType(), "Variable " << integerVariable.getName() << " is not of integer type.");
183 std::vector<int64_t> result;
184 result.reserve(getNumberOfEntities());
185 umbValuations->readCallback<int64_t>(integerVariable, [&result](auto const entity, auto, int64_t value) {
186 STORM_LOG_ASSERT(entity == result.size(), "Entities processed in unexpected order.");
187 result.push_back(value);
188 });
189 return result;
190}
191
192std::vector<double> Valuations::getDoubleValues(storm::expressions::Variable const& doubleVariable) const {
193 STORM_LOG_ASSERT(doubleVariable.hasRationalType(), "Variable " << doubleVariable.getName() << " is not of rational type.");
194 std::vector<double> result;
195 result.reserve(getNumberOfEntities());
196 umbValuations->readCallback<double>(doubleVariable, [&result](auto const entity, auto, double value) {
197 STORM_LOG_ASSERT(entity == result.size(), "Entities processed in unexpected order.");
198 result.push_back(value);
199 });
200 return result;
201}
202
203std::vector<storm::RationalNumber> Valuations::getRationalValues(storm::expressions::Variable const& rationalVariable) const {
204 STORM_LOG_ASSERT(rationalVariable.hasRationalType(), "Variable " << rationalVariable.getName() << " is not of rational type.");
205 std::vector<storm::RationalNumber> result;
206 result.reserve(getNumberOfEntities());
207 umbValuations->readCallback<storm::RationalNumber>(rationalVariable, [&result](auto const entity, auto, storm::RationalNumber value) {
208 STORM_LOG_ASSERT(entity == result.size(), "Entities processed in unexpected order.");
209 result.push_back(std::move(value));
210 });
211 return result;
212}
213
214std::vector<std::string> Valuations::getStringValues(storm::expressions::Variable const& stringVariable) const {
215 STORM_LOG_ASSERT(stringVariable.hasStringType(), "Variable " << stringVariable.getName() << " is not of rational type.");
216 std::vector<std::string> result;
217 result.reserve(getNumberOfEntities());
218 umbValuations->readCallback<std::string>(stringVariable, [&result](auto const entity, auto, std::string&& value) {
219 STORM_LOG_ASSERT(entity == result.size(), "Entities processed in unexpected order.");
220 result.push_back(std::move(value));
221 });
222 return result;
223}
224
225std::string Valuations::toString(uint64_t const entity, bool const pretty,
226 std::optional<std::set<storm::expressions::Variable>> const& selectedVariables) const {
227 std::vector<std::string> assignments;
228 umbValuations->readCallback(entity, [pretty, &selectedVariables, &assignments](auto, auto const& var, auto&& value) {
229 if (selectedVariables && !selectedVariables->contains(var)) {
230 return;
231 }
232 using ValueType = std::remove_cvref_t<decltype(value)>;
233 if constexpr (std::is_same_v<ValueType, std::nullopt_t>) {
234 assignments.push_back(pretty ? (var.getName() + "=none") : "none");
235 } else if constexpr (std::is_same_v<ValueType, bool>) {
236 if (pretty) {
237 assignments.push_back(value ? "" : "!" + var.getName());
238 } else {
239 assignments.push_back(value ? "true" : "false");
240 }
241 } else {
242 std::stringstream stream;
243 if (pretty) {
244 stream << var.getName() << "=";
245 }
246 stream << value;
247 assignments.push_back(stream.str());
248 }
249 });
250 return "[" + boost::join(assignments, pretty ? "\t& " : "\t") + "]";
251}
252
253template<typename JsonRationalType>
254storm::json<JsonRationalType> Valuations::toJson(uint64_t const entity, std::optional<std::set<storm::expressions::Variable>> const& selectedVariables) const {
256 umbValuations->readCallback<bool, uint64_t, int64_t, double, std::string>(entity, [&selectedVariables, &result](auto, auto const& var, auto&& value) {
257 if (selectedVariables && !selectedVariables->contains(var)) {
258 return;
259 }
260 result[var.getName()] = value;
261 });
262 return result;
263}
264
266 return Valuations(umbValuations->selectEntities(selectedEntities));
267}
268
269Valuations Valuations::selectEntities(std::vector<uint64_t> const& selectedEntities) const {
270 return Valuations(umbValuations->selectEntities(selectedEntities));
271}
272
273std::size_t Valuations::hash() const {
274 return umbValuations->hash();
275}
276
277template storm::json<double> Valuations::toJson<double>(uint64_t const, std::optional<std::set<storm::expressions::Variable>> const&) const;
279 std::optional<std::set<storm::expressions::Variable>> const&) const;
280
281} // namespace storm::storage::sparse
This class is responsible for managing a set of typed variables and all expressions using these varia...
bool hasBooleanType() const
Checks whether the variable is of boolean type.
Definition Variable.cpp:59
bool hasStringType() const
Checks whether the variable is of string type.
Definition Variable.cpp:79
bool hasIntegerType() const
Checks whether the variable is of integral type.
Definition Variable.cpp:63
bool hasRationalType() const
Checks whether the variable is of rational type.
Definition Variable.cpp:71
std::string const & getName() const
Retrieves the name of the variable.
Definition Variable.cpp:46
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
void set(uint64_t index, bool value=true)
Sets the given truth value at the given index.
Provides access to valuations of variables for a set of entities (e.g.
Definition Valuations.h:28
storm::RationalNumber getRationalValue(uint64_t const entity, storm::expressions::Variable const &rationalVariable) const
bool entityHasVariable(uint64_t entity, storm::expressions::Variable const &variable) const
Returns true iff the variable is relevant for the given entity.
void setValuesInEvaluator(uint64_t entity, storm::expressions::ExpressionEvaluator< RationalValueType > &evaluator) const
Reads the variable values for the given entity and sets them into the given expression evaluator.
double getDoubleValue(uint64_t const entity, storm::expressions::Variable const &doubleVariable) const
std::optional< bool > getOptionalBooleanValue(uint64_t const entity, storm::expressions::Variable const &booleanVariable) const
storm::storage::BitVector getBooleanValues(storm::expressions::Variable const &booleanVariable) const
Returns a vector of size getNumberOfEntities() such that the i'th entry is the value of the given var...
std::string toString(uint64_t const entity, bool const pretty=true, std::optional< std::set< storm::expressions::Variable > > const &selectedVariables={}) const
Returns a string representation of the valuation.
Valuations & operator=(Valuations &&other)
std::vector< int64_t > getInt64Values(storm::expressions::Variable const &integerVariable) const
Returns a vector of size getNumberOfEntities() such that the i'th entry is the value of the given var...
std::string getStringValue(uint64_t const entity, storm::expressions::Variable const &stringVariable) const
std::vector< std::string > getStringValues(storm::expressions::Variable const &stringVariable) const
Returns a vector of size getNumberOfEntities() such that the i'th entry is the value of the given var...
std::vector< storm::RationalNumber > getRationalValues(storm::expressions::Variable const &rationalVariable) const
Returns a vector of size getNumberOfEntities() such that the i'th entry is the value of the given var...
storm::json< JsonRationalType > toJson(uint64_t const entity, std::optional< std::set< storm::expressions::Variable > > const &selectedVariables={}) const
Returns a JSON representation of this valuation.
std::optional< storm::RationalNumber > getOptionalRationalValue(uint64_t const entity, storm::expressions::Variable const &rationalVariable) const
std::vector< double > getDoubleValues(storm::expressions::Variable const &doubleVariable) const
Returns a vector of size getNumberOfEntities() such that the i'th entry is the value of the given var...
Valuations(ValuationClassDescription const valuationClassDescription, std::shared_ptr< storm::expressions::ExpressionManager const > const &manager={}, uint64_t const numEntities=0)
std::set< storm::expressions::Variable > getAllVariables() const
storm::expressions::ExpressionManager const & getManager() const
std::optional< double > getOptionalDoubleValue(uint64_t const entity, storm::expressions::Variable const &doubleVariable) const
bool getBooleanValue(uint64_t const entity, storm::expressions::Variable const &booleanVariable) const
int64_t getInt64Value(uint64_t const entity, storm::expressions::Variable const &integerVariable) const
Valuations selectEntities(storm::storage::BitVector const &selectedEntities) const
Derive new valuations from this by selecting the given entities.
ValuationsStorage const & getStorage() const
std::optional< int64_t > getOptionalInt64Value(uint64_t const entity, storm::expressions::Variable const &integerVariable) const
std::optional< std::string > getOptionalStringValue(uint64_t const entity, storm::expressions::Variable const &stringVariable) const
Stores valuations of variables for a set of entities (e.g.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
nlohmann::basic_json< std::map, std::vector, std::string, bool, int64_t, uint64_t, ValueType > json
Definition JsonForward.h:11
Describes the layout of a class of valuations (e.g.