Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SparseChoiceLabelingParser.cpp
Go to the documentation of this file.
2
9
10namespace storm {
11namespace parser {
12
13using namespace storm::utility::cstring;
14
15std::vector<storm::storage::FlatSet<uint_fast64_t>> SparseChoiceLabelingParser::parseChoiceLabeling(
16 std::vector<uint_fast64_t> const& nondeterministicChoiceIndices, std::string const& filename) {
17 // Open file.
18 MappedFile file(filename.c_str());
19 char const* buf = file.getData();
20
21 uint_fast64_t totalNumberOfChoices = nondeterministicChoiceIndices.back();
22
23 // Create choice labeling vector with given choice count.
24 std::vector<storm::storage::FlatSet<uint_fast64_t>> result(totalNumberOfChoices);
25
26 // Now parse state reward assignments.
27 uint_fast64_t state = 0;
28 uint_fast64_t lastState = (uint_fast64_t)-1;
29 uint_fast64_t lastChoice = (uint_fast64_t)-1;
30 uint_fast64_t const startIndexComparison = lastState;
31 uint_fast64_t const startChoiceIndexComparison = lastChoice;
32 uint_fast64_t choice = 0;
33 uint_fast64_t label = 0;
34
35 // Iterate over states.
36 while (buf[0] != '\0') {
37 // Parse state number and choice.
38 state = checked_strtol(buf, &buf);
39 choice = checked_strtol(buf, &buf);
40
41 // If the state has already been read or skipped once there might be a problem with the file (doubled lines, or blocks).
42 // Note: The value -1 shows that lastState has not yet been set, i.e. this is the first run of the loop (state index (2^64)-1 is a really bad starting
43 // index).
44 if (state < lastState && lastState != startIndexComparison) {
45 STORM_LOG_THROW(false, storm::exceptions::WrongFormatException,
46 "Error while parsing " << filename << ": State " << state << " was found but has already been read or skipped previously.");
47 }
48 if (state == lastState && choice < lastChoice && lastChoice != startChoiceIndexComparison) {
49 STORM_LOG_THROW(false, storm::exceptions::WrongFormatException,
50 "Error while parsing " << filename << ": Choice " << choice << " was found but has already been read or skipped previously.");
51 }
52 if (state >= nondeterministicChoiceIndices.size()) {
53 STORM_LOG_THROW(false, storm::exceptions::WrongFormatException, "Error while parsing " << filename << ": Illegal state " << state << ".");
54 }
55 uint_fast64_t numberOfChoicesForState = nondeterministicChoiceIndices[state + 1] - nondeterministicChoiceIndices[state];
56 if (choice >= numberOfChoicesForState) {
58 false, storm::exceptions::WrongFormatException,
59 "Error while parsing " << filename << ": Choice " << choice << " is illegal for state " << state << ", because this state has fewer choices.");
60 }
61
62 label = checked_strtol(buf, &buf);
63
64 result[nondeterministicChoiceIndices[state] + choice].insert(label);
65
66 buf = trimWhitespaces(buf);
67 lastState = state;
68 lastChoice = choice;
69 }
70
71 return result;
72}
73} // namespace parser
74} // namespace storm
Opens a file and maps it to memory providing a char* containing the file content.
Definition MappedFile.h:33
char const * getData() const
Returns a pointer to the beginning of the mapped file data.
static std::vector< storm::storage::FlatSet< uint_fast64_t > > parseChoiceLabeling(std::vector< uint_fast64_t > const &nondeterministicChoiceIndices, std::string const &filename)
Parses the given file and returns the resulting choice labeling.
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
uint_fast64_t checked_strtol(char const *str, char const **end)
Calls strtol() internally and checks if the new pointer is different from the original one,...
Definition cstring.cpp:21
char const * trimWhitespaces(char const *buf)
Skips spaces, tabs, newlines and carriage returns.
Definition cstring.cpp:66
LabParser.cpp.
Definition cli.cpp:18