Storm
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 assert(predicate == PredicateType::AtMostOneOf || predicate == PredicateType::ExactlyOneOf);
63 trueCount++;
64 simplifiedOperands.push_back(res);
65 }
66 } else {
67 assert(res->isFalse());
68 assert(predicate == PredicateType::AtMostOneOf || predicate == PredicateType::AtLeastOneOf || predicate == PredicateType::ExactlyOneOf);
69 // do nothing, in particular, do not add.
70 }
71 } else {
72 simplifiedOperands.push_back(res);
73 }
74 }
75
76 if (simplifiedOperands.size() == 0) {
77 switch (predicate) {
79 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), trueCount == 1));
81 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), trueCount >= 1));
83 return std::shared_ptr<BaseExpression>(new BooleanLiteralExpression(this->getManager(), trueCount <= 1));
84 }
85 }
86 // Return new expression if something changed.
87 if (simplifiedOperands.size() != operands.size()) {
88 return std::shared_ptr<BaseExpression>(new PredicateExpression(this->getManager(), this->getType(), simplifiedOperands, predicate));
89 }
90 for (uint64_t i = 0; i < simplifiedOperands.size(); ++i) {
91 if (operands[i] != simplifiedOperands[i]) {
92 return std::shared_ptr<BaseExpression>(new PredicateExpression(this->getManager(), this->getType(), simplifiedOperands, predicate));
93 }
94 }
95 // All operands remained the same.
96 return this->shared_from_this();
97}
98
99boost::any PredicateExpression::accept(ExpressionVisitor& visitor, boost::any const& data) const {
100 return visitor.visit(*this, data);
101}
102
104 return true;
105}
106
108 return true;
109}
110
112 for (auto const& operand : operands) {
113 if (operand->containsVariables()) {
114 return true;
115 }
116 }
117 return false;
118}
119
120uint_fast64_t PredicateExpression::getArity() const {
121 return operands.size();
122}
123
124std::shared_ptr<BaseExpression const> PredicateExpression::getOperand(uint_fast64_t operandIndex) const {
125 STORM_LOG_ASSERT(operandIndex < this->getArity(), "Invalid operand access");
126 return operands[operandIndex];
127}
128
129void PredicateExpression::gatherVariables(std::set<storm::expressions::Variable>& variables) const {
130 for (auto const& operand : operands) {
131 operand->gatherVariables(variables);
132 }
133}
134
143
144void PredicateExpression::printToStream(std::ostream& stream) const {
145 switch (this->getPredicateType()) {
147 stream << "atMostOneOf(";
149 stream << "atLeastOneOf(";
151 stream << "exactlyOneOf(";
152 }
153 if (!operands.empty()) {
154 stream << *operands[0];
155 for (uint64_t i = 1; i < operands.size(); i++) {
156 stream << ", " << *operands[i];
157 }
158 }
159 stream << ")";
160}
161} // namespace expressions
162} // namespace storm
The base class of all expression classes.
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.
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 binary expressions.
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:16
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
OperatorType toOperatorType(PredicateExpression::PredicateType tp)
LabParser.cpp.
Definition cli.cpp:18