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.put_integer(1);
31 stream.put_integer(state.get());
33 else if (state.size()<=1)
36 stream.put_integer(1);
37 stream.put_integer(state.get());
42 stream.put_integer(state.size());
44 for(
const auto& p : state)
47 stream.put_integer(p.state());
48 stream << p.probability();
57 const std::size_t value = stream.get_integer();
61 state.set(stream.get_integer());
68 for (std::size_t i = 0; i < value; ++i)
70 const std::size_t index = stream.get_integer();
71 stream >> probability;
73 state.add(index, probability);
76 state.shrink_to_fit();
90 static const atermpp::
aterm plain_mark(atermpp::function_symbol(
"transition", 0));
96 static atermpp::
aterm probabilistic_mark(atermpp::function_symbol(
"probabilistic_transition", 0));
97 return probabilistic_mark;
102 static const atermpp::
aterm initial_state_mark(atermpp::function_symbol(
"initial_state", 0));
103 return initial_state_mark;
108 static const atermpp::
aterm lts_mark(atermpp::function_symbol(
"labelled_transition_system", 0));
116 if (initial_state.size() > 1)
118 throw mcrl2::runtime_error(
"The initial state of the non probabilistic input lts is probabilistic.");
121 lts.set_initial_state(initial_state.get());
126 lts.set_initial_probabilistic_state(initial_state);
132 static_assert(std::is_same_v<LTS, probabilistic_lts_lts_t> || std::is_same_v<LTS, lts_lts_t>,
133 "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;
192 const std::size_t from = stream.get_integer();
194 const std::size_t to = stream.get_integer();
196 const auto [index, inserted] = multi_actions.insert(action);
198 std::size_t target_index = to;
199 if constexpr (std::is_same_v<LTS, probabilistic_lts_lts_t>)
201 probabilistic_lts_lts_t::probabilistic_state_t lts_state(to);
205 std::tie(target_index, state_inserted) = probabilistic_states.insert(lts_state);
210 std::size_t actual_index = lts.add_probabilistic_state(lts_state);
211 assert(actual_index == target_index);
216 lts.add_transition(transition(from, index, target_index));
217 number_of_states = std::max({number_of_states, from + 1, to + 1});
222 std::size_t actual_index = lts.add_action(action);
223 assert(actual_index == index);
229 if constexpr (std::is_same_v<LTS, probabilistic_lts_lts_t>)
231 probabilistic_lts_lts_t::probabilistic_state_t to;
233 const std::size_t from = stream.get_integer();
237 const auto [index, inserted] = multi_actions.insert(action);
240 const auto [to_index, state_inserted] = probabilistic_states.insert(to);
245 std::size_t actual_index = lts.add_probabilistic_state(to);
246 assert(actual_index == to_index);
249 lts.add_transition(transition(from, index, to_index));
252 number_of_states = std::max({number_of_states, from + 1, to.maximal_state() + 1});
257 std::size_t actual_index = lts.add_action(action);
258 assert(actual_index == index);
263 throw mcrl2::runtime_error(
"Attempting to read a probabilistic LTS as a regular LTS.");
266 else if (term.type_is_list())
274 probabilistic_lts_lts_t::probabilistic_state_t state;
276 initial_state = state;
280 throw mcrl2::runtime_error(
"Unknown mark in labelled transition system (LTS) stream.");
288 lts.set_num_states(number_of_states, lts.has_state_info());
290 set_initial_state(lts, initial_state.value());
294 throw mcrl2::runtime_error(
"Missing initial state in labelled transition system (LTS) stream.");
298template <
class LTS_TRANSITION_SYSTEM>
299static void read_from_lts(LTS_TRANSITION_SYSTEM& lts,
const std::string& filename)
301 static_assert(std::is_same_v<LTS_TRANSITION_SYSTEM, probabilistic_lts_lts_t>
302 || std::is_same_v<LTS_TRANSITION_SYSTEM, lts_lts_t>,
303 "Function read_from_lts can only be applied to a (probabilistic) lts. ");
305 std::ifstream fstream;
306 if (!filename.empty())
308 fstream.open(filename, std::ifstream::in | std::ifstream::binary);
311 if (filename.empty())
313 throw mcrl2::runtime_error(
"Fail to open standard input to read an lts.");
317 throw mcrl2::runtime_error(
"Fail to open file " + filename +
" to read an lts.");
324 atermpp::binary_aterm_istream stream(filename.empty() ? std::cin : fstream);
327 catch (
const std::exception& ex)
329 mCRL2log(log::log_level_t::error) << ex.what() <<
"\n";
330 if (filename.empty())
332 throw mcrl2::runtime_error(
"Fail to correctly read an lts from standard input.");
336 throw mcrl2::runtime_error(
"Fail to correctly read an lts from the file " + filename +
".");
344 stream << lts.initial_probabilistic_state();
350 stream << probabilistic_lts_lts_t::probabilistic_state_t(lts.initial_state());
356 static_assert(std::is_same_v<LTS, probabilistic_lts_lts_t> || std::is_same_v<LTS, lts_lts_t>,
357 "Function write_lts can only be applied to a (probabilistic) lts. ");
360 write_lts_header(stream,
362 lts.process_parameters(),
363 lts.action_label_declarations());
365 for (
const transition& trans : lts.get_transitions())
367 lts_lts_t::action_label_t label = lts.action_label(lts.apply_hidden_label_map(trans.label()));
369 if constexpr (std::is_same_v<LTS, probabilistic_lts_lts_t>)
371 write_transition(stream, trans.from(), label, lts.probabilistic_state(trans.to()));
375 write_transition(stream, trans.from(), label, trans.to());
379 if (lts.has_state_info())
381 for (std::size_t i = 0; i < lts.num_state_labels(); ++i)
384 stream << lts.state_label(i);
389 write_initial_state(stream, lts);
392template <
class LTS_TRANSITION_SYSTEM>
393static void write_to_lts(
const LTS_TRANSITION_SYSTEM& lts,
const std::string& filename)
395 static_assert(std::is_same_v<LTS_TRANSITION_SYSTEM, probabilistic_lts_lts_t>
396 || std::is_same_v<LTS_TRANSITION_SYSTEM, lts_lts_t>,
397 "Function write_to_lts can only be applied to a (probabilistic) lts. ");
399 bool to_stdout = filename.empty() || filename ==
"-";
400 std::ofstream fstream;
403 fstream.open(filename, std::ofstream::out | std::ofstream::binary);
406 throw mcrl2::runtime_error(
"Fail to open file " + filename +
" for writing.");
412 atermpp::binary_aterm_ostream stream(to_stdout ? std::cout : fstream);
415 catch (
const std::exception& ex)
417 mCRL2log(log::log_level_t::error) << ex.what() <<
"\n";
418 throw mcrl2::runtime_error(
"Fail to write lts correctly to the file " + filename +
".");
428 read_lts(stream, lts);
434 read_lts(stream, lts);
440 detail::write_lts(stream, lts);
446 detail::write_lts(stream, lts);
452 const data::variable_list& parameters,
453 const process::action_label_list& action_labels)
461 stream << parameters;
462 stream << action_labels;
468 stream.put_integer(from);
470 stream.put_integer(to);
478 write_transition(stream, from, label, to.get());
483 stream.put_integer(from);
498 stream << probabilistic_lts_lts_t::probabilistic_state_t(index);
503 mCRL2log(log::log_level_t::verbose) <<
"Starting to save a probabilistic lts to the file " << filename <<
".\n";
504 detail::write_to_lts(*
this, filename);
509 mCRL2log(log::log_level_t::verbose) <<
"Starting to save an lts to the file " << filename <<
".\n";
510 detail::write_to_lts(*
this, filename);
515 mCRL2log(log::log_level_t::verbose) <<
"Starting to load a probabilistic lts from the file " << filename <<
".\n";
516 detail::read_from_lts(*
this, filename);
521 mCRL2log(log::log_level_t::verbose) <<
"Starting to load an lts from the file " << filename <<
".\n";
522 detail::read_from_lts(*
this, filename);
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)
Postpones automatic garbage collection while an object of this class exists, for instance while readi...
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.