mCRL2
Loading...
Searching...
No Matches
liblts_fsm.cpp
Go to the documentation of this file.
1// Author(s): Muck van Weerdenburg
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_fsm.cpp
10
11#include "mcrl2/lts/lts_io.h"
12#include "mcrl2/lts/parse.h"
13#include "mcrl2/lts/detail/liblts_swap_to_from_probabilistic_lts.h"
14
15
16
17namespace mcrl2::lts {
18
20{
24
25 fsm_writer(std::ostream& out_, const probabilistic_lts_fsm_t& fsm_)
26 : out(out_),
30 fsm(fsm_)
31 {
32 }
33
34 // This functions swaps 0 with the number number_of_initial_state of the initial state.
36 {
37 if (i==number_of_initial_state)
38 {
39 return 0;
40 }
41 if (i==0)
42 {
43 return number_of_initial_state;
44 }
45 return i;
46 }
47
49 {
50 // print parameters with used values
51 mCRL2log(log::verbose) << "writing parameter table..." << std::endl;
52 for (std::size_t i = 0; i < fsm.process_parameters().size(); i++)
53 {
54 const std::vector<std::string>& values = fsm.state_element_values(i);
55 out << fsm.process_parameter(i).first << "(" << values.size() << ") " << fsm.process_parameter(i).second << " ";
56 for (const std::string& s: values)
57 {
58 out << " \"" << s << "\"";
59 }
60 out << std::endl;
61 }
62 }
63
65 {
66 mCRL2log(log::verbose) << "writing states..." << std::endl;
67 for (std::size_t i = 0; i < fsm.num_states(); i++)
68 {
69 if (fsm.has_state_info())
70 {
71 const state_label_fsm& state_parameters = fsm.state_label(swap_initial_state(i));
72 for (std::size_t j = 0; j < state_parameters.size(); j++)
73 {
74 if (j > 0)
75 {
76 out << " ";
77 }
78 out << state_parameters[j];
79 }
80 out << std::endl;
81 }
82 }
83 }
84
85 // If there is only a single state with probility 1, write a single state, otherwise
86 // write "[state1 probability1, state2 probability2, ..., state_n probability_n]".
87 void write_probabilistic_state(const detail::lts_fsm_base::probabilistic_state& probabilistic_state)
88 {
89 if (probabilistic_state.size()<=1)
90 {
91 out << swap_initial_state(probabilistic_state.get())+1;
92 }
93 else
94 {
95 assert(probabilistic_state.size()>1);
96 out << "[";
97 bool first=true;
98 for(const lps::state_probability_pair< std::size_t, utilities::probabilistic_arbitrary_precision_fraction>& p: probabilistic_state)
99 {
100 if (first)
101 {
102 first=false;
103 }
104 else
105 {
106 out << ' ';
107 }
108 out << swap_initial_state(p.state()) + 1 << " " << p.probability();
109 }
110 out << "]";
111 }
112 }
113
115 {
116 mCRL2log(log::verbose) << "writing transitions..." << std::endl;
117 for (const transition& t: fsm.get_transitions())
118 {
119 // correct state numbering, by adding 1.
120 out << swap_initial_state(t.from()) + 1 << " ";
121 write_probabilistic_state(fsm.probabilistic_state(t.to()));
122 out << " \"" << mcrl2::lts::pp(fsm.action_label(fsm.apply_hidden_label_map(t.label()))) << "\"" << std::endl;
123 }
124 }
125
126 void write()
127 {
129 out << "---" << std::endl;
131 out << "---" << std::endl;
133 // If there is a initial distribution with more than one state, write the initial distribution.
134 if (fsm.initial_probabilistic_state().size()>1)
135 {
136 out << "---" << std::endl;
137 write_probabilistic_state(fsm.initial_probabilistic_state());
138 out << "\n" << std::endl;
139 }
140 }
141};
142
143void probabilistic_lts_fsm_t::load(const std::string& filename)
144{
145 if (filename.empty() || filename=="-")
146 {
147 try
148 {
149 parse_fsm_specification(std::cin, *this);
150 }
151 catch (mcrl2::runtime_error& e)
152 {
153 throw mcrl2::runtime_error(std::string("Error parsing .fsm file from standard input.\n") + e.what());
154 }
155 }
156 else
157 {
158 std::ifstream is(filename.c_str());
159
160 if (!is.is_open())
161 {
162 throw mcrl2::runtime_error("Cannot open .fsm file " + filename + ".");
163 }
164 try
165 {
166 parse_fsm_specification(is, *this);
167 }
168 catch (mcrl2::runtime_error& e)
169 {
170 throw mcrl2::runtime_error(std::string("Error parsing .fsm file.\n") + e.what());
171 }
172 is.close();
173 }
174}
175
176void probabilistic_lts_fsm_t::save(const std::string& filename) const
177{
178 if (filename.empty() || filename=="-")
179 {
180 fsm_writer(std::cout, *this).write();
181 }
182 else
183 {
184 std::ofstream os(filename.c_str());
185
186 if (!os.is_open())
187 {
188 throw mcrl2::runtime_error("Cannot create .fsm file '" + filename + ".");
189 return;
190 }
191
192 fsm_writer(os, *this).write();
193 os.close();
194 }
195}
196
197void lts_fsm_t::load(const std::string& filename)
198{
200 l.load(filename);
201 detail::swap_to_non_probabilistic_lts
202 <state_label_fsm,
203 action_label_string,
204 detail::lts_fsm_base::probabilistic_state,
205 detail::lts_fsm_base>(l,*this);
206}
207
208void lts_fsm_t::save(const std::string& filename) const
209{
211 detail::translate_to_probabilistic_lts
212 <state_label_fsm,
213 action_label_string,
214 detail::lts_fsm_base::probabilistic_state,
215 detail::lts_fsm_base>(*this,l);
216 l.save(filename);
217}
218
219
220} // namespace mcrl2::lts
The class lts_fsm_t contains labelled transition systems in .fsm format.
Definition lts_fsm.h:254
void load(const std::string &filename)
Save the labelled transition system to file.
void save(const std::string &filename) const
Save the labelled transition system to file.
The class lts_fsm_t contains labelled transition systems in .fsm format.
Definition lts_fsm.h:282
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:393
std::size_t swap_initial_state(const std::size_t i)
std::size_t number_of_initial_state
void write_probabilistic_state(const detail::lts_fsm_base::probabilistic_state &probabilistic_state)
fsm_writer(std::ostream &out_, const probabilistic_lts_fsm_t &fsm_)
const probabilistic_lts_fsm_t & fsm