Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
IfThenElseExpression.cpp
Go to the documentation of this file.
2
3#include "ExpressionVisitor.h"
7
8namespace storm {
9namespace expressions {
10IfThenElseExpression::IfThenElseExpression(ExpressionManager const& manager, Type const& type, std::shared_ptr<BaseExpression const> const& condition,
11 std::shared_ptr<BaseExpression const> const& thenExpression,
12 std::shared_ptr<BaseExpression const> const& elseExpression)
13 : BaseExpression(manager, type), condition(condition), thenExpression(thenExpression), elseExpression(elseExpression) {
14 // Intentionally left empty.
15}
16
17std::shared_ptr<BaseExpression const> IfThenElseExpression::getOperand(uint_fast64_t operandIndex) const {
18 STORM_LOG_THROW(operandIndex < 3, storm::exceptions::InvalidAccessException, "Unable to access operand " << operandIndex << " in expression of arity 3.");
19 if (operandIndex == 0) {
20 return this->getCondition();
21 } else if (operandIndex == 1) {
22 return this->getThenExpression();
23 } else {
24 return this->getElseExpression();
25 }
26}
27
31
33 return true;
34}
35
37 return this->getCondition()->containsVariables() || this->getThenExpression()->containsVariables() || this->getElseExpression()->containsVariables();
38}
39
40uint_fast64_t IfThenElseExpression::getArity() const {
41 return 3;
42}
43
44bool IfThenElseExpression::evaluateAsBool(Valuation const* valuation) const {
45 bool conditionValue = this->condition->evaluateAsBool(valuation);
46 if (conditionValue) {
47 return this->thenExpression->evaluateAsBool(valuation);
48 } else {
49 return this->elseExpression->evaluateAsBool(valuation);
50 }
51}
52
53int_fast64_t IfThenElseExpression::evaluateAsInt(Valuation const* valuation) const {
54 bool conditionValue = this->condition->evaluateAsBool(valuation);
55 if (conditionValue) {
56 return this->thenExpression->evaluateAsInt(valuation);
57 } else {
58 return this->elseExpression->evaluateAsInt(valuation);
59 }
60}
61
62double IfThenElseExpression::evaluateAsDouble(Valuation const* valuation) const {
63 bool conditionValue = this->condition->evaluateAsBool(valuation);
64 if (conditionValue) {
65 return this->thenExpression->evaluateAsDouble(valuation);
66 } else {
67 return this->elseExpression->evaluateAsDouble(valuation);
68 }
69}
70
71void IfThenElseExpression::gatherVariables(std::set<storm::expressions::Variable>& variables) const {
72 this->condition->gatherVariables(variables);
73 this->thenExpression->gatherVariables(variables);
74 this->elseExpression->gatherVariables(variables);
75}
76
77std::shared_ptr<BaseExpression const> IfThenElseExpression::simplify() const {
78 std::shared_ptr<BaseExpression const> conditionSimplified = this->condition->simplify();
79 if (conditionSimplified->isTrue()) {
80 return this->thenExpression->simplify();
81 } else if (conditionSimplified->isFalse()) {
82 return this->elseExpression->simplify();
83 } else {
84 std::shared_ptr<BaseExpression const> thenExpressionSimplified = this->thenExpression->simplify();
85 std::shared_ptr<BaseExpression const> elseExpressionSimplified = this->elseExpression->simplify();
86
87 if (conditionSimplified.get() == this->condition.get() && thenExpressionSimplified.get() == this->thenExpression.get() &&
88 elseExpressionSimplified.get() == this->elseExpression.get()) {
89 return this->shared_from_this();
90 } else {
91 return std::shared_ptr<BaseExpression>(
92 new IfThenElseExpression(this->getManager(), this->getType(), conditionSimplified, thenExpressionSimplified, elseExpressionSimplified));
93 }
94 }
95}
96
97boost::any IfThenElseExpression::accept(ExpressionVisitor& visitor, boost::any const& data) const {
98 return visitor.visit(*this, data);
99}
100
102 return true;
103}
104
105std::shared_ptr<BaseExpression const> IfThenElseExpression::getCondition() const {
106 return this->condition;
107}
108
109std::shared_ptr<BaseExpression const> IfThenElseExpression::getThenExpression() const {
110 return this->thenExpression;
111}
112
113std::shared_ptr<BaseExpression const> IfThenElseExpression::getElseExpression() const {
114 return this->elseExpression;
115}
116
117void IfThenElseExpression::printToStream(std::ostream& stream) const {
118 stream << "(" << *this->condition << " ? " << *this->thenExpression << " : " << *this->elseExpression << ")";
119}
120} // namespace expressions
121} // namespace storm
The base class of all expression classes.
ExpressionManager const & getManager() const
Retrieves the manager responsible for this expression.
Type const & getType() const
Retrieves the type of the expression.
This class is responsible for managing a set of typed variables and all expressions using these varia...
virtual boost::any visit(IfThenElseExpression const &expression, boost::any const &data)=0
virtual std::shared_ptr< BaseExpression const > getOperand(uint_fast64_t operandIndex) const override
Retrieves the given operand from the expression.
std::shared_ptr< BaseExpression const > getElseExpression() const
Retrieves the else expression of the if-then-else expression.
IfThenElseExpression(ExpressionManager const &manager, Type const &type, std::shared_ptr< BaseExpression const > const &condition, std::shared_ptr< BaseExpression const > const &thenExpression, std::shared_ptr< BaseExpression const > const &elseExpression)
Creates an if-then-else expression with the given return type, condition and operands.
virtual bool isIfThenElseExpression() const override
virtual bool evaluateAsBool(Valuation const *valuation=nullptr) const override
Evaluates the expression under the valuation of unknowns (variables and constants) given by the valua...
virtual uint_fast64_t getArity() const override
Returns the arity of the expression.
virtual int_fast64_t evaluateAsInt(Valuation const *valuation=nullptr) const override
Evaluates the expression under the valuation of unknowns (variables and constants) given by the valua...
virtual bool isFunctionApplication() const override
Checks if the expression is a function application (of any sort).
virtual bool containsVariables() const override
Retrieves whether the expression contains a variable.
std::shared_ptr< BaseExpression const > getCondition() const
Retrieves the condition expression of the if-then-else expression.
virtual OperatorType getOperator() const override
Retrieves the operator of a function application.
virtual double evaluateAsDouble(Valuation const *valuation=nullptr) const override
Evaluates the expression under the valuation of unknowns (variables and constants) given by the valua...
virtual std::shared_ptr< BaseExpression const > simplify() const override
Simplifies the expression according to some simple rules.
virtual void printToStream(std::ostream &stream) const override
Prints the expression to the given stream.
virtual boost::any accept(ExpressionVisitor &visitor, boost::any const &data) const override
Accepts the given visitor by calling its visit method.
virtual void gatherVariables(std::set< storm::expressions::Variable > &variables) const override
Retrieves the set of all variables that appear in the expression.
std::shared_ptr< BaseExpression const > getThenExpression() const
Retrieves the then expression of the if-then-else expression.
The base class of all valuations of variables.
Definition Valuation.h:16
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
LabParser.cpp.
Definition cli.cpp:18