Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ExpressionManager.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <iosfwd>
5#include <iterator>
6#include <unordered_map>
7#include <unordered_set>
8#include <vector>
9
10#include <boost/optional.hpp>
11
16
17namespace storm {
18namespace expressions {
19// Forward-declare manager class for iterator class.
21
23 public:
24 // Define iterator
25 using iterator_category = std::input_iterator_tag;
26 using value_type = std::pair<storm::expressions::Variable, storm::expressions::Type> const;
27 using difference_type = std::ptrdiff_t;
28 using pointer = std::pair<storm::expressions::Variable, storm::expressions::Type> const*;
29 using reference = std::pair<storm::expressions::Variable, storm::expressions::Type> const&;
30
32
33 VariableIterator(ExpressionManager const& manager, std::unordered_map<std::string, uint_fast64_t>::const_iterator nameIndexIterator,
34 std::unordered_map<std::string, uint_fast64_t>::const_iterator nameIndexIteratorEnd, VariableSelection const& selection);
36
37 // Define the basic input iterator operations.
38 bool operator==(VariableIterator const& other) const;
39 bool operator!=(VariableIterator const& other) const;
43
44 private:
50 void moveUntilNextSelectedElement(bool atLeastOneStep = true);
51
52 // The manager responsible for the variable to iterate over.
53 ExpressionManager const& manager;
54
55 // The underlying iterator that ranges over all names and the corresponding indices.
56 std::unordered_map<std::string, uint_fast64_t>::const_iterator nameIndexIterator;
57
58 // The iterator indicating the end of the underlying iterator range.
59 std::unordered_map<std::string, uint_fast64_t>::const_iterator nameIndexIteratorEnd;
60
61 // A field indicating which variables we are supposed to iterate over.
62 VariableSelection selection;
63
64 // The current element that is shown to the outside upon dereferencing.
65 std::pair<storm::expressions::Variable, storm::expressions::Type> currentElement;
66};
67
71class ExpressionManager : public std::enable_shared_from_this<ExpressionManager> {
72 public:
73 friend class VariableIterator;
74
76
81
83
87 std::shared_ptr<ExpressionManager> clone() const;
88
89 // Create default instantiations for the move construction/assignment.
92
99 Expression boolean(bool value) const;
100
107 Expression integer(int_fast64_t value) const;
108
115 Expression rational(double value) const;
116
123 Expression rational(storm::RationalNumber const& value) const;
124
128 bool operator==(ExpressionManager const& other) const;
129
135 Type const& getBooleanType() const;
136
142 Type const& getIntegerType() const;
143
150 Type const& getBitVectorType(std::size_t width) const;
151
157 Type const& getRationalType() const;
158
162 Type const& getArrayType(Type elementType) const;
163
168 Type const& getTranscendentalNumberType() const;
169
174 Type const& getStringType() const;
175
182 Variable declareVariableCopy(Variable const& variable);
183
193 Variable declareVariable(std::string const& name, storm::expressions::Type const& variableType, bool auxiliary = false);
194
203 Variable declareBooleanVariable(std::string const& name, bool auxiliary = false);
204
213 Variable declareIntegerVariable(std::string const& name, bool auxiliary = false);
214
225 Variable declareBitVectorVariable(std::string const& name, std::size_t width, bool auxiliary = false);
226
235 Variable declareRationalVariable(std::string const& name, bool auxiliary = false);
236
240 Variable declareArrayVariable(std::string const& name, Type const& elementType, bool auxiliary = false);
241
248 Variable declareStringVariable(std::string const& name, bool auxiliary = false);
249
258 Variable declareOrGetVariable(std::string const& name, storm::expressions::Type const& variableType, bool auxiliary = false);
259
265 Variable getVariable(std::string const& name) const;
266
270 std::set<Variable> const& getVariables() const;
271
278 bool hasVariable(std::string const& name) const;
279
286 Expression getVariableExpression(std::string const& name) const;
287
296 Variable declareFreshVariable(storm::expressions::Type const& variableType, bool auxiliary = false, std::string const& prefix = "_x");
297
305 Variable declareFreshRationalVariable(bool auxiliary = false, std::string const& prefix = "_x");
306
314 Variable declareFreshBooleanVariable(bool auxiliary = false, std::string const& prefix = "_x");
315
323 Variable declareFreshIntegerVariable(bool auxiliary = false, std::string const& prefix = "_x");
324
330 uint_fast64_t getNumberOfVariables() const;
331
337 uint_fast64_t getNumberOfBooleanVariables() const;
338
344 uint_fast64_t getNumberOfIntegerVariables() const;
345
351 uint_fast64_t getNumberOfBitVectorVariables() const;
352
358 uint_fast64_t getNumberOfRationalVariables() const;
359
363 uint_fast64_t getNumberOfArrayVariables() const;
364
368 uint_fast64_t getNumberOfStringVariables() const;
369
376 std::string const& getVariableName(uint_fast64_t index) const;
377
384 Type const& getVariableType(uint_fast64_t index) const;
385
392 uint_fast64_t getOffset(uint_fast64_t index) const;
393
399 const_iterator begin() const;
400
406 const_iterator end() const;
407
413 std::shared_ptr<ExpressionManager> getSharedPointer();
414
420 std::shared_ptr<ExpressionManager const> getSharedPointer() const;
421
422 friend std::ostream& operator<<(std::ostream& out, ExpressionManager const& manager);
423
424 private:
425 // Explicitly make copy construction/assignment private, since the manager is supposed to be stored as a pointer
426 // of some sort. This is because the expression classes store a reference to the manager and it must
427 // therefore be guaranteed that they do not become invalid, because the manager has been copied.
428 ExpressionManager(ExpressionManager const& other) = default;
429 ExpressionManager& operator=(ExpressionManager const& other) = default;
430
437 static bool isValidVariableName(std::string const& name);
438
445 bool variableExists(std::string const& name) const;
446
457 Variable declareOrGetVariable(std::string const& name, storm::expressions::Type const& variableType, bool auxiliary, bool checkName);
458
465 uint_fast64_t getNumberOfVariables(storm::expressions::Type const& variableType) const;
466
467 // The set of all known variables.
468 std::set<Variable> variableSet;
469
470 // A mapping from all variable names (auxiliary + normal) to their indices.
471 std::unordered_map<std::string, uint_fast64_t> nameToIndexMapping;
472
473 // A mapping from all variable indices to their names.
474 std::unordered_map<uint64_t, std::string> indexToNameMapping;
475
476 // A mapping from all variable indices to their types.
477 std::unordered_map<uint64_t, Type> indexToTypeMapping;
478
479 // Store counts for variables.
480 uint_fast64_t numberOfBooleanVariables;
481 uint_fast64_t numberOfIntegerVariables;
482 uint_fast64_t numberOfBitVectorVariables;
483 uint_fast64_t numberOfRationalVariables;
484 uint_fast64_t numberOfArrayVariables;
485 uint_fast64_t numberOfStringVariables;
486
487 // A counter used to create fresh variables.
488 uint_fast64_t freshVariableCounter;
489
490 // The types managed by this manager.
491 mutable boost::optional<Type> booleanType;
492 mutable boost::optional<Type> integerType;
493 mutable std::unordered_set<Type> bitvectorTypes;
494 mutable boost::optional<Type> rationalType;
495 mutable std::unordered_set<Type> arrayTypes;
496 mutable boost::optional<Type> transcendentalNumberType;
497 mutable boost::optional<Type> stringType;
498
499 // A mask that can be used to query whether a variable is an auxiliary variable.
500 static const uint64_t auxiliaryMask = (1ull << 50);
501
502 // A mask that can be used to project a variable index to its offset (with the group of equally typed variables).
503 static const uint64_t offsetMask = (1ull << 50) - 1;
504};
505
506std::ostream& operator<<(std::ostream& out, ExpressionManager const& manager);
507} // namespace expressions
508} // namespace storm
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.
Variable declareStringVariable(std::string const &name, bool auxiliary=false)
Declares a new string variable with the given name.
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.
uint_fast64_t getNumberOfStringVariables() const
Retrieves the number of string 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.
Type const & getStringType() const
Retrieves the string type.
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
bool operator==(VariableIterator const &other) const
VariableIterator(VariableIterator &&other)=default
VariableIterator(ExpressionManager const &manager, std::unordered_map< std::string, uint_fast64_t >::const_iterator nameIndexIterator, std::unordered_map< std::string, uint_fast64_t >::const_iterator nameIndexIteratorEnd, VariableSelection const &selection)
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) const
std::pair< storm::expressions::Variable, storm::expressions::Type > const value_type
std::ostream & operator<<(std::ostream &stream, BaseExpression const &expression)