mCRL2
Loading...
Searching...
No Matches
io.cpp
Go to the documentation of this file.
1// Author(s): Jan Friso Groote. Based on pbes/source/io.cpp by a.o. 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/pres/algorithms.h"
14#include "mcrl2/pres/detail/pres_io.h"
15#include "mcrl2/pres/io.h"
16#include "mcrl2/pres/parse.h"
17
18namespace mcrl2
19{
20
21namespace pres_system
22{
23
25{
26 static std::vector<utilities::file_format> result;
27 if (result.empty())
28 {
29 result.push_back(utilities::file_format("pres", "PRES in internal format", false));
30 result.back().add_extension("pres");
31 result.push_back(utilities::file_format("text", "PRES in textual (mCRL2) format", true));
32 result.back().add_extension("txt");
33 }
34 return result;
35}
36
37
38/// \brief Save a PRES in the format specified.
39/// \param pres The PRES to be stored
40/// \param stream The stream to which the output is saved.
41/// \param format Determines the format in which the result is written. If unspecified, or
42/// pres_file_unknown is specified, then a default format is chosen.
43void save_pres(const pres& pres,
44 std::ostream& stream,
45 utilities::file_format format)
46{
48 {
50 }
51 mCRL2log(log::verbose) << "Saving result in " << format.shortname() << " format..." << std::endl;
52 if (format == pres_format_internal())
53 {
54 atermpp::binary_aterm_ostream(stream) << pres;
55 }
56 else
57 if (format == pres_format_text())
58 {
59 stream << pp(pres);
60 }
61 else
62 {
63 throw mcrl2::runtime_error("Trying to save PRES in non-PRES format (" + format.shortname() + ")");
64 }
65}
66
67/// \brief Load a PRES from file.
68/// \param pres The PRES to which the result is loaded.
69/// \param stream The stream from which to load the PRES.
70/// \param format The format that should be assumed for the file in infilename. If unspecified, or
71/// pres_file_unknown is specified, then a default format is chosen.
72/// \param source The source from which the stream originates. Used for error messages.
73void load_pres(pres& pres, std::istream& stream, utilities::file_format format, const std::string& /*source*/)
74{
76 {
78 }
79 mCRL2log(log::verbose) << "Loading PRES in " << format.shortname() << " format..." << std::endl;
80 if (format == pres_format_internal())
81 {
82 atermpp::binary_aterm_istream(stream) >> pres;
83 }
84 else
85 if (format == pres_format_text())
86 {
87 stream >> pres;
88 }
89 else
90 {
91 throw mcrl2::runtime_error("Trying to load PRES from non-PRES format (" + format.shortname() + ")");
92 }
93}
94
95/// \brief save_pres Saves a PRES to a file.
96/// \param pres The PRES to save.
97/// \param filename The file to save the PRES in.
98/// \param format The format in which to save the PRES.
99/// \param welltypedness_check If set to false, skips checking whether pres is well typed before
100/// saving it to file.
101///
102/// The format of the file in infilename is guessed if format is not given or if it is equal to
103/// utilities::file_format().
104void save_pres(const pres& pres, const std::string& filename,
105 utilities::file_format format,
106 bool welltypedness_check)
107{
108 if (welltypedness_check)
109 {
110 assert(pres.is_well_typed());
111 }
112 if (format == utilities::file_format())
113 {
114 format = guess_format(filename);
115 }
116
117 if (filename.empty() || filename == "-")
118 {
119 save_pres(pres, std::cout, format);
120 }
121 else
122 {
123 std::ofstream filestream(filename,(format.text_format()?std::ios_base::out: std::ios_base::binary));
124 if (!filestream.good())
125 {
126 throw mcrl2::runtime_error("Could not open file " + filename);
127 }
128 save_pres(pres, filestream, format);
129 }
130}
131
132/// \brief Load pres from file.
133/// \param pres The pres to which the result is loaded.
134/// \param filename The file from which to load the PRES.
135/// \param format The format in which the PRES is stored in the file.
136///
137/// The format of the file in infilename is guessed if format is not given or if it is equal to
138/// utilities::file_format().
139void load_pres(pres& pres,
140 const std::string& filename,
141 utilities::file_format format)
142{
143 if (format == utilities::file_format())
144 {
145 format = guess_format(filename);
146 }
147 if (filename.empty() || filename == "-")
148 {
149 load_pres(pres, std::cin, format);
150 }
151 else
152 {
153 std::ifstream filestream(filename,(format.text_format()?std::ios_base::in: std::ios_base::binary));
154 if (!filestream.good())
155 {
156 throw mcrl2::runtime_error("Could not open file " + filename);
157 }
158 load_pres(pres, filestream, format, core::detail::file_source(filename));
159 }
160}
161
162// transforms DataVarId to DataVarIdNoIndex
163// transforms OpId to OpIdNoIndex
164// transforms PropVarInst to PropVarInstNoIndex
166{
168 {
169 return atermpp::aterm(core::detail::function_symbol_OpIdNoIndex(), x.begin(), --x.end());
170 }
171 return x;
172}
173
174// transforms DataVarIdNoIndex to DataVarId (obsolete)
175// transforms OpIdNoIndex to OpId
176// transforms PropVarInstNoIndex to PropVarInst (obsolete)
178{
180 {
181 const data::variable& y = reinterpret_cast<const data::variable&>(x);
183 }
184 else if (x.function() == core::detail::function_symbol_OpIdNoIndex()) // This should be removed in due time. Say 2025.
185 {
186 const data::function_symbol& y = reinterpret_cast<const data::function_symbol&>(x);
188 }
189 else if (x.function() == core::detail::function_symbol_PropVarInstNoIndex()) // This should be removed in due time. Say 2025.
190 {
193 }
194 return x;
195}
196
197inline atermpp::aterm_ostream& operator<<(atermpp::aterm_ostream& stream, const pres_equation& equation)
198{
199 stream << equation.symbol();
200 stream << equation.variable();
201 stream << equation.formula();
202 return stream;
203}
204
206{
207 fixpoint_symbol symbol;
208 propositional_variable var;
209 pres_expression expression;
210
211 stream >> symbol;
212 stream >> var;
213 stream >> expression;
214
215 equation = pres_equation(symbol, var, expression);
216
217 return stream;
218}
219
221{
222 return atermpp::aterm(atermpp::function_symbol("parameterised_boolean_equation_system", 0));
223}
224
225atermpp::aterm_ostream& operator<<(atermpp::aterm_ostream& stream, const pres& pres)
226{
227 atermpp::aterm_stream_state state(stream);
228 stream << remove_index_impl;
229
230 stream << pres_marker();
231 stream << pres.data();
232 stream << pres.global_variables();
233 stream << pres.equations();
234 stream << pres.initial_state();
235 return stream;
236}
237
239{
240 atermpp::aterm_stream_state state(stream);
241 stream >> add_index_impl;
242
243 try
244 {
245 atermpp::aterm marker;
246 stream >> marker;
247
248 if (marker != pres_marker())
249 {
250 throw mcrl2::runtime_error("Stream does not contain a parameterised boolean equation system (PRES).");
251 }
252
254 std::set<data::variable> global_variables;
255 std::vector<pres_equation> equations;
257
258 stream >> data;
259 stream >> global_variables;
260 stream >> equations;
261 stream >> initial_state;
262
263 pres = pres_system::pres(data, global_variables, equations, initial_state);
264
265 // Add all the sorts that are used in the specification
266 // to the data specification. This is important for those
267 // sorts that are built in, because these are not explicitly
268 // declared.
270 }
271 catch (std::exception& ex)
272 {
273 mCRL2log(log::error) << ex.what() << "\n";
274 throw mcrl2::runtime_error(std::string("Error reading parameterised boolean equation system (PRES)."));
275 }
276
277 return stream;
278}
279
280namespace detail
281{
282
283pres load_pres(const std::string& filename)
284{
285 pres result;
286 if (filename.empty() || filename == "-")
287 {
288 atermpp::binary_aterm_istream(std::cin) >> result;
289 }
290 else
291 {
292 std::ifstream from(filename, std::ifstream::in | std::ifstream::binary);
293 atermpp::binary_aterm_istream(from) >> result;
294 }
295 return result;
296}
297
298void save_pres(const pres& presspec, const std::string& filename)
299{
300 if (filename.empty() || filename == "-")
301 {
302 atermpp::binary_aterm_ostream(std::cout) << presspec;
303 }
304 else
305 {
306 std::ofstream to(filename, std::ofstream::out | std::ofstream::binary);
307 if (!to.good())
308 {
309 throw mcrl2::runtime_error("Could not write to filename " + filename);
310 }
311 atermpp::binary_aterm_ostream(to) << presspec;
312 }
313}
314
315} // namespace detail
316
317/// \brief Conversion to atermappl.
318/// \return The PRES converted to aterm format.
320{
321 atermpp::aterm global_variables = atermpp::aterm(core::detail::function_symbol_GlobVarSpec(),
322 data::variable_list(p.global_variables().begin(),
323 p.global_variables().end()));
324
325 atermpp::aterm_list eqn_list;
326 const std::vector<pres_equation>& eqn = p.equations();
327 for (std::vector<pres_equation>::const_reverse_iterator i = eqn.rbegin(); i != eqn.rend(); ++i)
328 {
329 atermpp::aterm a = pres_equation_to_aterm(*i);
330 eqn_list.push_front(a);
331 }
332 atermpp::aterm equations = atermpp::aterm(core::detail::function_symbol_PREqnSpec(), eqn_list);
333 atermpp::aterm initial_state = atermpp::aterm(core::detail::function_symbol_PRInit(), p.initial_state());
334 atermpp::aterm result;
335
336 result = atermpp::aterm(core::detail::function_symbol_PRES(),
338 global_variables,
339 equations,
340 initial_state);
341 return result;
342}
343
344} // namespace pres_system
345
346} // 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 propositional_variable & variable() const
Returns the pres variable of the equation.
pres_equation(const fixpoint_symbol &symbol, const propositional_variable &variable, const pres_expression &expr)
Constructor.
const pres_expression & formula() const
Returns the predicate formula on the right hand side of the equation.
const fixpoint_symbol & symbol() const
Returns the fixpoint symbol of the equation.
parameterized boolean equation system
Definition pres.h:59
const propositional_variable_instantiation & initial_state() const
Returns the initial state.
Definition pres.h:198
bool is_well_typed() const
Checks if the PRES is well typed.
Definition pres.h:273
\brief A propositional variable instantiation
propositional_variable_instantiation(const core::identifier_string &name, const data::data_expression_list &parameters)
Constructor.
const data::data_expression_list & parameters() const
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_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_PRES()
const atermpp::function_symbol & function_symbol_PRInit()
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
void save_pres(const pres &presspec, const std::string &filename)
Saves an PRES to filename, or to stdout if filename equals "".
Definition io.cpp:298
pres load_pres(const std::string &filename)
Loads a PRES from filename, or from stdin if filename equals "".
Definition io.cpp:283
The main namespace for the PRES library.
void load_pres(pres &pres, std::istream &stream, utilities::file_format format, const std::string &)
Load a PRES from file.
Definition io.cpp:73
const utilities::file_format guess_format(const std::string &filename)
Definition io.h:45
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, pres_equation &equation)
Definition io.cpp:205
static atermpp::aterm remove_index_impl(const atermpp::aterm &x)
Definition io.cpp:165
const utilities::file_format & pres_format_internal()
Definition io.h:40
std::istream & operator>>(std::istream &from, pres &result)
Reads a PRES from an input stream.
Definition parse.h:52
void save_pres(const pres &pres, std::ostream &stream, utilities::file_format format)
Save a PRES in the format specified.
Definition io.cpp:43
const utilities::file_format & pres_format_text()
Definition io.h:42
void load_pres(pres &pres, const std::string &filename, utilities::file_format format)
Load pres from file.
Definition io.cpp:139
atermpp::aterm pres_marker()
Definition io.cpp:220
void save_pres(const pres &pres, const std::string &filename, utilities::file_format format, bool welltypedness_check)
save_pres Saves a PRES to a file.
Definition io.cpp:104
const std::vector< utilities::file_format > & pres_file_formats()
Definition io.cpp:24
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, pres &pres)
Reads a pres from a stream.
Definition io.cpp:238
atermpp::aterm pres_to_aterm(const pres &p)
Conversion to atermappl.
Definition io.cpp:319
std::string pp(const pres_system::pres &x)
Definition pres.cpp:45
static atermpp::aterm add_index_impl(const atermpp::aterm &x)
Definition io.cpp:177
void complete_data_specification(pres &)
Adds all sorts that appear in the PRES p to the data specification of p.
Definition pres.h:320
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