mCRL2
Loading...
Searching...
No Matches
io.cpp
Go to the documentation of this file.
1// Author(s): anonymous, Thomas Neele
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9
10#include "mcrl2/atermpp/aterm_io_binary.h"
11#include "mcrl2/core/load_aterm.h"
12#include "mcrl2/data/data_io.h"
13#include "mcrl2/pbes/algorithms.h"
14#include "mcrl2/pbes/detail/pbes_io.h"
15#include "mcrl2/pbes/io.h"
16#include "mcrl2/pbes/join.h"
17#include "mcrl2/pbes/parse.h"
18#include "mcrl2/pbes/normal_forms.h"
19
20namespace mcrl2
21{
22
23namespace pbes_system
24{
25
27{
28 static std::vector<utilities::file_format> result;
29 if (result.empty())
30 {
31 result.push_back(utilities::file_format("pbes", "PBES in internal format", false));
32 result.back().add_extension("pbes");
33 result.push_back(utilities::file_format("text", "PBES in textual (mCRL2) format", true));
34 result.back().add_extension("txt");
35 result.push_back(utilities::file_format("bes", "BES in internal format", false));
36 result.back().add_extension("bes");
37 result.push_back(utilities::file_format("pgsolver", "BES in PGSolver format", true));
38 result.back().add_extension("gm");
39 result.back().add_extension("pg");
40 }
41 return result;
42}
43
44/// \brief Save a PBES in the format specified.
45/// \param pbes The PBES to be stored
46/// \param stream The stream to which the output is saved.
47/// \param format Determines the format in which the result is written. If unspecified, or
48/// pbes_file_unknown is specified, then a default format is chosen.
49void save_pbes(const pbes& pbes,
50 std::ostream& stream,
51 utilities::file_format format)
52{
54 {
56 }
57 mCRL2log(log::verbose) << "Saving result in " << format.shortname() << " format..." << std::endl;
59 {
60 atermpp::binary_aterm_ostream(stream) << pbes;
61 }
63 {
64 save_bes_pgsolver(pbes, stream);
65 }
66 else if (format == pbes_format_text())
67 {
68 stream << pp(pbes);
69 }
70 else
71 {
72 throw mcrl2::runtime_error("Trying to save PBES in non-PBES format (" + format.shortname() + ")");
73 }
74}
75
76/// \brief Load a PBES from file.
77/// \param pbes The PBES to which the result is loaded.
78/// \param stream The stream from which to load the PBES.
79/// \param format The format that should be assumed for the file in infilename. If unspecified, or
80/// pbes_file_unknown is specified, then a default format is chosen.
81/// \param source The source from which the stream originates. Used for error messages.
82void load_pbes(pbes& pbes, std::istream& stream, utilities::file_format format, const std::string& /*source*/)
83{
85 {
87 }
88 mCRL2log(log::verbose) << "Loading PBES in " << format.shortname() << " format..." << std::endl;
90 {
91 atermpp::binary_aterm_istream(stream) >> pbes;
92 }
93 else
94 if (format == pbes_format_text())
95 {
96 stream >> pbes;
97 }
98 else
99 {
100 throw mcrl2::runtime_error("Trying to load PBES from non-PBES format (" + format.shortname() + ")");
101 }
102}
103
104/// \brief save_pbes Saves a PBES to a file.
105/// \param pbes The PBES to save.
106/// \param filename The file to save the PBES in.
107/// \param format The format in which to save the PBES.
108/// \param welltypedness_check If set to false, skips checking whether pbes is well typed before
109/// saving it to file.
110///
111/// The format of the file in infilename is guessed if format is not given or if it is equal to
112/// utilities::file_format().
113void save_pbes(const pbes& pbes, const std::string& filename,
114 utilities::file_format format,
115 bool welltypedness_check)
116{
117 if (welltypedness_check)
118 {
119 assert(pbes.is_well_typed());
120 }
121 if (format == utilities::file_format())
122 {
123 format = guess_format(filename);
124 }
125
126 if (filename.empty() || filename == "-")
127 {
128 save_pbes(pbes, std::cout, format);
129 }
130 else
131 {
132 std::ofstream filestream(filename,(format.text_format()?std::ios_base::out: std::ios_base::binary));
133 if (!filestream.good())
134 {
135 throw mcrl2::runtime_error("Could not open file " + filename);
136 }
137 save_pbes(pbes, filestream, format);
138 }
139}
140
141/// \brief Load pbes from file.
142/// \param pbes The pbes to which the result is loaded.
143/// \param filename The file from which to load the PBES.
144/// \param format The format in which the PBES is stored in the file.
145///
146/// The format of the file in infilename is guessed if format is not given or if it is equal to
147/// utilities::file_format().
148void load_pbes(pbes& pbes,
149 const std::string& filename,
150 utilities::file_format format)
151{
152 if (format == utilities::file_format())
153 {
154 format = guess_format(filename);
155 }
156 if (filename.empty() || filename == "-")
157 {
158 load_pbes(pbes, std::cin, format);
159 }
160 else
161 {
162 std::ifstream filestream(filename,(format.text_format()?std::ios_base::in: std::ios_base::binary));
163 if (!filestream.good())
164 {
165 throw mcrl2::runtime_error("Could not open file " + filename);
166 }
167 load_pbes(pbes, filestream, format, core::detail::file_source(filename));
168 }
169}
170
171// transforms DataVarId to DataVarIdNoIndex
172// transforms OpId to OpIdNoIndex
173// transforms PropVarInst to PropVarInstNoIndex
175{
177 {
178 return atermpp::aterm(core::detail::function_symbol_OpIdNoIndex(), x.begin(), --x.end());
179 }
180 return x;
181}
182
183// transforms DataVarIdNoIndex to DataVarId (obsolete)
184// transforms OpIdNoIndex to OpId
185// transforms PropVarInstNoIndex to PropVarInst (obsolete)
187{
189 {
190 const data::variable& y = reinterpret_cast<const data::variable&>(x);
192 }
193 else if (x.function() == core::detail::function_symbol_OpIdNoIndex()) // This should be removed in due time. Say 2025.
194 {
195 const data::function_symbol& y = reinterpret_cast<const data::function_symbol&>(x);
197 }
198 else if (x.function() == core::detail::function_symbol_PropVarInstNoIndex()) // This should be removed in due time. Say 2025.
199 {
202 }
203 return x;
204}
205
206inline atermpp::aterm_ostream& operator<<(atermpp::aterm_ostream& stream, const pbes_equation& equation)
207{
208 stream << equation.symbol();
209 stream << equation.variable();
210 stream << equation.formula();
211 return stream;
212}
213
215{
216 fixpoint_symbol symbol;
218 pbes_expression expression;
219
220 stream >> symbol;
221 stream >> var;
222 stream >> expression;
223
224 equation = pbes_equation(symbol, var, expression);
225
226 return stream;
227}
228
230{
231 return atermpp::aterm(atermpp::function_symbol("parameterised_boolean_equation_system", 0));
232}
233
234atermpp::aterm_ostream& operator<<(atermpp::aterm_ostream& stream, const pbes& pbes)
235{
236 atermpp::aterm_stream_state state(stream);
237 stream << remove_index_impl;
238
239 stream << pbes_marker();
240 stream << pbes.data();
241 stream << pbes.global_variables();
242 stream << pbes.equations();
243 stream << pbes.initial_state();
244 return stream;
245}
246
248{
249 atermpp::aterm_stream_state state(stream);
250 stream >> add_index_impl;
251
252 try
253 {
254 atermpp::aterm marker;
255 stream >> marker;
256
257 if (marker != pbes_marker())
258 {
259 throw mcrl2::runtime_error("Stream does not contain a parameterised boolean equation system (PBES).");
260 }
261
263 std::set<data::variable> global_variables;
264 std::vector<pbes_equation> equations;
266
267 stream >> data;
268 stream >> global_variables;
269 stream >> equations;
270 stream >> initial_state;
271
272 pbes = pbes_system::pbes(data, global_variables, equations, initial_state);
273
274 // Add all the sorts that are used in the specification
275 // to the data specification. This is important for those
276 // sorts that are built in, because these are not explicitly
277 // declared.
279 }
280 catch (std::exception& ex)
281 {
282 mCRL2log(log::error) << ex.what() << "\n";
283 throw mcrl2::runtime_error(std::string("Error reading parameterised boolean equation system (PBES)."));
284 }
285
286 return stream;
287}
288
289namespace detail
290{
291
292pbes load_pbes(const std::string& filename)
293{
294 pbes result;
295 if (filename.empty() || filename == "-")
296 {
297 atermpp::binary_aterm_istream(std::cin) >> result;
298 }
299 else
300 {
301 std::ifstream from(filename, std::ifstream::in | std::ifstream::binary);
302 atermpp::binary_aterm_istream(from) >> result;
303 }
304 return result;
305}
306
307void save_pbes(const pbes& pbesspec, const std::string& filename)
308{
309 if (filename.empty() || filename == "-")
310 {
311 atermpp::binary_aterm_ostream(std::cout) << pbesspec;
312 }
313 else
314 {
315 std::ofstream to(filename, std::ofstream::out | std::ofstream::binary);
316 if (!to.good())
317 {
318 throw mcrl2::runtime_error("Could not write to filename " + filename);
319 }
320 atermpp::binary_aterm_ostream(to) << pbesspec;
321 }
322}
323
324} // namespace detail
325
326/// \brief Conversion to atermappl.
327/// \return The PBES converted to aterm format.
329{
330 atermpp::aterm global_variables = atermpp::aterm(core::detail::function_symbol_GlobVarSpec(),
331 data::variable_list(p.global_variables().begin(),
332 p.global_variables().end()));
333
334 atermpp::aterm_list eqn_list;
335 const std::vector<pbes_equation>& eqn = p.equations();
336 for (auto i = eqn.rbegin(); i != eqn.rend(); ++i)
337 {
338 atermpp::aterm a = pbes_equation_to_aterm(*i);
339 eqn_list.push_front(a);
340 }
341 atermpp::aterm equations = atermpp::aterm(core::detail::function_symbol_PBEqnSpec(), eqn_list);
342 atermpp::aterm initial_state = atermpp::aterm(core::detail::function_symbol_PBInit(), p.initial_state());
343 atermpp::aterm result;
344
345 result = atermpp::aterm(core::detail::function_symbol_PBES(),
347 global_variables,
348 equations,
349 initial_state);
350
351 return result;
352}
353
354} // namespace pbes_system
355
356} // namespace mcrl2
The interface for a class that reads aterm from a stream. The default constructed term aterm() indica...
Definition aterm_io.h:59
The interface for a class that writes aterm to a stream. Every written term is retrieved by the corre...
Definition aterm_io.h:48
A helper class to restore the state of the aterm_{i,o}stream objects upon destruction....
Definition aterm_io.h:84
aterm_stream_state(aterm_stream &stream)
Definition aterm_io.h:86
const function_symbol & function() const
Returns the function symbol belonging to an aterm.
Definition aterm.h:144
aterm & operator=(aterm &&other) noexcept=default
binary_aterm_istream(std::istream &is)
Provide the input stream from which terms are read.
binary_aterm_ostream(std::ostream &os)
Provide the output stream to which the terms are written.
bool operator==(const function_symbol &f) const
Equality test.
bool operator!=(const unprotected_aterm_core &t) const
Inequality operator on two unprotected aterms.
Definition aterm_core.h:92
\brief A function symbol
function_symbol(const core::identifier_string &name, const sort_expression &sort)
Constructor.
const core::identifier_string & name() const
const sort_expression & sort() const
\brief A data variable
Definition variable.h:28
const core::identifier_string & name() const
Definition variable.h:38
const sort_expression & sort() const
Definition variable.h:43
variable(const core::identifier_string &name, const sort_expression &sort)
Constructor.
Definition variable.h:62
logger(const log_level_t l)
Default constructor.
Definition logger.h:164
const pbes_expression & formula() const
Returns the predicate formula on the right hand side of the equation.
pbes_equation(const fixpoint_symbol &symbol, const propositional_variable &variable, const pbes_expression &expr)
Constructor.
const fixpoint_symbol & symbol() const
Returns the fixpoint symbol of the equation.
const propositional_variable & variable() const
Returns the pbes variable of the equation.
parameterized boolean equation system
Definition pbes.h:58
const propositional_variable_instantiation & initial_state() const
Returns the initial state.
Definition pbes.h:192
bool is_well_typed() const
Checks if the PBES is well typed.
Definition pbes.h:267
\brief A propositional variable instantiation
const data::data_expression_list & parameters() const
propositional_variable_instantiation(const core::identifier_string &name, const data::data_expression_list &parameters)
Constructor.
\brief A propositional variable declaration
Standard exception class for reporting runtime errors.
Definition exception.h:27
const std::string & shortname() const
bool operator==(const file_format &other) const
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:391
The main namespace for the aterm++ library.
Definition algorithm.h:21
aterm_istream & operator>>(aterm_istream &stream, aterm &term)
Read the given term from the stream, but for aterm_list we want to use a specific one that performs v...
Definition aterm_io.h:77
term_list< aterm > aterm_list
A term_list with elements of type aterm.
Definition aterm_list.h:497
aterm_istream & operator>>(aterm_istream &stream, aterm_transformer transformer)
Sets the given transformer to be applied to following reads.
Definition aterm_io.h:70
const atermpp::function_symbol & function_symbol_PBES()
const atermpp::function_symbol & function_symbol_OpIdNoIndex()
std::string file_source(const std::string &filename)
Definition load_aterm.h:25
const atermpp::function_symbol & function_symbol_GlobVarSpec()
const atermpp::function_symbol & function_symbol_DataVarIdNoIndex()
const atermpp::function_symbol & function_symbol_PropVarInstNoIndex()
const atermpp::function_symbol & function_symbol_PBInit()
const atermpp::function_symbol & function_symbol_OpId()
atermpp::aterm data_specification_to_aterm(const data_specification &s)
Definition data_io.cpp:44
Namespace for all data library functionality.
Definition data.cpp:22
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, data_specification &spec)
Reads a data specification from a stream.
Definition data_io.cpp:66
atermpp::term_list< variable > variable_list
\brief list of variables
@ verbose
Definition logger.h:37
bool is_bes(const pbes &x)
Returns true if a PBES is in BES form.
Definition pbes.cpp:70
pbes load_pbes(const std::string &filename)
Loads a PBES from filename, or from stdin if filename equals "".
Definition io.cpp:292
void save_pbes(const pbes &pbesspec, const std::string &filename)
Saves an PBES to filename, or to stdout if filename equals "".
Definition io.cpp:307
The main namespace for the PBES library.
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, pbes &pbes)
Reads a pbes from a stream.
Definition io.cpp:247
static atermpp::aterm remove_index_impl(const atermpp::aterm &x)
Definition io.cpp:174
void load_pbes(pbes &pbes, std::istream &stream, utilities::file_format format, const std::string &)
Load a PBES from file.
Definition io.cpp:82
void save_pbes(const pbes &pbes, const std::string &filename, utilities::file_format format, bool welltypedness_check)
save_pbes Saves a PBES to a file.
Definition io.cpp:113
const utilities::file_format & pbes_format_internal_bes()
Definition io.h:46
atermpp::aterm pbes_marker()
Definition io.cpp:229
void load_pbes(pbes &pbes, const std::string &filename, utilities::file_format format)
Load pbes from file.
Definition io.cpp:148
const utilities::file_format & pbes_format_pgsolver()
Definition io.h:48
std::istream & operator>>(std::istream &from, pbes &result)
Reads a PBES from an input stream.
Definition parse.h:52
atermpp::aterm pbes_to_aterm(const pbes &p)
Conversion to atermappl.
Definition io.cpp:328
const std::vector< utilities::file_format > & pbes_file_formats()
Definition io.cpp:26
void save_bes_pgsolver(const pbes &bes, std::ostream &stream, bool maxpg)
Definition pgsolver.cpp:142
std::string pp(const pbes_system::pbes &x)
Definition pbes.cpp:42
void save_pbes(const pbes &pbes, std::ostream &stream, utilities::file_format format)
Save a PBES in the format specified.
Definition io.cpp:49
void complete_data_specification(pbes &)
Adds all sorts that appear in the PBES p to the data specification of p.
Definition pbes.h:314
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, pbes_equation &equation)
Definition io.cpp:214
const utilities::file_format & pbes_format_internal()
Definition io.h:42
const utilities::file_format & pbes_format_text()
Definition io.h:44
static atermpp::aterm add_index_impl(const atermpp::aterm &x)
Definition io.cpp:186
const utilities::file_format guess_format(const std::string &filename)
Definition io.h:51
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
std::size_t operator()(const atermpp::detail::reference_aterm< T > &t) const