113 bool sawModelType{
false}, sawParameters{
false};
115 if (line.empty() || line.starts_with(
"//")) {
119 if (line.starts_with(
"@type: ")) {
121 STORM_LOG_THROW(!sawModelType, storm::exceptions::WrongFormatException,
"Type declared twice");
125 "Stochastic Two Player Games in DRN format are not supported.");
127 }
else if (line.starts_with(
"@value_type: ")) {
131 }
else if (line ==
"@parameters") {
133 STORM_LOG_THROW(!sawParameters, storm::exceptions::WrongFormatException,
"Parameters declared twice");
136 boost::split(header.
parameters, line, boost::is_any_of(
" "));
138 sawParameters =
true;
139 }
else if (line ==
"@placeholders") {
142 size_t posColon = line.find(
':');
143 STORM_LOG_THROW(posColon != std::string::npos, storm::exceptions::WrongFormatException,
"':' not found.");
144 std::string placeName = line.substr(0, posColon - 1);
145 STORM_LOG_THROW(placeName.front() ==
'$', storm::exceptions::WrongFormatException,
"Placeholder must start with dollar symbol $.");
146 std::string valueStr = line.substr(posColon + 2);
147 auto ret = header.
placeholders.emplace(placeName.substr(1), valueStr);
148 STORM_LOG_THROW(ret.second, storm::exceptions::WrongFormatException,
"Placeholder '$" << placeName <<
"' was already defined before.");
149 if (file.peek() ==
'@') {
154 }
else if (line ==
"@reward_models") {
159 }
else if (line ==
"@nr_states") {
161 STORM_LOG_THROW(header.
nrStates == 0, storm::exceptions::WrongFormatException,
"Number states declared twice");
164 }
else if (line ==
"@nr_choices") {
165 STORM_LOG_THROW(header.
nrChoices == 0, storm::exceptions::WrongFormatException,
"Number of actions declared twice");
168 }
else if (line ==
"@model") {
170 STORM_LOG_THROW(sawModelType, storm::exceptions::WrongFormatException,
"Model type has to be declared before model.");
171 STORM_LOG_THROW(header.
nrStates != 0, storm::exceptions::WrongFormatException,
"No. of states has to be declared before model.");
172 STORM_LOG_WARN_COND(header.
nrChoices != 0,
"No. of actions has to be declared. We may continue now, but future versions might not support this.");
180 STORM_LOG_THROW(
false, storm::exceptions::WrongFormatException,
"Could not parse line '" << line <<
"'.");
184 STORM_LOG_THROW(
false, storm::exceptions::WrongFormatException,
"Reached end of file before @model was found.");
200std::shared_ptr<storm::models::sparse::Model<ValueType, RewardModelType>>
parseModel(std::istream& file,
DrnHeader const& header,
209 for (std::string
const& parameter : header.
parameters) {
213 std::unordered_map<std::string, ValueType> placeholders;
214 for (
auto const& [placeName, valueStr] : header.
placeholders) {
215 ValueType v =
parseValue(valueStr, placeholders, valueParser);
217 placeholders.emplace(placeName, std::move(v));
219 size_t const nrStates = header.
nrStates;
220 size_t const nrChoices = header.
nrChoices;
230 STORM_LOG_THROW(nrChoices != 0, storm::exceptions::WrongFormatException,
231 "No. of actions (@nr_choices) has to be declared when building a model with choice labeling.");
234 std::vector<std::vector<ValueType>> stateRewards;
235 std::vector<std::vector<ValueType>> actionRewards;
236 if (continuousTime) {
237 modelComponents.
exitRates = std::vector<ValueType>(nrStates);
251 uint64_t lineNumber = 0;
252 bool firstState =
true;
253 bool firstActionForState =
true;
256 if (line.starts_with(
"//")) {
263 boost::trim_left(line);
264 if (line.starts_with(
"state ")) {
272 firstActionForState =
true;
274 STORM_LOG_THROW(state <= nrStates, storm::exceptions::WrongFormatException,
"More states detected than declared (in @nr_states).");
277 line = line.substr(6);
278 std::string curString = line;
279 size_t posEnd = line.find(
" ");
280 if (posEnd != std::string::npos) {
281 curString = line.substr(0, posEnd);
282 line = line.substr(posEnd + 1);
287 STORM_LOG_THROW(state == parsedId, storm::exceptions::WrongFormatException,
288 "In line " << lineNumber <<
" state ids are not ordered and without gaps. Expected " << state <<
" but got " << parsedId <<
".");
289 if (nonDeterministic) {
292 STORM_LOG_THROW(nrChoices == 0 ||
builder.getCurrentRowGroupCount() <= nrChoices, storm::exceptions::WrongFormatException,
293 "More actions detected than declared (in @nr_choices).");
296 if (continuousTime) {
298 STORM_LOG_THROW(line.starts_with(
"!"), storm::exceptions::WrongFormatException,
"Exit rate missing in " << lineNumber);
299 line = line.substr(1);
301 posEnd = line.find(
" ");
302 if (posEnd != std::string::npos) {
303 curString = line.substr(0, posEnd);
304 line = line.substr(posEnd + 1);
308 ValueType exitRate =
parseValue(curString, placeholders, valueParser);
313 modelComponents.
exitRates.get()[state] = exitRate;
317 if (line.starts_with(
"{")) {
318 size_t posEndObservation = line.find(
"}");
319 std::string observation = line.substr(1, posEndObservation - 1);
322 line = line.substr(posEndObservation + 1);
324 STORM_LOG_THROW(line.starts_with(
" "), storm::exceptions::WrongFormatException,
325 "Expected whitespace after observation in line " << lineNumber);
327 line = line.substr(1);
330 STORM_LOG_THROW(
false, storm::exceptions::WrongFormatException,
"Expected an observation for state " << state <<
" in line " << lineNumber);
334 if (line.starts_with(
"[")) {
336 size_t posEndReward = line.find(
']');
337 STORM_LOG_THROW(posEndReward != std::string::npos, storm::exceptions::WrongFormatException,
"] missing in line " << lineNumber <<
" .");
338 std::string rewardsStr = line.substr(1, posEndReward - 1);
340 std::vector<std::string> rewards;
341 boost::split(rewards, rewardsStr, boost::is_any_of(
","));
342 if (stateRewards.size() < rewards.size()) {
343 stateRewards.resize(rewards.size());
345 auto stateRewardsIt = stateRewards.begin();
346 for (
auto const& rew : rewards) {
347 auto rewardValue =
parseValue(rew, placeholders, valueParser);
349 if (stateRewardsIt->empty()) {
352 (*stateRewardsIt)[state] = std::move(rewardValue);
356 line = line.substr(posEndReward + 1);
361 std::vector<std::string> labels;
371 std::regex labelRegex(R
"(\"([^\"]+?)\"(?=(\s|$|\"))|([^\s\"]+?(?=(\s|$))))");
374 auto match_begin = std::sregex_iterator(line.begin(), line.end(), labelRegex);
375 auto match_end = std::sregex_iterator();
376 for (std::sregex_iterator i = match_begin; i != match_end; ++i) {
377 std::smatch match = *i;
379 if (match.length(1) > 0) {
380 labels.push_back(match.str(1));
382 labels.push_back(match.str(3));
386 for (std::string
const& label : labels) {
394 }
else if (line.starts_with(
"action ")) {
396 if (firstActionForState) {
397 firstActionForState =
false;
402 line = line.substr(7);
403 std::string curString = line;
404 size_t posEnd = line.find(
" ");
405 if (posEnd != std::string::npos) {
406 curString = line.substr(0, posEnd);
407 line = line.substr(posEnd + 1);
414 if (curString !=
"__NOLABEL__") {
415 if (!modelComponents.
choiceLabeling.value().containsLabel(curString)) {
418 modelComponents.
choiceLabeling.value().addLabelToChoice(curString, row);
422 if (line.starts_with(
"[")) {
424 size_t posEndReward = line.find(
']');
425 STORM_LOG_THROW(posEndReward != std::string::npos, storm::exceptions::WrongFormatException,
"] missing.");
426 std::string rewardsStr = line.substr(1, posEndReward - 1);
428 std::vector<std::string> rewards;
429 boost::split(rewards, rewardsStr, boost::is_any_of(
","));
430 if (actionRewards.size() < rewards.size()) {
431 actionRewards.resize(rewards.size());
433 auto actionRewardsIt = actionRewards.begin();
434 for (
auto const& rew : rewards) {
435 auto rewardValue =
parseValue(rew, placeholders, valueParser);
437 if (actionRewardsIt->size() <= row) {
440 (*actionRewardsIt)[row] = std::move(rewardValue);
444 line = line.substr(posEndReward + 1);
449 size_t posColon = line.find(
':');
450 STORM_LOG_THROW(posColon != std::string::npos, storm::exceptions::WrongFormatException,
451 "':' not found in '" << line <<
"' on line " << lineNumber <<
".");
453 std::string valueStr = line.substr(posColon + 2);
454 ValueType value =
parseValue(valueStr, placeholders, valueParser);
455 STORM_LOG_TRACE(
"Transition " << row <<
" -> " << target <<
": " << value);
456 STORM_LOG_THROW(target < nrStates, storm::exceptions::WrongFormatException,
457 "In line " << lineNumber <<
" target state " << target <<
" is greater than state size " << nrStates);
458 builder.addNextValue(row, target, value);
462 std::cout <<
"Parsed " << state <<
"/" << nrStates <<
" states before abort.\n";
463 STORM_LOG_THROW(
false, storm::exceptions::AbortException,
"Aborted in state space exploration.");
470 if (nonDeterministic) {
471 STORM_LOG_THROW(nrChoices == 0 ||
builder.getLastRow() + 1 == nrChoices, storm::exceptions::WrongFormatException,
472 "Number of actions detected (at least " <<
builder.getLastRow() + 1 <<
") does not match number of actions declared (" << nrChoices
473 <<
", in @nr_choices).");
481 uint64_t numRewardModels = std::max(stateRewards.size(), actionRewards.size());
482 for (uint64_t i = 0; i < numRewardModels; ++i) {
483 std::string rewardModelName;
485 rewardModelName =
"rew" + std::to_string(i);
489 std::optional<std::vector<ValueType>> stateRewardVector, actionRewardVector;
490 if (i < stateRewards.size() && !stateRewards[i].empty()) {
491 stateRewardVector = std::move(stateRewards[i]);
493 if (i < actionRewards.size() && !actionRewards[i].empty()) {
495 actionRewardVector = std::move(actionRewards[i]);