Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ReduceNestingVisitor.cpp
Go to the documentation of this file.
1#include <string>
2
5
6namespace storm {
7namespace expressions {
8
10 // Intentionally left empty.
11}
12
14 return Expression(boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getBaseExpression().accept(*this, boost::none)));
15}
16
17boost::any ReduceNestingVisitor::visit(IfThenElseExpression const& expression, boost::any const& data) {
18 std::shared_ptr<BaseExpression const> conditionExpression =
19 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getCondition()->accept(*this, data));
20 std::shared_ptr<BaseExpression const> thenExpression =
21 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getThenExpression()->accept(*this, data));
22 std::shared_ptr<BaseExpression const> elseExpression =
23 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getElseExpression()->accept(*this, data));
25 // If the arguments did not change, we simply push the expression itself.
26 if (conditionExpression.get() == expression.getCondition().get() && thenExpression.get() == expression.getThenExpression().get() &&
27 elseExpression.get() == expression.getElseExpression().get()) {
28 return expression.getSharedPointer();
29 } else {
30 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(
31 new IfThenElseExpression(expression.getManager(), expression.getType(), conditionExpression, thenExpression, elseExpression)));
32 }
33}
34
35template<typename BinaryFunc>
36std::vector<std::shared_ptr<BaseExpression const>> getAllOperands(BinaryFunc const& binaryExpression) {
37 auto opType = binaryExpression.getOperatorType();
38 std::vector<std::shared_ptr<BaseExpression const>> stack = {binaryExpression.getSharedPointer()};
39 std::vector<std::shared_ptr<BaseExpression const>> res;
40 while (!stack.empty()) {
41 auto f = std::move(stack.back());
42 stack.pop_back();
43
44 for (uint64_t opIndex = 0; opIndex < 2; ++opIndex) {
45 BinaryFunc const* subexp = dynamic_cast<BinaryFunc const*>(f->getOperand(opIndex).get());
46 if (subexp != nullptr && subexp->getOperatorType() == opType) {
47 stack.push_back(f->getOperand(opIndex));
48 } else {
49 res.push_back(f->getOperand(opIndex));
50 }
51 }
52 }
53 return res;
54}
55
56boost::any ReduceNestingVisitor::visit(BinaryBooleanFunctionExpression const& expression, boost::any const& data) {
57 // Check if the operator is commutative and associative
61 std::vector<std::shared_ptr<BaseExpression const>> operands = getAllOperands<BinaryBooleanFunctionExpression>(expression);
62
63 // Balance the syntax tree if there are enough operands
64 if (operands.size() >= 4) {
65 for (auto& operand : operands) {
66 operand = boost::any_cast<std::shared_ptr<BaseExpression const>>(operand->accept(*this, data));
67 }
68
69 auto opIt = operands.begin();
70 while (operands.size() > 1) {
71 if (opIt == operands.end() || opIt == operands.end() - 1) {
72 opIt = operands.begin();
73 }
74 *opIt = std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(
75 new BinaryBooleanFunctionExpression(expression.getManager(), expression.getType(), *opIt, operands.back(), expression.getOperatorType())));
76 operands.pop_back();
77 ++opIt;
78 }
79 return operands.front();
80 }
81 }
82
83 std::shared_ptr<BaseExpression const> firstExpression =
84 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getFirstOperand()->accept(*this, data));
85 std::shared_ptr<BaseExpression const> secondExpression =
86 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getSecondOperand()->accept(*this, data));
87
88 // If the arguments did not change, we simply push the expression itself.
89 if (firstExpression.get() == expression.getFirstOperand().get() && secondExpression.get() == expression.getSecondOperand().get()) {
90 return expression.getSharedPointer();
91 } else {
92 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(new BinaryBooleanFunctionExpression(
93 expression.getManager(), expression.getType(), firstExpression, secondExpression, expression.getOperatorType())));
94 }
95}
96
97boost::any ReduceNestingVisitor::visit(BinaryNumericalFunctionExpression const& expression, boost::any const& data) {
98 // Check if the operator is commutative and associative
103 std::vector<std::shared_ptr<BaseExpression const>> operands = getAllOperands<BinaryNumericalFunctionExpression>(expression);
104
105 // Balance the syntax tree if there are enough operands
106 if (operands.size() >= 4) {
107 for (auto& operand : operands) {
108 operand = boost::any_cast<std::shared_ptr<BaseExpression const>>(operand->accept(*this, data));
109 }
110
111 auto opIt = operands.begin();
112 while (operands.size() > 1) {
113 if (opIt == operands.end() || opIt == operands.end() - 1) {
114 opIt = operands.begin();
115 }
116 *opIt = std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(new BinaryNumericalFunctionExpression(
117 expression.getManager(), expression.getType(), *opIt, operands.back(), expression.getOperatorType())));
118 operands.pop_back();
119 ++opIt;
120 }
121 return operands.front();
122 }
123 }
124
125 std::shared_ptr<BaseExpression const> firstExpression =
126 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getFirstOperand()->accept(*this, data));
127 std::shared_ptr<BaseExpression const> secondExpression =
128 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getSecondOperand()->accept(*this, data));
129
130 // If the arguments did not change, we simply push the expression itself.
131 if (firstExpression.get() == expression.getFirstOperand().get() && secondExpression.get() == expression.getSecondOperand().get()) {
132 return expression.getSharedPointer();
133 } else {
134 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(new BinaryNumericalFunctionExpression(
135 expression.getManager(), expression.getType(), firstExpression, secondExpression, expression.getOperatorType())));
136 }
137}
138
139boost::any ReduceNestingVisitor::visit(BinaryRelationExpression const& expression, boost::any const& data) {
140 std::shared_ptr<BaseExpression const> firstExpression =
141 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getFirstOperand()->accept(*this, data));
142 std::shared_ptr<BaseExpression const> secondExpression =
143 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getSecondOperand()->accept(*this, data));
144
145 // If the arguments did not change, we simply push the expression itself.
146 if (firstExpression.get() == expression.getFirstOperand().get() && secondExpression.get() == expression.getSecondOperand().get()) {
147 return expression.getSharedPointer();
148 } else {
149 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(
150 new BinaryRelationExpression(expression.getManager(), expression.getType(), firstExpression, secondExpression, expression.getRelationType())));
151 }
152}
153
154boost::any ReduceNestingVisitor::visit(VariableExpression const& expression, boost::any const&) {
155 return expression.getSharedPointer();
156}
157
158boost::any ReduceNestingVisitor::visit(UnaryBooleanFunctionExpression const& expression, boost::any const& data) {
159 std::shared_ptr<BaseExpression const> operandExpression =
160 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getOperand()->accept(*this, data));
161
162 // If the argument did not change, we simply push the expression itself.
163 if (operandExpression.get() == expression.getOperand().get()) {
164 return expression.getSharedPointer();
165 } else {
166 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(
167 new UnaryBooleanFunctionExpression(expression.getManager(), expression.getType(), operandExpression, expression.getOperatorType())));
168 }
169}
170
171boost::any ReduceNestingVisitor::visit(UnaryNumericalFunctionExpression const& expression, boost::any const& data) {
172 std::shared_ptr<BaseExpression const> operandExpression =
173 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getOperand()->accept(*this, data));
174
175 // If the argument did not change, we simply push the expression itself.
176 if (operandExpression.get() == expression.getOperand().get()) {
177 return expression.getSharedPointer();
178 } else {
179 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(
180 new UnaryNumericalFunctionExpression(expression.getManager(), expression.getType(), operandExpression, expression.getOperatorType())));
181 }
182}
183
184boost::any ReduceNestingVisitor::visit(BooleanLiteralExpression const& expression, boost::any const&) {
185 return expression.getSharedPointer();
186}
187
188boost::any ReduceNestingVisitor::visit(IntegerLiteralExpression const& expression, boost::any const&) {
189 return expression.getSharedPointer();
190}
191
192boost::any ReduceNestingVisitor::visit(RationalLiteralExpression const& expression, boost::any const&) {
193 return expression.getSharedPointer();
194}
195
196} // namespace expressions
197} // 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.
Expression reduceNesting(Expression const &expression)
Reduces the nesting in the given expression.
ReduceNestingVisitor()
Creates a new reduce nesting visitor.
virtual boost::any visit(IfThenElseExpression const &expression, boost::any const &data) override
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.
std::vector< std::shared_ptr< BaseExpression const > > getAllOperands(BinaryFunc const &binaryExpression)
LabParser.cpp.
Definition cli.cpp:18