Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SubstitutionVisitor.cpp
Go to the documentation of this file.
1#include <map>
2#include <string>
3#include <unordered_map>
4
7
8namespace storm {
9namespace expressions {
10template<typename MapType>
11SubstitutionVisitor<MapType>::SubstitutionVisitor(MapType const& variableToExpressionMapping) : variableToExpressionMapping(variableToExpressionMapping) {
12 // Intentionally left empty.
13}
14
15template<typename MapType>
17 return Expression(boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getBaseExpression().accept(*this, boost::none)));
18}
19
20template<typename MapType>
21boost::any SubstitutionVisitor<MapType>::visit(IfThenElseExpression const& expression, boost::any const& data) {
22 std::shared_ptr<BaseExpression const> conditionExpression =
23 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getCondition()->accept(*this, data));
24 std::shared_ptr<BaseExpression const> thenExpression =
25 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getThenExpression()->accept(*this, data));
26 std::shared_ptr<BaseExpression const> elseExpression =
27 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getElseExpression()->accept(*this, data));
28
29 // If the arguments did not change, we simply push the expression itself.
30 if (conditionExpression.get() == expression.getCondition().get() && thenExpression.get() == expression.getThenExpression().get() &&
31 elseExpression.get() == expression.getElseExpression().get()) {
32 return expression.getSharedPointer();
33 } else {
34 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(
35 new IfThenElseExpression(expression.getManager(), expression.getType(), conditionExpression, thenExpression, elseExpression)));
36 }
37}
38
39template<typename MapType>
40boost::any SubstitutionVisitor<MapType>::visit(BinaryBooleanFunctionExpression const& expression, boost::any const& data) {
41 std::shared_ptr<BaseExpression const> firstExpression =
42 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getFirstOperand()->accept(*this, data));
43 std::shared_ptr<BaseExpression const> secondExpression =
44 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getSecondOperand()->accept(*this, data));
45
46 // If the arguments did not change, we simply push the expression itself.
47 if (firstExpression.get() == expression.getFirstOperand().get() && secondExpression.get() == expression.getSecondOperand().get()) {
48 return expression.getSharedPointer();
49 } else {
50 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(new BinaryBooleanFunctionExpression(
51 expression.getManager(), expression.getType(), firstExpression, secondExpression, expression.getOperatorType())));
52 }
53}
54
55template<typename MapType>
56boost::any SubstitutionVisitor<MapType>::visit(BinaryNumericalFunctionExpression const& expression, boost::any const& data) {
57 std::shared_ptr<BaseExpression const> firstExpression =
58 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getFirstOperand()->accept(*this, data));
59 std::shared_ptr<BaseExpression const> secondExpression =
60 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getSecondOperand()->accept(*this, data));
61
62 // If the arguments did not change, we simply push the expression itself.
63 if (firstExpression.get() == expression.getFirstOperand().get() && secondExpression.get() == expression.getSecondOperand().get()) {
64 return expression.getSharedPointer();
65 } else {
66 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(new BinaryNumericalFunctionExpression(
67 expression.getManager(), expression.getType(), firstExpression, secondExpression, expression.getOperatorType())));
68 }
69}
70
71template<typename MapType>
72boost::any SubstitutionVisitor<MapType>::visit(BinaryRelationExpression const& expression, boost::any const& data) {
73 std::shared_ptr<BaseExpression const> firstExpression =
74 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getFirstOperand()->accept(*this, data));
75 std::shared_ptr<BaseExpression const> secondExpression =
76 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getSecondOperand()->accept(*this, data));
77
78 // If the arguments did not change, we simply push the expression itself.
79 if (firstExpression.get() == expression.getFirstOperand().get() && secondExpression.get() == expression.getSecondOperand().get()) {
80 return expression.getSharedPointer();
81 } else {
82 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(
83 new BinaryRelationExpression(expression.getManager(), expression.getType(), firstExpression, secondExpression, expression.getRelationType())));
84 }
85}
86
87template<typename MapType>
88boost::any SubstitutionVisitor<MapType>::visit(VariableExpression const& expression, boost::any const&) {
89 // If the variable is in the key set of the substitution, we need to replace it.
90 auto const& nameExpressionPair = this->variableToExpressionMapping.find(expression.getVariable());
91 if (nameExpressionPair != this->variableToExpressionMapping.end()) {
92 return nameExpressionPair->second.getBaseExpressionPointer();
93 } else {
94 return expression.getSharedPointer();
95 }
96}
97
98template<typename MapType>
99boost::any SubstitutionVisitor<MapType>::visit(UnaryBooleanFunctionExpression const& expression, boost::any const& data) {
100 std::shared_ptr<BaseExpression const> operandExpression =
101 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getOperand()->accept(*this, data));
102
103 // If the argument did not change, we simply push the expression itself.
104 if (operandExpression.get() == expression.getOperand().get()) {
105 return expression.getSharedPointer();
106 } else {
107 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(
108 new UnaryBooleanFunctionExpression(expression.getManager(), expression.getType(), operandExpression, expression.getOperatorType())));
109 }
110}
111
112template<typename MapType>
113boost::any SubstitutionVisitor<MapType>::visit(UnaryNumericalFunctionExpression const& expression, boost::any const& data) {
114 std::shared_ptr<BaseExpression const> operandExpression =
115 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getOperand()->accept(*this, data));
116
117 // If the argument did not change, we simply push the expression itself.
118 if (operandExpression.get() == expression.getOperand().get()) {
119 return expression.getSharedPointer();
120 } else {
121 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(
122 new UnaryNumericalFunctionExpression(expression.getManager(), expression.getType(), operandExpression, expression.getOperatorType())));
123 }
124}
125
126template<typename MapType>
127boost::any SubstitutionVisitor<MapType>::visit(PredicateExpression const& expression, boost::any const& data) {
128 bool changed = false;
129 std::vector<std::shared_ptr<BaseExpression const>> newExpressions;
130 for (uint64_t i = 0; i < expression.getArity(); ++i) {
131 newExpressions.push_back(boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getOperand(i)->accept(*this, data)));
132 if (!changed && newExpressions.back() != expression.getOperand(i)) {
133 changed = true;
134 }
135 }
136
137 // If the arguments did not change, we simply push the expression itself.
138 if (!changed) {
139 return expression.getSharedPointer();
140 } else {
141 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(
142 new PredicateExpression(expression.getManager(), expression.getType(), newExpressions, expression.getPredicateType())));
143 }
144}
145
146template<typename MapType>
147boost::any SubstitutionVisitor<MapType>::visit(BooleanLiteralExpression const& expression, boost::any const&) {
148 return expression.getSharedPointer();
149}
150
151template<typename MapType>
152boost::any SubstitutionVisitor<MapType>::visit(IntegerLiteralExpression const& expression, boost::any const&) {
153 return expression.getSharedPointer();
154}
155
156template<typename MapType>
157boost::any SubstitutionVisitor<MapType>::visit(RationalLiteralExpression const& expression, boost::any const&) {
158 return expression.getSharedPointer();
159}
160
161// Explicitly instantiate the class with map and unordered_map.
164} // namespace expressions
165} // namespace storm
virtual boost::any accept(ExpressionVisitor &visitor, boost::any const &data) const =0
Accepts the given visitor by calling its visit method.
ExpressionManager const & getManager() const
Retrieves the manager responsible for this expression.
Type const & getType() const
Retrieves the type of the expression.
std::shared_ptr< BaseExpression const > getSharedPointer() const
Retrieves a shared pointer to this expression.
OperatorType getOperatorType() const
Retrieves the operator associated with the expression.
std::shared_ptr< BaseExpression const > const & getSecondOperand() const
Retrieves the second operand of the expression.
std::shared_ptr< BaseExpression const > const & getFirstOperand() const
Retrieves the first operand of the expression.
OperatorType getOperatorType() const
Retrieves the operator associated with the expression.
RelationType getRelationType() const
Retrieves the relation associated with the expression.
BaseExpression const & getBaseExpression() const
Retrieves the base expression underlying this expression object.
std::shared_ptr< BaseExpression const > getElseExpression() const
Retrieves the else expression of the if-then-else expression.
std::shared_ptr< BaseExpression const > getCondition() const
Retrieves the condition expression of the if-then-else expression.
std::shared_ptr< BaseExpression const > getThenExpression() const
Retrieves the then expression of the if-then-else expression.
The base class of all binary expressions.
virtual std::shared_ptr< BaseExpression const > getOperand(uint_fast64_t operandIndex) const override
Retrieves the given operand from the expression.
PredicateType getPredicateType() const
Retrieves the relation associated with the expression.
virtual uint_fast64_t getArity() const override
Returns the arity of the expression.
Expression substitute(Expression const &expression)
Substitutes the identifiers in the given expression according to the previously given map and returns...
virtual boost::any visit(IfThenElseExpression const &expression, boost::any const &data) override
SubstitutionVisitor(MapType const &variableToExpressionMapping)
Creates a new substitution visitor that uses the given map to replace variables.
OperatorType getOperatorType() const
Retrieves the operator associated with this expression.
virtual std::shared_ptr< BaseExpression const > getOperand(uint_fast64_t operandIndex) const override
Retrieves the given operand from the expression.
OperatorType getOperatorType() const
Retrieves the operator associated with this expression.
Variable const & getVariable() const
Retrieves the variable associated with this expression.
LabParser.cpp.
Definition cli.cpp:18