Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ValuationDescriptionBuilder.cpp
Go to the documentation of this file.
2
3#include <sstream>
11
12namespace storm::storage::sparse {
13
14ValuationDescriptionBuilder::ValuationDescriptionBuilder(std::shared_ptr<storm::expressions::ExpressionManager const> const& expressionManager)
15 : manager(expressionManager) {
16 STORM_LOG_ASSERT(this->manager != nullptr, "Initilization with empty expression manager is not allowed.");
17}
18
22
23void ValuationDescriptionBuilder::assertAndCollectVariable(storm::expressions::Variable const& variable) {
24 STORM_LOG_THROW(*manager == variable.getManager(), storm::exceptions::InvalidArgumentException,
25 "Variable " << variable.getName() << " has a different manager than previously specified for this valuation description.");
26 bool const inserted = addedVariables.insert(variable).second;
27 STORM_LOG_THROW(inserted, storm::exceptions::InvalidArgumentException,
28 "Duplicate variable with name '" << variable.getName() << "' has already been added to this valuation description.");
29}
30
32 assertAndCollectVariable(variable);
33 descr.variables.emplace_back(ValuationClassDescription::Variable{.name{variable.getName()},
34 .isOptional{optional ? std::optional<bool>(true) : std::nullopt},
35 .type{.type = storm::umb::Type::Bool, .size = 1},
36 .lower{},
37 .upper{},
38 .offset{}});
39}
40
41void ValuationDescriptionBuilder::addIntegerVariable(storm::expressions::Variable const& variable, int64_t const lowerBound, int64_t const upperBound,
42 bool optional) {
43 assertAndCollectVariable(variable);
44 STORM_LOG_ASSERT(lowerBound <= upperBound, "Lower bound " << lowerBound << " must not be above upper bound" << upperBound << ".");
45 // Cast to uint64_t *before* subtracting to avoid signed overflow UB.
46 uint64_t const bitSize = storm::utility::bitsize(static_cast<uint64_t>(upperBound) - static_cast<uint64_t>(lowerBound));
47 storm::umb::SizedType const t{.type{storm::umb::Type::Uint}, .size{std::max<uint64_t>(1, bitSize)}};
48 descr.variables.emplace_back(ValuationClassDescription::Variable{.name{variable.getName()},
49 .isOptional{optional ? std::optional<bool>(true) : std::nullopt},
50 .type{t},
51 .lower{lowerBound},
52 .upper{upperBound},
53 .offset{lowerBound}});
54}
55
57 bool optional) {
58 assertAndCollectVariable(variable);
59 STORM_LOG_ASSERT(lowerBound <= upperBound, "Lower bound " << lowerBound << " must not be above upper bound" << upperBound << ".");
60 if (lowerBound >= storm::utility::convertNumber<storm::RationalNumber>(std::numeric_limits<int64_t>::min()) &&
61 upperBound <= storm::utility::convertNumber<storm::RationalNumber>(std::numeric_limits<int64_t>::max())) {
62 // If the values fit into int64_t, we use that.
64 } else {
65 uint64_t const bitSize = storm::utility::bitsize<Integer>(upperBound - lowerBound);
66 storm::umb::SizedType const t{.type{lowerBound < 0 ? storm::umb::Type::Int : storm::umb::Type::Uint}, .size{std::max<uint64_t>(1, bitSize)}};
67 descr.variables.emplace_back(ValuationClassDescription::Variable{
68 .name{variable.getName()}, .isOptional{optional ? std::optional<bool>(true) : std::nullopt}, .type{t}, .lower{}, .upper{}, .offset{}});
69 }
70}
71
73 assertAndCollectVariable(variable);
74 descr.variables.emplace_back(ValuationClassDescription::Variable{.name{variable.getName()},
75 .isOptional{optional ? std::optional<bool>(true) : std::nullopt},
76 .type{storm::umb::Type::Double, std::nullopt},
77 .lower{},
78 .upper{},
79 .offset{}});
80}
81
82void ValuationDescriptionBuilder::addRationalVariable(storm::expressions::Variable const& variable, uint64_t bitSize, bool optional) {
83 assertAndCollectVariable(variable);
84 STORM_LOG_ASSERT(bitSize % 2 == 0, "Bit size for rational variables must be a multiple of 2.");
85 storm::umb::SizedType const t{.type{storm::umb::Type::Rational}, .size{std::max<uint64_t>(2, bitSize)}};
86 descr.variables.emplace_back(ValuationClassDescription::Variable{
87 .name{variable.getName()}, .isOptional{optional ? std::optional<bool>(true) : std::nullopt}, .type{t}, .lower{}, .upper{}, .offset{}});
88}
89
91 assertAndCollectVariable(variable);
92 descr.variables.emplace_back(ValuationClassDescription::Variable{.name{variable.getName()},
93 .isOptional{optional ? std::optional<bool>(true) : std::nullopt},
94 .type{storm::umb::Type::String, std::nullopt},
95 .lower{},
96 .upper{},
97 .offset{}});
98}
99
101 STORM_LOG_ASSERT(manager->hasVariable(variable.name), "Variable " << variable.name << " is not declared in the expression manager.");
102 assertAndCollectVariable(manager->getVariable(variable.name));
103 std::ostringstream errors;
104 STORM_LOG_THROW(storm::umb::validation::validateTypeDeclaration(variable.type, false, errors), storm::exceptions::WrongFormatException,
105 "Invalid type declaration for variable " << variable.name << ": " << errors.str() << ".");
106 descr.variables.push_back(variable);
107}
108
110 for (auto const& varVariant : description.variables) {
111 if (std::holds_alternative<ValuationClassDescription::Variable>(varVariant)) {
112 addVariable(std::get<ValuationClassDescription::Variable>(varVariant));
113 } else if (addPadding && std::holds_alternative<ValuationClassDescription::Padding>(varVariant)) {
114 descr.variables.push_back(varVariant);
115 }
116 }
117}
118
119void ValuationDescriptionBuilder::finalize() {
120 if (uint64_t const padding = descr.sizeInBits() % 8; padding > 0) {
121 descr.variables.emplace_back(ValuationClassDescription::Padding{.padding = 8 - padding});
122 }
123}
124
129
130} // namespace storm::storage::sparse
This class is responsible for managing a set of typed variables and all expressions using these varia...
ExpressionManager const & getManager() const
Retrieves the manager responsible for this variable.
Definition Variable.cpp:54
std::string const & getName() const
Retrieves the name of the variable.
Definition Variable.cpp:46
ValuationClassDescription buildClassDescription()
Creates the finalized state valuations object.
storm::expressions::ExpressionManager const & getManager() const
void addBooleanVariable(storm::expressions::Variable const &variable, bool optional=false)
Adds a new boolean variable to the builder.
storm::NumberTraits< storm::RationalNumber >::IntegerType Integer
ValuationDescriptionBuilder(std::shared_ptr< storm::expressions::ExpressionManager const > const &expressionManager)
void addRationalVariable(storm::expressions::Variable const &variable, uint64_t bitSize, bool optional=false)
Adds a new rational variable to the builder.
void addStringVariable(storm::expressions::Variable const &variable, bool optional=false)
Adds a new string variable to the builder.
void addIntegerVariable(storm::expressions::Variable const &variable, int64_t const lowerBound, int64_t const upperBound, bool optional=false)
Adds a new integer variable to the builder.
void addVariables(ValuationClassDescription const &description, bool addPadding=false)
Adds all variables from the given description.
void addVariable(ValuationClassDescription::Variable const &variable)
Adds the given variable.
void addDoubleVariable(storm::expressions::Variable const &variable, bool optional=false)
Adds a new double variable to the builder.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:28
bool validateTypeDeclaration(storm::umb::SizedType const &type, bool requireStandardSize, std::ostream &err)
Validates a single type declaration against the UMB specification, writing potential errors to the gi...
uint64_t bitsize(ValueType const &number)
Returns the minimum number of bits to represent the given number.
TargetType convertNumber(SourceType const &number)
Describes the layout of a class of valuations (e.g.
uint64_t sizeInBits() const
Computes the size in bits of a valuation.
std::vector< std::variant< Padding, Variable > > variables