Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ToRationalNumberVisitor.cpp
Go to the documentation of this file.
2
6
7namespace storm {
8namespace expressions {
9template<typename RationalNumberType>
13
14template<typename RationalNumberType>
16 : ExpressionVisitor(), evaluator(evaluator) {
17 // Intentionally left empty.
18}
19
20template<typename RationalNumberType>
22 return boost::any_cast<RationalNumberType>(expression.accept(*this, boost::none));
23}
24
25template<typename RationalNumberType>
26boost::any ToRationalNumberVisitor<RationalNumberType>::visit(IfThenElseExpression const& expression, boost::any const& data) {
27 bool conditionValue;
28 if (evaluator) {
29 conditionValue = evaluator->asBool(expression.getCondition());
30 } else {
31 // no evaluator, fall back to evaluateBool
32 conditionValue = expression.getCondition()->evaluateAsBool();
33 }
34 if (conditionValue) {
35 return expression.getThenExpression()->accept(*this, data);
36 } else {
37 return expression.getElseExpression()->accept(*this, data);
38 }
39}
40
41template<typename RationalNumberType>
43 STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression cannot be translated into a rational number.");
44}
45
46template<typename RationalNumberType>
48 RationalNumberType firstOperandAsRationalNumber = boost::any_cast<RationalNumberType>(expression.getFirstOperand()->accept(*this, data));
49 RationalNumberType secondOperandAsRationalNumber = boost::any_cast<RationalNumberType>(expression.getSecondOperand()->accept(*this, data));
50 RationalNumberType result;
51 switch (expression.getOperatorType()) {
53 result = firstOperandAsRationalNumber + secondOperandAsRationalNumber;
54 return result;
56 result = firstOperandAsRationalNumber - secondOperandAsRationalNumber;
57 return result;
59 result = firstOperandAsRationalNumber * secondOperandAsRationalNumber;
60 return result;
62 result = firstOperandAsRationalNumber / secondOperandAsRationalNumber;
63 return result;
65 result = std::min(firstOperandAsRationalNumber, secondOperandAsRationalNumber);
66 return result;
68 result = std::max(firstOperandAsRationalNumber, secondOperandAsRationalNumber);
69 return result;
71 STORM_LOG_THROW(storm::utility::isInteger(secondOperandAsRationalNumber), storm::exceptions::InvalidArgumentException,
72 "Exponent of power operator must be an integer.");
73 auto exponentAsInteger = storm::utility::convertNumber<int_fast64_t>(secondOperandAsRationalNumber);
74 result = storm::utility::pow(firstOperandAsRationalNumber, exponentAsInteger);
75 return result;
76 }
77 default:
78 STORM_LOG_ASSERT(false, "Illegal operator type.");
79 }
80
81 // Return a dummy. This point must, however, never be reached.
82 STORM_LOG_ASSERT(false, "Illegal operator type.");
83 return boost::any();
84}
85
86template<typename RationalNumberType>
88 STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression cannot be translated into a rational number.");
89}
90
91template<typename RationalNumberType>
92boost::any ToRationalNumberVisitor<RationalNumberType>::visit(VariableExpression const& expression, boost::any const&) {
93 return valueMapping.at(expression.getVariable());
94}
95
96template<typename RationalNumberType>
98 STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression cannot be translated into a rational number.");
99}
100
101template<typename RationalNumberType>
103 RationalNumberType operandAsRationalNumber = boost::any_cast<RationalNumberType>(expression.getOperand()->accept(*this, data));
104 RationalNumberType result;
105 switch (expression.getOperatorType()) {
107 result = -operandAsRationalNumber;
108 return result;
109 break;
111 result = storm::utility::floor(operandAsRationalNumber);
112 return result;
113 break;
115 result = storm::utility::ceil(operandAsRationalNumber);
116 return result;
117 break;
118 }
119 // Dummy return.
120 return result;
121}
122
123template<typename RationalNumberType>
125 STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression cannot be translated into a rational number.");
126}
127
128template<typename RationalNumberType>
129boost::any ToRationalNumberVisitor<RationalNumberType>::visit(IntegerLiteralExpression const& expression, boost::any const&) {
130 return RationalNumberType(carl::rationalize<storm::RationalNumber>(static_cast<carl::sint>(expression.getValue())));
131}
132
133template<typename RationalNumberType>
135 return expression.getValue();
136}
137
138template<typename RationalNumberType>
139void ToRationalNumberVisitor<RationalNumberType>::setMapping(storm::expressions::Variable const& variable, RationalNumberType const& value) {
140 valueMapping[variable] = value;
141}
142
144
145} // namespace expressions
146} // namespace storm
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.
boost::any accept(ExpressionVisitor &visitor, boost::any const &data) const
Accepts the given visitor.
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.
storm::RationalNumber getValue() const
Retrieves the value of the double literal.
RationalNumberType toRationalNumber(Expression const &expression)
void setMapping(storm::expressions::Variable const &variable, RationalNumberType const &value)
virtual boost::any visit(IfThenElseExpression const &expression, boost::any const &data) override
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.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
ValueType floor(ValueType const &number)
ValueType ceil(ValueType const &number)
bool isInteger(ValueType const &number)
Definition constants.cpp:76
ValueType pow(ValueType const &value, int_fast64_t exponent)
LabParser.cpp.
Definition cli.cpp:18