Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ExpressionManager.h
Go to the documentation of this file.
1#ifndef STORM_STORAGE_EXPRESSIONS_EXPRESSIONMANAGER_H_
2#define STORM_STORAGE_EXPRESSIONS_EXPRESSIONMANAGER_H_
3
4#include <cstdint>
5#include <iosfwd>
6#include <iterator>
7#include <unordered_map>
8#include <unordered_set>
9#include <vector>
10
11#include <boost/optional.hpp>
12
17
18namespace storm {
19namespace expressions {
20// Forward-declare manager class for iterator class.
21class ExpressionManager;
22
24 public:
25 // Define iterator
26 using iterator_category = std::input_iterator_tag;
27 using value_type = std::pair<storm::expressions::Variable, storm::expressions::Type> const;
28 using difference_type = std::ptrdiff_t;
29 using pointer = std::pair<storm::expressions::Variable, storm::expressions::Type> const*;
30 using reference = std::pair<storm::expressions::Variable, storm::expressions::Type> const&;
31
33
34 VariableIterator(ExpressionManager const& manager, std::unordered_map<std::string, uint_fast64_t>::const_iterator nameIndexIterator,
35 std::unordered_map<std::string, uint_fast64_t>::const_iterator nameIndexIteratorEnd, VariableSelection const& selection);
37
38 // Define the basic input iterator operations.
39 bool operator==(VariableIterator const& other);
40 bool operator!=(VariableIterator const& other);
44
45 private:
51 void moveUntilNextSelectedElement(bool atLeastOneStep = true);
52
53 // The manager responsible for the variable to iterate over.
54 ExpressionManager const& manager;
55
56 // The underlying iterator that ranges over all names and the corresponding indices.
57 std::unordered_map<std::string, uint_fast64_t>::const_iterator nameIndexIterator;
58
59 // The iterator indicating the end of the underlying iterator range.
60 std::unordered_map<std::string, uint_fast64_t>::const_iterator nameIndexIteratorEnd;
61
62 // A field indicating which variables we are supposed to iterate over.
63 VariableSelection selection;
64
65 // The current element that is shown to the outside upon dereferencing.
66 std::pair<storm::expressions::Variable, storm::expressions::Type> currentElement;
67};
68
72class ExpressionManager : public std::enable_shared_from_this<ExpressionManager> {
73 public:
74 friend class VariableIterator;
75
77
82
84
88 std::shared_ptr<ExpressionManager> clone() const;
89
90 // Create default instantiations for the move construction/assignment.
93
100 Expression boolean(bool value) const;
101
108 Expression integer(int_fast64_t value) const;
109
116 Expression rational(double value) const;
117
124 Expression rational(storm::RationalNumber const& value) const;
125
129 bool operator==(ExpressionManager const& other) const;
130
136 Type const& getBooleanType() const;
137
143 Type const& getIntegerType() const;
144
151 Type const& getBitVectorType(std::size_t width) const;
152
158 Type const& getRationalType() const;
159
163 Type const& getArrayType(Type elementType) const;
164
169 Type const& getTranscendentalNumberType() const;
170
177 Variable declareVariableCopy(Variable const& variable);
178
188 Variable declareVariable(std::string const& name, storm::expressions::Type const& variableType, bool auxiliary = false);
189
198 Variable declareBooleanVariable(std::string const& name, bool auxiliary = false);
199
208 Variable declareIntegerVariable(std::string const& name, bool auxiliary = false);
209
220 Variable declareBitVectorVariable(std::string const& name, std::size_t width, bool auxiliary = false);
221
230 Variable declareRationalVariable(std::string const& name, bool auxiliary = false);
231
235 Variable declareArrayVariable(std::string const& name, Type const& elementType, bool auxiliary = false);
236
245 Variable declareOrGetVariable(std::string const& name, storm::expressions::Type const& variableType, bool auxiliary = false);
246
252 Variable getVariable(std::string const& name) const;
253
257 std::set<Variable> const& getVariables() const;
258
265 bool hasVariable(std::string const& name) const;
266
273 Expression getVariableExpression(std::string const& name) const;
274
283 Variable declareFreshVariable(storm::expressions::Type const& variableType, bool auxiliary = false, std::string const& prefix = "_x");
284
292 Variable declareFreshRationalVariable(bool auxiliary = false, std::string const& prefix = "_x");
293
301 Variable declareFreshBooleanVariable(bool auxiliary = false, std::string const& prefix = "_x");
302
310 Variable declareFreshIntegerVariable(bool auxiliary = false, std::string const& prefix = "_x");
311
317 uint_fast64_t getNumberOfVariables() const;
318
324 uint_fast64_t getNumberOfBooleanVariables() const;
325
331 uint_fast64_t getNumberOfIntegerVariables() const;
332
338 uint_fast64_t getNumberOfBitVectorVariables() const;
339
345 uint_fast64_t getNumberOfRationalVariables() const;
346
350 uint_fast64_t getNumberOfArrayVariables() const;
351
358 std::string const& getVariableName(uint_fast64_t index) const;
359
366 Type const& getVariableType(uint_fast64_t index) const;
367
374 uint_fast64_t getOffset(uint_fast64_t index) const;
375
381 const_iterator begin() const;
382
388 const_iterator end() const;
389
395 std::shared_ptr<ExpressionManager> getSharedPointer();
396
402 std::shared_ptr<ExpressionManager const> getSharedPointer() const;
403
404 friend std::ostream& operator<<(std::ostream& out, ExpressionManager const& manager);
405
406 private:
407 // Explicitly make copy construction/assignment private, since the manager is supposed to be stored as a pointer
408 // of some sort. This is because the expression classes store a reference to the manager and it must
409 // therefore be guaranteed that they do not become invalid, because the manager has been copied.
410 ExpressionManager(ExpressionManager const& other) = default;
411 ExpressionManager& operator=(ExpressionManager const& other) = default;
412
419 static bool isValidVariableName(std::string const& name);
420
427 bool variableExists(std::string const& name) const;
428
439 Variable declareOrGetVariable(std::string const& name, storm::expressions::Type const& variableType, bool auxiliary, bool checkName);
440
447 uint_fast64_t getNumberOfVariables(storm::expressions::Type const& variableType) const;
448
455 uint_fast64_t getNumberOfAuxiliaryVariables(storm::expressions::Type const& variableType) const;
456
457 // The set of all known variables.
458 std::set<Variable> variableSet;
459
460 // A mapping from all variable names (auxiliary + normal) to their indices.
461 std::unordered_map<std::string, uint_fast64_t> nameToIndexMapping;
462
463 // A mapping from all variable indices to their names.
464 std::unordered_map<uint64_t, std::string> indexToNameMapping;
465
466 // A mapping from all variable indices to their types.
467 std::unordered_map<uint64_t, Type> indexToTypeMapping;
468
469 // Store counts for variables.
470 uint_fast64_t numberOfBooleanVariables;
471 uint_fast64_t numberOfIntegerVariables;
472 uint_fast64_t numberOfBitVectorVariables;
473 uint_fast64_t numberOfRationalVariables;
474 uint_fast64_t numberOfArrayVariables;
475
476 // The number of declared auxiliary variables.
477 uint_fast64_t numberOfAuxiliaryVariables;
478
479 // Store counts for auxiliary variables.
480 uint_fast64_t numberOfAuxiliaryBooleanVariables;
481 uint_fast64_t numberOfAuxiliaryIntegerVariables;
482 uint_fast64_t numberOfAuxiliaryBitVectorVariables;
483 uint_fast64_t numberOfAuxiliaryRationalVariables;
484 uint_fast64_t numberOfAuxiliaryArrayVariables;
485
486 // A counter used to create fresh variables.
487 uint_fast64_t freshVariableCounter;
488
489 // The types managed by this manager.
490 mutable boost::optional<Type> booleanType;
491 mutable boost::optional<Type> integerType;
492 mutable std::unordered_set<Type> bitvectorTypes;
493 mutable boost::optional<Type> rationalType;
494 mutable std::unordered_set<Type> arrayTypes;
495 mutable boost::optional<Type> transcendentalNumberType;
496
497 // A mask that can be used to query whether a variable is an auxiliary variable.
498 static const uint64_t auxiliaryMask = (1ull << 50);
499
500 // A mask that can be used to project a variable index to its offset (with the group of equally typed variables).
501 static const uint64_t offsetMask = (1ull << 50) - 1;
502};
503
504std::ostream& operator<<(std::ostream& out, ExpressionManager const& manager);
505} // namespace expressions
506} // namespace storm
507
508#endif /* STORM_STORAGE_EXPRESSIONS_EXPRESSIONMANAGER_H_ */
This class is responsible for managing a set of typed variables and all expressions using these varia...
Variable declareFreshIntegerVariable(bool auxiliary=false, std::string const &prefix="_x")
Declares a variable with integer type whose name is guaranteed to be unique and not yet in use.
Type const & getTranscendentalNumberType() const
Retrieves the transcendental numbers type (i.e.
Type const & getBitVectorType(std::size_t width) const
Retrieves the bit vector type of the given width.
std::set< Variable > const & getVariables() const
Retrieves the set of all variables known to this manager.
Type const & getArrayType(Type elementType) const
Retrieves the array type with the given element type.
uint_fast64_t getNumberOfIntegerVariables() const
Retrieves the number of integer variables.
uint_fast64_t getNumberOfBooleanVariables() const
Retrieves the number of boolean variables.
Variable declareFreshBooleanVariable(bool auxiliary=false, std::string const &prefix="_x")
Declares a variable with Boolean type whose name is guaranteed to be unique and not yet in use.
Variable declareRationalVariable(std::string const &name, bool auxiliary=false)
Declares a new rational variable with a name that must not yet exist and its corresponding type.
uint_fast64_t getNumberOfArrayVariables() const
Retrieves the number of array variables.
Variable declareBooleanVariable(std::string const &name, bool auxiliary=false)
Declares a new boolean variable with a name that must not yet exist and its corresponding type.
Expression integer(int_fast64_t value) const
Creates an expression that characterizes the given integer literal.
friend std::ostream & operator<<(std::ostream &out, ExpressionManager const &manager)
Type const & getVariableType(uint_fast64_t index) const
Retrieves the type of the variable with the given index.
bool hasVariable(std::string const &name) const
Retrieves whether a variable with the given name is known to the manager.
Variable declareOrGetVariable(std::string const &name, storm::expressions::Type const &variableType, bool auxiliary=false)
Declares a variable with the given name if it does not yet exist.
ExpressionManager(ExpressionManager &&other)=default
std::shared_ptr< ExpressionManager > clone() const
Creates a new expression manager with the same set of variables.
Type const & getBooleanType() const
Retrieves the boolean type.
const_iterator end() const
Retrieves an iterator that points beyond the last variable managed by this manager.
uint_fast64_t getOffset(uint_fast64_t index) const
Retrieves the offset of the variable with the given index within the group of equally typed variables...
const_iterator begin() const
Retrieves an iterator to all variables managed by this manager.
Variable declareVariable(std::string const &name, storm::expressions::Type const &variableType, bool auxiliary=false)
Declares a variable with a name that must not yet exist and its corresponding type.
ExpressionManager()
Creates a new manager that is unaware of any variables.
Type const & getIntegerType() const
Retrieves the unbounded integer type.
uint_fast64_t getNumberOfVariables() const
Retrieves the number of variables.
Variable declareFreshRationalVariable(bool auxiliary=false, std::string const &prefix="_x")
Declares a variable with rational type whose name is guaranteed to be unique and not yet in use.
Variable declareVariableCopy(Variable const &variable)
Declares a variable that is a copy of the provided variable (i.e.
Variable declareIntegerVariable(std::string const &name, bool auxiliary=false)
Declares a new integer variable with a name that must not yet exist and its corresponding type.
bool operator==(ExpressionManager const &other) const
Compares the two expression managers for equality, which holds iff they are the very same object.
uint_fast64_t getNumberOfRationalVariables() const
Retrieves the number of rational variables.
uint_fast64_t getNumberOfBitVectorVariables() const
Retrieves the number of bit vector variables.
Variable declareArrayVariable(std::string const &name, Type const &elementType, bool auxiliary=false)
Declares a new array variable with the given name and the given element type.
Expression getVariableExpression(std::string const &name) const
Retrieves an expression that represents the variable with the given name.
Expression rational(double value) const
Creates an expression that characterizes the given rational literal.
Variable declareFreshVariable(storm::expressions::Type const &variableType, bool auxiliary=false, std::string const &prefix="_x")
Declares a variable with the given type whose name is guaranteed to be unique and not yet in use.
std::string const & getVariableName(uint_fast64_t index) const
Retrieves the name of the variable with the given index.
std::shared_ptr< ExpressionManager > getSharedPointer()
Retrieves a shared pointer to the expression manager.
Type const & getRationalType() const
Retrieves the rational type.
Variable declareBitVectorVariable(std::string const &name, std::size_t width, bool auxiliary=false)
Declares a new bit vector variable with a name that must not yet exist and the bounded type of the gi...
Variable getVariable(std::string const &name) const
Retrieves the expression that represents the variable with the given name.
Expression boolean(bool value) const
Creates an expression that characterizes the given boolean literal.
ExpressionManager & operator=(ExpressionManager &&other)=default
VariableIterator(VariableIterator &&other)=default
bool operator!=(VariableIterator const &other)
std::pair< storm::expressions::Variable, storm::expressions::Type > const * pointer
std::pair< storm::expressions::Variable, storm::expressions::Type > const & reference
std::input_iterator_tag iterator_category
bool operator==(VariableIterator const &other)
std::pair< storm::expressions::Variable, storm::expressions::Type > const value_type
std::ostream & operator<<(std::ostream &stream, BaseExpression const &expression)
LabParser.cpp.
Definition cli.cpp:18