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