Storm 1.14.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 STORM_LOG_THROW(parseNumber(value, result), storm::exceptions::WrongFormatException,
58 "Could not parse value '" << value << "' into " << typeid(NumberType).name() << ".");
59 return result;
60}
61
62bool parseDouble(std::string const& value, double& result) {
63 if (boost::conversion::try_lexical_convert(value, result)) {
64 return true;
65 } else {
66 // Try as rational number
67 storm::RationalNumber rationalResult;
68 if (parseNumber(value, rationalResult)) {
69 result = storm::utility::convertNumber<double>(rationalResult);
70 return true;
71 } else {
72 return false;
73 }
74 }
75}
76
77template<typename IntervalType>
78bool parseInterval(std::string const& value, IntervalType& result) {
79 // Try whether it is a constant.
80 if (IntervalBaseType<IntervalType> pointResult; parseNumber(value, pointResult)) {
81 result = IntervalType(pointResult);
82 return true;
83 }
84
85 std::string intermediate = value;
86 boost::trim(intermediate);
87 carl::BoundType leftBound;
88 carl::BoundType rightBound;
89 if (intermediate.front() == '(') {
90 leftBound = carl::BoundType::STRICT;
91 } else if (intermediate.front() == '[') {
92 leftBound = carl::BoundType::WEAK;
93 } else {
94 return false; // Expect start with '(' or '['.
95 }
96 if (intermediate.back() == ')') {
97 rightBound = carl::BoundType::STRICT;
98 } else if (intermediate.back() == ']') {
99 rightBound = carl::BoundType::WEAK;
100 } else {
101 return false; // Expected end with ')' or ']'.
102 }
103 intermediate = intermediate.substr(1, intermediate.size() - 2);
104
105 std::vector<std::string> words;
106 boost::split(words, intermediate, boost::is_any_of(","));
107 if (words.size() != 2) {
108 return false; // Did not find exactly one comma.
109 }
110 IntervalBaseType<IntervalType> leftVal, rightVal;
111 boost::trim(words[0]);
112 if (!parseNumber(words[0], leftVal)) {
113 return false; // lower value of interval invalid.
114 }
115 boost::trim(words[1]);
116 if (!parseNumber(words[1], rightVal)) {
117 return false; // upper value of interval invalid.
118 }
119 result = IntervalType(leftVal, leftBound, rightVal, rightBound);
120 return true;
121}
122
123template<typename NumberType>
124bool parseNumber(std::string const& value, NumberType& result) {
125 if constexpr (std::is_same_v<NumberType, double>) {
126 return parseDouble(value, result);
127 } else if constexpr (std::is_same_v<NumberType, storm::RationalNumber>) {
128 return carl::try_parse(value, result);
129 } else if constexpr (std::is_same_v<NumberType, storm::Interval>) {
130 return parseInterval<storm::Interval>(value, result);
131 } else if constexpr (std::is_same_v<NumberType, storm::RationalInterval>) {
132 return parseInterval<storm::RationalInterval>(value, result);
133 } else {
134 return boost::conversion::try_lexical_convert(value, result);
135 }
136}
137
138// Template instantiations.
139template class ValueParser<double>;
140template class ValueParser<storm::RationalNumber>;
141template class ValueParser<storm::RationalFunction>;
142template class ValueParser<storm::Interval>;
143template class ValueParser<storm::RationalInterval>;
144
145template std::size_t parseNumber<std::size_t>(std::string const&);
146
147} // namespace parser
148} // 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:15
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:28
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.