Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
storm-conv.cpp
Go to the documentation of this file.
2
4#include "storm/io/file.h"
15
16namespace storm {
17namespace api {
18
19void transformJani(storm::jani::Model& janiModel, std::vector<storm::jani::Property>& properties, storm::converter::JaniConversionOptions const& options) {
22 }
23
24 if (options.substituteConstants) {
25 janiModel.substituteConstantsInPlace(false);
26 }
27
28 if (options.localVars) {
29 STORM_LOG_WARN_COND(!options.globalVars, "Ignoring 'globalvars' option, since 'localvars' is also set.");
31 } else if (options.globalVars) {
33 }
34
35 if (!options.locationVariables.empty()) {
36 // Make variables local if necessary/possible
37 for (auto const& pair : options.locationVariables) {
38 if (janiModel.hasGlobalVariable(pair.second)) {
39 auto var = janiModel.getGlobalVariable(pair.second).getExpressionVariable();
40 if (storm::jani::JaniScopeChanger().canMakeVariableLocal(var, janiModel, properties, janiModel.getAutomatonIndex(pair.first)).first) {
41 storm::jani::JaniScopeChanger().makeVariableLocal(var, janiModel, janiModel.getAutomatonIndex(pair.first));
42 } else {
43 STORM_LOG_ERROR("Can not transform variable " << pair.second << " into locations since it can not be made local to automaton " << pair.first
44 << ".");
45 }
46 }
47 }
48
49 for (auto const& pair : options.locationVariables) {
50 storm::jani::JaniLocationExpander expander(janiModel);
51 janiModel = expander.transform(pair.first, pair.second).newModel;
52 }
53 }
54
55 if (options.simplifyComposition) {
56 janiModel.simplifyComposition();
57 }
58
59 if (options.locationElimination) {
60 auto locationHeuristic = options.locationEliminationLocationHeuristic;
61 auto edgesHeuristic = options.locationEliminationEdgeHeuristic;
62 janiModel = storm::jani::JaniLocalEliminator::eliminateAutomatically(janiModel, properties, locationHeuristic, edgesHeuristic);
63 }
64
65 if (options.flatten) {
66 std::shared_ptr<storm::utility::solver::SmtSolverFactory> smtSolverFactory;
68 smtSolverFactory = std::make_shared<storm::utility::solver::SmtSolverFactory>();
69 } else {
70 smtSolverFactory = std::make_shared<storm::utility::solver::Z3SmtSolverFactory>();
71 }
72 janiModel = janiModel.flattenComposition(smtSolverFactory);
73 }
74
75 if (!options.edgeAssignments) {
77 }
78
79 auto uneliminatedFeatures = janiModel.restrictToFeatures(options.allowedModelFeatures, properties);
80 STORM_LOG_WARN_COND(uneliminatedFeatures.empty(), "The following model features could not be eliminated: " << uneliminatedFeatures.toString());
81
82 if (options.modelName) {
83 janiModel.setName(options.modelName.get());
84 }
85
86 if (options.addPropertyConstants) {
87 for (auto& f : properties) {
88 for (auto const& constant : f.getUndefinedConstants()) {
89 if (!janiModel.hasConstant(constant.getName())) {
90 janiModel.addConstant(storm::jani::Constant(constant.getName(), constant));
91 }
92 }
93 }
94 }
95}
96
97void transformPrism(storm::prism::Program& prismProgram, std::vector<storm::jani::Property>& properties, bool simplify, bool flatten) {
98 if (simplify) {
99 prismProgram = prismProgram.simplify().simplify();
101 }
102 if (flatten) {
103 prismProgram = prismProgram.flattenModules();
104 if (simplify) {
105 // Let's simplify the flattened program again ... just to be sure ... twice ...
106 prismProgram = prismProgram.simplify().simplify();
107 }
108 }
109}
110
111std::pair<storm::jani::Model, std::vector<storm::jani::Property>> convertPrismToJani(storm::prism::Program const& program,
113 return convertPrismToJani(program, {}, options);
114}
115
116std::pair<storm::jani::Model, std::vector<storm::jani::Property>> convertPrismToJani(storm::prism::Program const& program,
117 std::vector<storm::jani::Property> const& properties,
119 // Perform conversion
120 auto res = program.toJani(properties, options.allVariablesGlobal);
121 if (res.second.empty()) {
122 std::vector<storm::jani::Property> clonedProperties;
123 for (auto const& p : properties) {
124 clonedProperties.push_back(p.clone());
125 }
126 res.second = std::move(clonedProperties);
127 }
128
129 // Postprocess Jani model based on the options
130 transformJani(res.first, res.second, options.janiOptions);
131
132 return res;
133}
134
135void exportJaniToFile(storm::jani::Model const& model, std::vector<storm::jani::Property> const& properties, std::string const& filename, bool compact) {
136 storm::jani::JsonExporter::toFile(model, properties, filename, true, compact);
137}
138
139void printJaniToStream(storm::jani::Model const& model, std::vector<storm::jani::Property> const& properties, std::ostream& ostream, bool compact) {
140 storm::jani::JsonExporter::toStream(model, properties, ostream, true, compact);
141}
142
143void exportPrismToFile(storm::prism::Program const& program, std::vector<storm::jani::Property> const& properties, std::string const& filename) {
144 std::ofstream stream;
145 storm::io::openFile(filename, stream);
146 stream << program << '\n';
147 storm::io::closeFile(stream);
148
149 if (!properties.empty()) {
150 storm::io::openFile(filename + ".props", stream);
151 for (auto const& prop : properties) {
152 stream << prop.asPrismSyntax() << '\n';
153 STORM_LOG_WARN_COND(!prop.containsUndefinedConstants(), "A property contains undefined constants. These might not be exported correctly.");
154 }
155 storm::io::closeFile(stream);
156 }
157}
158void printPrismToStream(storm::prism::Program const& program, std::vector<storm::jani::Property> const& properties, std::ostream& ostream) {
159 ostream << program << '\n';
160 for (auto const& prop : properties) {
161 STORM_LOG_WARN_COND(!prop.containsUndefinedConstants(), "A property contains undefined constants. These might not be exported correctly.");
162 ostream << prop.asPrismSyntax() << '\n';
163 }
164}
165
166} // namespace api
167} // namespace storm
static Model eliminateAutomatically(const Model &model, std::vector< jani::Property > properties, uint64_t locationHeuristic, uint64_t edgesHeuristic)
ReturnType transform(std::string const &automatonName, std::string const &variableName)
void makeVariablesGlobal(Model &model) const
Moves as many variables to the global scope as possible.
void makeVariablesLocal(Model &model, storm::OptionalRef< std::vector< Property > const > properties=storm::NullRef) const
Moves as many variables to a local scope as possible.
void makeVariableLocal(storm::expressions::Variable const &variable, Model &model, uint64_t automatonIndex) const
Moves the given variable into the local scope of the automaton with the given index.
std::pair< bool, uint64_t > canMakeVariableLocal(storm::expressions::Variable const &variable, Model const &model, storm::OptionalRef< std::vector< Property > const > properties=storm::NullRef, std::optional< uint64_t > automatonIndex=std::nullopt) const
Checks whether this variable can be made local without introducing out-of-scope accesses.
static void toFile(storm::jani::Model const &janiModel, std::vector< storm::jani::Property > const &formulas, std::string const &filepath, bool checkValid=true, bool compact=false)
static void toStream(storm::jani::Model const &janiModel, std::vector< storm::jani::Property > const &formulas, std::ostream &ostream, bool checkValid=false, bool compact=false)
Model & replaceUnassignedVariablesWithConstants()
Replaces each variable to which we never assign a value with a constant.
Definition Model.cpp:1104
Variable const & getGlobalVariable(std::string const &name) const
Retrieves the global variable with the given name if one exists.
Definition Model.cpp:761
Model & substituteConstantsInPlace(bool const substituteTranscendentalNumbers)
Substitutes all constants in all expressions of the model.
Definition Model.cpp:1109
void pushEdgeAssignmentsToDestinations()
Definition Model.cpp:1539
void addConstant(Constant const &constant)
Adds the given constant to the model.
Definition Model.cpp:654
bool hasConstant(std::string const &name) const
Retrieves whether the model has a constant with the given name.
Definition Model.cpp:663
void simplifyComposition()
Attempts to simplify the composition.
Definition Model.cpp:985
Model flattenComposition(std::shared_ptr< storm::utility::solver::SmtSolverFactory > const &smtSolverFactory=std::make_shared< storm::utility::solver::SmtSolverFactory >()) const
Flatten the composition to obtain an equivalent model that contains exactly one automaton that has th...
Definition Model.cpp:442
bool hasGlobalVariable(std::string const &name) const
Retrieves whether this model has a global variable with the given name.
Definition Model.cpp:757
void setName(std::string const &newName)
Sets the name of the model.
Definition Model.cpp:141
uint64_t getAutomatonIndex(std::string const &name) const
Retrieves the index of the given automaton.
Definition Model.cpp:908
ModelFeatures restrictToFeatures(ModelFeatures const &modelFeatures)
Attempts to eliminate all features of this model that are not in the given set of features.
Definition Model.cpp:1243
storm::expressions::Variable const & getExpressionVariable() const
Retrieves the associated expression variable.
Definition Variable.cpp:26
std::map< storm::expressions::Variable, storm::expressions::Expression > getConstantsFormulasSubstitution(bool getConstantsSubstitution=true, bool getFormulasSubstitution=true) const
Retrieves a mapping of all defined constants and formula variables to their defining expressions.
Definition Program.cpp:414
Program flattenModules(std::shared_ptr< storm::utility::solver::SmtSolverFactory > const &smtSolverFactory=std::shared_ptr< storm::utility::solver::SmtSolverFactory >(new storm::utility::solver::SmtSolverFactory())) const
Creates an equivalent program that contains exactly one module.
Definition Program.cpp:1951
Program simplify()
Entry point for static analysis for simplify.
Definition Program.cpp:1803
storm::jani::Model toJani(bool allVariablesGlobal=true, std::string suffix="") const
Converts the PRISM model into an equivalent JANI model.
Definition Program.cpp:2321
#define STORM_LOG_ERROR(message)
Definition logging.h:31
#define STORM_LOG_WARN_COND(cond, message)
Definition macros.h:38
void transformPrism(storm::prism::Program &prismProgram, std::vector< storm::jani::Property > &properties, bool simplify, bool flatten)
void exportJaniToFile(storm::jani::Model const &model, std::vector< storm::jani::Property > const &properties, std::string const &filename, bool compact)
void transformJani(storm::jani::Model &janiModel, std::vector< storm::jani::Property > &properties, storm::converter::JaniConversionOptions const &options)
void printPrismToStream(storm::prism::Program const &program, std::vector< storm::jani::Property > const &properties, std::ostream &ostream)
std::pair< storm::jani::Model, std::vector< storm::jani::Property > > convertPrismToJani(storm::prism::Program const &program, storm::converter::PrismToJaniConverterOptions options)
void exportPrismToFile(storm::prism::Program const &program, std::vector< storm::jani::Property > const &properties, std::string const &filename)
std::vector< storm::jani::Property > substituteConstantsInProperties(std::vector< storm::jani::Property > const &properties, std::map< storm::expressions::Variable, storm::expressions::Expression > const &substitution)
void printJaniToStream(storm::jani::Model const &model, std::vector< storm::jani::Property > const &properties, std::ostream &ostream, bool compact)
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
SettingsType const & getModule()
Get module.
LabParser.cpp.
Definition cli.cpp:18
uint64_t locationEliminationLocationHeuristic
Controls which locations are eliminated if location elimination is enabled.
boost::optional< std::string > modelName
If given, the model will get this name.
bool globalVars
If set, variables will be made global wherever possible.
bool replaceUnassignedVariablesWithConstants
If set, local and global variables that are (a) not assigned to some value and (b) have a known initi...
bool addPropertyConstants
Add constants that are defined in the properties to the model.
bool localVars
If set, variables will be made local wherever possible.
bool locationElimination
If set, attempts to perform location elimination of states to reduce the state space of the final mod...
storm::jani::ModelFeatures allowedModelFeatures
Only these model features are allowed in the output.
bool substituteConstants
If set, constants in expressions are substituted with their definition.
std::vector< std::pair< std::string, std::string > > locationVariables
(Automaton,Variable)-pairs that will be transformed to location variables of the respective automaton...
bool simplifyComposition
If set, attempts to simplify the system composition.
bool flatten
If set, the model is transformed into a single automaton.
bool edgeAssignments
If set, the model might have transient assignments to the edges.