Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ExprtkExpressionEvaluator.cpp
Go to the documentation of this file.
1#include <string>
2
4
7
9
12
13namespace storm {
14namespace expressions {
15template<typename RationalType>
18 parser(std::make_unique<exprtk::parser<ValueType>>()),
19 symbolTable(std::make_unique<exprtk::symbol_table<ValueType>>()),
20 booleanValues(manager.getNumberOfBooleanVariables()),
21 integerValues(manager.getNumberOfIntegerVariables()),
22 rationalValues(manager.getNumberOfRationalVariables()) {
23 // Since some expressions are very long, we need to increase the stack depth of the exprtk parser.
24 // Otherwise, we'll get
25 // ERR000 - Current stack depth X exceeds maximum allowed stack depth of Y
26 // on some models.
27 parser->settings().set_max_stack_depth(10000);
28 for (auto const& variableTypePair : manager) {
29 if (variableTypePair.second.isBooleanType()) {
30 symbolTable->add_variable("v" + std::to_string(variableTypePair.first.getIndex()), this->booleanValues[variableTypePair.first.getOffset()]);
31 } else if (variableTypePair.second.isIntegerType()) {
32 symbolTable->add_variable("v" + std::to_string(variableTypePair.first.getIndex()), this->integerValues[variableTypePair.first.getOffset()]);
33 } else if (variableTypePair.second.isRationalType()) {
34 symbolTable->add_variable("v" + std::to_string(variableTypePair.first.getIndex()), this->rationalValues[variableTypePair.first.getOffset()]);
35 }
36 }
38
39template<typename RationalType>
41 auto const& compiledExpression = getCompiledExpression(expression);
42 return compiledExpression.value() == ValueType(1);
43}
44
45template<typename RationalType>
47 auto const& compiledExpression = getCompiledExpression(expression);
48 return static_cast<int_fast64_t>(compiledExpression.value());
49}
50
51template<typename RationalType>
53 storm::expressions::Expression const& expression) const {
54 if (!expression.hasCompiledExpression() || !expression.getCompiledExpression().isExprtkCompiledExpression()) {
55 CompiledExpressionType compiledExpression;
56 compiledExpression.register_symbol_table(*symbolTable);
57 bool parsingOk = parser->compile(ToExprtkStringVisitor().toString(expression), compiledExpression);
58 STORM_LOG_THROW(parsingOk, storm::exceptions::UnexpectedException,
59 "Expression was not properly parsed by ExprTk: " << expression << ". (Returned error: " << parser->error() << ")");
60 expression.setCompiledExpression(std::make_shared<ExprtkCompiledExpression>(compiledExpression));
61 }
63}
64
65template<typename RationalType>
67 this->booleanValues[variable.getOffset()] = static_cast<ValueType>(value);
68}
69
70template<typename RationalType>
72 this->integerValues[variable.getOffset()] = static_cast<ValueType>(value);
73}
74
75template<typename RationalType>
77 this->rationalValues[variable.getOffset()] = static_cast<ValueType>(value);
78}
79
83
84double ExprtkExpressionEvaluator::asRational(Expression const& expression) const {
85 auto const& compiledExpression = getCompiledExpression(expression);
86 return static_cast<double>(compiledExpression.value());
87}
88
90
91#ifdef STORM_HAVE_CARL
94#endif
95} // namespace expressions
96} // 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
LabParser.cpp.
Definition cli.cpp:18