Storm
A Modern Probabilistic Model Checker
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BinaryBooleanFunctionExpression.cpp
Go to the documentation of this file.
2#include "Expressions.h"
9
10namespace storm {
11namespace expressions {
13 std::shared_ptr<BaseExpression const> const& firstOperand,
14 std::shared_ptr<BaseExpression const> const& secondOperand, OperatorType operatorType)
15 : BinaryExpression(manager, type, firstOperand, secondOperand), operatorType(operatorType) {
16 // Intentionally left empty.
17}
18
22
44
46 STORM_LOG_THROW(this->hasBooleanType(), storm::exceptions::InvalidTypeException, "Unable to evaluate expression as boolean.");
47
48 bool firstOperandEvaluation = this->getFirstOperand()->evaluateAsBool(valuation);
49 bool secondOperandEvaluation = this->getSecondOperand()->evaluateAsBool(valuation);
50
51 bool result;
52 switch (this->getOperatorType()) {
54 result = firstOperandEvaluation && secondOperandEvaluation;
55 break;
57 result = firstOperandEvaluation || secondOperandEvaluation;
58 break;
60 result = firstOperandEvaluation ^ secondOperandEvaluation;
61 break;
63 result = !firstOperandEvaluation || secondOperandEvaluation;
64 break;
66 result = (firstOperandEvaluation && secondOperandEvaluation) || (!firstOperandEvaluation && !secondOperandEvaluation);
67 break;
68 }
69
70 return result;
71}
72
73std::shared_ptr<BaseExpression const> BinaryBooleanFunctionExpression::simplify() const {
74 std::shared_ptr<BaseExpression const> firstOperandSimplified = this->getFirstOperand()->simplify();
75 std::shared_ptr<BaseExpression const> secondOperandSimplified = this->getSecondOperand()->simplify();
76
77 if (firstOperandSimplified->isLiteral() || secondOperandSimplified->isLiteral()) {
78 switch (this->getOperatorType()) {
80 if (firstOperandSimplified->isTrue()) {
81 return secondOperandSimplified;
82 } else if (firstOperandSimplified->isFalse()) {
83 return firstOperandSimplified;
84 } else if (secondOperandSimplified->isTrue()) {
85 return firstOperandSimplified;
86 } else if (secondOperandSimplified->isFalse()) {
87 return secondOperandSimplified;
88 }
89 break;
91 if (firstOperandSimplified->isTrue()) {
92 return firstOperandSimplified;
93 } else if (firstOperandSimplified->isFalse()) {
94 return secondOperandSimplified;
95 } else if (secondOperandSimplified->isTrue()) {
96 return secondOperandSimplified;
97 } else if (secondOperandSimplified->isFalse()) {
98 return firstOperandSimplified;
99 }
100 break;
102 if (firstOperandSimplified->isTrue()) {
103 if (secondOperandSimplified->isFalse()) {
104 return firstOperandSimplified;
105 } else if (secondOperandSimplified->isTrue()) {
106 return this->getManager().boolean(false).getBaseExpressionPointer();
107 }
108 } else if (firstOperandSimplified->isFalse()) {
109 if (secondOperandSimplified->isTrue()) {
110 return secondOperandSimplified;
111 } else if (secondOperandSimplified->isFalse()) {
112 return this->getManager().boolean(false).getBaseExpressionPointer();
113 }
114 }
115 break;
117 if (firstOperandSimplified->isTrue()) {
118 return secondOperandSimplified;
119 } else if (firstOperandSimplified->isFalse()) {
120 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), true));
121 } else if (secondOperandSimplified->isTrue()) {
122 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), true));
123 }
124 break;
126 if (firstOperandSimplified->isTrue() && secondOperandSimplified->isTrue()) {
127 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), true));
128 } else if (firstOperandSimplified->isFalse() && secondOperandSimplified->isFalse()) {
129 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), true));
130 } else if (firstOperandSimplified->isTrue() && secondOperandSimplified->isFalse()) {
131 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), false));
132 } else if (firstOperandSimplified->isFalse() && secondOperandSimplified->isTrue()) {
133 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), false));
134 }
135 if (firstOperandSimplified->isTrue()) {
136 return secondOperandSimplified;
137 }
138 if (secondOperandSimplified->isTrue()) {
139 return firstOperandSimplified;
140 }
141 if (firstOperandSimplified->isFalse()) {
142 return std::shared_ptr<BaseExpression>(new UnaryBooleanFunctionExpression(this->getManager(), this->getType(), secondOperandSimplified,
144 }
145 if (secondOperandSimplified->isFalse()) {
146 return std::shared_ptr<BaseExpression>(new UnaryBooleanFunctionExpression(this->getManager(), this->getType(), firstOperandSimplified,
148 }
149 break;
150 }
151 }
152
153 // If the two successors remain unchanged, we can return a shared_ptr to this very object.
154 if (firstOperandSimplified.get() == this->getFirstOperand().get() && secondOperandSimplified.get() == this->getSecondOperand().get()) {
155 return this->shared_from_this();
156 } else {
157 return std::shared_ptr<BaseExpression>(
158 new BinaryBooleanFunctionExpression(this->getManager(), this->getType(), firstOperandSimplified, secondOperandSimplified, this->getOperatorType()));
159 }
160}
161
162boost::any BinaryBooleanFunctionExpression::accept(ExpressionVisitor& visitor, boost::any const& data) const {
163 return visitor.visit(*this, data);
164}
165
169
170void BinaryBooleanFunctionExpression::printToStream(std::ostream& stream) const {
171 stream << "(" << *this->getFirstOperand();
172 switch (this->getOperatorType()) {
174 stream << " & ";
175 break;
176 case OperatorType::Or:
177 stream << " | ";
178 break;
180 stream << " != ";
181 break;
183 stream << " => ";
184 break;
186 stream << " = ";
187 break;
188 }
189 stream << *this->getSecondOperand() << ")";
190}
191} // namespace expressions
192} // namespace storm
bool hasBooleanType() const
Retrieves whether the expression has a boolean type.
ExpressionManager const & getManager() const
Retrieves the manager responsible for this expression.
Type const & getType() const
Retrieves the type of the expression.
virtual boost::any accept(ExpressionVisitor &visitor, boost::any const &data) const override
Accepts the given visitor by calling its visit method.
BinaryBooleanFunctionExpression(ExpressionManager const &manager, Type const &type, std::shared_ptr< BaseExpression const > const &firstOperand, std::shared_ptr< BaseExpression const > const &secondOperand, OperatorType operatorType)
Creates a binary boolean function expression with the given return type, operands and operator.
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 std::shared_ptr< BaseExpression const > simplify() const override
Simplifies the expression according to some simple rules.
OperatorType getOperatorType() const
Retrieves the operator associated with the expression.
OperatorType
An enum type specifying the different operators applicable.
virtual void printToStream(std::ostream &stream) const override
Prints the expression to the given stream.
virtual storm::expressions::OperatorType getOperator() const override
Retrieves the operator of a function application.
The base class of all binary expressions.
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.
std::shared_ptr< BaseExpression const > const & getBaseExpressionPointer() const
Retrieves a pointer to the base expression underlying this expression object.
This class is responsible for managing a set of typed variables and all expressions using these varia...
Expression boolean(bool value) const
Creates an expression that characterizes the given boolean literal.
virtual boost::any visit(IfThenElseExpression const &expression, boost::any const &data)=0
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