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);
331 "Expected an observation for state " << state <<
" in line " << lineNumber <<
".");
335 if (line.starts_with(
"[")) {
337 size_t posEndReward = line.find(
']');
338 STORM_LOG_THROW(posEndReward != std::string::npos, storm::exceptions::WrongFormatException,
"] missing in line " << lineNumber <<
" .");
339 std::string rewardsStr = line.substr(1, posEndReward - 1);
341 std::vector<std::string> rewards;
342 boost::split(rewards, rewardsStr, boost::is_any_of(
","));
343 if (stateRewards.size() < rewards.size()) {
344 stateRewards.resize(rewards.size());
346 auto stateRewardsIt = stateRewards.begin();
347 for (
auto const& rew : rewards) {
348 auto rewardValue =
parseValue(rew, placeholders, valueParser);
350 if (stateRewardsIt->empty()) {
353 (*stateRewardsIt)[state] = std::move(rewardValue);
357 line = line.substr(posEndReward + 1);
362 std::vector<std::string> labels;
372 std::regex labelRegex(R
"(\"([^\"]+?)\"(?=(\s|$|\"))|([^\s\"]+?(?=(\s|$))))");
375 auto match_begin = std::sregex_iterator(line.begin(), line.end(), labelRegex);
376 auto match_end = std::sregex_iterator();
377 for (std::sregex_iterator i = match_begin; i != match_end; ++i) {
378 std::smatch match = *i;
380 if (match.length(1) > 0) {
381 labels.push_back(match.str(1));
383 labels.push_back(match.str(3));
387 for (std::string
const& label : labels) {
395 }
else if (line.starts_with(
"action ")) {
397 if (firstActionForState) {
398 firstActionForState =
false;
403 line = line.substr(7);
404 std::string curString = line;
405 size_t posEnd = line.find(
" ");
406 if (posEnd != std::string::npos) {
407 curString = line.substr(0, posEnd);
408 line = line.substr(posEnd + 1);
415 if (curString !=
"__NOLABEL__") {
416 if (!modelComponents.
choiceLabeling.value().containsLabel(curString)) {
419 modelComponents.
choiceLabeling.value().addLabelToChoice(curString, row);
423 if (line.starts_with(
"[")) {
425 size_t posEndReward = line.find(
']');
426 STORM_LOG_THROW(posEndReward != std::string::npos, storm::exceptions::WrongFormatException,
"] missing.");
427 std::string rewardsStr = line.substr(1, posEndReward - 1);
429 std::vector<std::string> rewards;
430 boost::split(rewards, rewardsStr, boost::is_any_of(
","));
431 if (actionRewards.size() < rewards.size()) {
432 actionRewards.resize(rewards.size());
434 auto actionRewardsIt = actionRewards.begin();
435 for (
auto const& rew : rewards) {
436 auto rewardValue =
parseValue(rew, placeholders, valueParser);
438 if (actionRewardsIt->size() <= row) {
441 (*actionRewardsIt)[row] = std::move(rewardValue);
445 line = line.substr(posEndReward + 1);
450 size_t posColon = line.find(
':');
451 STORM_LOG_THROW(posColon != std::string::npos, storm::exceptions::WrongFormatException,
452 "':' not found in '" << line <<
"' on line " << lineNumber <<
".");
454 std::string valueStr = line.substr(posColon + 2);
455 ValueType value =
parseValue(valueStr, placeholders, valueParser);
456 STORM_LOG_TRACE(
"Transition " << row <<
" -> " << target <<
": " << value);
457 STORM_LOG_THROW(target < nrStates, storm::exceptions::WrongFormatException,
458 "In line " << lineNumber <<
" target state " << target <<
" is greater than state size " << nrStates <<
".");
459 builder.addNextValue(row, target, value);
463 std::cout <<
"Parsed " << state <<
"/" << nrStates <<
" states before abort.\n";
464 STORM_LOG_THROW(
false, storm::exceptions::AbortException,
"Aborted in state space exploration.");
471 if (nonDeterministic) {
472 STORM_LOG_THROW(nrChoices == 0 ||
builder.getLastRow() + 1 == nrChoices, storm::exceptions::WrongFormatException,
473 "Number of actions detected (at least " <<
builder.getLastRow() + 1 <<
") does not match number of actions declared (" << nrChoices
474 <<
", in @nr_choices).");
482 uint64_t numRewardModels = std::max(stateRewards.size(), actionRewards.size());
483 for (uint64_t i = 0; i < numRewardModels; ++i) {
484 std::string rewardModelName;
486 rewardModelName =
"rew" + std::to_string(i);
490 std::optional<std::vector<ValueType>> stateRewardVector, actionRewardVector;
491 if (i < stateRewards.size() && !stateRewards[i].empty()) {
492 stateRewardVector = std::move(stateRewards[i]);
494 if (i < actionRewards.size() && !actionRewards[i].empty()) {
496 actionRewardVector = std::move(actionRewards[i]);