Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
MonotonicityParser.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 VariableType>
16std::pair<std::set<VariableType>, std::set<VariableType>> MonotonicityParser<VariableType>::parseMonotoneVariablesFromFile(
17 std::string const& fileName, std::set<VariableType> const& consideredVariables) {
18 // Open file and initialize result.
19 std::ifstream inputFileStream;
20 storm::io::openFile(fileName, inputFileStream);
21
22 std::set<VariableType> monotoneIncrVars;
23 std::set<VariableType> monotoneDecrVars;
24
25 // Now try to parse the contents of the file.
26 try {
27 std::string fileContent((std::istreambuf_iterator<char>(inputFileStream)), (std::istreambuf_iterator<char>()));
28 std::vector<std::string> fileSplitted;
29
30 boost::split(fileSplitted, fileContent, boost::is_any_of(";"));
31 STORM_LOG_THROW(fileSplitted.size() == 2, storm::exceptions::WrongFormatException, "Expecting content to contain \";\" between monotone variables");
32 std::vector<std::string> monotoneIncrVarsString;
33 boost::split(monotoneIncrVarsString, fileSplitted[0], boost::is_any_of(" "));
34 std::vector<std::string> monotoneDecrVarsString;
35 boost::split(monotoneDecrVarsString, fileSplitted[0], boost::is_any_of(" "));
36 // TODO: throw errors if file not formatted correctly
37 for (auto varString : monotoneIncrVarsString) {
38 VariableType var;
39 for (auto const& v : consideredVariables) {
40 std::stringstream stream;
41 stream << v;
42 if (varString == stream.str()) {
43 var = v;
44 break;
45 }
46 }
47 monotoneIncrVars.insert(var);
48 }
49 for (auto varString : monotoneDecrVarsString) {
50 VariableType var;
51 for (auto const& v : consideredVariables) {
52 std::stringstream stream;
53 stream << v;
54 if (varString == stream.str()) {
55 var = v;
56 break;
57 }
58 }
59 monotoneDecrVars.insert(var);
60 }
61
62 } catch (std::exception& e) {
63 // In case of an exception properly close the file before passing exception.
64 storm::io::closeFile(inputFileStream);
65 throw e;
66 }
67
68 // Close the stream in case everything went smoothly and return result.
69 storm::io::closeFile(inputFileStream);
70 return {std::move(monotoneIncrVars), std::move(monotoneDecrVars)};
71}
72
74} // namespace parser
75} // namespace storm
static std::pair< std::set< VariableType >, std::set< VariableType > > parseMonotoneVariablesFromFile(std::string const &fileName, std::set< VariableType > const &consideredVariables)
#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