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