Storm
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 if (denominator > 0) {
104 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Dice does not support division with non-powers of two");
105 }
106 if (shifts > 0) {
107 stream << "(";
108 expression.getFirstOperand()->accept(*this, data);
109 stream << " >> " << shifts;
110 stream << ")";
111 } else {
112 expression.getFirstOperand()->accept(*this, data);
113 }
114
115 } break;
117 stream << "(";
118 expression.getFirstOperand()->accept(*this, data);
119 stream << "^";
120 expression.getSecondOperand()->accept(*this, data);
121 stream << ")";
122 break;
124 STORM_LOG_THROW(expression.getSecondOperand()->isIntegerLiteralExpression(), storm::exceptions::NotSupportedException,
125 "Dice does not support modulo with nonconst rhs");
126 STORM_LOG_THROW(expression.getSecondOperand()->evaluateAsInt() == 2, storm::exceptions::NotSupportedException,
127 "Dice does not support modulo with rhs != 2");
128
129 stream << "( nth_bit(int(" << nrBits << "," << nrBits - 1 << "), ";
130 expression.getFirstOperand()->accept(*this, data);
131 stream << "))";
132 break;
134 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Dice translation not supported for log expressions.");
136 stream << "max(";
137 expression.getFirstOperand()->accept(*this, data);
138 stream << ",";
139 expression.getSecondOperand()->accept(*this, data);
140 stream << ")";
141 break;
143 stream << "min(";
144 expression.getFirstOperand()->accept(*this, data);
145 stream << ",";
146 expression.getSecondOperand()->accept(*this, data);
147 stream << ")";
148 break;
149 }
150 return boost::any();
151}
152
153boost::any ToDiceStringVisitor::visit(BinaryRelationExpression const& expression, boost::any const& data) {
154 switch (expression.getRelationType()) {
156 stream << "(";
157 expression.getFirstOperand()->accept(*this, data);
158 stream << "==";
159 expression.getSecondOperand()->accept(*this, data);
160 stream << ")";
161 break;
163 stream << "(";
164 expression.getFirstOperand()->accept(*this, data);
165 stream << "!=";
166 expression.getSecondOperand()->accept(*this, data);
167 stream << ")";
168 break;
170 stream << "(";
171 expression.getFirstOperand()->accept(*this, data);
172 stream << "<";
173 expression.getSecondOperand()->accept(*this, data);
174 stream << ")";
175 break;
177 stream << "(";
178 expression.getFirstOperand()->accept(*this, data);
179 stream << "<=";
180 expression.getSecondOperand()->accept(*this, data);
181 stream << ")";
182 break;
184 stream << "(";
185 expression.getFirstOperand()->accept(*this, data);
186 stream << ">";
187 expression.getSecondOperand()->accept(*this, data);
188 stream << ")";
189 break;
191 stream << "(";
192 expression.getFirstOperand()->accept(*this, data);
193 stream << ">=";
194 expression.getSecondOperand()->accept(*this, data);
195 stream << ")";
196 break;
197 }
198 return boost::any();
199}
200
201boost::any ToDiceStringVisitor::visit(VariableExpression const& expression, boost::any const&) {
202 stream << expression.getVariable().getName();
203 return boost::any();
204}
205
206boost::any ToDiceStringVisitor::visit(UnaryBooleanFunctionExpression const& expression, boost::any const& data) {
207 switch (expression.getOperatorType()) {
209 stream << "!(";
210 expression.getOperand()->accept(*this, data);
211 stream << ")";
212 }
213 return boost::any();
214}
215
216boost::any ToDiceStringVisitor::visit(UnaryNumericalFunctionExpression const& expression, boost::any const& data) {
217 switch (expression.getOperatorType()) {
219 stream << "-(";
220 expression.getOperand()->accept(*this, data);
221 stream << ")";
222 break;
224 stream << "floor(";
225 expression.getOperand()->accept(*this, data);
226 stream << ")";
227 break;
229 stream << "ceil(";
230 expression.getOperand()->accept(*this, data);
231 stream << ")";
232 break;
233 }
234 return boost::any();
235}
236
237boost::any ToDiceStringVisitor::visit(PredicateExpression const& expression, boost::any const& data) {
238 auto const& pdt = expression.getPredicateType();
241 "Only some predicate types are supported.");
242 stream << "(";
244 stream << "(true ";
245 for (uint64_t operandi = 0; operandi < expression.getArity(); ++operandi) {
246 for (uint64_t operandj = operandi + 1; operandj < expression.getArity(); ++operandj) {
247 stream << "&& !(";
248 expression.getOperand(operandi)->accept(*this, data);
249 stream << " && ";
250 expression.getOperand(operandj)->accept(*this, data);
251 stream << ")";
252 }
253 }
254 stream << ")";
255 }
257 stream << " && ";
258 }
260 stream << "( false";
261 for (uint64_t operandj = 0; operandj < expression.getArity(); ++operandj) {
262 stream << "|| ";
263 expression.getOperand(operandj)->accept(*this, data);
264 }
265 stream << ")";
266 }
267 stream << ")";
268 return boost::any();
269}
270
271boost::any ToDiceStringVisitor::visit(BooleanLiteralExpression const& expression, boost::any const&) {
272 stream << (expression.getValue() ? " true " : " false ");
273 return boost::any();
274}
275
276boost::any ToDiceStringVisitor::visit(IntegerLiteralExpression const& expression, boost::any const&) {
277 stream << "int(" << nrBits << "," << expression.getValue() << ")";
278 return boost::any();
279}
280
281boost::any ToDiceStringVisitor::visit(RationalLiteralExpression const& expression, boost::any const&) {
282 stream << std::scientific << std::setprecision(std::numeric_limits<double>::max_digits10) << "(" << expression.getValueAsDouble() << ")";
283 return boost::any();
284}
285} // namespace expressions
286} // 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:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
LabParser.cpp.
Definition cli.cpp:18