Storm 1.13.0.1
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"
16
18
19namespace storm {
20
21namespace jani {
22class Model;
23}
24
25namespace api {
26
27void exportJaniModelAsDot(storm::jani::Model const& model, std::string const& filename);
28
29template<typename ValueType>
30void exportSparseModelAsDrn(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::string const& filename,
31 std::vector<std::string> const& parameterNames = {}, bool allowPlaceholders = true) {
33 options.allowPlaceholders = allowPlaceholders;
34 storm::io::explicitExportSparseModel(filename, model, parameterNames, options);
35}
36
37template<typename ValueType>
38void exportSparseModelAsDrn(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::string const& filename,
39 storm::io::DirectEncodingExporterOptions options, std::vector<std::string> const& parameterNames = {}) {
40 storm::io::explicitExportSparseModel(filename, model, parameterNames, options);
41}
42
43template<storm::dd::DdType Type, typename ValueType>
44void exportSymbolicModelAsDrdd(std::shared_ptr<storm::models::symbolic::Model<Type, ValueType>> const& model, std::string const& filename) {
46}
47
48template<typename ValueType>
49void exportSparseModelAsDot(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::string const& filename, size_t maxWidth = 30) {
50 std::ofstream stream;
51 storm::io::openFile(filename, stream);
52 model->writeDotToStream(stream, maxWidth);
54}
55
56template<typename ValueType>
57void exportSparseModelAsJson(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::string const& filename) {
58 std::ofstream stream;
59 storm::io::openFile(filename, stream);
60 model->writeJsonToStream(stream);
62}
63
64template<typename ValueType>
65void exportSparseModelAsUmb(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::string const& filename,
66 storm::umb::ExportOptions const& options = {}) {
67 if constexpr (std::is_same_v<ValueType, storm::RationalFunction>) {
68 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Export of rational functions to UMB is not supported.");
69 } else {
70 storm::umb::exportModelToUmb(*model, filename, options);
71 }
72}
73
74template<storm::dd::DdType Type, typename ValueType>
75void exportSymbolicModelAsDot(std::shared_ptr<storm::models::symbolic::Model<Type, ValueType>> const& model, std::string const& filename) {
76 model->writeDotToFile(filename);
77}
78
79template<typename ValueType>
81 std::string const& filename) {
82 std::ofstream stream;
83 storm::io::openFile(filename, stream);
84 std::string jsonFileExtension = ".json";
85 if (filename.size() > 4 && std::equal(jsonFileExtension.rbegin(), jsonFileExtension.rend(), filename.rbegin())) {
86 scheduler.printJsonToStream(stream, model, false, true);
87 } else {
88 scheduler.printToStream(stream, model, false, true);
89 }
91}
92
93template<typename ValueType, typename PointType>
94void exportParetoScheduler(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::vector<PointType> const& points,
95 std::vector<storm::storage::Scheduler<ValueType>> const& schedulers, std::string const& baseFilenameStr) {
96 // We export each scheduler in a separate file, named baseFilename_pointi.ext, where ext is the extension of the baseFilename.
97 // Additionally, we create a CSV file that associates each scheduler file with the corresponding point.
98 // Note that we cannot directly put the point coordinates into the filename, as some characters (e.g., ',' or '/' (occurring in rational numbers)) are not
99 // handled well in filenames.
100 std::filesystem::path baseFilename(baseFilenameStr);
101 std::ofstream infoStream;
102 auto infoFilePath = baseFilename;
103 infoFilePath.replace_filename(infoFilePath.stem().string() + "_info.csv");
104 storm::io::openFile(infoFilePath, infoStream);
105 infoStream << "file;point\n";
106 STORM_LOG_THROW(points.size() == schedulers.size(), storm::exceptions::UnexpectedException, "Number of points and schedulers must match.");
107 for (uint64_t i = 0; i < points.size(); ++i) {
108 std::string schedulerFileName = baseFilename.stem().string() + "_point" + std::to_string(i) + baseFilename.extension().string();
109 infoStream << schedulerFileName << ";[";
110 bool first = true;
111 for (auto const& pointEntry : points[i]) {
112 if (!first) {
113 infoStream << ",";
114 }
115 first = false;
116 infoStream << pointEntry;
117 }
118 infoStream << "]\n";
119 std::filesystem::path schedulerPath = baseFilename;
120 schedulerPath.replace_filename(schedulerFileName);
121 storm::api::exportScheduler(model, schedulers[i], schedulerPath);
122 }
123 storm::io::closeFile(infoStream);
124}
125
126template<typename ValueType>
127inline void exportCheckResultToJson(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model,
128 std::unique_ptr<storm::modelchecker::CheckResult> const& checkResult, std::string const& filename) {
129 std::ofstream stream;
130 storm::io::openFile(filename, stream);
131 if (checkResult->isExplicitQualitativeCheckResult()) {
132 auto j = checkResult->template asExplicitQualitativeCheckResult<ValueType>().template toJson<storm::RationalNumber>(model->getOptionalStateValuations(),
133 model->getStateLabeling());
134 stream << storm::dumpJson(j);
135 } else {
136 STORM_LOG_THROW(checkResult->isExplicitQuantitativeCheckResult(), storm::exceptions::NotSupportedException,
137 "Export of check results is only supported for explicit check results (e.g. in the sparse engine)");
138 auto j = checkResult->template asExplicitQuantitativeCheckResult<ValueType>().toJson(model->getOptionalStateValuations(), model->getStateLabeling());
139 stream << storm::dumpJson(j);
140 }
141 storm::io::closeFile(stream);
142}
143
144template<>
146 std::unique_ptr<storm::modelchecker::CheckResult> const&, std::string const&) {
147 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Export of check results is not supported for rational functions. ");
148}
149
150} // namespace api
151} // namespace storm
Base class for all sparse models.
Definition Model.h:30
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:30
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:80
void exportSymbolicModelAsDot(std::shared_ptr< storm::models::symbolic::Model< Type, ValueType > > const &model, std::string const &filename)
Definition export.h:75
void exportSparseModelAsUmb(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, std::string const &filename, storm::umb::ExportOptions const &options={})
Definition export.h:65
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:145
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:44
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:30
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:127
void exportSparseModelAsJson(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, std::string const &filename)
Definition export.h:57
void exportSparseModelAsDot(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, std::string const &filename, size_t maxWidth=30)
Definition export.h:49
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:94
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
void exportModelToUmb(storm::models::sparse::Model< ValueType > const &model, std::filesystem::path const &targetLocation, ExportOptions const &options)
Definition Umb.cpp:37
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.