Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
JaniExpressionSubstitutionVisitor.cpp
Go to the documentation of this file.
2
4
5namespace storm {
6
7namespace jani {
9 std::map<storm::expressions::Variable, storm::expressions::Expression> const& identifierToExpressionMap,
10 bool const substituteTranscendentalNumbers) {
12 identifierToExpressionMap, substituteTranscendentalNumbers)
13 .substitute(expression);
14}
15
17 storm::expressions::Expression const& expression,
18 std::unordered_map<storm::expressions::Variable, storm::expressions::Expression> const& identifierToExpressionMap,
19 bool const substituteTranscendentalNumbers) {
21 identifierToExpressionMap, substituteTranscendentalNumbers)
22 .substitute(expression);
23}
24} // namespace jani
25
26namespace expressions {
27
28template<typename MapType>
30 bool const substituteTranscendentalNumbers)
31 : SubstitutionVisitor<MapType>(variableToExpressionMapping), shallSubstituteTranscendentalNumbers(substituteTranscendentalNumbers) {
32 // Intentionally left empty.
33}
34
35template<typename MapType>
36boost::any JaniExpressionSubstitutionVisitor<MapType>::visit(ValueArrayExpression const& expression, boost::any const& data) {
37 uint64_t size = expression.size()->evaluateAsInt();
38 std::vector<std::shared_ptr<BaseExpression const>> newElements;
39 newElements.reserve(size);
40 bool changed = false;
41 for (uint64_t i = 0; i < size; ++i) {
42 newElements.push_back(boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.at(i)->accept(*this, data)));
43 changed = changed || expression.at(i).get() != newElements.back().get();
44 }
45
46 if (changed) {
47 return std::const_pointer_cast<BaseExpression const>(
48 std::shared_ptr<BaseExpression>(new ValueArrayExpression(expression.getManager(), expression.getType(), newElements)));
49 } else {
50 return expression.getSharedPointer();
51 }
52}
53
54template<typename MapType>
55boost::any JaniExpressionSubstitutionVisitor<MapType>::visit(ConstructorArrayExpression const& expression, boost::any const& data) {
56 std::shared_ptr<BaseExpression const> newSize = boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.size()->accept(*this, data));
57 std::shared_ptr<BaseExpression const> elementExpression =
58 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getElementExpression()->accept(*this, data));
59 STORM_LOG_THROW(this->variableToExpressionMapping.find(expression.getIndexVar()) == this->variableToExpressionMapping.end(),
60 storm::exceptions::InvalidArgumentException, "substitution of the index variable of a constructorArrayExpression is not possible.");
61 // If the arguments did not change, we simply push the expression itself.
62 if (newSize.get() == expression.size().get() && elementExpression.get() == expression.getElementExpression().get()) {
63 return expression.getSharedPointer();
64 } else {
65 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(
66 new ConstructorArrayExpression(expression.getManager(), expression.getType(), newSize, expression.getIndexVar(), elementExpression)));
67 }
68}
69
70template<typename MapType>
71boost::any JaniExpressionSubstitutionVisitor<MapType>::visit(ArrayAccessExpression const& expression, boost::any const& data) {
72 std::shared_ptr<BaseExpression const> firstExpression =
73 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getFirstOperand()->accept(*this, data));
74 std::shared_ptr<BaseExpression const> secondExpression =
75 boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getSecondOperand()->accept(*this, data));
76
77 // If the arguments did not change, we simply push the expression itself.
78 if (firstExpression.get() == expression.getFirstOperand().get() && secondExpression.get() == expression.getSecondOperand().get()) {
79 return expression.getSharedPointer();
80 } else {
81 return std::const_pointer_cast<BaseExpression const>(
82 std::shared_ptr<BaseExpression>(new ArrayAccessExpression(expression.getManager(), expression.getType(), firstExpression, secondExpression)));
83 }
84}
85
86template<typename MapType>
87boost::any JaniExpressionSubstitutionVisitor<MapType>::visit(FunctionCallExpression const& expression, boost::any const& data) {
88 std::vector<std::shared_ptr<BaseExpression const>> newArguments;
89 newArguments.reserve(expression.getNumberOfArguments());
90 for (uint64_t i = 0; i < expression.getNumberOfArguments(); ++i) {
91 newArguments.push_back(boost::any_cast<std::shared_ptr<BaseExpression const>>(expression.getArgument(i)->accept(*this, data)));
92 }
93 return std::const_pointer_cast<BaseExpression const>(std::shared_ptr<BaseExpression>(
94 new FunctionCallExpression(expression.getManager(), expression.getType(), expression.getFunctionIdentifier(), newArguments)));
95}
96
97template<typename MapType>
99 if (shallSubstituteTranscendentalNumbers) {
100 return std::const_pointer_cast<BaseExpression const>(
101 std::shared_ptr<BaseExpression>(new RationalLiteralExpression(expression.getManager(), expression.evaluateAsDouble())));
102 }
103 // No substitution requested for transcendental numbers
104 return expression.getSharedPointer();
105}
106
107// Explicitly instantiate the class with map and unordered_map.
110
111} // namespace expressions
112} // namespace storm
ExpressionManager const & getManager() const
Retrieves the manager responsible for this expression.
Type const & getType() const
Retrieves the type of the expression.
std::shared_ptr< BaseExpression const > getSharedPointer() const
Retrieves a shared pointer to this expression.
std::shared_ptr< BaseExpression const > const & getSecondOperand() const
Retrieves the second operand of the expression.
std::shared_ptr< BaseExpression const > const & getFirstOperand() const
Retrieves the first operand of the expression.
Represents an array of the given size, where the i'th entry is determined by the elementExpression,...
virtual std::shared_ptr< BaseExpression const > size() const override
std::shared_ptr< BaseExpression const > const & getElementExpression() const
storm::expressions::Variable const & getIndexVar() const
Represents an array with a given list of elements.
std::shared_ptr< BaseExpression const > getArgument(uint64_t i) const
virtual boost::any visit(ValueArrayExpression const &expression, boost::any const &data) override
JaniExpressionSubstitutionVisitor(MapType const &variableToExpressionMapping, bool const substituteTranscendentalNumbers)
Creates a new substitution visitor that uses the given map to replace variables.
virtual double evaluateAsDouble(Valuation const *valuation=nullptr) const override
Evaluates the expression under the valuation of unknowns (variables and constants) given by the valua...
Represents an array with a given list of elements.
virtual std::shared_ptr< BaseExpression const > size() const override
virtual std::shared_ptr< BaseExpression const > at(uint64_t i) const override
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
storm::expressions::Expression substituteJaniExpression(storm::expressions::Expression const &expression, std::map< storm::expressions::Variable, storm::expressions::Expression > const &identifierToExpressionMap, bool const substituteTranscendentalNumbers)
LabParser.cpp.
Definition cli.cpp:18