mCRL2
Loading...
Searching...
No Matches
io.h
Go to the documentation of this file.
1// Author(s): Sjoerd Cranen; simplified by Jan Friso Groote
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/// \file mcrl2/lps/io.h
10/// \brief IO routines for linear process specifications.
11
12#ifndef MCRL2_LPS_IO_H
13#define MCRL2_LPS_IO_H
14
15#include "mcrl2/atermpp/aterm_io_binary.h"
16#include "mcrl2/lps/specification.h"
17#include "mcrl2/lps/stochastic_specification.h"
18
19#include <fstream>
20
21namespace mcrl2::lps
22{
23
24/// \brief Writes LPS to the stream.
25atermpp::aterm_ostream& operator<<(atermpp::aterm_ostream& stream, const specification& spec);
26atermpp::aterm_ostream& operator<<(atermpp::aterm_ostream& stream, const stochastic_specification& spec);
27
28/// \brief Reads LPS from the stream.
31
32/// \brief Save an LPS in the format specified.
33/// \param spec The LPS to be stored
34/// \param stream The stream to which the output is written.
35/// \param target A string indicating the target stream, used to print information.
36/// utilities::file_format::unknown() is specified, then a default format is chosen.
37///
38template <typename Specification>
39void save_lps(const Specification& spec, std::ostream& stream, const std::string& target = "")
40{
41 mCRL2log(log::log_level_t::debug) << "Saving LPS" << (target.empty() ? "" : " to " + target) << ".\n";
42 atermpp::binary_aterm_ostream(stream) << spec;
43}
44
45/// \brief Load LPS from file.
46/// \param spec The LPS to which the result is loaded.
47/// \param stream The stream from which to load the LPS (it is assumed to have been opened in the
48/// right mode).
49/// \param source The source from which the stream originates. Used for error messages.
50template <typename Specification>
51void load_lps(Specification& spec, std::istream& stream, const std::string& source = "")
52{
53 mCRL2log(log::log_level_t::debug) << "Loading LPS" << (source.empty() ? "" : " from " + source) << ".\n";
54
55 if constexpr (std::is_same_v<Specification, specification>)
56 {
57 stochastic_specification stoch_spec;
58 atermpp::binary_aterm_istream(stream) >> stoch_spec;
59 spec=remove_stochastic_operators(stoch_spec);
60 }
61 else
62 {
63 atermpp::binary_aterm_istream(stream) >> spec;
64 }
65}
66
67/// \brief Saves an LPS to a file.
68/// \param spec The LPS to save.
69/// \param filename The file to save the LPS in. If empty, the file is written to stdout.
70template <typename Specification>
71void save_lps(const Specification& spec, const std::string& filename)
72{
73 if (filename.empty() || filename == "-")
74 {
75 save_lps(spec, std::cout, "standard output");
76 return;
77 }
78
79 std::ofstream ofs(filename, std::ios_base::binary);
80 if (!ofs.good())
81 {
82 throw mcrl2::runtime_error("Could not open file " + filename + ".");
83 }
84 save_lps(spec, ofs, filename);
85}
86
87/// \brief Load LPS from file.
88/// \param spec The LPS to which the result is loaded.
89/// \param filename The file from which to load the LPS. If empty, the file is read from stdin.
90
91template <typename Specification>
92void load_lps(Specification& spec, const std::string& filename)
93{
94 if (filename.empty() || filename == "-")
95 {
96 load_lps(spec, std::cin, "standard input");
97 return;
98 }
99
100 std::ifstream ifs(filename, std::ios_base::binary);
101 if (!ifs.good())
102 {
103 throw mcrl2::runtime_error("Could not open file " + filename + ".");
104 }
105 load_lps(spec, ifs, filename);
106}
107
108} // namespace mcrl2::lps
109
110#endif // MCRL2_LPS_IO_H
The interface for a class that reads aterm from a stream. The default constructed term aterm() indica...
Definition aterm_io.h:67
The interface for a class that writes aterm to a stream. Every written term is retrieved by the corre...
Definition aterm_io.h:51
A helper class to restore the state of the aterm_{i,o}stream objects upon destruction....
Definition aterm_io.h:114
aterm_stream_state(aterm_stream &stream)
Definition aterm_io.h:116
A unordered_map class in which aterms can be stored.
LPS summand containing a deadlock.
const data::data_expression & time() const
Returns the time.
Definition deadlock.h:46
\brief A timed multi-action
const process::action_list & actions() const
const data::data_expression & time() const
Linear process specification.
LPS summand containing a multi-action.
const stochastic_distribution & distribution() const
Returns the distribution of this summand.
\brief A stochastic distribution
stochastic_specification(const specification &other)
Constructor. This constructor is explicit as implicit conversions of this kind is a source of bugs.
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:435
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:103
aterm_istream & operator>>(aterm_istream &stream, aterm_transformer transformer)
Sets the given transformer to be applied to following reads.
Definition aterm_io.h:83
atermpp::aterm remove_index_impl(const atermpp::aterm &x)
Definition io.h:23
atermpp::aterm add_index_impl(const atermpp::aterm &x)
Definition io.h:33
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, data_specification &spec)
Reads a data specification from a stream.
Definition data_io.cpp:61
The main namespace for the LPS library.
Definition constelm.h:18
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, deadlock_summand &summand)
Definition lps_io.cpp:50
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, linear_process_base< ActionSummand > &lps)
Definition lps_io.cpp:107
void load_lps(Specification &spec, const std::string &filename)
Load LPS from file.
Definition io.h:92
void save_lps(const Specification &spec, std::ostream &stream, const std::string &target="")
Save an LPS in the format specified.
Definition io.h:39
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, lps::stochastic_specification &spec)
Definition lps_io.cpp:191
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, lps::specification &spec)
Reads LPS from the stream.
Definition lps_io.cpp:183
atermpp::aterm linear_process_specification_marker()
Definition lps_io.cpp:18
void save_lps(const Specification &spec, const std::string &filename)
Saves an LPS to a file.
Definition io.h:71
specification remove_stochastic_operators(const stochastic_specification &spec)
Converts a stochastic specification to a specification. Throws an exception if non-empty distribution...
void load_lps(Specification &spec, std::istream &stream, const std::string &source="")
Load LPS from file.
Definition io.h:51
static void read_spec(atermpp::aterm_istream &stream, stochastic_specification &spec)
Definition lps_io.cpp:136
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, multi_action &action)
Definition lps_io.cpp:30
static void write_spec(atermpp::aterm_ostream &stream, const stochastic_specification &spec)
Definition lps_io.cpp:122
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, stochastic_action_summand &summand)
Definition lps_io.cpp:74
The main namespace for the Process library.