File: liblts_lts.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 (393 lines) | stat: -rwxr-xr-x 11,014 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// 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_lts.cpp

#include <string>
#include <cstring>
#include <sstream>
#include "svc/svc.h"
#include "svc/svcerrno.h"
#include "mcrl2/core/nil.h"
#include "mcrl2/data/detail/io.h"
#include "mcrl2/lts/lts_lts.h"
#include "mcrl2/lps/multi_action.h"
#include "mcrl2/atermpp/aterm_int.h"

namespace mcrl2
{
namespace lts
{

/**
 * @brief An aterm_appl that contains a data specification, process parameter
 *        list and action label list. This is the information that is appended
 *        at the end of an LTS file.
 */
class trailer_data : public atermpp::aterm_appl
{
private:
  static const atermpp::function_symbol m_function_symbol;
  trailer_data(const atermpp::aterm& data_spec, 
               const atermpp::aterm& process_parameters,
               const atermpp::aterm& action_labels)
    : atermpp::aterm_appl(m_function_symbol, data_spec, process_parameters, action_labels)
  { }

  const atermpp::aterm_appl& get_arg(size_t i) const
  {
    return atermpp::down_cast<atermpp::aterm_appl>((*this)[i]);
  }

public:

  trailer_data(const atermpp::aterm& term)
    : atermpp::aterm_appl(term)
  { }

  trailer_data()
    : atermpp::aterm_appl(m_function_symbol, core::nil(), core::nil(), core::nil())
  { }

  static trailer_data create(const lts_lts_t& ts)
  {
    atermpp::aterm d = data::detail::remove_index(data::detail::data_specification_to_aterm_data_spec(ts.data()));
    atermpp::aterm p = ts.has_process_parameters() ? data::detail::remove_index(ts.process_parameters()) : core::nil();
    atermpp::aterm a = ts.has_action_labels() ? data::detail::remove_index(ts.action_labels()) : core::nil();
    return trailer_data(d, p, a);
  }

  bool is_valid() const
  {
    return !core::is_nil(get_arg(0));
  }

  data::data_specification data() const
  {
    data::data_specification data_spec(atermpp::down_cast<atermpp::aterm_appl>(data::detail::add_index((*this)[0])));
    data_spec.declare_data_specification_to_be_type_checked();  // Assume that the data specification in an lts is well typed.
    return data_spec;
  }

  bool has_process_parameters() const
  { 
    return !core::is_nil(get_arg(1)); 
  }

  data::variable_list process_parameters() const
  {
    assert(has_process_parameters());
    return data::variable_list(data::detail::add_index(get_arg(1)));
  }  

  bool has_action_labels() const
  { 
    return !core::is_nil(get_arg(2)); 
  }

  process::action_label_list action_labels() const
  {
    assert(has_action_labels());
    return process::action_label_list(data::detail::add_index(get_arg(2)));
  }  
};

const atermpp::function_symbol trailer_data::m_function_symbol = atermpp::function_symbol("mCRL2LTS1", 3);

#define LTS_TRAILER_TAG_LENGTH 12

/**
 * @brief A file stream that can append and retrieve trailer_data objects
 *        to and from a file.
 */
class trailer : public std::fstream
{
private:
  static const char* LTS_TRAILER_TAG;
  union
  {
    char buf[LTS_TRAILER_TAG_LENGTH + 8];
    struct
    {
      unsigned char info_pos[8];
      char trailer_tag[LTS_TRAILER_TAG_LENGTH];
    };
  } m_data;

  std::string m_filename;

  long stream_length()
  {
    /* Determine the position at which the additional information starts.
     * Due to the way in which file operations are implemented on Windows, 
     * we need to use the get pointer for determining the length of the 
     * stream. (seekp gives invalid results, leading to a wrong encoded 
     * position in the output of the LTS file).
	   * According to the example at 
     *   http://www.cplusplus.com/reference/istream/istream/seekg/
	   * this is the more-or-less standard way to determine the lenght of 
     * the file. 
     */
    long oldpos = tellg();
    seekg(0, std::ios::end);
    long position = tellg();
    seekg(oldpos, std::ios::beg);
    return position;
  }

  void write_tag(long extra_info_pos)
  {
    for (int i = 0; i < 8; ++i)
    {
      m_data.info_pos[i] = extra_info_pos % 0x100;
      extra_info_pos >>= 8;
    }
    std::fstream::write(&m_data.buf[0], sizeof(m_data));
  }

  bool read_tag(long& extra_info_pos)
  {
    seekg(-(std::streamoff)sizeof(m_data), std::ios_base::end);
    if (good())
    {
      std::fstream::read(&m_data.buf[0], sizeof(m_data));
      if (good())
      {
        if (!strncmp(LTS_TRAILER_TAG, m_data.trailer_tag, LTS_TRAILER_TAG_LENGTH))
        {
          extra_info_pos = 0;
          for (int i = 7; i >= 0; --i)
          {
            extra_info_pos <<= 8;
            extra_info_pos |= m_data.info_pos[i];
          }
        }
        return true;
      }
    }
    return false;
  }

public:
  trailer(const std::string& filename, std::ios_base::openmode mode)
    : std::fstream(filename, mode | std::ios_base::binary), m_filename(filename)
  {
    if (mode & std::ios_base::out)
    {
      strncpy(m_data.trailer_tag, LTS_TRAILER_TAG, LTS_TRAILER_TAG_LENGTH);
    }
  }

  trailer_data read()
  {
    long position = 0;
    if (read_tag(position))
    {
      if (position == 0)
      {
        return trailer_data();
      }
      seekg(position, std::ios::beg);
      if (good())
      {
        return trailer_data(data::detail::add_index(atermpp::read_term_from_binary_stream(*this)));  
      }
    }
    throw mcrl2::runtime_error("Could not read trailer tag in '" + m_filename + "'.");
  }

  void write(const trailer_data& data)
  {
    long position = stream_length();
    if (position == -1)
    {
      throw mcrl2::runtime_error("Could not determine file size of '" + m_filename + "'; not adding extra information.");
    }
    seekp(0, std::ios::end);
    atermpp::write_term_to_binary_stream(data, *this);
    write_tag(position);
  }
  
};

const char* trailer::LTS_TRAILER_TAG = "   1STL2LRCm";
#undef LTS_TRAILER_TAG_LENGTH

static void read_from_lts(lts_lts_t& l, const std::string& filename)
{
  SVCfile f;
  SVCbool b;

  // Open file
  if (SVCopen(&f, const_cast<char*>(filename.c_str()), SVCread, &b))
  {
    throw mcrl2::runtime_error("cannot open lts file '" + filename + "' for reading (" + SVCerror(SVCerrno) + ").");
  }

  // Determine file type
  bool svc_file_has_state_info = false;
  std::string svc_type = SVCgetType(&f);
  if (svc_type == "mCRL2+info")
  {
    svc_file_has_state_info = true;
  }
  else if (svc_type != "mCRL2")
  {
    throw mcrl2::runtime_error("lts file '" + filename + "' is not in the mCRL2 format.");
  }

  // Read transitions
  SVCstateIndex from, to;
  SVClabelIndex label;
  SVCparameterIndex param;
  while (SVCgetNextTransition(&f, &from, &label, &to, &param))
  {
    l.add_transition(transition(from, label, to));
  }

  // Read state labels
  SVCint num_states = SVCnumStates(&f);
  detail::state_label_lts state_label;
  for (SVCstateIndex i = 0; i < num_states; ++i)
  {
    if (svc_file_has_state_info)
    {
      state_label = detail::state_label_lts(atermpp::down_cast<atermpp::aterm_appl>(data::detail::add_index(SVCstate2ATerm(&f, i))));
    }
    l.add_state(state_label);
  }

  // Set initial state
  assert(SVCgetInitialState(&f) == 0);
  l.set_initial_state((size_t)SVCgetInitialState(&f));

  // Read action labels
  SVCint num_labels = SVCnumLabels(&f);
  detail::action_label_lts action_label;
  const process::action_list tau = process::action_list();
  for (SVCstateIndex i = 0; i < num_labels; ++i)
  {
    action_label = detail::action_label_lts(atermpp::down_cast<atermpp::aterm_appl>(data::detail::add_index(SVClabel2ATerm(&f, i))));
    l.add_action(action_label, action_label.actions() == tau);
  }

  SVCclose(&f);

  // Check to see if there is extra data at the end
  try
  {
    trailer t(filename, std::ios::in);
    trailer_data data = t.read();
    if (data.is_valid())
    {
      l.set_data(data.data());
      if (data.has_process_parameters())
      {
        l.set_process_parameters(data.process_parameters());
      }
      if (data.has_action_labels())
      {
        l.set_action_labels(data.action_labels());
      }
    }
    t.close();
  }
  catch (std::runtime_error& e)
  {
    throw mcrl2::runtime_error("Error while reading datatypes, action declarations and "
                               "process parameters from '" + filename + "' (" + e.what() + ")");
  }
}

static void write_to_lts(const lts_lts_t& l, const std::string& filename)
{
  SVCfile f;
  SVCbool b = l.has_state_info() ? SVCfalse : SVCtrue;
  if (SVCopen(&f, const_cast<char*>(filename.c_str()), SVCwrite, &b))
  {
    throw mcrl2::runtime_error("cannot open .lts file '" + filename + "' for writing (" + SVCerror(SVCerrno) + ").");
  }

  if (l.has_state_info())
  {
    SVCsetType(&f,const_cast < char* >("mCRL2+info"));
  }
  else
  {
    SVCsetType(&f,const_cast < char* >("mCRL2"));
  }

  SVCsetCreator(&f,const_cast < char* >("liblts (mCRL2)"));

  assert(l.initial_state()< ((size_t)1 << (sizeof(int)*8-1)));
  if (l.has_state_info())
  {
    SVCsetInitialState(&f, SVCnewState(&f, data::detail::remove_index(l.state_label(l.initial_state())), &b));
  }
  else 
  {
    SVCsetInitialState(&f, SVCnewState(&f, atermpp::aterm_int(l.initial_state()), &b));
  }

  SVCparameterIndex param = SVCnewParameter(&f, atermpp::aterm_list(), &b);

  const std::vector<transition> &trans = l.get_transitions();
  SVCstateIndex from, label, to;
  for (std::vector<transition>::const_iterator t = trans.begin(); t != trans.end(); ++t)
  {
    assert(t->from() < ((size_t)1 << (sizeof(int)*8-1)));
    assert(t->to() < ((size_t)1 << (sizeof(int)*8-1)));
    if (l.has_state_info())
    {
      from = SVCnewState(&f, data::detail::remove_index(l.state_label(t->from())), &b); 
      to = SVCnewState(&f, data::detail::remove_index(l.state_label(t->to())), &b);
    }
    else
    {
      from = SVCnewState(&f, atermpp::aterm_int(t->from()), &b); 
      to = SVCnewState(&f, atermpp::aterm_int(t->to()), &b);
    }
    label = SVCnewLabel(&f, data::detail::remove_index(l.action_label(t->label()).aterm_without_time()), &b);
    SVCputTransition(&f, from, label, to, param);
  }

  SVCclose(&f);

  trailer t(filename, std::ios::out | std::ios::app);
  t.write(trailer_data::create(l));
  t.close();
}

void lts_lts_t::save(const std::string& filename) const
{
  if (filename=="")
  {
    throw mcrl2::runtime_error("Cannot write svc/lts file " + filename + " to stdout");
  }
  else
  {
    mCRL2log(log::verbose) << "Starting to save file " << filename << "\n";
    write_to_lts(*this,filename);
  }
}

void lts_lts_t::load(const std::string& filename)
{
  if (filename=="")
  {
    throw mcrl2::runtime_error("Cannot read svc/lts file " + filename + " from stdin");
  }
  else
  {
    mCRL2log(log::verbose) << "Starting to load file " << filename << "\n";
    read_from_lts(*this,filename);
  }
}

}
}