22template<
typename ValueType>
34 std::stringstream stream;
36 auto nameInArchive = filename.filename();
39 nameInArchive.replace_extension();
42 archiveWriter.
addTextFile(nameInArchive, stream.str());
46template<
typename ValueType>
57 std::vector<ValueType> exitRates;
59 exitRates = sparseModel->template as<storm::models::sparse::Ctmc<ValueType>>()->getExitRateVector();
61 exitRates = sparseModel->template as<storm::models::sparse::MarkovAutomaton<ValueType>>()->getExitRates();
65 os <<
"// Exported by storm\n";
66 os <<
"// Original model type: " << sparseModel->getType() <<
'\n';
67 os <<
"@type: " << sparseModel->getType() <<
'\n';
68 os <<
"@value_type: ";
69 if constexpr (std::is_same_v<ValueType, double>) {
71 }
else if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
73 }
else if constexpr (std::is_same_v<ValueType, storm::Interval>) {
74 os <<
"double-interval";
75 }
else if constexpr (std::is_same_v<ValueType, storm::RationalFunction>) {
81 os <<
"@parameters\n";
82 if (parameters.empty()) {
83 for (std::string
const& parameter :
getParameters(sparseModel)) {
84 os << parameter <<
" ";
87 for (std::string
const& parameter : parameters) {
88 os << parameter <<
" ";
96 std::unordered_map<ValueType, std::string> placeholders;
100 if (!placeholders.empty()) {
101 os <<
"@placeholders\n";
102 for (
auto const& entry : placeholders) {
103 os <<
"$" << entry.second <<
" : " << entry.first <<
'\n';
107 os <<
"@reward_models\n";
108 for (
auto const& rewardModel : sparseModel->getRewardModels()) {
109 os << rewardModel.first <<
" ";
112 os <<
"@nr_states\n" << sparseModel->getNumberOfStates() <<
'\n';
113 os <<
"@nr_choices\n" << sparseModel->getNumberOfChoices() <<
'\n';
120 os <<
"state " << group;
123 if (!exitRates.empty()) {
125 writeValue(os, exitRates.at(group), placeholders);
129 os <<
" {" << sparseModel->template as<storm::models::sparse::Pomdp<ValueType>>()->getObservation(group) <<
"}";
134 for (
auto const& rewardModelEntry : sparseModel->getRewardModels()) {
142 if (rewardModelEntry.second.hasStateRewards()) {
143 writeValue(os, rewardModelEntry.second.getStateRewardVector().at(group), placeholders);
154 for (
auto const& label : sparseModel->getStateLabeling().getLabelsOfState(group)) {
155 STORM_LOG_THROW(std::count(label.begin(), label.end(),
'\"') == 0, storm::exceptions::NotSupportedException,
156 "Labels with quotation marks are not supported in the DRN format and therefore may not be exported.");
158 if (std::count_if(label.begin(), label.end(), isspace) > 0) {
159 os <<
" \"" << label <<
"\"";
166 if (sparseModel->hasStateValuations()) {
167 os <<
"//" << sparseModel->getStateValuations().getStateInfo(group) <<
'\n';
177 if (sparseModel->hasChoiceLabeling()) {
180 if (sparseModel->getChoiceLabeling().getLabelsOfChoice(row).empty()) {
183 for (
auto const& label : sparseModel->getChoiceLabeling().getLabelsOfChoice(row)) {
191 os <<
"\taction " << row - start;
196 for (
auto const& rewardModelEntry : sparseModel->getRewardModels()) {
204 if (rewardModelEntry.second.hasStateActionRewards()) {
205 writeValue(os, rewardModelEntry.second.getStateActionRewardVector().at(row), placeholders);
216 for (
auto it = matrix.
begin(row); it != matrix.
end(row); ++it) {
217 ValueType prob = it->getValue();
218 os <<
"\t\t" << it->getColumn() <<
" : ";
226template<
typename ValueType>
233 std::vector<std::string> parameters;
236 parametersProb.insert(parametersReward.begin(), parametersReward.end());
237 for (
auto const& parameter : parametersProb) {
238 std::stringstream stream;
240 parameters.push_back(stream.str());
245template<
typename ValueType>
260 auto ret = placeholders.insert(std::make_pair(value, std::to_string(i)));
271 std::unordered_map<storm::RationalFunction, std::string> placeholders;
275 for (
auto const& exitRate : exitRates) {
280 for (
auto const& rewardModelEntry : sparseModel->getRewardModels()) {
281 if (rewardModelEntry.second.hasStateRewards()) {
282 for (
auto const& reward : rewardModelEntry.second.getStateRewardVector()) {
286 if (rewardModelEntry.second.hasStateActionRewards()) {
287 for (
auto const& reward : rewardModelEntry.second.getStateActionRewardVector()) {
294 for (
auto const& entry : sparseModel->getTransitionMatrix()) {
301template<
typename ValueType>
302void writeValue(std::ostream& os, ValueType value, std::unordered_map<ValueType, std::string>
const& placeholders) {
309 auto it = placeholders.find(value);
310 if (it != placeholders.end()) {
312 os <<
"$" << it->second;
void addTextFile(std::filesystem::path const &archivePath, std::string const &data)
Add a text file to the archive.
Base class for all sparse models.
A class that holds a possibly non-square matrix in the compressed row storage format.
const_iterator begin(index_type row) const
Retrieves an iterator that points to the beginning of the given row.
const_iterator end(index_type row) const
Retrieves an iterator that points past the end of the given row.
index_type getRowGroupCount() const
Returns the number of row groups in the matrix.
bool hasTrivialRowGrouping() const
Retrieves whether the matrix has a trivial row grouping.
std::vector< index_type > const & getRowGroupIndices() const
Returns the grouping of rows of this matrix.
SparseMatrixIndexType index_type
#define STORM_LOG_ASSERT(cond, message)
#define STORM_LOG_THROW(cond, exception, message)
template void explicitExportSparseModel< double >(std::filesystem::path const &os, std::shared_ptr< storm::models::sparse::Model< double > > sparseModel, std::vector< std::string > const ¶meters, DirectEncodingExporterOptions const &options)
void explicitExportSparseModel(std::filesystem::path const &filename, std::shared_ptr< storm::models::sparse::Model< ValueType > > sparseModel, std::vector< std::string > const ¶meters, DirectEncodingExporterOptions const &options)
Exports a sparse model into the explicit DRN format.
CompressionMode getCompressionModeFromFileExtension(std::filesystem::path const &filename)
template void explicitExportSparseModel< storm::Interval >(std::filesystem::path const &os, std::shared_ptr< storm::models::sparse::Model< storm::Interval > > sparseModel, std::vector< std::string > const ¶meters, DirectEncodingExporterOptions const &options)
void createPlaceholder(std::unordered_map< storm::RationalFunction, std::string > &placeholders, storm::RationalFunction const &value, size_t &i)
Helper function to create a possible placeholder.
template void explicitExportSparseModel< storm::RationalNumber >(std::filesystem::path const &os, std::shared_ptr< storm::models::sparse::Model< storm::RationalNumber > > sparseModel, std::vector< std::string > const ¶meters, DirectEncodingExporterOptions const &options)
void writeValue(std::ostream &os, ValueType value, std::unordered_map< ValueType, std::string > const &placeholders)
Write value to stream while using the placeholders.
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.
std::vector< std::string > getParameters(std::shared_ptr< storm::models::sparse::Model< ValueType > >)
Accumulate parameters in the model.
std::unordered_map< ValueType, std::string > generatePlaceholders(std::shared_ptr< storm::models::sparse::Model< ValueType > >, std::vector< ValueType >)
Generate placeholders for rational functions in the model.
template void explicitExportSparseModel< storm::RationalFunction >(std::filesystem::path const &os, std::shared_ptr< storm::models::sparse::Model< storm::RationalFunction > > sparseModel, std::vector< std::string > const ¶meters, DirectEncodingExporterOptions const &options)
std::set< storm::RationalFunctionVariable > getRewardParameters(Model< storm::RationalFunction > const &model)
Get all parameters occurring in rewards.
std::set< storm::RationalFunctionVariable > getProbabilityParameters(Model< storm::RationalFunction > const &model)
Get all probability parameters occurring on transitions.
bool isConstant(ValueType const &)
bool allowPlaceholders
Allow placeholders for rational functions in the exported DRN file.
storm::io::CompressionMode compression
The type of compression used for the exported DRN file.
std::optional< std::size_t > outputPrecision
If set, the output precision for floating point numbers in the exported DRN file is set to the given ...