Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
VariableSet.cpp
Go to the documentation of this file.
2
8
9namespace storm {
10namespace jani {
11
13 // Intentionally left empty.
14}
15
17 return detail::Variables<Variable>(booleanVariables.begin(), booleanVariables.end());
18}
19
21 return detail::ConstVariables<Variable>(booleanVariables.begin(), booleanVariables.end());
22}
23
25 return detail::Variables<Variable>(boundedIntegerVariables.begin(), boundedIntegerVariables.end());
26}
27
29 return detail::ConstVariables<Variable>(boundedIntegerVariables.begin(), boundedIntegerVariables.end());
30}
31
33 return detail::Variables<Variable>(unboundedIntegerVariables.begin(), unboundedIntegerVariables.end());
34}
35
37 return detail::ConstVariables<Variable>(unboundedIntegerVariables.begin(), unboundedIntegerVariables.end());
38}
39
41 return detail::Variables<Variable>(realVariables.begin(), realVariables.end());
42}
43
45 return detail::ConstVariables<Variable>(realVariables.begin(), realVariables.end());
46}
47
49 return detail::Variables<Variable>(arrayVariables.begin(), arrayVariables.end());
50}
51
53 return detail::ConstVariables<Variable>(arrayVariables.begin(), arrayVariables.end());
54}
55
57 return detail::Variables<Variable>(clockVariables.begin(), clockVariables.end());
58}
59
61 return detail::ConstVariables<Variable>(clockVariables.begin(), clockVariables.end());
62}
63
65 return detail::Variables<Variable>(continuousVariables.begin(), continuousVariables.end());
66}
67
69 return detail::ConstVariables<Variable>(continuousVariables.begin(), continuousVariables.end());
70}
71
72std::vector<std::shared_ptr<Variable>>& VariableSet::getVariableVectorForType(JaniType const& type) {
73 if (type.isBasicType()) {
74 switch (type.asBasicType().get()) {
76 return booleanVariables;
78 return unboundedIntegerVariables;
80 return realVariables;
81 }
82 } else if (type.isBoundedType()) {
83 switch (type.asBoundedType().getBaseType()) {
85 return boundedIntegerVariables;
87 STORM_LOG_THROW(false, storm::exceptions::InvalidTypeException, "Cannot add variable of bounded real type.");
88 }
89 } else if (type.isArrayType()) {
90 return arrayVariables;
91 } else if (type.isClockType()) {
92 return clockVariables;
93 } else if (type.isContinuousType()) {
94 return continuousVariables;
95 }
96 STORM_LOG_THROW(false, storm::exceptions::InvalidTypeException, "Unhandled variable type" << type);
97}
98
100 STORM_LOG_THROW(!this->hasVariable(variable.getName()), storm::exceptions::WrongFormatException,
101 "Cannot add variable with name '" << variable.getName() << "', because a variable with that name already exists.");
102 std::shared_ptr<Variable> newVariable = variable.clone();
103 variables.push_back(newVariable);
104 if (variable.isTransient()) {
105 transientVariables.push_back(newVariable);
106 }
107 nameToVariable.emplace(variable.getName(), variable.getExpressionVariable());
108 variableToVariable.emplace(variable.getExpressionVariable(), newVariable);
109
110 auto& variableVectorForType = getVariableVectorForType(variable.getType());
111 variableVectorForType.push_back(newVariable);
112 return *newVariable;
113}
114
115std::vector<std::shared_ptr<Variable>> VariableSet::dropAllArrayVariables() {
116 if (!arrayVariables.empty()) {
117 for (auto const& arrVar : arrayVariables) {
118 nameToVariable.erase(arrVar->getName());
119 variableToVariable.erase(arrVar->getExpressionVariable());
120 }
121 std::vector<std::shared_ptr<Variable>> newVariables;
122 for (auto const& v : variables) {
123 if (!v->getType().isArrayType()) {
124 newVariables.push_back(v);
125 }
126 }
127 variables = std::move(newVariables);
128 newVariables.clear();
129 for (auto const& v : transientVariables) {
130 if (!v->getType().isArrayType()) {
131 newVariables.push_back(v);
132 }
133 }
134 transientVariables = std::move(newVariables);
135 }
136
137 std::vector<std::shared_ptr<Variable>> result = std::move(arrayVariables);
138 arrayVariables.clear();
139 return result;
140}
141
142bool VariableSet::hasVariable(std::string const& name) const {
143 return nameToVariable.find(name) != nameToVariable.end();
144}
145
146bool VariableSet::hasVariable(Variable const& var) const {
147 return hasVariable(var.getName());
148}
149
150Variable const& VariableSet::getVariable(std::string const& name) const {
151 auto it = nameToVariable.find(name);
152 STORM_LOG_THROW(it != nameToVariable.end(), storm::exceptions::InvalidArgumentException, "Unable to retrieve unknown variable '" << name << "'.");
153 return getVariable(it->second);
154}
155
156template<typename VarType>
157void eraseFromVariableVector(std::vector<std::shared_ptr<VarType>>& varVec, storm::expressions::Variable const& variable) {
158 for (auto vIt = varVec.begin(); vIt != varVec.end(); ++vIt) {
159 if ((*vIt)->getExpressionVariable() == variable) {
160 varVec.erase(vIt);
161 break;
162 }
163 }
164}
165
166std::shared_ptr<Variable> VariableSet::eraseVariable(storm::expressions::Variable const& variable) {
167 auto vToVIt = variableToVariable.find(variable);
168 STORM_LOG_THROW(vToVIt != variableToVariable.end(), storm::exceptions::InvalidArgumentException,
169 "Unable to erase unknown variable '" << variable.getName() << "'.");
170 std::shared_ptr<Variable> janiVar = std::move(vToVIt->second);
171 variableToVariable.erase(vToVIt);
172
173 nameToVariable.erase(janiVar->getName());
174 eraseFromVariableVector(variables, variable);
175 eraseFromVariableVector(getVariableVectorForType(janiVar->getType()), variable);
176 if (janiVar->isTransient()) {
177 eraseFromVariableVector(transientVariables, variable);
178 }
179 return janiVar;
180}
181
185
189
193
197
199 auto it = variableToVariable.find(variable);
200 STORM_LOG_THROW(it != variableToVariable.end(), storm::exceptions::InvalidArgumentException,
201 "Unable to retrieve unknown variable '" << variable.getName() << "'.");
202 return *it->second;
203}
204
206 return variableToVariable.find(variable) != variableToVariable.end();
207}
208
210 for (auto const& variable : variables) {
211 if (variable->isTransient()) {
212 return true;
213 }
214 }
215 return false;
216}
217
219 return !booleanVariables.empty();
220}
221
223 return !boundedIntegerVariables.empty();
224}
225
227 return !unboundedIntegerVariables.empty();
228}
229
231 return !realVariables.empty();
232}
233
235 return !arrayVariables.empty();
236}
237
239 return !clockVariables.empty();
240}
241
243 return !continuousVariables.empty();
244}
245
247 for (auto const& variable : realVariables) {
248 if (!variable->isTransient()) {
249 return true;
250 }
251 }
252 return false;
253}
254
256 for (auto const& variable : unboundedIntegerVariables) {
257 if (!variable->isTransient()) {
258 return true;
259 }
260 }
261 return false;
262}
263
268
270 return variables.size();
271}
272
276
278 uint_fast64_t result = 0;
279 for (auto const& variable : variables) {
280 if (variable->isTransient()) {
281 ++result;
282 }
283 }
284 return result;
285}
286
288 uint_fast64_t result = 0;
289 for (auto const& variable : realVariables) {
290 if (variable->isTransient()) {
291 ++result;
292 }
293 }
294 return result;
295}
296
298 uint_fast64_t result = 0;
299 for (auto const& variable : unboundedIntegerVariables) {
300 if (variable->isTransient()) {
301 ++result;
302 }
303 }
304 return result;
305}
306
308 uint_fast64_t result = 0;
309 for (auto const& variable : transientVariables) {
310 auto const& type = variable->getType();
311 if (type.isBasicType() && (type.asBasicType().isIntegerType() || type.asBasicType().isRealType())) {
312 ++result;
313 } else if (type.isBoundedType() && (type.asBoundedType().isIntegerType() || type.asBoundedType().isRealType())) {
314 ++result;
315 }
316 }
317 return result;
318}
319
321 return detail::ConstVariables<Variable>(transientVariables.begin(), transientVariables.end());
322}
323
324bool VariableSet::containsVariablesInBoundExpressionsOrInitialValues(std::set<storm::expressions::Variable> const& variables) const {
325 for (auto const& variable : this->variables) {
326 if (variable->hasInitExpression() && variable->getInitExpression().containsVariable(variables)) {
327 return true;
328 }
329 auto const& varType = variable->getType();
330 auto const& type = varType.isArrayType() ? varType.asArrayType().getBaseTypeRecursive() : varType;
331 if (type.isBoundedType()) {
332 auto const& boundedType = type.asBoundedType();
333 if (boundedType.hasLowerBound() && boundedType.getLowerBound().containsVariable(variables)) {
334 return true;
335 }
336 if (boundedType.hasUpperBound() && boundedType.getUpperBound().containsVariable(variables)) {
337 return true;
338 }
339 }
340 }
341 return false;
342}
343
344std::map<std::string, std::reference_wrapper<Variable const>> VariableSet::getNameToVariableMap() const {
345 std::map<std::string, std::reference_wrapper<Variable const>> result;
346
347 for (auto const& variable : variables) {
348 result.emplace(variable->getName(), *variable);
349 }
350
351 return result;
352}
353
354void VariableSet::substitute(std::map<storm::expressions::Variable, storm::expressions::Expression> const& substitution,
355 bool const substituteTranscendentalNumbers) {
356 for (auto& variable : variables) {
357 variable->substitute(substitution, substituteTranscendentalNumbers);
358 }
359}
360
361void VariableSet::substituteExpressionVariables(std::map<storm::expressions::Variable, storm::expressions::Expression> const& substitution) {
362 for (auto& variable : variables) {
363 auto varIt = substitution.find(variable->getExpressionVariable());
364 if (varIt != substitution.end()) {
365 STORM_LOG_ASSERT(varIt->second.isVariable(), "Expected that variables are only substituted by other variables. However, we substitute "
366 << varIt->first.getName() << " by " << varIt->second << ".");
367 variable->setExpressionVariable(varIt->second.getBaseExpression().asVariableExpression().getVariable());
368 }
369 }
370}
371} // namespace jani
372} // namespace storm
boost::transform_iterator< Dereferencer< value_type >, input_iterator > iterator
std::string const & getName() const
Retrieves the name of the variable.
Definition Variable.cpp:46
Type const & get() const
Definition BasicType.cpp:16
bool isRealType() const
Definition BasicType.cpp:28
bool isIntegerType() const
Definition BasicType.cpp:24
BaseType const & getBaseType() const
virtual void substitute(std::map< storm::expressions::Variable, storm::expressions::Expression > const &substitution, bool const substituteTranscendentalNumbers) override
Substitutes all variables in all expressions according to the given substitution.
virtual bool isContinuousType() const
Definition JaniType.cpp:27
virtual bool isArrayType() const
Definition JaniType.cpp:19
virtual bool isBoundedType() const
Definition JaniType.cpp:15
virtual bool isBasicType() const
Definition JaniType.cpp:11
BoundedType const & asBoundedType() const
Definition JaniType.cpp:39
virtual bool isClockType() const
Definition JaniType.cpp:23
BasicType const & asBasicType() const
Definition JaniType.cpp:31
std::unique_ptr< Variable > clone() const
Clones the variable.
Definition Variable.cpp:22
storm::expressions::Variable const & getExpressionVariable() const
Retrieves the associated expression variable.
Definition Variable.cpp:26
JaniType & getType()
Definition Variable.cpp:67
bool isTransient() const
Definition Variable.cpp:42
std::string const & getName() const
Retrieves the name of the variable.
Definition Variable.cpp:34
bool containsArrayVariables() const
Retrieves whether the set of variables contains a Array variable.
detail::Variables< Variable > getBoundedIntegerVariables()
Retrieves the bounded integer variables in this set.
bool containsNonTransientRealVariables() const
Retrieves whether the set of variables contains a non-transient real variable.
Variable const & addVariable(Variable const &variable)
Adds the given variable to this set.
bool hasVariable(std::string const &name) const
Retrieves whether this variable set contains a variable with the given name.
uint64_t getNumberOfUnboundedIntegerTransientVariables() const
Retrieves the number of unbounded integer transient variables in this variable set.
detail::Variables< Variable > getUnboundedIntegerVariables()
Retrieves the unbounded integer variables in this set.
detail::Variables< Variable > getRealVariables()
Retrieves the real variables in this set.
uint64_t getNumberOfNumericalTransientVariables() const
Retrieves the number of numerical (i.e.
VariableSet()
Creates an empty variable set.
void substitute(std::map< storm::expressions::Variable, storm::expressions::Expression > const &substitution, bool const substituteTranscendentalNumbers)
Applies the given substitution to all variables in this set.
detail::Variables< Variable >::iterator end()
Retrieves the end iterator to the variables in this set.
bool hasTransientVariable() const
Retrieves whether this variable set contains a transient variable.
bool containsContinuousVariables() const
Retrieves whether the set of variables contains a clock variable.
bool containsBooleanVariable() const
Retrieves whether the set of variables contains a boolean variable.
std::shared_ptr< Variable > eraseVariable(storm::expressions::Variable const &variable)
Erases the given variable from this set.
Variable const & getVariable(std::string const &name) const
Retrieves the variable with the given name.
detail::Variables< Variable > getContinuousVariables()
Retrieves the continous variables in this set.
uint64_t getNumberOfVariables() const
Total number of variables, including transient variables.
bool containsUnboundedIntegerVariables() const
Retrieves whether the set of variables contains an unbounded integer variable.
uint64_t getNumberOfNontransientVariables() const
bool containsNonTransientUnboundedIntegerVariables() const
Retrieves whether the set of variables contains a non-transient unbounded integer variable.
bool containsBoundedIntegerVariable() const
Retrieves whether the set of variables contains a bounded integer variable.
std::vector< std::shared_ptr< Variable > > dropAllArrayVariables()
Removes all array variables in this set.
bool containsRealVariables() const
Retrieves whether the set of variables contains a real variable.
bool empty() const
Retrieves whether this variable set is empty.
detail::Variables< Variable > getClockVariables()
Retrieves the clock variables in this set.
uint64_t getNumberOfTransientVariables() const
Retrieves the number of transient variables in this variable set.
bool containsVariablesInBoundExpressionsOrInitialValues(std::set< storm::expressions::Variable > const &variables) const
Checks whether any of the provided variables appears in bound expressions or initial values of the va...
void substituteExpressionVariables(std::map< storm::expressions::Variable, storm::expressions::Expression > const &substitution)
Substitutes the actual variables according to the given substitution.
detail::Variables< Variable >::iterator begin()
Retrieves an iterator to the variables in this set.
bool containsClockVariables() const
Retrieves whether the set of variables contains a clock variable.
std::map< std::string, std::reference_wrapper< Variable const > > getNameToVariableMap() const
Retrieves a mapping from variable names to (references of) the variable objects.
detail::Variables< Variable > getBooleanVariables()
Retrieves the boolean variables in this set.
detail::Variables< Variable > getArrayVariables()
Retrieves the Array variables in this set.
uint64_t getNumberOfRealTransientVariables() const
Retrieves the number of real transient variables in this variable set.
detail::ConstVariables< Variable > getTransientVariables() const
Retrieves the transient variables in this variable set.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
void eraseFromVariableVector(std::vector< std::shared_ptr< VarType > > &varVec, storm::expressions::Variable const &variable)
LabParser.cpp.
Definition cli.cpp:18