Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ToDiceStringVisitor.cpp
Go to the documentation of this file.
4
5namespace storm {
6namespace expressions {
7ToDiceStringVisitor::ToDiceStringVisitor(uint64_t nrBits) : nrBits(nrBits) {}
8
9std::string ToDiceStringVisitor::toString(Expression const& expression) {
10 return toString(expression.getBaseExpressionPointer().get());
11}
12
13std::string ToDiceStringVisitor::toString(BaseExpression const* expression) {
14 stream.str("");
15 stream.clear();
16 expression->accept(*this, boost::none);
17 return stream.str();
18}
19
20boost::any ToDiceStringVisitor::visit(IfThenElseExpression const& expression, boost::any const& data) {
21 stream << "if ";
22 expression.getCondition()->accept(*this, data);
23 stream << " then ";
24 expression.getThenExpression()->accept(*this, data);
25 stream << " else ";
26 expression.getElseExpression()->accept(*this, data);
27 stream << "";
28 return boost::any();
29}
30
31boost::any ToDiceStringVisitor::visit(BinaryBooleanFunctionExpression const& expression, boost::any const& data) {
32 switch (expression.getOperatorType()) {
34 stream << "(";
35 expression.getFirstOperand()->accept(*this, data);
36 stream << " && ";
37 expression.getSecondOperand()->accept(*this, data);
38 stream << ")";
39 break;
41 stream << "(";
42 expression.getFirstOperand()->accept(*this, data);
43 stream << " || ";
44 expression.getSecondOperand()->accept(*this, data);
45 stream << ")";
46 break;
48 stream << "(";
49 expression.getFirstOperand()->accept(*this, data);
50 stream << " ^ ";
51 expression.getSecondOperand()->accept(*this, data);
52 stream << ")";
53 break;
55 stream << "(!(";
56 expression.getFirstOperand()->accept(*this, data);
57 stream << ") || ";
58 expression.getSecondOperand()->accept(*this, data);
59 stream << ")";
60 break;
62 expression.getFirstOperand()->accept(*this, data);
63 stream << " <=> ";
64 expression.getSecondOperand()->accept(*this, data);
65 break;
66 }
67 return boost::any();
68}
69
70boost::any ToDiceStringVisitor::visit(BinaryNumericalFunctionExpression const& expression, boost::any const& data) {
71 switch (expression.getOperatorType()) {
73 stream << "(";
74 expression.getFirstOperand()->accept(*this, data);
75 stream << "+";
76 expression.getSecondOperand()->accept(*this, data);
77 stream << ")";
78 break;
80 stream << "(";
81 expression.getFirstOperand()->accept(*this, data);
82 stream << "-";
83 expression.getSecondOperand()->accept(*this, data);
84 stream << ")";
85 break;
87 stream << "(";
88 expression.getFirstOperand()->accept(*this, data);
89 stream << "*";
90 expression.getSecondOperand()->accept(*this, data);
91 stream << ")";
92 break;
94 STORM_LOG_THROW(expression.getSecondOperand()->isIntegerLiteralExpression(), storm::exceptions::NotSupportedException,
95 "Dice does not support modulo with nonconst rhs.");
96 uint64_t denominator = expression.getSecondOperand()->evaluateAsInt();
97 int shifts = 0;
98 while (denominator % 2 == 0) {
99 denominator = denominator >> 1;
100 shifts++;
101 }
102 denominator = denominator >> 1;
103 STORM_LOG_THROW(denominator <= 0, storm::exceptions::NotSupportedException, "Dice does not support division with non-powers of two.");
104 if (shifts > 0) {
105 stream << "(";
106 expression.getFirstOperand()->accept(*this, data);
107 stream << " >> " << shifts;
108 stream << ")";
109 } else {
110 expression.getFirstOperand()->accept(*this, data);
111 }
112
113 } break;
115 stream << "(";
116 expression.getFirstOperand()->accept(*this, data);
117 stream << "^";
118 expression.getSecondOperand()->accept(*this, data);
119 stream << ")";
120 break;
122 STORM_LOG_THROW(expression.getSecondOperand()->isIntegerLiteralExpression(), storm::exceptions::NotSupportedException,
123 "Dice does not support modulo with nonconst rhs.");
124 STORM_LOG_THROW(expression.getSecondOperand()->evaluateAsInt() == 2, storm::exceptions::NotSupportedException,
125 "Dice does not support modulo with rhs != 2.");
126
127 stream << "( nth_bit(int(" << nrBits << "," << nrBits - 1 << "), ";
128 expression.getFirstOperand()->accept(*this, data);
129 stream << "))";
130 break;
132 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Dice translation not supported for log expressions.");
134 stream << "max(";
135 expression.getFirstOperand()->accept(*this, data);
136 stream << ",";
137 expression.getSecondOperand()->accept(*this, data);
138 stream << ")";
139 break;
141 stream << "min(";
142 expression.getFirstOperand()->accept(*this, data);
143 stream << ",";
144 expression.getSecondOperand()->accept(*this, data);
145 stream << ")";
146 break;
147 }
148 return boost::any();
149}
150
151boost::any ToDiceStringVisitor::visit(BinaryRelationExpression const& expression, boost::any const& data) {
152 switch (expression.getRelationType()) {
154 stream << "(";
155 expression.getFirstOperand()->accept(*this, data);
156 stream << "==";
157 expression.getSecondOperand()->accept(*this, data);
158 stream << ")";
159 break;
161 stream << "(";
162 expression.getFirstOperand()->accept(*this, data);
163 stream << "!=";
164 expression.getSecondOperand()->accept(*this, data);
165 stream << ")";
166 break;
168 stream << "(";
169 expression.getFirstOperand()->accept(*this, data);
170 stream << "<";
171 expression.getSecondOperand()->accept(*this, data);
172 stream << ")";
173 break;
175 stream << "(";
176 expression.getFirstOperand()->accept(*this, data);
177 stream << "<=";
178 expression.getSecondOperand()->accept(*this, data);
179 stream << ")";
180 break;
182 stream << "(";
183 expression.getFirstOperand()->accept(*this, data);
184 stream << ">";
185 expression.getSecondOperand()->accept(*this, data);
186 stream << ")";
187 break;
189 stream << "(";
190 expression.getFirstOperand()->accept(*this, data);
191 stream << ">=";
192 expression.getSecondOperand()->accept(*this, data);
193 stream << ")";
194 break;
195 }
196 return boost::any();
197}
198
199boost::any ToDiceStringVisitor::visit(VariableExpression const& expression, boost::any const&) {
200 stream << expression.getVariable().getName();
201 return boost::any();
202}
203
204boost::any ToDiceStringVisitor::visit(UnaryBooleanFunctionExpression const& expression, boost::any const& data) {
205 switch (expression.getOperatorType()) {
207 stream << "!(";
208 expression.getOperand()->accept(*this, data);
209 stream << ")";
210 }
211 return boost::any();
212}
213
214boost::any ToDiceStringVisitor::visit(UnaryNumericalFunctionExpression const& expression, boost::any const& data) {
215 switch (expression.getOperatorType()) {
217 stream << "-(";
218 expression.getOperand()->accept(*this, data);
219 stream << ")";
220 break;
222 stream << "floor(";
223 expression.getOperand()->accept(*this, data);
224 stream << ")";
225 break;
227 stream << "ceil(";
228 expression.getOperand()->accept(*this, data);
229 stream << ")";
230 break;
233 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Dice does not support sin/cos functions.");
234 break;
235 }
236 return boost::any();
237}
238
239boost::any ToDiceStringVisitor::visit(PredicateExpression const& expression, boost::any const& data) {
240 auto const& pdt = expression.getPredicateType();
243 "Only some predicate types are supported.");
244 stream << "(";
246 stream << "(true ";
247 for (uint64_t operandi = 0; operandi < expression.getArity(); ++operandi) {
248 for (uint64_t operandj = operandi + 1; operandj < expression.getArity(); ++operandj) {
249 stream << "&& !(";
250 expression.getOperand(operandi)->accept(*this, data);
251 stream << " && ";
252 expression.getOperand(operandj)->accept(*this, data);
253 stream << ")";
254 }
255 }
256 stream << ")";
257 }
259 stream << " && ";
260 }
262 stream << "( false";
263 for (uint64_t operandj = 0; operandj < expression.getArity(); ++operandj) {
264 stream << "|| ";
265 expression.getOperand(operandj)->accept(*this, data);
266 }
267 stream << ")";
268 }
269 stream << ")";
270 return boost::any();
271}
272
273boost::any ToDiceStringVisitor::visit(BooleanLiteralExpression const& expression, boost::any const&) {
274 stream << (expression.getValue() ? " true " : " false ");
275 return boost::any();
276}
277
278boost::any ToDiceStringVisitor::visit(IntegerLiteralExpression const& expression, boost::any const&) {
279 stream << "int(" << nrBits << "," << expression.getValue() << ")";
280 return boost::any();
281}
282
283boost::any ToDiceStringVisitor::visit(RationalLiteralExpression const& expression, boost::any const&) {
284 stream << std::scientific << std::setprecision(std::numeric_limits<double>::max_digits10) << "(" << expression.getValueAsDouble() << ")";
285 return boost::any();
286}
287} // namespace expressions
288} // namespace storm
The base class of all expression classes.
virtual boost::any accept(ExpressionVisitor &visitor, boost::any const &data) const =0
Accepts the given visitor by calling its visit method.
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.
bool getValue() const
Retrieves the value of the boolean literal.
std::shared_ptr< BaseExpression const > const & getBaseExpressionPointer() const
Retrieves a pointer to 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.
int_fast64_t getValue() const
Retrieves the value of the integer literal.
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.
double getValueAsDouble() const
Retrieves the value of the double literal.
virtual boost::any visit(IfThenElseExpression const &expression, boost::any const &data) override
std::string toString(Expression const &expression)
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.
std::string const & getName() const
Retrieves the name of the variable.
Definition Variable.cpp:46
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:28