3#include <boost/algorithm/string.hpp>
4#include <boost/algorithm/string/predicate.hpp>
33template<
typename ValueType,
typename RewardModelType>
45 bool sawParameters =
false;
46 std::unordered_map<std::string, ValueType> placeholders;
50 std::vector<std::string> rewardModelNames;
51 std::shared_ptr<storm::storage::sparse::ModelComponents<ValueType, RewardModelType>> modelComponents;
55 if (line.empty() || boost::starts_with(line,
"//")) {
59 if (boost::starts_with(line,
"@type: ")) {
61 STORM_LOG_THROW(!sawType, storm::exceptions::WrongFormatException,
"Type declared twice");
65 "Stochastic Two Player Games in DRN format are not supported.");
68 }
else if (line ==
"@parameters") {
70 STORM_LOG_THROW(!sawParameters, storm::exceptions::WrongFormatException,
"Parameters declared twice");
73 std::vector<std::string> parameters;
74 boost::split(parameters, line, boost::is_any_of(
" "));
75 for (std::string
const& parameter : parameters) {
82 }
else if (line ==
"@placeholders") {
85 size_t posColon = line.find(
':');
86 STORM_LOG_THROW(posColon != std::string::npos, storm::exceptions::WrongFormatException,
"':' not found.");
87 std::string placeName = line.substr(0, posColon - 1);
88 STORM_LOG_THROW(placeName.front() ==
'$', storm::exceptions::WrongFormatException,
"Placeholder must start with dollar symbol $.");
89 std::string valueStr = line.substr(posColon + 2);
90 ValueType value = parseValue(valueStr, placeholders, valueParser);
92 auto ret = placeholders.insert(std::make_pair(placeName.substr(1), value));
93 STORM_LOG_THROW(ret.second, storm::exceptions::WrongFormatException,
"Placeholder '$" << placeName <<
"' was already defined before.");
94 if (file.peek() ==
'@') {
99 }
else if (line ==
"@reward_models") {
101 STORM_LOG_THROW(rewardModelNames.empty(), storm::exceptions::WrongFormatException,
"Reward model names declared twice");
103 boost::split(rewardModelNames, line, boost::is_any_of(
"\t "));
104 }
else if (line ==
"@nr_states") {
106 STORM_LOG_THROW(nrStates == 0, storm::exceptions::WrongFormatException,
"Number states declared twice");
108 nrStates = parseNumber<size_t>(line);
109 }
else if (line ==
"@nr_choices") {
110 STORM_LOG_THROW(nrChoices == 0, storm::exceptions::WrongFormatException,
"Number of actions declared twice");
112 nrChoices = parseNumber<size_t>(line);
113 }
else if (line ==
"@model") {
115 STORM_LOG_THROW(sawType, storm::exceptions::WrongFormatException,
"Type has to be declared before model.");
116 STORM_LOG_THROW(nrStates != 0, storm::exceptions::WrongFormatException,
"No. of states has to be declared before model.");
118 "No. of actions (@nr_choices) has to be declared before model.");
119 STORM_LOG_WARN_COND(nrChoices != 0,
"No. of actions has to be declared. We may continue now, but future versions might not support this.");
121 modelComponents = parseStates(file, type, nrStates, nrChoices, placeholders, valueParser, rewardModelNames, options);
124 STORM_LOG_THROW(
false, storm::exceptions::WrongFormatException,
"Could not parse line '" << line <<
"'.");
134template<
typename ValueType,
typename RewardModelType>
136 std::istream& file,
storm::models::ModelType type,
size_t stateSize,
size_t nrChoices, std::unordered_map<std::string, ValueType>
const& placeholders,
139 auto modelComponents = std::make_shared<storm::storage::sparse::ModelComponents<ValueType, RewardModelType>>();
140 bool nonDeterministic =
145 modelComponents->observabilityClasses = std::vector<uint32_t>();
146 modelComponents->observabilityClasses->resize(stateSize);
150 std::vector<std::vector<ValueType>> stateRewards;
151 std::vector<std::vector<ValueType>> actionRewards;
152 if (continuousTime) {
153 modelComponents->exitRates = std::vector<ValueType>(stateSize);
160 modelComponents->rateTransitions =
true;
167 uint64_t lineNumber = 0;
168 bool firstState =
true;
169 bool firstActionForState =
true;
172 if (boost::starts_with(line,
"//")) {
179 boost::trim_left(line);
180 if (boost::starts_with(line,
"state ")) {
188 firstActionForState =
true;
190 STORM_LOG_THROW(state <= stateSize, storm::exceptions::WrongFormatException,
"More states detected than declared (in @nr_states).");
193 line = line.substr(6);
194 std::string curString = line;
195 size_t posEnd = line.find(
" ");
196 if (posEnd != std::string::npos) {
197 curString = line.substr(0, posEnd);
198 line = line.substr(posEnd + 1);
202 size_t parsedId = parseNumber<size_t>(curString);
203 STORM_LOG_THROW(state == parsedId, storm::exceptions::WrongFormatException,
204 "In line " << lineNumber <<
" state ids are not ordered and without gaps. Expected " << state <<
" but got " << parsedId <<
".");
205 if (nonDeterministic) {
209 "More actions detected than declared (in @nr_choices).");
212 if (continuousTime) {
214 STORM_LOG_THROW(boost::starts_with(line,
"!"), storm::exceptions::WrongFormatException,
"Exit rate missing in " << lineNumber);
215 line = line.substr(1);
217 posEnd = line.find(
" ");
218 if (posEnd != std::string::npos) {
219 curString = line.substr(0, posEnd);
220 line = line.substr(posEnd + 1);
224 ValueType exitRate = parseValue(curString, placeholders, valueParser);
226 modelComponents->markovianStates.get().set(state);
229 modelComponents->exitRates.get()[state] = exitRate;
232 if (boost::starts_with(line,
"[")) {
234 size_t posEndReward = line.find(
']');
235 STORM_LOG_THROW(posEndReward != std::string::npos, storm::exceptions::WrongFormatException,
"] missing in line " << lineNumber <<
" .");
236 std::string rewardsStr = line.substr(1, posEndReward - 1);
238 std::vector<std::string> rewards;
239 boost::split(rewards, rewardsStr, boost::is_any_of(
","));
240 if (stateRewards.size() < rewards.size()) {
241 stateRewards.resize(rewards.size());
243 auto stateRewardsIt = stateRewards.begin();
244 for (
auto const& rew : rewards) {
245 auto rewardValue = parseValue(rew, placeholders, valueParser);
247 if (stateRewardsIt->empty()) {
248 stateRewardsIt->resize(stateSize, storm::utility::zero<ValueType>());
250 (*stateRewardsIt)[state] = std::move(rewardValue);
254 line = line.substr(posEndReward + 1);
258 if (boost::starts_with(line,
"{")) {
259 size_t posEndObservation = line.find(
"}");
260 std::string observation = line.substr(1, posEndObservation - 1);
262 modelComponents->observabilityClasses.value()[state] = std::stoi(observation);
263 line = line.substr(posEndObservation + 1);
265 STORM_LOG_THROW(
false, storm::exceptions::WrongFormatException,
"Expected an observation for state " << state <<
" in line " << lineNumber);
271 std::vector<std::string> labels;
281 std::regex labelRegex(R
"(\"([^\"]+?)\"(?=(\s|$|\"))|([^\s\"]+?(?=(\s|$))))");
284 auto match_begin = std::sregex_iterator(line.begin(), line.end(), labelRegex);
285 auto match_end = std::sregex_iterator();
286 for (std::sregex_iterator i = match_begin;
i != match_end; ++
i) {
287 std::smatch match = *
i;
289 if (match.length(1) > 0) {
290 labels.push_back(match.str(1));
292 labels.push_back(match.str(3));
296 for (std::string
const& label : labels) {
297 if (!modelComponents->stateLabeling.containsLabel(label)) {
298 modelComponents->stateLabeling.addLabel(label);
300 modelComponents->stateLabeling.addLabelToState(label, state);
304 }
else if (boost::starts_with(line,
"action ")) {
306 if (firstActionForState) {
307 firstActionForState =
false;
312 line = line.substr(7);
313 std::string curString = line;
314 size_t posEnd = line.find(
" ");
315 if (posEnd != std::string::npos) {
316 curString = line.substr(0, posEnd);
317 line = line.substr(posEnd + 1);
324 if (curString !=
"__NOLABEL__") {
325 if (!modelComponents->choiceLabeling.value().containsLabel(curString)) {
326 modelComponents->choiceLabeling.value().addLabel(curString);
328 modelComponents->choiceLabeling.value().addLabelToChoice(curString, row);
332 if (boost::starts_with(line,
"[")) {
334 size_t posEndReward = line.find(
']');
335 STORM_LOG_THROW(posEndReward != std::string::npos, storm::exceptions::WrongFormatException,
"] missing.");
336 std::string rewardsStr = line.substr(1, posEndReward - 1);
338 std::vector<std::string> rewards;
339 boost::split(rewards, rewardsStr, boost::is_any_of(
","));
340 if (actionRewards.size() < rewards.size()) {
341 actionRewards.resize(rewards.size());
343 auto actionRewardsIt = actionRewards.begin();
344 for (
auto const& rew : rewards) {
345 auto rewardValue = parseValue(rew, placeholders, valueParser);
347 if (actionRewardsIt->size() <= row) {
348 actionRewardsIt->resize(std::max(row + 1, stateSize), storm::utility::zero<ValueType>());
350 (*actionRewardsIt)[row] = std::move(rewardValue);
354 line = line.substr(posEndReward + 1);
359 size_t posColon = line.find(
':');
360 STORM_LOG_THROW(posColon != std::string::npos, storm::exceptions::WrongFormatException,
361 "':' not found in '" << line <<
"' on line " << lineNumber <<
".");
362 size_t target = parseNumber<size_t>(line.substr(0, posColon - 1));
363 std::string valueStr = line.substr(posColon + 2);
364 ValueType value = parseValue(valueStr, placeholders, valueParser);
365 STORM_LOG_TRACE(
"Transition " << row <<
" -> " << target <<
": " << value);
366 STORM_LOG_THROW(target < stateSize, storm::exceptions::WrongFormatException,
367 "In line " << lineNumber <<
" target state " << target <<
" is greater than state size " << stateSize);
372 std::cout <<
"Parsed " << state <<
"/" << stateSize <<
" states before abort.\n";
373 STORM_LOG_THROW(
false, storm::exceptions::AbortException,
"Aborted in state space exploration.");
380 if (nonDeterministic) {
382 "Number of actions detected (at least " << builder.
getLastRow() + 1 <<
") does not match number of actions declared (" << nrChoices
383 <<
", in @nr_choices).");
387 modelComponents->transitionMatrix = builder.
build(row + 1, stateSize, nonDeterministic ? stateSize : 0);
391 uint64_t numRewardModels = std::max(stateRewards.size(), actionRewards.size());
392 for (uint64_t i = 0;
i < numRewardModels; ++
i) {
393 std::string rewardModelName;
394 if (rewardModelNames.size() <= i) {
395 rewardModelName =
"rew" + std::to_string(i);
397 rewardModelName = rewardModelNames[
i];
399 std::optional<std::vector<ValueType>> stateRewardVector, actionRewardVector;
400 if (i < stateRewards.size() && !stateRewards[i].empty()) {
401 stateRewardVector = std::move(stateRewards[i]);
403 if (i < actionRewards.size() && !actionRewards[i].empty()) {
404 actionRewards[
i].resize(row + 1, storm::utility::zero<ValueType>());
405 actionRewardVector = std::move(actionRewards[i]);
407 modelComponents->rewardModels.emplace(
411 return modelComponents;
414template<
typename ValueType,
typename RewardModelType>
415ValueType DirectEncodingParser<ValueType, RewardModelType>::parseValue(std::string
const& valueStr,
416 std::unordered_map<std::string, ValueType>
const& placeholders,
417 ValueParser<ValueType>
const& valueParser) {
418 if (boost::starts_with(valueStr,
"$")) {
419 auto it = placeholders.find(valueStr.substr(1));
420 STORM_LOG_THROW(it != placeholders.end(), storm::exceptions::WrongFormatException,
"Placeholder " << valueStr <<
" unknown.");
424 return valueParser.parseValue(valueStr);
429template class DirectEncodingParser<double>;
430template class DirectEncodingParser<storm::RationalNumber>;
431template class DirectEncodingParser<storm::RationalFunction>;
432template class DirectEncodingParser<storm::Interval>;
This class manages the labeling of the choice space with a number of (atomic) labels.
This class manages the labeling of the state space with a number of (atomic) labels.
Parser for models in the DRN format with explicit encoding.
static std::shared_ptr< storm::models::sparse::Model< ValueType, RewardModelType > > parseModel(std::string const &fil, DirectEncodingParserOptions const &options=DirectEncodingParserOptions())
Load a model in DRN format from a file and create the model.
Parser for values according to their ValueType.
void addParameter(std::string const ¶meter)
Add declaration of parameter.
A bit vector that is internally represented as a vector of 64-bit values.
A class that can be used to build a sparse matrix by adding value by value.
index_type getCurrentRowGroupCount() const
Retrieves the current row group count.
index_type getLastRow() const
Retrieves the most recently used row.
void addNextValue(index_type row, index_type column, value_type const &value)
Sets the matrix entry at the given row and column to the given value.
void newRowGroup(index_type startingRow)
Starts a new row group in the matrix.
SparseMatrix< value_type > build(index_type overriddenRowCount=0, index_type overriddenColumnCount=0, index_type overriddenRowGroupCount=0)
#define STORM_LOG_INFO(message)
#define STORM_LOG_TRACE(message)
#define STORM_LOG_WARN_COND(cond, message)
#define STORM_LOG_THROW(cond, exception, message)
SFTBDDChecker::ValueType ValueType
std::basic_istream< CharT, Traits > & getline(std::basic_istream< CharT, Traits > &input, std::basic_string< CharT, Traits, Allocator > &str)
Overloaded getline function which handles different types of newline ( and \r).
void closeFile(std::ofstream &stream)
Close the given file after writing.
void openFile(std::string const &filepath, std::ofstream &filestream, bool append=false, bool silent=false)
Open the given file for writing.
ModelType getModelType(std::string const &type)
std::shared_ptr< storm::models::sparse::Model< ValueType, RewardModelType > > buildModelFromComponents(storm::models::ModelType modelType, storm::storage::sparse::ModelComponents< ValueType, RewardModelType > &&components)
bool isTerminate()
Check whether the program should terminate (due to some abort signal).
bool isZero(ValueType const &a)