#include "src/storage/expressions/ToRationalNumberVisitor.h" #include "src/utility/macros.h" #include "src/exceptions/InvalidArgumentException.h" #include "src/exceptions/NotSupportedException.h" namespace storm { namespace expressions { template ToRationalNumberVisitor::ToRationalNumberVisitor() : ExpressionVisitor() { // Intentionally left empty. } template RationalNumberType ToRationalNumberVisitor::toRationalNumber(Expression const& expression) { return boost::any_cast(expression.accept(*this)); } template boost::any ToRationalNumberVisitor::visit(IfThenElseExpression const& expression) { STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression cannot be translated into a rational number."); } template boost::any ToRationalNumberVisitor::visit(BinaryBooleanFunctionExpression const& expression) { STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression cannot be translated into a rational number."); } template boost::any ToRationalNumberVisitor::visit(BinaryNumericalFunctionExpression const& expression) { RationalNumberType firstOperandAsRationalFunction = boost::any_cast(expression.getFirstOperand()->accept(*this)); RationalNumberType secondOperandAsRationalFunction = boost::any_cast(expression.getSecondOperand()->accept(*this)); switch(expression.getOperatorType()) { case BinaryNumericalFunctionExpression::OperatorType::Plus: return firstOperandAsRationalFunction + secondOperandAsRationalFunction; break; case BinaryNumericalFunctionExpression::OperatorType::Minus: return firstOperandAsRationalFunction - secondOperandAsRationalFunction; break; case BinaryNumericalFunctionExpression::OperatorType::Times: return firstOperandAsRationalFunction * secondOperandAsRationalFunction; break; case BinaryNumericalFunctionExpression::OperatorType::Divide: return firstOperandAsRationalFunction / secondOperandAsRationalFunction; break; default: STORM_LOG_ASSERT(false, "Illegal operator type."); } // Return a dummy. This point must, however, never be reached. return boost::any(); } template boost::any ToRationalNumberVisitor::visit(BinaryRelationExpression const& expression) { STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression cannot be translated into a rational number."); } template boost::any ToRationalNumberVisitor::visit(VariableExpression const& expression) { STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Cannot transform expressions containing variables to a rational number."); } template boost::any ToRationalNumberVisitor::visit(UnaryBooleanFunctionExpression const& expression) { STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression cannot be translated into a rational number."); } template boost::any ToRationalNumberVisitor::visit(UnaryNumericalFunctionExpression const& expression) { STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression cannot be translated into a rational number."); } template boost::any ToRationalNumberVisitor::visit(BooleanLiteralExpression const& expression) { STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression cannot be translated into a rational number."); } template boost::any ToRationalNumberVisitor::visit(IntegerLiteralExpression const& expression) { #ifdef STORM_HAVE_CARL return RationalNumberType(carl::rationalize(static_cast(expression.getValue()))); #else STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Rational numbers are not supported in this build."); #endif } template boost::any ToRationalNumberVisitor::visit(DoubleLiteralExpression const& expression) { #ifdef STORM_HAVE_CARL return RationalNumberType(carl::rationalize(expression.getValue())); #else STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Rational numbers are not supported in this build."); #endif } #ifdef STORM_HAVE_CARL template class ToRationalNumberVisitor; #endif } }