Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SparseStateRewardParser.cpp
Go to the documentation of this file.
2#include <iostream>
3
9
12namespace storm {
13namespace parser {
14
15using namespace storm::utility::cstring;
16
17template<typename ValueType>
18std::vector<ValueType> SparseStateRewardParser<ValueType>::parseSparseStateReward(uint_fast64_t stateCount, std::string const& filename) {
19 // Open file.
20 MappedFile file(filename.c_str());
21 char const* buf = file.getData();
22
23 // Create state reward vector with given state count.
24 std::vector<ValueType> stateRewards(stateCount);
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 const startIndexComparison = lastState;
30 double reward;
31
32 // Iterate over states.
33 while (buf[0] != '\0') {
34 // Parse state.
35 state = checked_strtol(buf, &buf);
36
37 // If the state has already been read or skipped once there might be a problem with the file (doubled lines, or blocks).
38 // 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
39 // index).
40 if (state <= lastState && lastState != startIndexComparison) {
41 STORM_LOG_ERROR("Error while parsing " << filename << ": State " << state << " was found but has already been read or skipped previously.");
42 throw storm::exceptions::WrongFormatException()
43 << "Error while parsing " << filename << ": State " << state << " was found but has already been read or skipped previously.";
44 }
45
46 if (stateCount <= state) {
47 STORM_LOG_ERROR("Error while parsing " << filename << ": Found reward for a state of an invalid index \"" << state << "\". The model has only "
48 << stateCount << " states.");
49 throw storm::exceptions::OutOfRangeException()
50 << "Error while parsing " << filename << ": Found reward for a state of an invalid index \"" << state << "\"";
51 }
52
53 // Parse reward value.
54 reward = checked_strtod(buf, &buf);
55
56 if (reward < 0.0) {
57 STORM_LOG_ERROR("Error while parsing " << filename << ": Expected positive reward value but got \"" << reward << "\".");
58 throw storm::exceptions::WrongFormatException() << "Error while parsing " << filename << ": State reward file specifies illegal reward value.";
59 }
60
61 stateRewards[state] = reward;
62
63 buf = trimWhitespaces(buf);
64 lastState = state;
65 }
66 return stateRewards;
67}
68
70
71#ifdef STORM_HAVE_CARL
73#endif
74
75} // namespace parser
76} // 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.
A class providing the functionality to parse a the state rewards of a model.
static std::vector< ValueType > parseSparseStateReward(uint_fast64_t stateCount, std::string const &filename)
Reads a state reward file and puts the result in a state reward vector.
#define STORM_LOG_ERROR(message)
Definition logging.h:31
double checked_strtod(char const *str, char const **end)
Calls strtod() internally and checks if the new pointer is different from the original one,...
Definition cstring.cpp:39
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