You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.8 KiB

  1. #ifndef STORM_STORAGE_EXPRESSIONS_EXPRTKEXPRESSIONEVALUATOR_H_
  2. #define STORM_STORAGE_EXPRESSIONS_EXPRTKEXPRESSIONEVALUATOR_H_
  3. #include <memory>
  4. #include <unordered_map>
  5. #include <vector>
  6. #include "src/storage/expressions/ExpressionEvaluatorBase.h"
  7. #include "exprtk.hpp"
  8. #include "src/storage/expressions/ToExprtkStringVisitor.h"
  9. namespace storm {
  10. namespace expressions {
  11. template <typename RationalType>
  12. class ExprtkExpressionEvaluatorBase : public ExpressionEvaluatorBase<RationalType> {
  13. public:
  14. ExprtkExpressionEvaluatorBase(storm::expressions::ExpressionManager const& manager);
  15. bool asBool(Expression const& expression) const override;
  16. int_fast64_t asInt(Expression const& expression) const override;
  17. void setBooleanValue(storm::expressions::Variable const& variable, bool value) override;
  18. void setIntegerValue(storm::expressions::Variable const& variable, int_fast64_t value) override;
  19. void setRationalValue(storm::expressions::Variable const& variable, double value) override;
  20. protected:
  21. typedef double ValueType;
  22. typedef exprtk::expression<ValueType> CompiledExpressionType;
  23. typedef std::unordered_map<std::shared_ptr<BaseExpression const>, CompiledExpressionType> CacheType;
  24. /*!
  25. * Adds a compiled version of the given expression to the internal storage.
  26. *
  27. * @param expression The expression that is to be compiled.
  28. */
  29. CompiledExpressionType& getCompiledExpression(storm::expressions::Expression const& expression) const;
  30. // The parser used.
  31. mutable exprtk::parser<ValueType> parser;
  32. // The symbol table used.
  33. mutable exprtk::symbol_table<ValueType> symbolTable;
  34. // The actual data that is fed into the expression.
  35. std::vector<ValueType> booleanValues;
  36. std::vector<ValueType> integerValues;
  37. std::vector<ValueType> rationalValues;
  38. // A mapping of expressions to their compiled counterpart.
  39. mutable CacheType compiledExpressions;
  40. };
  41. class ExprtkExpressionEvaluator : public ExprtkExpressionEvaluatorBase<double> {
  42. public:
  43. /*!
  44. * Creates an expression evaluator that is capable of evaluating expressions managed by the given manager.
  45. *
  46. * @param manager The manager responsible for the expressions.
  47. */
  48. ExprtkExpressionEvaluator(storm::expressions::ExpressionManager const& manager);
  49. double asRational(Expression const& expression) const override;
  50. };
  51. }
  52. }
  53. #endif /* STORM_STORAGE_EXPRESSIONS_EXPRTKEXPRESSIONEVALUATOR_H_ */