#include #include #include #include "storm/storage/expressions/SubstitutionVisitor.h" #include "storm/storage/expressions/Expressions.h" namespace storm { namespace expressions { template SubstitutionVisitor::SubstitutionVisitor(MapType const& variableToExpressionMapping) : variableToExpressionMapping(variableToExpressionMapping) { // Intentionally left empty. } template Expression SubstitutionVisitor::substitute(Expression const& expression) { return Expression(boost::any_cast>(expression.getBaseExpression().accept(*this, boost::none))); } template boost::any SubstitutionVisitor::visit(IfThenElseExpression const& expression, boost::any const& data) { std::shared_ptr conditionExpression = boost::any_cast>(expression.getCondition()->accept(*this, data)); std::shared_ptr thenExpression = boost::any_cast>(expression.getThenExpression()->accept(*this, data)); std::shared_ptr elseExpression = boost::any_cast>(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(std::shared_ptr(new IfThenElseExpression(expression.getManager(), expression.getType(), conditionExpression, thenExpression, elseExpression))); } } template boost::any SubstitutionVisitor::visit(BinaryBooleanFunctionExpression const& expression, boost::any const& data) { std::shared_ptr firstExpression = boost::any_cast>(expression.getFirstOperand()->accept(*this, data)); std::shared_ptr secondExpression = boost::any_cast>(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(std::shared_ptr(new BinaryBooleanFunctionExpression(expression.getManager(), expression.getType(), firstExpression, secondExpression, expression.getOperatorType()))); } } template boost::any SubstitutionVisitor::visit(BinaryNumericalFunctionExpression const& expression, boost::any const& data) { std::shared_ptr firstExpression = boost::any_cast>(expression.getFirstOperand()->accept(*this, data)); std::shared_ptr secondExpression = boost::any_cast>(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(std::shared_ptr(new BinaryNumericalFunctionExpression(expression.getManager(), expression.getType(), firstExpression, secondExpression, expression.getOperatorType()))); } } template boost::any SubstitutionVisitor::visit(BinaryRelationExpression const& expression, boost::any const& data) { std::shared_ptr firstExpression = boost::any_cast>(expression.getFirstOperand()->accept(*this, data)); std::shared_ptr secondExpression = boost::any_cast>(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(std::shared_ptr(new BinaryRelationExpression(expression.getManager(), expression.getType(), firstExpression, secondExpression, expression.getRelationType()))); } } template boost::any SubstitutionVisitor::visit(VariableExpression const& expression, boost::any const&) { // If the variable is in the key set of the substitution, we need to replace it. auto const& nameExpressionPair = this->variableToExpressionMapping.find(expression.getVariable()); if (nameExpressionPair != this->variableToExpressionMapping.end()) { return nameExpressionPair->second.getBaseExpressionPointer(); } else { return expression.getSharedPointer(); } } template boost::any SubstitutionVisitor::visit(UnaryBooleanFunctionExpression const& expression, boost::any const& data) { std::shared_ptr operandExpression = boost::any_cast>(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(std::shared_ptr(new UnaryBooleanFunctionExpression(expression.getManager(), expression.getType(), operandExpression, expression.getOperatorType()))); } } template boost::any SubstitutionVisitor::visit(UnaryNumericalFunctionExpression const& expression, boost::any const& data) { std::shared_ptr operandExpression = boost::any_cast>(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(std::shared_ptr(new UnaryNumericalFunctionExpression(expression.getManager(), expression.getType(), operandExpression, expression.getOperatorType()))); } } template boost::any SubstitutionVisitor::visit(BooleanLiteralExpression const& expression, boost::any const&) { return expression.getSharedPointer(); } template boost::any SubstitutionVisitor::visit(IntegerLiteralExpression const& expression, boost::any const&) { return expression.getSharedPointer(); } template boost::any SubstitutionVisitor::visit(RationalLiteralExpression const& expression, boost::any const&) { return expression.getSharedPointer(); } // Explicitly instantiate the class with map and unordered_map. template class SubstitutionVisitor>; template class SubstitutionVisitor>; } }