Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
export.h
Go to the documentation of this file.
1#pragma once
2
3#include <filesystem>
4
9#include "storm/io/file.h"
10#include "storm/io/umb/Umb.h"
18
20
21namespace storm {
22
23namespace jani {
24class Model;
25}
26
27namespace api {
28
29void exportJaniModelAsDot(storm::jani::Model const& model, std::string const& filename);
30
31template<typename ValueType>
32void exportSparseModelAsDrn(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::string const& filename,
33 std::vector<std::string> const& parameterNames = {}, bool allowPlaceholders = true) {
35 options.allowPlaceholders = allowPlaceholders;
36 storm::io::explicitExportSparseModel(filename, model, parameterNames, options);
37}
38
39template<typename ValueType>
40void exportSparseModelAsDrn(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::string const& filename,
41 storm::io::DirectEncodingExporterOptions options, std::vector<std::string> const& parameterNames = {}) {
42 storm::io::explicitExportSparseModel(filename, model, parameterNames, options);
43}
44
45template<storm::dd::DdType Type, typename ValueType>
46void exportSymbolicModelAsDrdd(std::shared_ptr<storm::models::symbolic::Model<Type, ValueType>> const& model, std::string const& filename) {
48}
49
50template<typename ValueType>
51void exportSparseModelAsDot(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::string const& filename, size_t maxWidth = 30) {
52 std::ofstream stream;
53 storm::io::openFile(filename, stream);
54 model->writeDotToStream(stream, maxWidth);
56}
57
58template<typename ValueType>
59void exportSparseModelAsJson(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::string const& filename) {
60 std::ofstream stream;
61 storm::io::openFile(filename, stream);
62 model->writeJsonToStream(stream);
64}
65
66template<typename ValueType>
67void exportSparseModelAsUmb(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::string const& filename,
68 storm::umb::ExportOptions const& options = {}) {
69 if constexpr (std::is_same_v<ValueType, storm::RationalFunction>) {
70 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Export of rational functions to UMB is not supported.");
71 } else {
72 storm::umb::exportModelToUmb(*model, filename, options);
73 }
74}
75
76template<storm::dd::DdType Type, typename ValueType>
77void exportSymbolicModelAsDot(std::shared_ptr<storm::models::symbolic::Model<Type, ValueType>> const& model, std::string const& filename) {
78 model->writeDotToFile(filename);
79}
80
81template<typename ValueType>
83 std::string const& filename) {
84 std::ofstream stream;
85 storm::io::openFile(filename, stream);
86 std::string jsonFileExtension = ".json";
87 if (filename.size() > 4 && std::equal(jsonFileExtension.rbegin(), jsonFileExtension.rend(), filename.rbegin())) {
88 scheduler.printJsonToStream(stream, model, false, true);
89 } else {
90 scheduler.printToStream(stream, model, false, true);
91 }
93}
94
95template<typename ValueType, typename PointType>
96void exportParetoScheduler(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::vector<PointType> const& points,
97 std::vector<storm::storage::Scheduler<ValueType>> const& schedulers, std::string const& baseFilenameStr) {
98 // We export each scheduler in a separate file, named baseFilename_pointi.ext, where ext is the extension of the baseFilename.
99 // Additionally, we create a CSV file that associates each scheduler file with the corresponding point.
100 // Note that we cannot directly put the point coordinates into the filename, as some characters (e.g., ',' or '/' (occurring in rational numbers)) are not
101 // handled well in filenames.
102 std::filesystem::path baseFilename(baseFilenameStr);
103 std::ofstream infoStream;
104 auto infoFilePath = baseFilename;
105 infoFilePath.replace_filename(infoFilePath.stem().string() + "_info.csv");
106 storm::io::openFile(infoFilePath, infoStream);
107 infoStream << "file;point\n";
108 STORM_LOG_THROW(points.size() == schedulers.size(), storm::exceptions::UnexpectedException, "Number of points and schedulers must match.");
109 for (uint64_t i = 0; i < points.size(); ++i) {
110 std::string schedulerFileName = baseFilename.stem().string() + "_point" + std::to_string(i) + baseFilename.extension().string();
111 infoStream << schedulerFileName << ";[";
112 bool first = true;
113 for (auto const& pointEntry : points[i]) {
114 if (!first) {
115 infoStream << ",";
116 }
117 first = false;
118 infoStream << pointEntry;
119 }
120 infoStream << "]\n";
121 std::filesystem::path schedulerPath = baseFilename;
122 schedulerPath.replace_filename(schedulerFileName);
123 storm::api::exportScheduler(model, schedulers[i], schedulerPath);
124 }
125 storm::io::closeFile(infoStream);
126}
127
128template<typename ValueType>
129inline void exportCheckResultToJson(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model,
130 std::unique_ptr<storm::modelchecker::CheckResult> const& checkResult, std::string const& filename) {
131 std::ofstream stream;
132 storm::io::openFile(filename, stream);
133 if (checkResult->isExplicitQualitativeCheckResult()) {
134 auto j = checkResult->template asExplicitQualitativeCheckResult<ValueType>().template toJson<storm::RationalNumber>(model->getOptionalStateValuations(),
135 model->getStateLabeling());
136 stream << storm::dumpJson(j);
137 } else if (checkResult->isExplicitQuantitativeCheckResult()) {
138 auto j = checkResult->template asExplicitQuantitativeCheckResult<ValueType>().toJson(model->getOptionalStateValuations(), model->getStateLabeling());
139 stream << storm::dumpJson(j);
140 } else {
141 STORM_LOG_THROW(checkResult->isExplicitCertificateCheckResult(), storm::exceptions::NotSupportedException,
142 "Export of check results is only supported for explicit check results and certificates (e.g. in the sparse engine).");
144 auto j = checkResult->template asExplicitCertificateCheckResult<ValueType>().getCertificate().toJson();
145 stream << storm::dumpJson(j);
146 } else {
147 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Export of certificates is not supported for this value type.");
148 }
149 }
150 storm::io::closeFile(stream);
151}
152
153template<>
155 std::unique_ptr<storm::modelchecker::CheckResult> const&, std::string const&) {
156 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Export of check results is not supported for rational functions.");
157}
158
159} // namespace api
160} // namespace storm
Base class for all sparse models.
Definition Model.h:34
Base class for all symbolic models.
Definition Model.h:42
This class defines which action is chosen in a particular state of a non-deterministic model.
Definition Scheduler.h:18
void printJsonToStream(std::ostream &out, std::shared_ptr< storm::models::sparse::Model< ValueType > > model=nullptr, bool skipUniqueChoices=false, bool skipDontCareStates=false) const
Prints the scheduler in json format to the given output stream.
void printToStream(std::ostream &out, std::shared_ptr< storm::models::sparse::Model< ValueType > > model=nullptr, bool skipUniqueChoices=false, bool skipDontCareStates=false) const
Prints the scheduler to the given output stream.
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
void exportScheduler(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, storm::storage::Scheduler< ValueType > const &scheduler, std::string const &filename)
Definition export.h:82
void exportSymbolicModelAsDot(std::shared_ptr< storm::models::symbolic::Model< Type, ValueType > > const &model, std::string const &filename)
Definition export.h:77
void exportSparseModelAsUmb(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, std::string const &filename, storm::umb::ExportOptions const &options={})
Definition export.h:67
void exportCheckResultToJson< storm::RationalFunction >(std::shared_ptr< storm::models::sparse::Model< storm::RationalFunction > > const &, std::unique_ptr< storm::modelchecker::CheckResult > const &, std::string const &)
Definition export.h:154
void exportJaniModelAsDot(storm::jani::Model const &model, std::string const &filename)
Definition export.cpp:7
void exportSymbolicModelAsDrdd(std::shared_ptr< storm::models::symbolic::Model< Type, ValueType > > const &model, std::string const &filename)
Definition export.h:46
void exportSparseModelAsDrn(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, std::string const &filename, std::vector< std::string > const &parameterNames={}, bool allowPlaceholders=true)
Definition export.h:32
void exportCheckResultToJson(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, std::unique_ptr< storm::modelchecker::CheckResult > const &checkResult, std::string const &filename)
Definition export.h:129
void exportSparseModelAsJson(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, std::string const &filename)
Definition export.h:59
void exportSparseModelAsDot(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, std::string const &filename, size_t maxWidth=30)
Definition export.h:51
void exportParetoScheduler(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, std::vector< PointType > const &points, std::vector< storm::storage::Scheduler< ValueType > > const &schedulers, std::string const &baseFilenameStr)
Definition export.h:96
void explicitExportSparseModel(std::filesystem::path const &filename, std::shared_ptr< storm::models::sparse::Model< ValueType > > sparseModel, std::vector< std::string > const &parameters, DirectEncodingExporterOptions const &options)
Exports a sparse model into the explicit DRN format.
void explicitExportSymbolicModel(std::string const &filename, std::shared_ptr< storm::models::symbolic::Model< Type, ValueType > > symbolicModel)
Exports a sparse model into the explicit drdd format.
void closeFile(std::ofstream &stream)
Close the given file after writing.
Definition file.h:47
void openFile(std::string const &filepath, std::ofstream &filestream, bool append=false, bool silent=false)
Open the given file for writing.
Definition file.h:18
constexpr bool SupportsCertificates
Certificates can be computed for models with these value types.
Definition Certificate.h:27
void exportModelToUmb(storm::models::sparse::Model< ValueType > const &model, std::filesystem::path const &targetLocation, ExportOptions const &options)
Definition Umb.cpp:37
The contract of a DD backend.
std::string dumpJson(storm::json< ValueType > const &j, bool compact)
Dumps the given json object, producing a String.
bool allowPlaceholders
Allow placeholders for rational functions in the exported DRN file.