File: liblts_fsm.cpp

package info (click to toggle)
mcrl2 201409.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd
  • size: 46,348 kB
  • ctags: 29,960
  • sloc: cpp: 213,160; ansic: 16,219; python: 13,238; yacc: 309; lex: 214; xml: 197; makefile: 83; sh: 82; pascal: 17
file content (189 lines) | stat: -rwxr-xr-x 4,236 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Author(s): Muck van Weerdenburg
// Copyright: see the accompanying file COPYING or copy at
// https://svn.win.tue.nl/trac/MCRL2/browser/trunk/COPYING
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
/// \file liblts_fsm.cpp

#include <string>
#include <fstream>
#include "mcrl2/core/print.h"
#include "mcrl2/lps/typecheck.h"
#include "mcrl2/data/typecheck.h"
#include "mcrl2/data/data_specification.h"
#include "mcrl2/lps/specification.h"
#include "mcrl2/lts/lts_io.h"
#include "mcrl2/lts/parse.h"
#include "liblts_fsmparser.h"

using namespace mcrl2::core;
using namespace mcrl2::lts;
using namespace mcrl2::lts::detail;
using namespace mcrl2::log;


static void write_to_fsm(std::ostream& os, const lts_fsm_t& l)
{
  // determine number of state parameters
  size_t num_params;
  num_params=l.process_parameters().size();

  // print parameters with used values
  mCRL2log(verbose) << "writing parameter table..." << std::endl;
  for (size_t i=0; i<num_params; i++)
  {

    const std::vector < std::string > vals = l.state_element_values(i);
    os << l.process_parameter(i).first << "(" << vals.size() << ") " << l.process_parameter(i).second << " ";

    for (std::vector < std::string >::const_iterator j=vals.begin(); j!=vals.end(); ++j)
    {
      os << " \"" << *j << "\"";
    }
    os << std::endl;
  }

  // print states
  mCRL2log(verbose) << "writing states..." << std::endl;
  os << "---" << std::endl;
  for (size_t i=0; i<l.num_states(); i++)
  {
    size_t idx = i;
    // make sure that the initial state is first
    if (i == 0)
    {
      idx = l.initial_state();
    }
    else if (i == l.initial_state())
    {
      idx = 0;
    }

    if (l.has_state_info())
    {
      const state_label_fsm state_pars=l.state_label(idx);

      for (size_t j=0; j<state_pars.size() ; j++)
      {
        if (j > 0)
        {
          os << " ";
        }
        os << state_pars[j];

      }
      os << std::endl;
    }
  }

  // print transitions
  mCRL2log(verbose) << "writing transitions..." << std::endl;
  os << "---" << std::endl;
  const std::vector<transition> &trans=l.get_transitions();
  for (std::vector<transition>::const_iterator t=trans.begin(); t!=trans.end(); ++t)
  {
    transition::size_type from = t->from();
    // correct state numbering
    if (from == 0)
    {
      from = l.initial_state();
    }
    else if (from == l.initial_state())
    {
      from = 0;
    }
    transition::size_type to = t->to();
    if (to == 0)
    {
      to = l.initial_state();
    }
    else if (to == l.initial_state())
    {
      to = 0;
    }
    // correct state numbering
    os << from+1 << " " << to+1 << " \"";
    os << mcrl2::lts::detail::pp(l.action_label(t->label()));
    os << "\"" << std::endl;
  }
}

void mcrl2::lts::lts_fsm_t::load(const std::string& filename)
{
  if (filename.empty())
  {
    if (!parse_fsm(std::cin,*this))
    {
      throw mcrl2::runtime_error("Error parsing .fsm file from standard input.");
    }
  }
  else
  {
    std::ifstream is(filename.c_str());

    if (!is.is_open())
    {
      throw mcrl2::runtime_error("Cannot open .fsm file " + filename + ".");
    }


    if (!parse_fsm(is,*this))
    {
      throw mcrl2::runtime_error("Error parsing .fsm file");
    }
    is.close();
  }
  if (num_states()==0)
  {
    add_state();
  }
  set_initial_state(0);
}

void mcrl2::lts::lts_fsm_t::loadnew(const std::string& filename)
{
  if (filename.empty())
  {
    parse_fsm_specification(std::cin, *this);
  }
  else
  {
    std::ifstream in(filename.c_str());
    if (!in)
    {
      throw mcrl2::runtime_error("Cannot open .fsm file " + filename + ".");
    }
    parse_fsm_specification(in, *this);
  }
  if (num_states() == 0)
  {
    add_state();
  }
  set_initial_state(0);
}

void mcrl2::lts::lts_fsm_t::save(const std::string& filename) const
{
  if (filename=="")
  {
    write_to_fsm(std::cout,*this);
  }
  else
  {
    std::ofstream os(filename.c_str());

    if (!os.is_open())
    {
      throw mcrl2::runtime_error("Cannot create .fsm file '" + filename + ".");
      return;
    }


    write_to_fsm(os,*this);
    os.close();
  }
}