Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ExprtkExpressionEvaluator.cpp
Go to the documentation of this file.
2
3#include <string>
4
10
11namespace storm {
12namespace expressions {
13template<typename RationalType>
16 parser(std::make_unique<exprtk::parser<ValueType>>()),
17 symbolTable(std::make_unique<exprtk::symbol_table<ValueType>>()),
18 booleanValues(manager.getNumberOfBooleanVariables()),
19 integerValues(manager.getNumberOfIntegerVariables()),
20 rationalValues(manager.getNumberOfRationalVariables()) {
21 // Since some expressions are very long, we need to increase the stack depth of the exprtk parser.
22 // Otherwise, we'll get
23 // ERR000 - Current stack depth X exceeds maximum allowed stack depth of Y
24 // on some models.
25 parser->settings().set_max_stack_depth(10000);
26 for (auto const& variableTypePair : manager) {
27 if (variableTypePair.second.isBooleanType()) {
28 symbolTable->add_variable("v" + std::to_string(variableTypePair.first.getIndex()), this->booleanValues[variableTypePair.first.getOffset()]);
29 } else if (variableTypePair.second.isIntegerType()) {
30 symbolTable->add_variable("v" + std::to_string(variableTypePair.first.getIndex()), this->integerValues[variableTypePair.first.getOffset()]);
31 } else if (variableTypePair.second.isRationalType()) {
32 symbolTable->add_variable("v" + std::to_string(variableTypePair.first.getIndex()), this->rationalValues[variableTypePair.first.getOffset()]);
33 }
34 }
35}
36
37template<typename RationalType>
39 auto const& compiledExpression = getCompiledExpression(expression);
40 return compiledExpression.value() == ValueType(1);
41}
42
43template<typename RationalType>
45 auto const& compiledExpression = getCompiledExpression(expression);
46 return static_cast<int_fast64_t>(compiledExpression.value());
47}
48
49template<typename RationalType>
51 storm::expressions::Expression const& expression) const {
52 if (!expression.hasCompiledExpression() || !expression.getCompiledExpression().isExprtkCompiledExpression()) {
53 CompiledExpressionType compiledExpression;
54 compiledExpression.register_symbol_table(*symbolTable);
55 bool parsingOk = parser->compile(ToExprtkStringVisitor().toString(expression), compiledExpression);
56 STORM_LOG_THROW(parsingOk, storm::exceptions::UnexpectedException,
57 "Expression was not properly parsed by ExprTk: " << expression << ". (Returned error: " << parser->error() << ")");
58 expression.setCompiledExpression(std::make_shared<ExprtkCompiledExpression>(compiledExpression));
59 }
61}
62
63template<typename RationalType>
65 this->booleanValues[variable.getOffset()] = static_cast<ValueType>(value);
66}
67
68template<typename RationalType>
70 this->integerValues[variable.getOffset()] = static_cast<ValueType>(value);
71}
72
73template<typename RationalType>
75 this->rationalValues[variable.getOffset()] = static_cast<ValueType>(value);
76}
77
81
82double ExprtkExpressionEvaluator::asRational(Expression const& expression) const {
83 auto const& compiledExpression = getCompiledExpression(expression);
84 return static_cast<double>(compiledExpression.value());
85}
86
88
91} // namespace expressions
92} // namespace storm
ExprtkCompiledExpression & asExprtkCompiledExpression()
void setCompiledExpression(std::shared_ptr< CompiledExpression > const &compiledExpression) const
Associates the given compiled expression with this expression object.
bool hasCompiledExpression() const
Retrieves whether this expression object has an associated compiled expression.
CompiledExpression const & getCompiledExpression() const
Retrieves the associated compiled expression object (if there is any).
This class is responsible for managing a set of typed variables and all expressions using these varia...
CompiledExpressionType const & getCompiledExpression() const
void setIntegerValue(storm::expressions::Variable const &variable, int_fast64_t value) override
ExprtkExpressionEvaluatorBase(storm::expressions::ExpressionManager const &manager)
bool asBool(Expression const &expression) const override
void setBooleanValue(storm::expressions::Variable const &variable, bool value) override
int_fast64_t asInt(Expression const &expression) const override
CompiledExpressionType const & getCompiledExpression(storm::expressions::Expression const &expression) const
Retrieves a compiled version of the given expression.
ExprtkCompiledExpression::CompiledExpressionType CompiledExpressionType
void setRationalValue(storm::expressions::Variable const &variable, double value) override
double asRational(Expression const &expression) const override
ExprtkExpressionEvaluator(storm::expressions::ExpressionManager const &manager)
Creates an expression evaluator that is capable of evaluating expressions managed by the given manage...
uint_fast64_t getOffset() const
Retrieves the offset of the variable in the group of all equally typed variables.
Definition Variable.cpp:42
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30