Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
PredicateExpression.cpp
Go to the documentation of this file.
1
9
10namespace storm {
11namespace expressions {
23
24PredicateExpression::PredicateExpression(ExpressionManager const& manager, Type const& type, std::vector<std::shared_ptr<BaseExpression const>> const& operands,
25 PredicateType predicateType)
26 : BaseExpression(manager, type), predicate(predicateType), operands(operands) {}
27
28// Override base class methods.
32
33bool PredicateExpression::evaluateAsBool(Valuation const* valuation) const {
34 STORM_LOG_THROW(this->hasBooleanType(), storm::exceptions::InvalidTypeException, "Unable to evaluate expression as boolean.");
35 uint64_t nrTrue = 0;
36 for (auto const& operand : operands) {
37 if (operand->evaluateAsBool(valuation)) {
38 nrTrue++;
39 }
40 }
41 switch (predicate) {
43 return nrTrue == 1;
45 return nrTrue <= 1;
47 return nrTrue >= 1;
48 }
49 STORM_LOG_THROW(false, storm::exceptions::InvalidTypeException, "Predicate type not supported.");
50}
51
52std::shared_ptr<BaseExpression const> PredicateExpression::simplify() const {
53 std::vector<std::shared_ptr<BaseExpression const>> simplifiedOperands;
54 uint64_t trueCount = 0;
55 for (auto const& operand : operands) {
56 auto res = operand->simplify();
57 if (res->isLiteral()) {
58 if (res->isTrue()) {
59 if (predicate == PredicateType::AtLeastOneOf) {
60 return res;
61 } else {
62 STORM_LOG_ASSERT(predicate == PredicateType::AtMostOneOf || predicate == PredicateType::ExactlyOneOf, "Unexpected predicate.");
63 simplifiedOperands.push_back(res);
64 }
65 } else {
66 STORM_LOG_ASSERT(res->isFalse(), "Expected false literal.");
69 "Unexpected predicate.");
70 // do nothing, in particular, do not add.
71 }
72 } else {
73 simplifiedOperands.push_back(res);
74 }
75 }
76
77 if (simplifiedOperands.size() == 0) {
78 switch (predicate) {
80 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), trueCount == 1));
82 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), trueCount >= 1));
84 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), trueCount <= 1));
85 }
86 }
87 // Return new expression if something changed.
88 if (simplifiedOperands.size() != operands.size()) {
89 return std::shared_ptr<BaseExpression>(new PredicateExpression(this->getManager(), this->getType(), simplifiedOperands, predicate));
90 }
91 for (uint64_t i = 0; i < simplifiedOperands.size(); ++i) {
92 if (operands[i] != simplifiedOperands[i]) {
93 return std::shared_ptr<BaseExpression>(new PredicateExpression(this->getManager(), this->getType(), simplifiedOperands, predicate));
94 }
95 }
96 // All operands remained the same.
97 return this->shared_from_this();
98}
99
100boost::any PredicateExpression::accept(ExpressionVisitor& visitor, boost::any const& data) const {
101 return visitor.visit(*this, data);
102}
103
105 return true;
106}
107
109 return true;
110}
111
113 for (auto const& operand : operands) {
114 if (operand->containsVariables()) {
115 return true;
116 }
117 }
118 return false;
119}
120
121uint_fast64_t PredicateExpression::getArity() const {
122 return operands.size();
123}
124
125std::shared_ptr<BaseExpression const> PredicateExpression::getOperand(uint_fast64_t operandIndex) const {
126 STORM_LOG_ASSERT(operandIndex < this->getArity(), "Invalid operand access.");
127 return operands[operandIndex];
128}
129
130void PredicateExpression::gatherVariables(std::set<storm::expressions::Variable>& variables) const {
131 for (auto const& operand : operands) {
132 operand->gatherVariables(variables);
133 }
134}
135
144
145void PredicateExpression::printToStream(std::ostream& stream) const {
146 switch (this->getPredicateType()) {
148 stream << "atMostOneOf(";
149 break;
151 stream << "atLeastOneOf(";
152 break;
154 stream << "exactlyOneOf(";
155 break;
156 }
157 if (!operands.empty()) {
158 stream << *operands[0];
159 for (uint64_t i = 1; i < operands.size(); i++) {
160 stream << ", " << *operands[i];
161 }
162 }
163 stream << ")";
164}
165} // namespace expressions
166} // 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.
BaseExpression(ExpressionManager const &manager, Type const &type)
Constructs a base expression with the given return type.
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 bool evaluateAsBool(Valuation const *valuation=nullptr) const override
Evaluates the expression under the valuation of unknowns (variables and constants) given by the valua...
PredicateExpression(ExpressionManager const &manager, Type const &type, std::vector< std::shared_ptr< BaseExpression const > > const &operands, PredicateType predicateType)
virtual std::shared_ptr< BaseExpression const > simplify() const override
Simplifies the expression according to some simple rules.
virtual boost::any accept(ExpressionVisitor &visitor, boost::any const &data) const override
Accepts the given visitor by calling its visit method.
virtual std::shared_ptr< BaseExpression const > getOperand(uint_fast64_t operandIndex) const override
Retrieves the given operand from the expression.
PredicateType getPredicateType() const
Retrieves the relation associated with the expression.
virtual uint_fast64_t getArity() const override
Returns the arity of the expression.
virtual void printToStream(std::ostream &stream) const override
Prints the expression to the given stream.
virtual void gatherVariables(std::set< storm::expressions::Variable > &variables) const override
Retrieves the set of all variables that appear in the expression.
virtual bool containsVariables() const override
Retrieves whether the expression contains a variable.
virtual bool isPredicateExpression() const override
virtual bool isFunctionApplication() const override
Checks if the expression is a function application (of any sort).
virtual storm::expressions::OperatorType getOperator() const override
Retrieves the operator of a function application.
The base class of all valuations of variables.
Definition Valuation.h:15
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:28
OperatorType toOperatorType(PredicateExpression::PredicateType tp)