15#include "mcrl2/atermpp/standard_containers/indexed_set.h"
17#include "mcrl2/lts/lts_lts.h"
18#include "mcrl2/lts/lts_io.h"
30 stream << atermpp::aterm_int(1);
31 stream << atermpp::aterm_int(state.get());
33 else if (state.size()<=1)
36 stream << atermpp::aterm_int(1);
37 stream << atermpp::aterm_int(state.get());
42 stream << atermpp::aterm_int(state.size());
44 for(
const auto& p : state)
47 stream << atermpp::aterm_int(p.state());
48 stream << p.probability();
60 const std::size_t value=index.value();
65 state.set(index.value());
72 for (std::size_t i = 0; i < value; ++i)
75 stream >> probability;
77 state.add(index.value(), probability);
80 state.shrink_to_fit();
94 static const atermpp::
aterm plain_mark(atermpp::function_symbol(
"transition", 0));
100 static atermpp::
aterm probabilistic_mark(atermpp::function_symbol(
"probabilistic_transition", 0));
101 return probabilistic_mark;
106 static const atermpp::
aterm initial_state_mark(atermpp::function_symbol(
"initial_state", 0));
107 return initial_state_mark;
112 static const atermpp::
aterm lts_mark(atermpp::function_symbol(
"labelled_transition_system", 0));
120 if (initial_state.size() > 1)
122 throw mcrl2::runtime_error(
"The initial state of the non probabilistic input lts is probabilistic.");
125 lts.set_initial_state(initial_state.get());
130 lts.set_initial_probabilistic_state(initial_state);
136 static_assert(std::is_same_v<LTS, probabilistic_lts_lts_t> || std::is_same_v<LTS, lts_lts_t>,
137 "Function read_lts can only be applied to a (probabilistic) lts. ");
147 throw mcrl2::runtime_error(
"Stream does not contain a labelled transition system (LTS).");
152 data::variable_list parameters;
153 process::action_label_list action_labels;
156 stream >> parameters;
157 stream >> action_labels;
160 lts.set_process_parameters(parameters);
161 lts.set_action_label_declarations(action_labels);
164 mcrl2::utilities::indexed_set<action_label_lts> multi_actions;
165 multi_actions.insert(action_label_lts::tau_action());
168 std::optional<probabilistic_lts_lts_t::probabilistic_state_t> initial_state;
171 mcrl2::utilities::indexed_set<
172 probabilistic_lts_lts_t::probabilistic_state_t> probabilistic_states;
175 std::size_t number_of_states = 1;
197 const auto [index, inserted] = multi_actions.insert(action);
199 std::size_t target_index = to.value();
200 if constexpr (std::is_same_v<LTS, probabilistic_lts_lts_t>)
202 probabilistic_lts_lts_t::probabilistic_state_t lts_state(to.value());
206 std::tie(target_index, state_inserted) = probabilistic_states.insert(lts_state);
211 std::size_t actual_index = lts.add_probabilistic_state(lts_state);
212 assert(actual_index == target_index);
217 lts.add_transition(transition(from.value(), index, target_index));
218 number_of_states = std::max({number_of_states, from.value() + 1, to.value() + 1});
223 std::size_t actual_index = lts.add_action(action);
224 assert(actual_index == index);
230 if constexpr (std::is_same_v<LTS, probabilistic_lts_lts_t>)
232 probabilistic_lts_lts_t::probabilistic_state_t to;
238 const auto [index, inserted] = multi_actions.insert(action);
241 const auto [to_index, state_inserted] = probabilistic_states.insert(to);
246 std::size_t actual_index = lts.add_probabilistic_state(to);
247 assert(actual_index == to_index);
250 lts.add_transition(transition(from.value(), index, to_index));
253 number_of_states = std::max({number_of_states, from.value() + 1, to.maximal_state() + 1});
258 std::size_t actual_index = lts.add_action(action);
259 assert(actual_index == index);
264 throw mcrl2::runtime_error(
"Attempting to read a probabilistic LTS as a regular LTS.");
267 else if (term.type_is_list())
275 probabilistic_lts_lts_t::probabilistic_state_t state;
277 initial_state = state;
281 throw mcrl2::runtime_error(
"Unknown mark in labelled transition system (LTS) stream.");
289 lts.set_num_states(number_of_states, lts.has_state_info());
291 set_initial_state(lts, initial_state.value());
295 throw mcrl2::runtime_error(
"Missing initial state in labelled transition system (LTS) stream.");
299template <
class LTS_TRANSITION_SYSTEM>
300static void read_from_lts(LTS_TRANSITION_SYSTEM& lts,
const std::string& filename)
302 static_assert(std::is_same_v<LTS_TRANSITION_SYSTEM, probabilistic_lts_lts_t>
303 || std::is_same_v<LTS_TRANSITION_SYSTEM, lts_lts_t>,
304 "Function read_from_lts can only be applied to a (probabilistic) lts. ");
306 std::ifstream fstream;
307 if (!filename.empty())
309 fstream.open(filename, std::ifstream::in | std::ifstream::binary);
312 if (filename.empty())
314 throw mcrl2::runtime_error(
"Fail to open standard input to read an lts.");
318 throw mcrl2::runtime_error(
"Fail to open file " + filename +
" to read an lts.");
325 atermpp::binary_aterm_istream stream(filename.empty() ? std::cin : fstream);
328 catch (
const std::exception& ex)
330 mCRL2log(log::log_level_t::error) << ex.what() <<
"\n";
331 if (filename.empty())
333 throw mcrl2::runtime_error(
"Fail to correctly read an lts from standard input.");
337 throw mcrl2::runtime_error(
"Fail to correctly read an lts from the file " + filename +
".");
345 stream << lts.initial_probabilistic_state();
351 stream << probabilistic_lts_lts_t::probabilistic_state_t(lts.initial_state());
357 static_assert(std::is_same_v<LTS, probabilistic_lts_lts_t> || std::is_same_v<LTS, lts_lts_t>,
358 "Function write_lts can only be applied to a (probabilistic) lts. ");
361 write_lts_header(stream,
363 lts.process_parameters(),
364 lts.action_label_declarations());
366 for (
const transition& trans : lts.get_transitions())
368 lts_lts_t::action_label_t label = lts.action_label(lts.apply_hidden_label_map(trans.label()));
370 if constexpr (std::is_same_v<LTS, probabilistic_lts_lts_t>)
372 write_transition(stream, trans.from(), label, lts.probabilistic_state(trans.to()));
376 write_transition(stream, trans.from(), label, trans.to());
380 if (lts.has_state_info())
382 for (std::size_t i = 0; i < lts.num_state_labels(); ++i)
385 stream << lts.state_label(i);
390 write_initial_state(stream, lts);
393template <
class LTS_TRANSITION_SYSTEM>
394static void write_to_lts(
const LTS_TRANSITION_SYSTEM& lts,
const std::string& filename)
396 static_assert(std::is_same_v<LTS_TRANSITION_SYSTEM, probabilistic_lts_lts_t>
397 || std::is_same_v<LTS_TRANSITION_SYSTEM, lts_lts_t>,
398 "Function write_to_lts can only be applied to a (probabilistic) lts. ");
400 bool to_stdout = filename.empty() || filename ==
"-";
401 std::ofstream fstream;
404 fstream.open(filename, std::ofstream::out | std::ofstream::binary);
407 throw mcrl2::runtime_error(
"Fail to open file " + filename +
" for writing.");
413 atermpp::binary_aterm_ostream stream(to_stdout ? std::cout : fstream);
416 catch (
const std::exception& ex)
418 mCRL2log(log::log_level_t::error) << ex.what() <<
"\n";
419 throw mcrl2::runtime_error(
"Fail to write lts correctly to the file " + filename +
".");
429 read_lts(stream, lts);
435 read_lts(stream, lts);
441 detail::write_lts(stream, lts);
447 detail::write_lts(stream, lts);
453 const data::variable_list& parameters,
454 const process::action_label_list& action_labels)
462 stream << parameters;
463 stream << action_labels;
469 stream << atermpp::aterm_int(from);
471 stream << atermpp::aterm_int(to);
479 write_transition(stream, from, label, to.get());
484 stream << atermpp::aterm_int(from);
499 stream << probabilistic_lts_lts_t::probabilistic_state_t(index);
504 mCRL2log(log::log_level_t::verbose) <<
"Starting to save a probabilistic lts to the file " << filename <<
".\n";
505 detail::write_to_lts(*
this, filename);
510 mCRL2log(log::log_level_t::verbose) <<
"Starting to save an lts to the file " << filename <<
".\n";
511 detail::write_to_lts(*
this, filename);
516 mCRL2log(log::log_level_t::verbose) <<
"Starting to load a probabilistic lts from the file " << filename <<
".\n";
517 detail::read_from_lts(*
this, filename);
522 mCRL2log(log::log_level_t::verbose) <<
"Starting to load an lts from the file " << filename <<
".\n";
523 detail::read_from_lts(*
this, filename);
An integer term stores a single std::size_t value. It carries no arguments.
The interface for a class that reads aterm from a stream. The default constructed term aterm() indica...
virtual void get(aterm &t)=0
Reads an aterm from this stream.
The interface for a class that writes aterm to a stream. Every written term is retrieved by the corre...
A helper class to restore the state of the aterm_{i,o}stream objects upon destruction....
aterm_stream_state(aterm_stream &stream)
A unordered_map class in which aterms can be stored.
\brief A timed multi-action
This class contains labels for probabilistic transistions, consisting of a numerator and a denumerato...
A class containing the values for action labels for the .lts format.
function object to compare two constln_t pointers based on their contents
This class contains labelled transition systems in .lts format.
void save(const std::string &filename) const
Save the labelled transition system to file.
void load(const std::string &filename)
Load the labelled transition system from file.
This class contains probabilistic labelled transition systems in .lts format.
void load(const std::string &filename)
Load the labelled transition system from file.
void save(const std::string &filename) const
Save the labelled transition system to file.
This class contains state labels for an labelled transition system in .lts format.
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
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...
aterm_istream & operator>>(aterm_istream &stream, aterm_transformer transformer)
Sets the given transformer to be applied to following reads.
atermpp::aterm remove_index_impl(const atermpp::aterm &x)
atermpp::aterm add_index_impl(const atermpp::aterm &x)
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, data_specification &spec)
Reads a data specification from a stream.
The main namespace for the LPS library.
static void set_initial_state(probabilistic_lts_lts_t <s, const probabilistic_lts_lts_t::probabilistic_state_t &initial_state)
static const atermpp::aterm & initial_state_mark()
static void set_initial_state(lts_lts_t <s, const probabilistic_lts_lts_t::probabilistic_state_t &initial_state)
static const atermpp::aterm & probabilistic_transition_mark()
static void write_to_lts(const LTS_TRANSITION_SYSTEM <s, const std::string &filename)
void write_initial_state(atermpp::aterm_ostream &stream, const lts_lts_t <s)
static void write_lts(atermpp::aterm_ostream &stream, const LTS <s)
static void read_from_lts(LTS_TRANSITION_SYSTEM <s, const std::string &filename)
static const atermpp::aterm & transition_mark()
void write_initial_state(atermpp::aterm_ostream &stream, const probabilistic_lts_lts_t <s)
static void read_lts(atermpp::aterm_istream &stream, LTS <s)
static const atermpp::aterm & labelled_transition_system_mark()
void write_transition(atermpp::aterm_ostream &stream, std::size_t from, const lps::multi_action &label, const probabilistic_lts_lts_t::probabilistic_state_t &to)
void write_lts_header(atermpp::aterm_ostream &stream, const data::data_specification &data, const data::variable_list ¶meters, const process::action_label_list &action_labels)
Writes the start of an LTS stream.
void write_initial_state(atermpp::aterm_ostream &stream, std::size_t index)
Write the initial state to the LTS stream.
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, probabilistic_lts_lts_t <s)
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, probabilistic_lts_lts_t::probabilistic_state_t &state)
void write_state_label(atermpp::aterm_ostream &stream, const state_label_lts &label)
Write a state label to the LTS stream.
void write_transition(atermpp::aterm_ostream &stream, std::size_t from, const lps::multi_action &label, std::size_t to)
Write a transition to the LTS stream.
atermpp::aterm_istream & operator>>(atermpp::aterm_istream &stream, lts_lts_t <s)
Read a (probabilistic) LTS from the given stream.
The main namespace for the Process library.