Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
BinaryRelationExpression.cpp
Go to the documentation of this file.
2
10
11namespace storm {
12namespace expressions {
14 std::shared_ptr<BaseExpression const> const& firstOperand,
15 std::shared_ptr<BaseExpression const> const& secondOperand, RelationType relationType)
16 : BinaryExpression(manager, type, firstOperand, secondOperand), relationType(relationType) {
17 // Intentionally left empty.
18}
19
44
46 STORM_LOG_THROW(this->hasBooleanType(), storm::exceptions::InvalidTypeException, "Unable to evaluate expression as boolean.");
47
48 double firstOperandEvaluated = this->getFirstOperand()->evaluateAsDouble(valuation);
49 double secondOperandEvaluated = this->getSecondOperand()->evaluateAsDouble(valuation);
50 bool result = false;
51 switch (this->getRelationType()) {
53 result = firstOperandEvaluated == secondOperandEvaluated;
54 break;
56 result = firstOperandEvaluated != secondOperandEvaluated;
57 break;
59 result = firstOperandEvaluated > secondOperandEvaluated;
60 break;
62 result = firstOperandEvaluated >= secondOperandEvaluated;
63 break;
65 result = firstOperandEvaluated < secondOperandEvaluated;
66 break;
68 result = firstOperandEvaluated <= secondOperandEvaluated;
69 break;
70 }
71 return result;
72}
73
74std::shared_ptr<BaseExpression const> BinaryRelationExpression::simplify() const {
75 std::shared_ptr<BaseExpression const> firstOperandSimplified = this->getFirstOperand()->simplify();
76 std::shared_ptr<BaseExpression const> secondOperandSimplified = this->getSecondOperand()->simplify();
77
78 if (firstOperandSimplified->isLiteral() && secondOperandSimplified->isLiteral()) {
79 storm::RationalNumber firstOperandEvaluation;
80 storm::RationalNumber secondOperandEvaluation;
81
82 if (firstOperandSimplified->hasIntegerType()) {
83 firstOperandEvaluation = storm::utility::convertNumber<storm::RationalNumber>(firstOperandSimplified->evaluateAsInt());
84 } else {
85 firstOperandEvaluation = firstOperandSimplified->evaluateAsRational();
86 }
87 if (secondOperandSimplified->hasIntegerType()) {
88 secondOperandEvaluation = storm::utility::convertNumber<storm::RationalNumber>(secondOperandSimplified->evaluateAsInt());
89 } else {
90 secondOperandEvaluation = secondOperandSimplified->evaluateAsRational();
91 }
92
93 bool truthValue = false;
94 switch (this->getRelationType()) {
96 truthValue = firstOperandEvaluation == secondOperandEvaluation;
97 break;
99 truthValue = firstOperandEvaluation != secondOperandEvaluation;
100 break;
102 truthValue = firstOperandEvaluation > secondOperandEvaluation;
103 break;
105 truthValue = firstOperandEvaluation >= secondOperandEvaluation;
106 break;
108 truthValue = firstOperandEvaluation < secondOperandEvaluation;
109 break;
111 truthValue = firstOperandEvaluation <= secondOperandEvaluation;
112 break;
113 }
114 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), truthValue));
115 }
116
117 if (firstOperandSimplified.get() == this->getFirstOperand().get() && secondOperandSimplified.get() == this->getSecondOperand().get()) {
118 return this->shared_from_this();
119 } else {
120 return std::shared_ptr<BaseExpression>(
121 new BinaryRelationExpression(this->getManager(), this->getType(), firstOperandSimplified, secondOperandSimplified, this->getRelationType()));
122 }
123}
124
125boost::any BinaryRelationExpression::accept(ExpressionVisitor& visitor, boost::any const& data) const {
126 return visitor.visit(*this, data);
127}
128
130 return true;
131}
132
134 return this->relationType;
135}
136
137void BinaryRelationExpression::printToStream(std::ostream& stream) const {
138 stream << "(" << *this->getFirstOperand();
139 switch (this->getRelationType()) {
141 stream << " = ";
142 break;
144 stream << " != ";
145 break;
147 stream << " > ";
148 break;
150 stream << " >= ";
151 break;
153 stream << " < ";
154 break;
156 stream << " <= ";
157 break;
158 }
159 stream << *this->getSecondOperand() << ")";
160}
161} // namespace expressions
162} // 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.