Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ParameterRegionParser.cpp
Go to the documentation of this file.
1#include <boost/algorithm/string.hpp>
3
5
7#include "storm/io/file.h"
10
11namespace storm {
12namespace parser {
13
14template<typename ParametricType>
16 std::string const& parameterBoundariesString,
17 std::set<VariableType> const& consideredVariables) {
18 std::string::size_type positionOfFirstRelation = parameterBoundariesString.find("<=");
19 STORM_LOG_THROW(positionOfFirstRelation != std::string::npos, storm::exceptions::InvalidArgumentException,
20 "When parsing the region" << parameterBoundariesString << " I could not find a '<=' after the first number");
21 std::string::size_type positionOfSecondRelation = parameterBoundariesString.find("<=", positionOfFirstRelation + 2);
22 STORM_LOG_THROW(positionOfSecondRelation != std::string::npos, storm::exceptions::InvalidArgumentException,
23 "When parsing the region" << parameterBoundariesString << " I could not find a '<=' after the parameter");
24
25 std::string parameter = parameterBoundariesString.substr(positionOfFirstRelation + 2, positionOfSecondRelation - (positionOfFirstRelation + 2));
26
27 // removes all whitespaces from the parameter string:
28 parameter.erase(std::remove_if(parameter.begin(), parameter.end(), ::isspace), parameter.end());
29 STORM_LOG_THROW(parameter.length() > 0, storm::exceptions::InvalidArgumentException,
30 "When parsing the region" << parameterBoundariesString << " I could not find a parameter");
31
32 std::unique_ptr<VariableType> var;
33 for (auto const& v : consideredVariables) {
34 std::stringstream stream;
35 stream << v;
36 if (parameter == stream.str()) {
37 var = std::make_unique<VariableType>(v);
38 break;
39 }
40 }
41 if (var) {
42 CoefficientType lb = storm::utility::convertNumber<CoefficientType>(parameterBoundariesString.substr(0, positionOfFirstRelation));
43 CoefficientType ub = storm::utility::convertNumber<CoefficientType>(parameterBoundariesString.substr(positionOfSecondRelation + 2));
44 lowerBoundaries.emplace(std::make_pair(*var, lb));
45 upperBoundaries.emplace(std::make_pair(*var, ub));
46 } else {
47 STORM_LOG_WARN("Could not find parameter " << parameter << " in the set of considered variables. Ignoring this parameter.");
48 }
49}
50
51template<typename ParametricType>
53 std::set<VariableType> const& consideredVariables) {
54 Valuation lowerBoundaries;
55 Valuation upperBoundaries;
56 std::vector<std::string> parameterBoundaries;
57 boost::split(parameterBoundaries, regionString, boost::is_any_of(","));
58 for (auto const& parameterBoundary : parameterBoundaries) {
59 if (!std::all_of(parameterBoundary.begin(), parameterBoundary.end(), ::isspace)) { // skip this string if it only consists of space
60 parseParameterBoundaries(lowerBoundaries, upperBoundaries, parameterBoundary, consideredVariables);
61 }
62 }
63
64 // Check that all considered variables are bounded
65 for (auto const& v : consideredVariables) {
66 STORM_LOG_THROW(lowerBoundaries.count(v) > 0, storm::exceptions::WrongFormatException, "Variable " << v << " was not defined in region string.");
67 STORM_LOG_ASSERT(upperBoundaries.count(v) > 0, "Variable " << v << " has a lower but not an upper bound.");
68 }
69 auto res = storm::storage::ParameterRegion<ParametricType>(std::move(lowerBoundaries), std::move(upperBoundaries));
70
71 return res;
72}
73
74template<typename ParametricType>
76 std::set<VariableType> const& consideredVariables) {
77 Valuation lowerBoundaries;
78 Valuation upperBoundaries;
79 std::vector<std::string> parameterBoundaries;
80 CoefficientType bound = storm::utility::convertNumber<CoefficientType>(regionBound);
81 STORM_LOG_THROW(0 < bound && bound < 1, storm::exceptions::WrongFormatException, "Bound must be between 0 and 1, " << bound << " is not.");
82 for (auto const& v : consideredVariables) {
83 lowerBoundaries.emplace(std::make_pair(v, 0 + bound));
84 upperBoundaries.emplace(std::make_pair(v, 1 - bound));
85 }
86 auto res = storm::storage::ParameterRegion<ParametricType>(std::move(lowerBoundaries), std::move(upperBoundaries));
87
88 return res;
89}
90
91template<typename ParametricType>
92std::vector<storm::storage::ParameterRegion<ParametricType>> ParameterRegionParser<ParametricType>::parseMultipleRegions(
93 std::string const& regionsString, std::set<VariableType> const& consideredVariables) {
94 std::vector<storm::storage::ParameterRegion<ParametricType>> result;
95 std::vector<std::string> regionsStrVec;
96 boost::split(regionsStrVec, regionsString, boost::is_any_of(";"));
97 for (auto const& regionStr : regionsStrVec) {
98 if (!std::all_of(regionStr.begin(), regionStr.end(), ::isspace)) { // skip this string if it only consists of space
99 result.emplace_back(parseRegion(regionStr, consideredVariables));
100 }
101 }
102 return result;
103}
104
105template<typename ParametricType>
106std::vector<storm::storage::ParameterRegion<ParametricType>> ParameterRegionParser<ParametricType>::parseMultipleRegionsFromFile(
107 std::string const& fileName, std::set<VariableType> const& consideredVariables) {
108 // Open file and initialize result.
109 std::ifstream inputFileStream;
110 storm::io::openFile(fileName, inputFileStream);
111
112 std::vector<storm::storage::ParameterRegion<ParametricType>> result;
113
114 // Now try to parse the contents of the file.
115 try {
116 std::string fileContent((std::istreambuf_iterator<char>(inputFileStream)), (std::istreambuf_iterator<char>()));
117 result = parseMultipleRegions(fileContent, consideredVariables);
118 } catch (std::exception& e) {
119 // In case of an exception properly close the file before passing exception.
120 storm::io::closeFile(inputFileStream);
121 throw e;
122 }
123
124 // Close the stream in case everything went smoothly and return result.
125 storm::io::closeFile(inputFileStream);
126 return result;
127}
128
129#ifdef STORM_HAVE_CARL
131#endif
132} // namespace parser
133} // namespace storm
static storm::storage::ParameterRegion< ParametricType > parseRegion(std::string const &regionString, std::set< VariableType > const &consideredVariables)
static storm::storage::ParameterRegion< ParametricType > createRegion(std::string const &regionBound, std::set< VariableType > const &consideredVariables)
storm::storage::ParameterRegion< ParametricType >::CoefficientType CoefficientType
storm::storage::ParameterRegion< ParametricType >::Valuation Valuation
static void parseParameterBoundaries(Valuation &lowerBoundaries, Valuation &upperBoundaries, std::string const &parameterBoundariesString, std::set< VariableType > const &consideredVariables)
static std::vector< storm::storage::ParameterRegion< ParametricType > > parseMultipleRegions(std::string const &regionsString, std::set< VariableType > const &consideredVariables)
static std::vector< storm::storage::ParameterRegion< ParametricType > > parseMultipleRegionsFromFile(std::string const &fileName, std::set< VariableType > const &consideredVariables)
#define STORM_LOG_WARN(message)
Definition logging.h:30
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
void closeFile(std::ofstream &stream)
Close the given file after writing.
Definition file.h:47
void openFile(std::string const &filepath, std::ofstream &filestream, bool append=false, bool silent=false)
Open the given file for writing.
Definition file.h:18
LabParser.cpp.
Definition cli.cpp:18