4#include <boost/algorithm/string/join.hpp>
14 std::string
const& filename, uint_fast64_t lineNumber)
16 likelihoodExpression(likelihoodExpression),
17 assignments(assignments),
18 variableToAssignmentIndexMap(),
19 globalIndex(globalIndex) {
21 [&likelihoodExpression]() {
22 auto simplified = likelihoodExpression.
simplify();
23 return !simplified.
isLiteral() || simplified.evaluateAsRational() >= 0;
25 "Negative likelihood expressions are not allowed.");
27 bool smaller = assignment1.getVariable().getType().isBooleanType() && !assignment2.getVariable().getType().isBooleanType();
29 smaller = assignment1.getVariable() < assignment2.getVariable();
33 this->createAssignmentMapping();
37 return likelihoodExpression;
40std::size_t Update::getNumberOfAssignments()
const {
41 return this->assignments.size();
44std::vector<storm::prism::Assignment>
const& Update::getAssignments()
const {
45 return this->assignments;
48std::vector<storm::prism::Assignment>& Update::getAssignments() {
49 return this->assignments;
53 auto const& variableIndexPair = this->variableToAssignmentIndexMap.find(variableName);
54 STORM_LOG_THROW(variableIndexPair != this->variableToAssignmentIndexMap.end(), storm::exceptions::OutOfRangeException,
55 "Variable '" << variableName <<
"' is not assigned in update.");
56 return this->getAssignments()[variableIndexPair->second];
59std::map<storm::expressions::Variable, storm::expressions::Expression> Update::getAsVariableToExpressionMap()
const {
60 std::map<storm::expressions::Variable, storm::expressions::Expression> result;
62 for (
auto const& assignment : this->getAssignments()) {
63 result.emplace(assignment.getVariable(), assignment.getExpression());
69uint_fast64_t Update::getGlobalIndex()
const {
70 return this->globalIndex;
73void Update::createAssignmentMapping() {
74 this->variableToAssignmentIndexMap.clear();
75 for (uint_fast64_t assignmentIndex = 0; assignmentIndex < this->getAssignments().size(); ++assignmentIndex) {
76 this->variableToAssignmentIndexMap[this->getAssignments()[assignmentIndex].getVariableName()] = assignmentIndex;
80Update Update::substitute(std::map<storm::expressions::Variable, storm::expressions::Expression>
const& substitution)
const {
81 std::vector<Assignment> newAssignments;
82 newAssignments.reserve(this->getNumberOfAssignments());
83 for (
auto const& assignment : this->getAssignments()) {
84 newAssignments.emplace_back(assignment.substitute(substitution));
86 auto newLikelihoodExpression = this->getLikelihoodExpression().
substitute(substitution).
simplify();
87 STORM_LOG_THROW(!newLikelihoodExpression.isLiteral() || newLikelihoodExpression.evaluateAsRational() >= 0, storm::exceptions::IllegalArgumentException,
88 "Substitution yielding negative probabilities in '" << this->getLikelihoodExpression() <<
"' are not allowed.");
89 return Update(this->getGlobalIndex(), newLikelihoodExpression, newAssignments, this->getFilename(), this->getLineNumber());
92Update Update::substituteNonStandardPredicates()
const {
93 std::vector<Assignment> newAssignments;
94 newAssignments.reserve(this->getNumberOfAssignments());
95 for (
auto const& assignment : this->getAssignments()) {
96 newAssignments.emplace_back(assignment.substituteNonStandardPredicates());
99 STORM_LOG_THROW(!newLikelihoodExpression.isLiteral() || newLikelihoodExpression.evaluateAsRational() >= 0, storm::exceptions::IllegalArgumentException,
100 "Substitution yielding negative probabilities in '" << this->getLikelihoodExpression() <<
"' are not allowed.");
101 return Update(this->getGlobalIndex(), newLikelihoodExpression, newAssignments, this->getFilename(), this->getLineNumber());
104Update Update::removeIdentityAssignments()
const {
105 std::vector<Assignment> newAssignments;
106 newAssignments.reserve(getNumberOfAssignments());
107 for (
auto const& ass : this->assignments) {
108 if (!ass.isIdentity()) {
109 newAssignments.push_back(ass);
112 return Update(this->globalIndex, this->likelihoodExpression, newAssignments, getFilename(), getLineNumber());
116 std::vector<Assignment> newAssignments;
117 newAssignments.reserve(getNumberOfAssignments());
118 for (
auto const& ass : this->assignments) {
119 Assignment newAssignment(ass.getVariable(), ass.getExpression().simplify().reduceNesting(), ass.getFilename(), ass.getLineNumber());
121 newAssignments.push_back(std::move(newAssignment));
124 return Update(this->globalIndex, this->likelihoodExpression.simplify().reduceNesting(), newAssignments, getFilename(), getLineNumber());
127std::ostream& operator<<(std::ostream& stream,
Update const& update) {
132 std::vector<std::string> assignmentsAsStrings;
134 std::stringstream stream;
135 stream << assignment;
136 assignmentsAsStrings.push_back(stream.str());
138 stream << boost::algorithm::join(assignmentsAsStrings,
" & ");
Expression simplify() const
Simplifies the expression according to some basic rules.
bool isLiteral() const
Retrieves whether the expression is a literal.
bool isIdentity() const
Checks whether the assignment is an identity (lhs equals rhs)
Update substituteNonStandardPredicates() 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::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)