Storm 1.13.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ValueParser.cpp
Go to the documentation of this file.
2
3#include <boost/algorithm/string.hpp>
4#include <boost/lexical_cast.hpp>
5
13
14namespace storm {
15namespace parser {
16
17template<typename ValueType>
18ValueParser<ValueType>::ParametricData::ParametricData()
19 : manager(new storm::expressions::ExpressionManager()),
20 parser(std::make_unique<ExpressionParser>(*manager)),
21 evaluator(new storm::expressions::ExpressionEvaluator<storm::RationalFunction>(*manager)) {
22 // Set empty mapping to enable expression creation even without parameters
23 parser->setIdentifierMapping(identifierMapping);
24}
25
26template<typename ValueType>
27ValueParser<ValueType>::ParametricData::~ParametricData() = default;
28
29template<typename ValueType>
30void ValueParser<ValueType>::addParameter(std::string const& parameter) {
31 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Parameters are not supported in this build (Have you checked storm-pars?).");
32}
33
34template<>
35void ValueParser<storm::RationalFunction>::addParameter(std::string const& parameter) {
36 storm::expressions::Variable var = data.manager->declareRationalVariable(parameter);
37 data.identifierMapping.emplace(var.getName(), var);
38 data.parser->setIdentifierMapping(data.identifierMapping);
39 STORM_LOG_TRACE("Added parameter: " << var.getName());
40}
41
42template<>
44 storm::RationalFunction rationalFunction = data.evaluator->asRational(data.parser->parseFromString(value));
45 STORM_LOG_TRACE("Parsed expression: " << rationalFunction);
46 return rationalFunction;
47}
48
49template<typename ValueType>
50ValueType ValueParser<ValueType>::parseValue(std::string const& value) const {
51 return parseNumber<ValueType>(value);
52}
53
54template<typename NumberType>
55NumberType parseNumber(std::string const& value) {
56 NumberType result;
57 if (!parseNumber(value, result)) {
58 STORM_LOG_THROW(false, storm::exceptions::WrongFormatException, "Could not parse value '" << value << "' into " << typeid(NumberType).name() << ".");
59 }
60 return result;
61}
62
63bool parseDouble(std::string const& value, double& result) {
64 if (boost::conversion::try_lexical_convert(value, result)) {
65 return true;
66 } else {
67 // Try as rational number
68 storm::RationalNumber rationalResult;
69 if (parseNumber(value, rationalResult)) {
70 result = storm::utility::convertNumber<double>(rationalResult);
71 return true;
72 } else {
73 return false;
74 }
75 }
76}
77
78template<typename IntervalType>
79bool parseInterval(std::string const& value, IntervalType& result) {
80 // Try whether it is a constant.
81 if (IntervalBaseType<IntervalType> pointResult; parseNumber(value, pointResult)) {
82 result = IntervalType(pointResult);
83 return true;
84 }
85
86 std::string intermediate = value;
87 boost::trim(intermediate);
88 carl::BoundType leftBound;
89 carl::BoundType rightBound;
90 if (intermediate.front() == '(') {
91 leftBound = carl::BoundType::STRICT;
92 } else if (intermediate.front() == '[') {
93 leftBound = carl::BoundType::WEAK;
94 } else {
95 return false; // Expect start with '(' or '['.
96 }
97 if (intermediate.back() == ')') {
98 rightBound = carl::BoundType::STRICT;
99 } else if (intermediate.back() == ']') {
100 rightBound = carl::BoundType::WEAK;
101 } else {
102 return false; // Expected end with ')' or ']'.
103 }
104 intermediate = intermediate.substr(1, intermediate.size() - 2);
105
106 std::vector<std::string> words;
107 boost::split(words, intermediate, boost::is_any_of(","));
108 if (words.size() != 2) {
109 return false; // Did not find exactly one comma.
110 }
111 IntervalBaseType<IntervalType> leftVal, rightVal;
112 boost::trim(words[0]);
113 if (!parseNumber(words[0], leftVal)) {
114 return false; // lower value of interval invalid.
115 }
116 boost::trim(words[1]);
117 if (!parseNumber(words[1], rightVal)) {
118 return false; // upper value of interval invalid.
119 }
120 result = IntervalType(leftVal, leftBound, rightVal, rightBound);
121 return true;
122}
123
124template<typename NumberType>
125bool parseNumber(std::string const& value, NumberType& result) {
126 if constexpr (std::is_same_v<NumberType, double>) {
127 return parseDouble(value, result);
128 } else if constexpr (std::is_same_v<NumberType, storm::RationalNumber>) {
129 return carl::try_parse(value, result);
130 } else if constexpr (std::is_same_v<NumberType, storm::Interval>) {
131 return parseInterval<storm::Interval>(value, result);
132 } else if constexpr (std::is_same_v<NumberType, storm::RationalInterval>) {
133 return parseInterval<storm::RationalInterval>(value, result);
134 } else {
135 return boost::conversion::try_lexical_convert(value, result);
136 }
137}
138
139// Template instantiations.
140template class ValueParser<double>;
141template class ValueParser<storm::RationalNumber>;
142template class ValueParser<storm::RationalFunction>;
143template class ValueParser<storm::Interval>;
144template class ValueParser<storm::RationalInterval>;
145
146template std::size_t parseNumber<std::size_t>(std::string const&);
147
148} // namespace parser
149} // namespace storm
Variable declareRationalVariable(std::string const &name, bool auxiliary=false)
Declares a new rational variable with a name that must not yet exist and its corresponding type.
std::string const & getName() const
Retrieves the name of the variable.
Definition Variable.cpp:46
void addParameter(std::string const &parameter)
Add declaration of parameter.
ValueType parseValue(std::string const &value) const
Parse ValueType from string.
#define STORM_LOG_TRACE(message)
Definition logging.h:12
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
Contains all file parsers and helper classes.
NumberType parseNumber(std::string const &value)
Parse number from string.
bool parseInterval(std::string const &value, IntervalType &result)
bool parseDouble(std::string const &value, double &result)
SettingsManager const & manager()
Retrieves the settings manager.
TargetType convertNumber(SourceType const &number)
carl::RationalFunction< Polynomial, true > RationalFunction
typename detail::IntervalMetaProgrammingHelper< ValueType >::BaseType IntervalBaseType
Helper to access the type in which interval boundaries are stored.