Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ParameterRegionParser.cpp
Go to the documentation of this file.
2
3#include <boost/algorithm/string.hpp>
4
8#include "storm/io/file.h"
11
12namespace storm {
13namespace parser {
14
15template<typename ParametricType>
17 std::string const& parameterBoundariesString,
18 std::set<VariableType> const& consideredVariables) {
19 std::string::size_type positionOfFirstRelation = parameterBoundariesString.find("<=");
20 STORM_LOG_THROW(positionOfFirstRelation != std::string::npos, storm::exceptions::InvalidArgumentException,
21 "When parsing the region" << parameterBoundariesString << " I could not find a '<=' after the first number");
22 std::string::size_type positionOfSecondRelation = parameterBoundariesString.find("<=", positionOfFirstRelation + 2);
23 STORM_LOG_THROW(positionOfSecondRelation != std::string::npos, storm::exceptions::InvalidArgumentException,
24 "When parsing the region" << parameterBoundariesString << " I could not find a '<=' after the parameter");
25
26 std::string parameter = parameterBoundariesString.substr(positionOfFirstRelation + 2, positionOfSecondRelation - (positionOfFirstRelation + 2));
27
28 // removes all whitespaces from the parameter string:
29 parameter.erase(std::remove_if(parameter.begin(), parameter.end(), ::isspace), parameter.end());
30 STORM_LOG_THROW(parameter.length() > 0, storm::exceptions::InvalidArgumentException,
31 "When parsing the region" << parameterBoundariesString << " I could not find a parameter");
32
33 std::unique_ptr<VariableType> var;
34 for (auto const& v : consideredVariables) {
35 std::stringstream stream;
36 stream << v;
37 if (parameter == stream.str()) {
38 var = std::make_unique<VariableType>(v);
39 break;
40 }
41 }
42 if (var) {
43 CoefficientType lb = storm::utility::convertNumber<CoefficientType>(parameterBoundariesString.substr(0, positionOfFirstRelation));
44 CoefficientType ub = storm::utility::convertNumber<CoefficientType>(parameterBoundariesString.substr(positionOfSecondRelation + 2));
45 lowerBoundaries.emplace(std::make_pair(*var, lb));
46 upperBoundaries.emplace(std::make_pair(*var, ub));
47 } else {
48 STORM_LOG_WARN("Could not find parameter " << parameter << " in the set of considered variables. Ignoring this parameter.");
49 }
50}
51
52template<typename ParametricType>
54 std::set<VariableType> const& consideredVariables) {
55 Valuation lowerBoundaries;
56 Valuation upperBoundaries;
57 std::vector<std::string> parameterBoundaries;
58 boost::split(parameterBoundaries, regionString, boost::is_any_of(","));
59 for (auto const& parameterBoundary : parameterBoundaries) {
60 if (!std::all_of(parameterBoundary.begin(), parameterBoundary.end(), ::isspace)) { // skip this string if it only consists of space
61 parseParameterBoundaries(lowerBoundaries, upperBoundaries, parameterBoundary, consideredVariables);
62 }
63 }
64
65 // Check that all considered variables are bounded
66 for (auto const& v : consideredVariables) {
67 STORM_LOG_THROW(lowerBoundaries.count(v) > 0, storm::exceptions::WrongFormatException, "Variable " << v << " was not defined in region string.");
68 STORM_LOG_ASSERT(upperBoundaries.count(v) > 0, "Variable " << v << " has a lower but not an upper bound.");
69 }
70 auto res = storm::storage::ParameterRegion<ParametricType>(std::move(lowerBoundaries), std::move(upperBoundaries));
71
72 return res;
73}
74
75template<typename ParametricType>
77 std::set<VariableType> const& consideredVariables) {
78 Valuation lowerBoundaries;
79 Valuation upperBoundaries;
80 std::vector<std::string> parameterBoundaries;
81 CoefficientType bound = storm::utility::convertNumber<CoefficientType>(regionBound);
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:25
#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