24 changed files with 400 additions and 9 deletions
-
5src/storm/storage/expressions/Expression.cpp
-
5src/storm/storage/expressions/Expression.h
-
171src/storm/storage/expressions/SimplificationVisitor.cpp
-
41src/storm/storage/expressions/SimplificationVisitor.h
-
32src/storm/storage/expressions/ToDiceStringVisitor.cpp
-
2src/storm/storage/expressions/ToDiceStringVisitor.h
-
4src/storm/storage/prism/Assignment.cpp
-
2src/storm/storage/prism/Assignment.h
-
4src/storm/storage/prism/BooleanVariable.cpp
-
1src/storm/storage/prism/BooleanVariable.h
-
10src/storm/storage/prism/Command.cpp
-
1src/storm/storage/prism/Command.h
-
9src/storm/storage/prism/Formula.cpp
-
1src/storm/storage/prism/Formula.h
-
4src/storm/storage/prism/IntegerVariable.cpp
-
2src/storm/storage/prism/IntegerVariable.h
-
9src/storm/storage/prism/Label.cpp
-
3src/storm/storage/prism/Label.h
-
22src/storm/storage/prism/Module.cpp
-
2src/storm/storage/prism/Module.h
-
44src/storm/storage/prism/Program.cpp
-
5src/storm/storage/prism/Program.h
-
12src/storm/storage/prism/Update.cpp
-
2src/storm/storage/prism/Update.h
@ -0,0 +1,171 @@ |
|||||
|
#include <map>
|
||||
|
#include <unordered_map>
|
||||
|
#include <string>
|
||||
|
|
||||
|
#include "storm/storage/expressions/SimplificationVisitor.h"
|
||||
|
#include "storm/storage/expressions/Expressions.h"
|
||||
|
#include "storm/storage/expressions/PredicateExpression.h"
|
||||
|
#include "storm/storage/expressions/ExpressionManager.h"
|
||||
|
|
||||
|
namespace storm { |
||||
|
namespace expressions { |
||||
|
SimplificationVisitor::SimplificationVisitor() { |
||||
|
// Intentionally left empty.
|
||||
|
} |
||||
|
|
||||
|
Expression SimplificationVisitor::substitute(Expression const &expression) { |
||||
|
return Expression(boost::any_cast<std::shared_ptr<BaseExpression const>>( |
||||
|
expression.getBaseExpression().accept(*this, boost::none))); |
||||
|
} |
||||
|
|
||||
|
boost::any SimplificationVisitor::visit(IfThenElseExpression const &expression, boost::any const &data) { |
||||
|
std::shared_ptr<BaseExpression const> conditionExpression = boost::any_cast<std::shared_ptr<BaseExpression const>>( |
||||
|
expression.getCondition()->accept(*this, data)); |
||||
|
std::shared_ptr<BaseExpression const> thenExpression = boost::any_cast<std::shared_ptr<BaseExpression const>>( |
||||
|
expression.getThenExpression()->accept(*this, data)); |
||||
|
std::shared_ptr<BaseExpression const> elseExpression = boost::any_cast<std::shared_ptr<BaseExpression const>>( |
||||
|
expression.getElseExpression()->accept(*this, data)); |
||||
|
|
||||
|
// If the arguments did not change, we simply push the expression itself.
|
||||
|
if (conditionExpression.get() == expression.getCondition().get() && |
||||
|
thenExpression.get() == expression.getThenExpression().get() && |
||||
|
elseExpression.get() == expression.getElseExpression().get()) { |
||||
|
return expression.getSharedPointer(); |
||||
|
} else { |
||||
|
return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>( |
||||
|
new IfThenElseExpression(expression.getManager(), expression.getType(), conditionExpression, |
||||
|
thenExpression, elseExpression))); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
boost::any |
||||
|
SimplificationVisitor::visit(BinaryBooleanFunctionExpression const &expression, boost::any const &data) { |
||||
|
std::shared_ptr<BaseExpression const> firstExpression = boost::any_cast<std::shared_ptr<BaseExpression const>>( |
||||
|
expression.getFirstOperand()->accept(*this, data)); |
||||
|
std::shared_ptr<BaseExpression const> secondExpression = boost::any_cast<std::shared_ptr<BaseExpression const>>( |
||||
|
expression.getSecondOperand()->accept(*this, data)); |
||||
|
|
||||
|
// If the arguments did not change, we simply push the expression itself.
|
||||
|
if (firstExpression.get() == expression.getFirstOperand().get() && |
||||
|
secondExpression.get() == expression.getSecondOperand().get()) { |
||||
|
return expression.getSharedPointer(); |
||||
|
} else { |
||||
|
return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>( |
||||
|
new BinaryBooleanFunctionExpression(expression.getManager(), expression.getType(), |
||||
|
firstExpression, secondExpression, |
||||
|
expression.getOperatorType()))); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
boost::any |
||||
|
SimplificationVisitor::visit(BinaryNumericalFunctionExpression const &expression, boost::any const &data) { |
||||
|
std::shared_ptr<BaseExpression const> firstExpression = boost::any_cast<std::shared_ptr<BaseExpression const>>( |
||||
|
expression.getFirstOperand()->accept(*this, data)); |
||||
|
std::shared_ptr<BaseExpression const> secondExpression = boost::any_cast<std::shared_ptr<BaseExpression const>>( |
||||
|
expression.getSecondOperand()->accept(*this, data)); |
||||
|
|
||||
|
// If the arguments did not change, we simply push the expression itself.
|
||||
|
if (firstExpression.get() == expression.getFirstOperand().get() && |
||||
|
secondExpression.get() == expression.getSecondOperand().get()) { |
||||
|
return expression.getSharedPointer(); |
||||
|
} else { |
||||
|
return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>( |
||||
|
new BinaryNumericalFunctionExpression(expression.getManager(), expression.getType(), |
||||
|
firstExpression, secondExpression, |
||||
|
expression.getOperatorType()))); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
boost::any SimplificationVisitor::visit(BinaryRelationExpression const &expression, boost::any const &data) { |
||||
|
std::shared_ptr<BaseExpression const> firstExpression = boost::any_cast<std::shared_ptr<BaseExpression const>>( |
||||
|
expression.getFirstOperand()->accept(*this, data)); |
||||
|
std::shared_ptr<BaseExpression const> secondExpression = boost::any_cast<std::shared_ptr<BaseExpression const>>( |
||||
|
expression.getSecondOperand()->accept(*this, data)); |
||||
|
|
||||
|
// If the arguments did not change, we simply push the expression itself.
|
||||
|
if (firstExpression.get() == expression.getFirstOperand().get() && |
||||
|
secondExpression.get() == expression.getSecondOperand().get()) { |
||||
|
return expression.getSharedPointer(); |
||||
|
} else { |
||||
|
return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>( |
||||
|
new BinaryRelationExpression(expression.getManager(), expression.getType(), firstExpression, |
||||
|
secondExpression, expression.getRelationType()))); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
boost::any SimplificationVisitor::visit(VariableExpression const &expression, boost::any const &) { |
||||
|
|
||||
|
return expression.getSharedPointer(); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
boost::any |
||||
|
SimplificationVisitor::visit(UnaryBooleanFunctionExpression const &expression, boost::any const &data) { |
||||
|
std::shared_ptr<BaseExpression const> operandExpression = boost::any_cast<std::shared_ptr<BaseExpression const>>( |
||||
|
expression.getOperand()->accept(*this, data)); |
||||
|
|
||||
|
// If the argument did not change, we simply push the expression itself.
|
||||
|
if (operandExpression.get() == expression.getOperand().get()) { |
||||
|
return expression.getSharedPointer(); |
||||
|
} else { |
||||
|
return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>( |
||||
|
new UnaryBooleanFunctionExpression(expression.getManager(), expression.getType(), |
||||
|
operandExpression, expression.getOperatorType()))); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
boost::any |
||||
|
SimplificationVisitor::visit(UnaryNumericalFunctionExpression const &expression, boost::any const &data) { |
||||
|
std::shared_ptr<BaseExpression const> operandExpression = boost::any_cast<std::shared_ptr<BaseExpression const>>( |
||||
|
expression.getOperand()->accept(*this, data)); |
||||
|
|
||||
|
// If the argument did not change, we simply push the expression itself.
|
||||
|
if (operandExpression.get() == expression.getOperand().get()) { |
||||
|
return expression.getSharedPointer(); |
||||
|
} else { |
||||
|
return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>( |
||||
|
new UnaryNumericalFunctionExpression(expression.getManager(), expression.getType(), |
||||
|
operandExpression, expression.getOperatorType()))); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
boost::any SimplificationVisitor::visit(PredicateExpression const &expression, boost::any const &data) { |
||||
|
std::vector<Expression> newExpressions; |
||||
|
for (uint64_t i = 0; i < expression.getArity(); ++i) { |
||||
|
newExpressions.emplace_back(boost::any_cast<std::shared_ptr<BaseExpression const>>( |
||||
|
expression.getOperand(i)->accept(*this, data))); |
||||
|
} |
||||
|
std::vector<Expression> newSumExpressions; |
||||
|
for (auto const &expr : newExpressions) { |
||||
|
newSumExpressions.push_back( |
||||
|
ite(expr, expression.getManager().integer(1), expression.getManager().integer(0))); |
||||
|
} |
||||
|
|
||||
|
storm::expressions::Expression finalexpr; |
||||
|
if (expression.getPredicateType() == PredicateExpression::PredicateType::AtLeastOneOf) { |
||||
|
finalexpr = storm::expressions::sum(newSumExpressions) > expression.getManager().integer(0); |
||||
|
} else if (expression.getPredicateType() == PredicateExpression::PredicateType::AtMostOneOf) { |
||||
|
finalexpr = storm::expressions::sum(newSumExpressions) <= expression.getManager().integer(1); |
||||
|
} else if (expression.getPredicateType() == PredicateExpression::PredicateType::ExactlyOneOf) { |
||||
|
finalexpr = storm::expressions::sum(newSumExpressions) == expression.getManager().integer(1); |
||||
|
} else { |
||||
|
STORM_LOG_ASSERT(false, "Unknown predicate type."); |
||||
|
} |
||||
|
return std::const_pointer_cast<BaseExpression const>(finalexpr.getBaseExpressionPointer()); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
boost::any SimplificationVisitor::visit(BooleanLiteralExpression const &expression, boost::any const &) { |
||||
|
return expression.getSharedPointer(); |
||||
|
} |
||||
|
|
||||
|
boost::any SimplificationVisitor::visit(IntegerLiteralExpression const &expression, boost::any const &) { |
||||
|
return expression.getSharedPointer(); |
||||
|
} |
||||
|
|
||||
|
boost::any SimplificationVisitor::visit(RationalLiteralExpression const &expression, boost::any const &) { |
||||
|
return expression.getSharedPointer(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,41 @@ |
|||||
|
#pragma once |
||||
|
#include <stack> |
||||
|
|
||||
|
#include "storm/storage/expressions/Expression.h" |
||||
|
#include "storm/storage/expressions/ExpressionVisitor.h" |
||||
|
|
||||
|
namespace storm { |
||||
|
namespace expressions { |
||||
|
class SimplificationVisitor : public ExpressionVisitor { |
||||
|
public: |
||||
|
/*! |
||||
|
* Creates a new simplification visitor that replaces predicates by other (simpler?) predicates. |
||||
|
* |
||||
|
* Configuration: |
||||
|
* Currently, the visitor only replaces nonstandard predicates |
||||
|
* |
||||
|
*/ |
||||
|
SimplificationVisitor(); |
||||
|
|
||||
|
/*! |
||||
|
* Simplifies based on the configuration. |
||||
|
*/ |
||||
|
Expression substitute(Expression const& expression); |
||||
|
|
||||
|
virtual boost::any visit(IfThenElseExpression const& expression, boost::any const& data) override; |
||||
|
virtual boost::any visit(BinaryBooleanFunctionExpression const& expression, boost::any const& data) override; |
||||
|
virtual boost::any visit(BinaryNumericalFunctionExpression const& expression, boost::any const& data) override; |
||||
|
virtual boost::any visit(BinaryRelationExpression const& expression, boost::any const& data) override; |
||||
|
virtual boost::any visit(VariableExpression const& expression, boost::any const& data) override; |
||||
|
virtual boost::any visit(UnaryBooleanFunctionExpression const& expression, boost::any const& data) override; |
||||
|
virtual boost::any visit(UnaryNumericalFunctionExpression const& expression, boost::any const& data) override; |
||||
|
virtual boost::any visit(BooleanLiteralExpression const& expression, boost::any const& data) override; |
||||
|
virtual boost::any visit(IntegerLiteralExpression const& expression, boost::any const& data) override; |
||||
|
virtual boost::any visit(RationalLiteralExpression const& expression, boost::any const& data) override; |
||||
|
virtual boost::any visit(PredicateExpression const& expression, boost::any const& data) override; |
||||
|
|
||||
|
protected: |
||||
|
// |
||||
|
}; |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue