4#include <boost/algorithm/string/join.hpp>
14 std::string
const& filename, uint_fast64_t lineNumber)
15 :
Update(globalIndex,
std::make_pair(likelihoodExpression,
storm::expressions::Expression()), assignments, filename, lineNumber) {
30 auto simplified = expression.
simplify();
34Update::Update(uint_fast64_t globalIndex,
ExpressionPair const& likelihoodExpressionInterval, std::vector<storm::prism::Assignment>
const& assignments,
35 std::string
const& filename, uint_fast64_t lineNumber)
37 likelihoodExpressions(likelihoodExpressionInterval),
38 assignments(assignments),
39 variableToAssignmentIndexMap(),
40 globalIndex(globalIndex) {
41 STORM_LOG_ASSERT(likelihoodExpressions.first.isInitialized(),
"likelihoodExpression must be initialized");
44 "Negative likelihood expressions are not allowed. Got " << likelihoodExpressions.first <<
".");
46 "Negative likelihood expressions are not allowed. Got " << likelihoodExpressions.second <<
".");
48 bool smaller = assignment1.getVariable().getType().isBooleanType() && !assignment2.getVariable().getType().isBooleanType();
50 smaller = assignment1.getVariable() < assignment2.getVariable();
54 this->createAssignmentMapping();
57bool Update::isLikelihoodInterval()
const {
58 return likelihoodExpressions.second.isInitialized();
62 STORM_LOG_ASSERT(!isLikelihoodInterval(),
"Cannot retrieve likelihood expression of an interval update.");
63 return likelihoodExpressions.first;
67 STORM_LOG_ASSERT(isLikelihoodInterval(),
"Cannot retrieve likelihood expressions of non-interval update.");
68 return likelihoodExpressions;
71std::size_t Update::getNumberOfAssignments()
const {
72 return this->assignments.size();
75std::vector<storm::prism::Assignment>
const& Update::getAssignments()
const {
76 return this->assignments;
79std::vector<storm::prism::Assignment>& Update::getAssignments() {
80 return this->assignments;
84 auto const& variableIndexPair = this->variableToAssignmentIndexMap.find(variableName);
85 STORM_LOG_THROW(variableIndexPair != this->variableToAssignmentIndexMap.end(), storm::exceptions::OutOfRangeException,
86 "Variable '" << variableName <<
"' is not assigned in update.");
87 return this->getAssignments()[variableIndexPair->second];
90std::map<storm::expressions::Variable, storm::expressions::Expression> Update::getAsVariableToExpressionMap()
const {
91 std::map<storm::expressions::Variable, storm::expressions::Expression> result;
93 for (
auto const& assignment : this->getAssignments()) {
94 result.emplace(assignment.getVariable(), assignment.getExpression());
100uint_fast64_t Update::getGlobalIndex()
const {
101 return this->globalIndex;
104void Update::createAssignmentMapping() {
105 this->variableToAssignmentIndexMap.clear();
106 for (uint_fast64_t assignmentIndex = 0; assignmentIndex < this->getAssignments().size(); ++assignmentIndex) {
107 this->variableToAssignmentIndexMap[this->getAssignments()[assignmentIndex].getVariableName()] = assignmentIndex;
111Update Update::substitute(std::map<storm::expressions::Variable, storm::expressions::Expression>
const& substitution)
const {
112 std::vector<Assignment> newAssignments;
113 newAssignments.reserve(this->getNumberOfAssignments());
114 for (
auto const& assignment : this->getAssignments()) {
115 newAssignments.emplace_back(assignment.substitute(substitution));
120 "Substitution yielding negative probabilities in '" << expr <<
"' are not allowed.");
123 if (isLikelihoodInterval()) {
124 ExpressionPair newLikelihoodExpression(subsAndSimpl(this->getLikelihoodExpressionInterval().first),
125 subsAndSimpl(this->getLikelihoodExpressionInterval().second));
126 return Update(this->getGlobalIndex(), newLikelihoodExpression, newAssignments, this->getFilename(), this->getLineNumber());
128 auto newLikelihoodExpression = subsAndSimpl(this->getLikelihoodExpression());
129 return Update(this->getGlobalIndex(), newLikelihoodExpression, newAssignments, this->getFilename(), this->getLineNumber());
133Update Update::substituteNonStandardPredicates()
const {
134 std::vector<Assignment> newAssignments;
135 newAssignments.reserve(this->getNumberOfAssignments());
136 for (
auto const& assignment : this->getAssignments()) {
137 newAssignments.emplace_back(assignment.substituteNonStandardPredicates());
142 "Predicate substitution yielding negative probabilities in '" << expr <<
"' are not allowed.");
145 if (isLikelihoodInterval()) {
146 ExpressionPair newLikelihoodExpression(subsAndSimpl(this->getLikelihoodExpressionInterval().first),
147 subsAndSimpl(this->getLikelihoodExpressionInterval().second));
148 return Update(this->getGlobalIndex(), newLikelihoodExpression, newAssignments, this->getFilename(), this->getLineNumber());
150 auto newLikelihoodExpression = subsAndSimpl(this->getLikelihoodExpression());
151 return Update(this->getGlobalIndex(), newLikelihoodExpression, newAssignments, this->getFilename(), this->getLineNumber());
155Update Update::removeIdentityAssignments()
const {
156 std::vector<Assignment> newAssignments;
157 newAssignments.reserve(getNumberOfAssignments());
158 for (
auto const& ass : this->assignments) {
159 if (!ass.isIdentity()) {
160 newAssignments.push_back(ass);
163 return Update(this->globalIndex, this->likelihoodExpressions, newAssignments, getFilename(), getLineNumber());
167 std::vector<Assignment> newAssignments;
168 newAssignments.reserve(getNumberOfAssignments());
169 for (
auto const& ass : this->assignments) {
170 Assignment newAssignment(ass.getVariable(), ass.getExpression().simplify().reduceNesting(), ass.getFilename(), ass.getLineNumber());
172 newAssignments.push_back(std::move(newAssignment));
175 if (isLikelihoodInterval()) {
176 ExpressionPair simplLikelihoodExpr(getLikelihoodExpressionInterval().first.simplify().reduceNesting(),
177 getLikelihoodExpressionInterval().second.simplify().reduceNesting());
178 return Update(this->globalIndex, simplLikelihoodExpr, newAssignments, getFilename(), getLineNumber());
180 return Update(this->globalIndex, getLikelihoodExpression().simplify().reduceNesting(), newAssignments, getFilename(), getLineNumber());
184std::ostream& operator<<(std::ostream& stream,
Update const& update) {
193 std::vector<std::string> assignmentsAsStrings;
195 std::stringstream stream;
196 stream << assignment;
197 assignmentsAsStrings.push_back(stream.str());
199 stream << boost::algorithm::join(assignmentsAsStrings,
" & ");
Expression simplify() const
Simplifies the expression according to some basic rules.
storm::RationalNumber evaluateAsRational() const
Evaluates the expression and returns the resulting rational number.
bool isLiteral() const
Retrieves whether the expression is a literal.
bool isInitialized() const
Checks whether the object encapsulates a base-expression.
bool isIdentity() const
Checks whether the assignment is an identity (lhs equals rhs)
Update substituteNonStandardPredicates() const
bool isLikelihoodInterval() const
Update substitute(std::map< storm::expressions::Variable, storm::expressions::Expression > const &substitution) const
Substitutes all identifiers in the update according to the given map.
Update simplify() const
Simplifies the update in various ways (also removes identity assignments)
std::pair< storm::expressions::Expression, storm::expressions::Expression > ExpressionPair
ExpressionPair const & getLikelihoodExpressionInterval() const
Retrieves the two expression for the interval likelihood of this update.
std::vector< storm::prism::Assignment > const & getAssignments() const
Retrieves a reference to the map of variable names to their respective assignments.
storm::expressions::Expression const & getLikelihoodExpression() const
Retrieves the expression for the likelihood of this update.
#define STORM_LOG_ASSERT(cond, message)
#define STORM_LOG_THROW(cond, exception, message)
bool isValidLikelihood(storm::expressions::Expression const &expression)
bool isValidLikelihoodSimpl(storm::expressions::Expression const &simplifiedExpression)
helper to assert valid likelihood expressions