#include "src/storage/expressions/IfThenElseExpression.h" #include "src/utility/macros.h" #include "src/exceptions/InvalidAccessException.h" namespace storm { namespace expressions { IfThenElseExpression::IfThenElseExpression(ExpressionManager const& manager, ExpressionReturnType returnType, std::shared_ptr const& condition, std::shared_ptr const& thenExpression, std::shared_ptr const& elseExpression) : BaseExpression(manager, returnType), condition(condition), thenExpression(thenExpression), elseExpression(elseExpression) { // Intentionally left empty. } std::shared_ptr IfThenElseExpression::getOperand(uint_fast64_t operandIndex) const { STORM_LOG_THROW(operandIndex < 3, storm::exceptions::InvalidAccessException, "Unable to access operand " << operandIndex << " in expression of arity 3."); if (operandIndex == 0) { return this->getCondition(); } else if (operandIndex == 1) { return this->getThenExpression(); } else { return this->getElseExpression(); } } OperatorType IfThenElseExpression::getOperator() const { return OperatorType::Ite; } bool IfThenElseExpression::isFunctionApplication() const { return true; } bool IfThenElseExpression::containsVariables() const { return this->getCondition()->containsVariables() || this->getThenExpression()->containsVariables() || this->getElseExpression()->containsVariables(); } uint_fast64_t IfThenElseExpression::getArity() const { return 3; } bool IfThenElseExpression::evaluateAsBool(Valuation const* valuation) const { bool conditionValue = this->condition->evaluateAsBool(valuation); if (conditionValue) { return this->thenExpression->evaluateAsBool(valuation); } else { return this->elseExpression->evaluateAsBool(valuation); } } int_fast64_t IfThenElseExpression::evaluateAsInt(Valuation const* valuation) const { bool conditionValue = this->condition->evaluateAsBool(valuation); if (conditionValue) { return this->thenExpression->evaluateAsInt(valuation); } else { return this->elseExpression->evaluateAsInt(valuation); } } double IfThenElseExpression::evaluateAsDouble(Valuation const* valuation) const { bool conditionValue = this->condition->evaluateAsBool(valuation); if (conditionValue) { return this->thenExpression->evaluateAsDouble(valuation); } else { return this->elseExpression->evaluateAsDouble(valuation); } } std::set IfThenElseExpression::getVariables() const { std::set result = this->condition->getVariables(); std::set tmp = this->thenExpression->getVariables(); result.insert(tmp.begin(), tmp.end()); tmp = this->elseExpression->getVariables(); result.insert(tmp.begin(), tmp.end()); return result; } std::shared_ptr IfThenElseExpression::simplify() const { std::shared_ptr conditionSimplified; if (conditionSimplified->isTrue()) { return this->thenExpression->simplify(); } else if (conditionSimplified->isFalse()) { return this->elseExpression->simplify(); } else { std::shared_ptr thenExpressionSimplified = this->thenExpression->simplify(); std::shared_ptr elseExpressionSimplified = this->elseExpression->simplify(); if (conditionSimplified.get() == this->condition.get() && thenExpressionSimplified.get() == this->thenExpression.get() && elseExpressionSimplified.get() == this->elseExpression.get()) { return this->shared_from_this(); } else { return std::shared_ptr(new IfThenElseExpression(this->getManager(), this->getReturnType(), conditionSimplified, thenExpressionSimplified, elseExpressionSimplified)); } } } boost::any IfThenElseExpression::accept(ExpressionVisitor& visitor) const { return visitor.visit(*this); } std::shared_ptr IfThenElseExpression::getCondition() const { return this->condition; } std::shared_ptr IfThenElseExpression::getThenExpression() const { return this->thenExpression; } std::shared_ptr IfThenElseExpression::getElseExpression() const { return this->elseExpression; } void IfThenElseExpression::printToStream(std::ostream& stream) const { stream << "(" << *this->condition << " ? " << *this->thenExpression << " : " << *this->elseExpression << ")"; } } }