Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
BinaryRelationExpression.cpp
Go to the documentation of this file.
2
3#include <boost/variant.hpp>
4
12
13namespace storm {
14namespace expressions {
16 std::shared_ptr<BaseExpression const> const& firstOperand,
17 std::shared_ptr<BaseExpression const> const& secondOperand, RelationType relationType)
18 : BinaryExpression(manager, type, firstOperand, secondOperand), relationType(relationType) {
19 // Intentionally left empty.
20}
21
46
48 STORM_LOG_THROW(this->hasBooleanType(), storm::exceptions::InvalidTypeException, "Unable to evaluate expression as boolean.");
49
50 double firstOperandEvaluated = this->getFirstOperand()->evaluateAsDouble(valuation);
51 double secondOperandEvaluated = this->getSecondOperand()->evaluateAsDouble(valuation);
52 bool result = false;
53 switch (this->getRelationType()) {
55 result = firstOperandEvaluated == secondOperandEvaluated;
56 break;
58 result = firstOperandEvaluated != secondOperandEvaluated;
59 break;
61 result = firstOperandEvaluated > secondOperandEvaluated;
62 break;
64 result = firstOperandEvaluated >= secondOperandEvaluated;
65 break;
67 result = firstOperandEvaluated < secondOperandEvaluated;
68 break;
70 result = firstOperandEvaluated <= secondOperandEvaluated;
71 break;
72 }
73 return result;
74}
75
76std::shared_ptr<BaseExpression const> BinaryRelationExpression::simplify() const {
77 std::shared_ptr<BaseExpression const> firstOperandSimplified = this->getFirstOperand()->simplify();
78 std::shared_ptr<BaseExpression const> secondOperandSimplified = this->getSecondOperand()->simplify();
79
80 if (firstOperandSimplified->isLiteral() && secondOperandSimplified->isLiteral()) {
81 storm::RationalNumber firstOperandEvaluation;
82 storm::RationalNumber secondOperandEvaluation;
83
84 if (firstOperandSimplified->hasIntegerType()) {
85 firstOperandEvaluation = storm::utility::convertNumber<storm::RationalNumber>(firstOperandSimplified->evaluateAsInt());
86 } else {
87 firstOperandEvaluation = firstOperandSimplified->evaluateAsRational();
88 }
89 if (secondOperandSimplified->hasIntegerType()) {
90 secondOperandEvaluation = storm::utility::convertNumber<storm::RationalNumber>(secondOperandSimplified->evaluateAsInt());
91 } else {
92 secondOperandEvaluation = secondOperandSimplified->evaluateAsRational();
93 }
94
95 bool truthValue = false;
96 switch (this->getRelationType()) {
98 truthValue = firstOperandEvaluation == secondOperandEvaluation;
99 break;
101 truthValue = firstOperandEvaluation != secondOperandEvaluation;
102 break;
104 truthValue = firstOperandEvaluation > secondOperandEvaluation;
105 break;
107 truthValue = firstOperandEvaluation >= secondOperandEvaluation;
108 break;
110 truthValue = firstOperandEvaluation < secondOperandEvaluation;
111 break;
113 truthValue = firstOperandEvaluation <= secondOperandEvaluation;
114 break;
115 }
116 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), truthValue));
117 }
118
119 if (firstOperandSimplified.get() == this->getFirstOperand().get() && secondOperandSimplified.get() == this->getSecondOperand().get()) {
120 return this->shared_from_this();
121 } else {
122 return std::shared_ptr<BaseExpression>(
123 new BinaryRelationExpression(this->getManager(), this->getType(), firstOperandSimplified, secondOperandSimplified, this->getRelationType()));
124 }
125}
126
127boost::any BinaryRelationExpression::accept(ExpressionVisitor& visitor, boost::any const& data) const {
128 return visitor.visit(*this, data);
129}
130
132 return true;
133}
134
136 return this->relationType;
137}
138
139void BinaryRelationExpression::printToStream(std::ostream& stream) const {
140 stream << "(" << *this->getFirstOperand();
141 switch (this->getRelationType()) {
143 stream << " = ";
144 break;
146 stream << " != ";
147 break;
149 stream << " > ";
150 break;
152 stream << " >= ";
153 break;
155 stream << " < ";
156 break;
158 stream << " <= ";
159 break;
160 }
161 stream << *this->getSecondOperand() << ")";
162}
163} // namespace expressions
164} // 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.
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.
RelationType getRelationType() const
Retrieves the relation associated with the expression.
virtual boost::any accept(ExpressionVisitor &visitor, boost::any const &data) const override
Accepts the given visitor by calling its visit method.
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.
BinaryRelationExpression(ExpressionManager const &manager, Type const &type, std::shared_ptr< BaseExpression const > const &firstOperand, std::shared_ptr< BaseExpression const > const &secondOperand, RelationType relationType)
Creates a binary relation expression with the given return type, operands and relation type.
virtual std::shared_ptr< BaseExpression const > simplify() const override
Simplifies the expression according to some simple rules.
virtual bool evaluateAsBool(Valuation const *valuation=nullptr) const override
Evaluates the expression under the valuation of unknowns (variables and constants) given by the valua...
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
The base class of all valuations of variables.
Definition Valuation.h:16
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
RelationType
An enum type specifying the different relations applicable.
LabParser.cpp.
Definition cli.cpp:18