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.");
133 break;
135 stream << "max(";
136 expression.getFirstOperand()->accept(*this, data);
137 stream << ",";
138 expression.getSecondOperand()->accept(*this, data);
139 stream << ")";
140 break;
142 stream << "min(";
143 expression.getFirstOperand()->accept(*this, data);
144 stream << ",";
145 expression.getSecondOperand()->accept(*this, data);
146 stream << ")";
147 break;
148 }
149 return boost::any();
150}
151
152boost::any ToDiceStringVisitor::visit(BinaryRelationExpression const& expression, boost::any const& data) {
153 switch (expression.getRelationType()) {
155 stream << "(";
156 expression.getFirstOperand()->accept(*this, data);
157 stream << "==";
158 expression.getSecondOperand()->accept(*this, data);
159 stream << ")";
160 break;
162 stream << "(";
163 expression.getFirstOperand()->accept(*this, data);
164 stream << "!=";
165 expression.getSecondOperand()->accept(*this, data);
166 stream << ")";
167 break;
169 stream << "(";
170 expression.getFirstOperand()->accept(*this, data);
171 stream << "<";
172 expression.getSecondOperand()->accept(*this, data);
173 stream << ")";
174 break;
176 stream << "(";
177 expression.getFirstOperand()->accept(*this, data);
178 stream << "<=";
179 expression.getSecondOperand()->accept(*this, data);
180 stream << ")";
181 break;
183 stream << "(";
184 expression.getFirstOperand()->accept(*this, data);
185 stream << ">";
186 expression.getSecondOperand()->accept(*this, data);
187 stream << ")";
188 break;
190 stream << "(";
191 expression.getFirstOperand()->accept(*this, data);
192 stream << ">=";
193 expression.getSecondOperand()->accept(*this, data);
194 stream << ")";
195 break;
196 }
197 return boost::any();
198}
199
200boost::any ToDiceStringVisitor::visit(VariableExpression const& expression, boost::any const&) {
201 stream << expression.getVariable().getName();
202 return boost::any();
203}
204
205boost::any ToDiceStringVisitor::visit(UnaryBooleanFunctionExpression const& expression, boost::any const& data) {
206 switch (expression.getOperatorType()) {
208 stream << "!(";
209 expression.getOperand()->accept(*this, data);
210 stream << ")";
211 }
212 return boost::any();
213}
214
215boost::any ToDiceStringVisitor::visit(UnaryNumericalFunctionExpression const& expression, boost::any const& data) {
216 switch (expression.getOperatorType()) {
218 stream << "-(";
219 expression.getOperand()->accept(*this, data);
220 stream << ")";
221 break;
223 stream << "floor(";
224 expression.getOperand()->accept(*this, data);
225 stream << ")";
226 break;
228 stream << "ceil(";
229 expression.getOperand()->accept(*this, data);
230 stream << ")";
231 break;
234 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Dice does not support sin/cos functions.");
235 break;
236 }
237 return boost::any();
238}
239
240boost::any ToDiceStringVisitor::visit(PredicateExpression const& expression, boost::any const& data) {
241 auto const& pdt = expression.getPredicateType();
244 "Only some predicate types are supported.");
245 stream << "(";
247 stream << "(true ";
248 for (uint64_t operandi = 0; operandi < expression.getArity(); ++operandi) {
249 for (uint64_t operandj = operandi + 1; operandj < expression.getArity(); ++operandj) {
250 stream << "&& !(";
251 expression.getOperand(operandi)->accept(*this, data);
252 stream << " && ";
253 expression.getOperand(operandj)->accept(*this, data);
254 stream << ")";
255 }
256 }
257 stream << ")";
258 }
260 stream << " && ";
261 }
263 stream << "( false";
264 for (uint64_t operandj = 0; operandj < expression.getArity(); ++operandj) {
265 stream << "|| ";
266 expression.getOperand(operandj)->accept(*this, data);
267 }
268 stream << ")";
269 }
270 stream << ")";
271 return boost::any();
272}
273
274boost::any ToDiceStringVisitor::visit(BooleanLiteralExpression const& expression, boost::any const&) {
275 stream << (expression.getValue() ? " true " : " false ");
276 return boost::any();
277}
278
279boost::any ToDiceStringVisitor::visit(IntegerLiteralExpression const& expression, boost::any const&) {
280 stream << "int(" << nrBits << "," << expression.getValue() << ")";
281 return boost::any();
282}
283
284boost::any ToDiceStringVisitor::visit(RationalLiteralExpression const& expression, boost::any const&) {
285 stream << std::scientific << std::setprecision(std::numeric_limits<double>::max_digits10) << "(" << expression.getValueAsDouble() << ")";
286 return boost::any();
287}
288} // namespace expressions
289} // 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:39