mCRL2
Loading...
Searching...
No Matches
liblts_lts.cpp
Go to the documentation of this file.
1// Author(s): Jeroen Keiren, Muck van Weerdenburg; completely rewritten 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 liblts_lts.cpp
10
11#include <algorithm>
12#include <fstream>
13#include <optional>
14
15#include "mcrl2/atermpp/standard_containers/indexed_set.h"
16
17#include "mcrl2/lts/lts_lts.h"
18#include "mcrl2/lts/lts_io.h"
19
20
21namespace mcrl2::lts
22{
23
24/// \brief Converts a probabilistic state into an aterm that encodes it.
25atermpp::aterm_ostream& operator<<(atermpp::aterm_ostream& stream, const probabilistic_lts_lts_t::probabilistic_state_t& state)
26{
27 if (state.size()==0)
28 {
29 // The probabilistic state is represented as a singular state and is stored without probability;
30 stream.put_integer(1);
31 stream.put_integer(state.get());
32 }
33 else if (state.size()<=1)
34 {
35 // The probabilistic state is the single element in a vector and is stored without probability;
36 stream.put_integer(1);
37 stream.put_integer(state.get());
38 }
39 else
40 {
41 // The probabilistic state is stored as a sequence of state probability pairs.
42 stream.put_integer(state.size());
43
44 for(const auto& p : state)
45 {
46 // Push a (index, probability) pair into the list.
47 stream.put_integer(p.state());
48 stream << p.probability();
49 }
50 }
51
52 return stream;
53}
54
55atermpp::aterm_istream& operator>>(atermpp::aterm_istream& stream, probabilistic_lts_lts_t::probabilistic_state_t& state)
56{
57 const std::size_t value = stream.get_integer(); // Read the number of states.
58 assert(value>0);
59 if (value==1)
60 {
61 state.set(stream.get_integer());
62 }
63 else
64 {
65 state.clear();
67
68 for (std::size_t i = 0; i < value; ++i)
69 {
70 const std::size_t index = stream.get_integer();
71 stream >> probability;
72
73 state.add(index, probability);
74 }
75
76 state.shrink_to_fit();
77 }
78 return stream;
79}
80
81namespace detail
82{
83
84using namespace atermpp;
85
86// Special terms to indicate the type of the following structure.
87
89{
90 static const atermpp::aterm plain_mark(atermpp::function_symbol("transition", 0));
91 return plain_mark;
92}
93
95{
96 static atermpp::aterm probabilistic_mark(atermpp::function_symbol("probabilistic_transition", 0));
97 return probabilistic_mark;
98}
99
101{
102 static const atermpp::aterm initial_state_mark(atermpp::function_symbol("initial_state", 0));
103 return initial_state_mark;
104}
105
107{
108 static const atermpp::aterm lts_mark(atermpp::function_symbol("labelled_transition_system", 0));
109 return lts_mark;
110}
111
112// Utility functions
113
114static void set_initial_state(lts_lts_t& lts, const probabilistic_lts_lts_t::probabilistic_state_t& initial_state)
115{
116 if (initial_state.size() > 1)
117 {
118 throw mcrl2::runtime_error("The initial state of the non probabilistic input lts is probabilistic.");
119 }
120
121 lts.set_initial_state(initial_state.get());
122}
123
124static void set_initial_state(probabilistic_lts_lts_t& lts, const probabilistic_lts_lts_t::probabilistic_state_t& initial_state)
125{
126 lts.set_initial_probabilistic_state(initial_state);
127}
128
129template <class LTS>
130static void read_lts(atermpp::aterm_istream& stream, LTS& lts)
131{
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. ");
134
135 atermpp::aterm_stream_state state(stream);
137
138 // The stream keeps every term that it reads in use, so garbage collection while reading would find almost nothing
139 // to free, but would still mark all terms read so far every time.
141
142 atermpp::aterm marker;
143 stream >> marker;
144
146 {
147 throw mcrl2::runtime_error("Stream does not contain a labelled transition system (LTS).");
148 }
149
150 // Read the header of the lts.
152 data::variable_list parameters;
153 process::action_label_list action_labels;
154
155 stream >> spec;
156 stream >> parameters;
157 stream >> action_labels;
158
159 lts.set_data(spec);
160 lts.set_process_parameters(parameters);
161 lts.set_action_label_declarations(action_labels);
162
163 // An indexed set to keep indices for multi actions.
164 mcrl2::utilities::indexed_set<action_label_lts> multi_actions;
165 multi_actions.insert(action_label_lts::tau_action()); // This action list represents 'tau'.
166
167 // The initial state is stored and set as last.
168 std::optional<probabilistic_lts_lts_t::probabilistic_state_t> initial_state;
169
170 // Ensure unique indices for the probabilistic states.
171 mcrl2::utilities::indexed_set<
172 probabilistic_lts_lts_t::probabilistic_state_t> probabilistic_states;
173
174 // Keep track of the number of states (derived from the transitions).
175 std::size_t number_of_states = 1;
176
177 aterm term;
178 action_label_lts action;
179
180 while (true)
181 {
182 stream.get(term);
183 if (!term.defined())
184 {
185 // The default constructed term indicates the end of the stream.
186 break;
187 }
188
189 if (term == transition_mark())
190 {
191 // The states are read as plain integers, which avoids creating an aterm_int for each of them.
192 const std::size_t from = stream.get_integer();
193 stream >> action;
194 const std::size_t to = stream.get_integer();
195
196 const auto [index, inserted] = multi_actions.insert(action);
197
198 std::size_t target_index = to;
199 if constexpr (std::is_same_v<LTS, probabilistic_lts_lts_t>)
200 {
201 probabilistic_lts_lts_t::probabilistic_state_t lts_state(to);
202
203 // For probabilistic lts it is necessary to add the state first (and use the returned index).
204 bool state_inserted;
205 std::tie(target_index, state_inserted) = probabilistic_states.insert(lts_state);
206
207 if (state_inserted)
208 {
209 [[maybe_unused]]
210 std::size_t actual_index = lts.add_probabilistic_state(lts_state);
211 assert(actual_index == target_index);
212 }
213 }
214
215 // Add the transition and update the number of states.
216 lts.add_transition(transition(from, index, target_index));
217 number_of_states = std::max({number_of_states, from + 1, to + 1});
218
219 if (inserted)
220 {
221 [[maybe_unused]]
222 std::size_t actual_index = lts.add_action(action);
223 assert(actual_index == index);
224 }
225
226 }
227 else if(term == probabilistic_transition_mark())
228 {
229 if constexpr (std::is_same_v<LTS, probabilistic_lts_lts_t>)
230 {
231 probabilistic_lts_lts_t::probabilistic_state_t to;
232
233 const std::size_t from = stream.get_integer();
234 stream >> action;
235 stream >> to;
236
237 const auto [index, inserted] = multi_actions.insert(action);
238
239 // Compute the index of the probabilistic state.
240 const auto [to_index, state_inserted] = probabilistic_states.insert(to);
241
242 if (state_inserted)
243 {
244 [[maybe_unused]]
245 std::size_t actual_index = lts.add_probabilistic_state(to);
246 assert(actual_index == to_index);
247 }
248
249 lts.add_transition(transition(from, index, to_index));
250
251 // Update the number of states
252 number_of_states = std::max({number_of_states, from + 1, to.maximal_state() + 1});
253
254 if (inserted)
255 {
256 [[maybe_unused]]
257 std::size_t actual_index = lts.add_action(action);
258 assert(actual_index == index);
259 }
260 }
261 else
262 {
263 throw mcrl2::runtime_error("Attempting to read a probabilistic LTS as a regular LTS.");
264 }
265 }
266 else if (term.type_is_list())
267 {
268 // Lists always represent state labels, only need to add the indices.
269 lts.add_state(reinterpret_cast<const state_label_lts&>(term));
270 }
271 else if (term == initial_state_mark())
272 {
273 // Read the initial state.
274 probabilistic_lts_lts_t::probabilistic_state_t state;
275 stream >> state;
276 initial_state = state;
277 }
278 else
279 {
280 throw mcrl2::runtime_error("Unknown mark in labelled transition system (LTS) stream.");
281 }
282 }
283
284 // The initial state can only be set after the states are known.
285 if (initial_state)
286 {
287 // If the lts has no state labels, we need to add empty states labels.
288 lts.set_num_states(number_of_states, lts.has_state_info());
289
290 set_initial_state(lts, initial_state.value());
291 }
292 else
293 {
294 throw mcrl2::runtime_error("Missing initial state in labelled transition system (LTS) stream.");
295 }
296}
297
298template <class LTS_TRANSITION_SYSTEM>
299static void read_from_lts(LTS_TRANSITION_SYSTEM& lts, const std::string& filename)
300{
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. ");
304
305 std::ifstream fstream;
306 if (!filename.empty())
307 {
308 fstream.open(filename, std::ifstream::in | std::ifstream::binary);
309 if (fstream.fail())
310 {
311 if (filename.empty())
312 {
313 throw mcrl2::runtime_error("Fail to open standard input to read an lts.");
314 }
315 else
316 {
317 throw mcrl2::runtime_error("Fail to open file " + filename + " to read an lts.");
318 }
319 }
320 }
321
322 try
323 {
324 atermpp::binary_aterm_istream stream(filename.empty() ? std::cin : fstream);
325 stream >> lts;
326 }
327 catch (const std::exception& ex)
328 {
329 mCRL2log(log::log_level_t::error) << ex.what() << "\n";
330 if (filename.empty())
331 {
332 throw mcrl2::runtime_error("Fail to correctly read an lts from standard input.");
333 }
334 else
335 {
336 throw mcrl2::runtime_error("Fail to correctly read an lts from the file " + filename + ".");
337 }
338 }
339}
340
342{
344 stream << lts.initial_probabilistic_state();
345}
346
348{
350 stream << probabilistic_lts_lts_t::probabilistic_state_t(lts.initial_state());
351}
352
353template <class LTS>
354static void write_lts(atermpp::aterm_ostream& stream, const LTS& lts)
355{
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. ");
358
359 // Write the process related information.
360 write_lts_header(stream,
361 lts.data(),
362 lts.process_parameters(),
363 lts.action_label_declarations());
364
365 for (const transition& trans : lts.get_transitions())
366 {
367 lts_lts_t::action_label_t label = lts.action_label(lts.apply_hidden_label_map(trans.label()));
368
369 if constexpr (std::is_same_v<LTS, probabilistic_lts_lts_t>)
370 {
371 write_transition(stream, trans.from(), label, lts.probabilistic_state(trans.to()));
372 }
373 else
374 {
375 write_transition(stream, trans.from(), label, trans.to());
376 }
377 }
378
379 if (lts.has_state_info())
380 {
381 for (std::size_t i = 0; i < lts.num_state_labels(); ++i)
382 {
383 // Write state labels as such, we assume that all list terms without headers are state labels.
384 stream << lts.state_label(i);
385 }
386 }
387
388 // Write the initial state.
389 write_initial_state(stream, lts);
390}
391
392template <class LTS_TRANSITION_SYSTEM>
393static void write_to_lts(const LTS_TRANSITION_SYSTEM& lts, const std::string& filename)
394{
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. ");
398
399 bool to_stdout = filename.empty() || filename == "-";
400 std::ofstream fstream;
401 if (!to_stdout)
402 {
403 fstream.open(filename, std::ofstream::out | std::ofstream::binary);
404 if (fstream.fail())
405 {
406 throw mcrl2::runtime_error("Fail to open file " + filename + " for writing.");
407 }
408 }
409
410 try
411 {
412 atermpp::binary_aterm_ostream stream(to_stdout ? std::cout : fstream);
413 stream << lts;
414 }
415 catch (const std::exception& ex)
416 {
417 mCRL2log(log::log_level_t::error) << ex.what() << "\n";
418 throw mcrl2::runtime_error("Fail to write lts correctly to the file " + filename + ".");
419 }
420}
421
422} // namespace detail
423
424// Implementation of public functions.
425
427{
428 read_lts(stream, lts);
429 return stream;
430}
431
433{
434 read_lts(stream, lts);
435 return stream;
436}
437
438atermpp::aterm_ostream& operator<<(atermpp::aterm_ostream& stream, const lts_lts_t& lts)
439{
440 detail::write_lts(stream, lts);
441 return stream;
442}
443
445{
446 detail::write_lts(stream, lts);
447 return stream;
448}
449
451 const data::data_specification& data_spec,
452 const data::variable_list& parameters,
453 const process::action_label_list& action_labels)
454{
455 // We set the transformer for all other elements (transitions, state labels and the initial state).
457
458 // Write the header of the lts.
460 stream << data_spec;
461 stream << parameters;
462 stream << action_labels;
463}
464
465void write_transition(atermpp::aterm_ostream& stream, std::size_t from, const lps::multi_action& label, std::size_t to)
466{
468 stream.put_integer(from);
469 stream << label;
470 stream.put_integer(to);
471}
472
473void write_transition(atermpp::aterm_ostream& stream, std::size_t from, const lps::multi_action& label, const probabilistic_lts_lts_t::probabilistic_state_t& to)
474{
475 if (to.size() <= 1)
476 {
477 // Actually a non probabilistic transition.
478 write_transition(stream, from, label, to.get());
479 }
480 else
481 {
483 stream.put_integer(from);
484 stream << label;
485 stream << to;
486 }
487}
488
490{
491 // During reading we assume that state labels are the only aterm_list.
492 stream << label;
493}
494
495void write_initial_state(atermpp::aterm_ostream& stream, std::size_t index)
496{
498 stream << probabilistic_lts_lts_t::probabilistic_state_t(index);
499}
500
501void probabilistic_lts_lts_t::save(const std::string& filename) const
502{
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);
505}
506
507void lts_lts_t::save(std::string const& filename) const
508{
509 mCRL2log(log::log_level_t::verbose) << "Starting to save an lts to the file " << filename << ".\n";
510 detail::write_to_lts(*this, filename);
511}
512
513void probabilistic_lts_lts_t::load(const std::string& filename)
514{
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);
517}
518
519void lts_lts_t::load(const std::string& filename)
520{
521 mCRL2log(log::log_level_t::verbose) << "Starting to load an lts from the file " << filename << ".\n";
522 detail::read_from_lts(*this, filename);
523}
524
525} // namespace mcrl2::lts
The interface for a class that reads aterm from a stream. The default constructed term aterm() indica...
Definition aterm_io.h:67
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...
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
Postpones automatic garbage collection while an object of this class exists, for instance while readi...
Definition aterm.h:173
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.
Definition lts_lts.h:142
function object to compare two constln_t pointers based on their contents
This class contains labelled transition systems in .lts format.
Definition lts_lts.h:369
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.
Definition lts_lts.h:397
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.
Definition lts_lts.h:38
#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
static void set_initial_state(probabilistic_lts_lts_t &lts, 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 &lts, 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 &lts, const std::string &filename)
void write_initial_state(atermpp::aterm_ostream &stream, const lts_lts_t &lts)
static void write_lts(atermpp::aterm_ostream &stream, const LTS &lts)
static void read_from_lts(LTS_TRANSITION_SYSTEM &lts, const std::string &filename)
static const atermpp::aterm & transition_mark()
void write_initial_state(atermpp::aterm_ostream &stream, const probabilistic_lts_lts_t &lts)
static void read_lts(atermpp::aterm_istream &stream, LTS &lts)
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 &parameters, 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 &lts)
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 &lts)
Read a (probabilistic) LTS from the given stream.
The main namespace for the Process library.