File: ltsmin_test.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 (315 lines) | stat: -rwxr-xr-x 9,471 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
// Author(s): Wieger Wesselink
// 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 ltsmin_test.cpp
/// \brief Test for the pins class that connects mCRL2 with LTSMin (http://fmt.cs.utwente.nl/tools/ltsmin/).

#include <iostream>
#include <cstdio>

#include <boost/test/minimal.hpp>

#include "mcrl2/lps/linearise.h"
#include "mcrl2/lps/ltsmin.h"
#include "mcrl2/lps/detail/test_input.h"

using namespace mcrl2;

typedef int* state_vector;

inline
std::string print_state(const state_vector& s, std::size_t size, const std::string& msg = "state: ")
{
  int* first = s;
  int* last = s + size;

  std::ostringstream out;
  out << msg << "[";
  for (int* i = first; i != last; ++i)
  {
    if (i != first)
    {
      out << ", ";
    }
    out << *i;
  }
  out << "]";
  return out.str();
}

struct state_callback_function
{
  std::size_t state_size;
  std::size_t state_count;

  state_callback_function(std::size_t size)
    : state_size(size),
      state_count(0)
  {}

  void operator()(state_vector const& next_state, int* const& labels, int group=-1)
  {
    state_count++;
    std::cout << print_state(next_state, state_size, "visit state: ") << " edge label = " << labels[0] << " group = " << group << std::endl;
  }
};

static void test_data_type(lps::pins_data_type& type)
{
  std::cout << "test_data_type(" << type.name() << ")" << std::endl;

  // check serialize/deserialize
  for (std::size_t i = 0; i < type.size(); ++i)
  {
    std::string s = type.serialize(i);
    std::cout << "serialize(" << i << ") = " << type.serialize(i) << std::endl;
    std::size_t j = type.deserialize(s);
    BOOST_CHECK(i == j);
  }

  // check print/parse
  for (std::size_t i = 0; i < type.size(); ++i)
  {
    std::string s = type.print(i);
    std::cout << "print(" <<  i << ") = " << s << std::endl;
    std::size_t j = type.parse(s);
    std::cout << "parse(" <<  s << ") = " << j << std::endl;
  }

  // test iterator
  for (lps::pins_data_type::index_iterator i = type.index_begin(); i != type.index_end(); ++i)
  {
    std::cout << "iterator " << (i - type.index_begin()) << " -> " << *i << std::endl;
    BOOST_CHECK(*i == static_cast<size_t>(i - type.index_begin()));
  }
}

static void test_ltsmin(const std::string& rewriter_strategy)
{
  // create an input file
  lps::specification spec = lps::linearise(lps::detail::ABP_SPECIFICATION());
  std::string abp_filename = "temporary_abp.lps";
  save_lps(spec, abp_filename);

  lps::pins p(abp_filename, rewriter_strategy);
  std::size_t N = p.process_parameter_count();

  BOOST_CHECK(p.process_parameter_count() == 11);

  BOOST_CHECK(p.edge_label_count() == 1);
  for (std::size_t i = 0; i < p.edge_label_count(); i++)
  {
    BOOST_CHECK(p.edge_label_type(i) == p.datatype_count() - 1);
  }

  std::set<data::sort_expression> params;
  for (data::variable_list::const_iterator i = spec.process().process_parameters().begin(); i != spec.process().process_parameters().end(); ++i)
  {
    params.insert(i->sort());
  }
  BOOST_CHECK(p.datatype_count() == params.size() + 1);
  BOOST_CHECK(p.group_count() == 10);

  std::size_t index = 0;
  for (data::variable_list::const_iterator i = spec.process().process_parameters().begin(); i != spec.process().process_parameters().end(); ++i)
  {
  	const lps::pins_data_type& type = p.data_type(p.process_parameter_type(index++));
  	BOOST_CHECK(type.name() == data::pp(i->sort()));
  }

  std::cout << p.info() << std::endl;

  // get the initial state
  state_vector initial_state = new int[N];
  state_vector dest_state = new int[N];
  int* labels = new int[p.edge_label_count()];
  p.get_initial_state(initial_state);
  std::cout << print_state(initial_state, N, "initial state: ") << std::endl;

  // find all successors of the initial state
  state_callback_function f_all(p.process_parameter_count());
  p.next_state_all(initial_state, f_all, dest_state, labels);
  std::cout << "f_all.state_count = " << f_all.state_count << std::endl;

  // find successors of the initial state for individual groups
  std::size_t count = 0;
  for (std::size_t group = 0; group < p.group_count(); group++)
  {
    state_callback_function f_long(p.process_parameter_count());
    p.next_state_long(initial_state, group, f_long, dest_state, labels);
    std::cout << "group " << group << " count = " << f_long.state_count << std::endl;
    count += f_long.state_count;
  }
  BOOST_CHECK(count == f_all.state_count);

  lps::pins_data_type& action_label_type = p.data_type(p.datatype_count() - 1);
  test_data_type(action_label_type);

  // cleanup temporary files
  std::remove(abp_filename.c_str());

  delete[] initial_state;
  delete[] dest_state;
  delete[] labels;

  std::cout << p.info() << std::endl;

  // test summand_action_names()
  std::cout << "--- summand action names ---" << std::endl;
  for (std::size_t i = 0; i < p.group_count(); i++)
  {
    std::cout << i << " {";
    std::set<std::string> s = p.summand_action_names(i);
    for (std::set<std::string>::iterator j = s.begin(); j != s.end(); ++j)
    {
      if (j != s.begin())
      {
        std::cout << ", ";
      }
      std::cout << *j;
    }
    std::cout << "}" << std::endl;
  }

#ifdef MCRL2_FORCE_LTSMIN_TEST_FAILURE
  BOOST_CHECK(false);
#endif
}

template <typename Iter>
std::string print_vector(Iter first, Iter last)
{
  std::ostringstream out;
  out << "[";
  for (Iter i = first; i != last; ++i)
  {
    if (i != first)
    {
      out << ", ";
    }
    out << *i;
  }
  out << "]";
  return out.str();
}

template <typename Container>
std::string print_vector(const Container& c)
{
  return print_vector(c.begin(), c.end());
}

inline
std::string read_groups(const lps::pins& p)
{
  std::ostringstream out;
  for (std::size_t i = 0; i < p.group_count(); i++)
  {
    out << print_vector(p.read_group(i)) << std::endl;
  }
  return out.str();
}

inline
std::string write_groups(const lps::pins& p)
{
  std::ostringstream out;
  for (std::size_t i = 0; i < p.group_count(); i++)
  {
    out << print_vector(p.write_group(i)) << std::endl;
  }
  return out.str();
}

inline
void test_dependency_matrix()
{
  std::string ONEBIT =
    "proc P(b: Bool) =          \n"
    "       tau . P(b = false); \n"
    "                           \n"
    "init P(true);              \n"
    ;

  lps::specification spec = lps::parse_linear_process_specification(ONEBIT);
  std::string filename = "temporary_onebit.lps";
  save_lps(spec, filename);
  lps::pins p(filename, "jitty");

  for (std::size_t i = 0; i < p.group_count(); i++)
  {
    std::cout << "\n";
    std::cout << " read_group(" << i << ") = " << print_vector(p.read_group(i)) << std::endl;
    std::cout << "write_group(" << i << ") = " << print_vector(p.write_group(i)) << std::endl;
  }
  BOOST_CHECK(print_vector(p.read_group(0)) == "[]");
  BOOST_CHECK(print_vector(p.write_group(0)) == "[0]");

  // cleanup temporary files
  std::remove(filename.c_str());
}

inline
void test_serialisation()
{
  std::string EXAMPLE  =
    "sort Piece = struct Red | Yellow | None;   \n"
    "                                           \n"
    "proc P(i: Pos, j: Nat, p: Piece) =         \n"
    "       tau . P(i = 1, j = 0, p = Red);     \n"
    "                                           \n"
    "init P(1, 1, None);                        \n"
    ;

  lps::specification spec = lps::parse_linear_process_specification(EXAMPLE);
  std::string filename = "temporary_onebit.lps";
  save_lps(spec, filename);
  lps::pins p1(filename, "jitty");
  lps::pins p2(filename, "jitty");
  std::cout << "datatypes: " << p1.datatype_count() << std::endl;
  for(size_t i=0; i < p1.datatype_count(); i++)
  {
    std::cout << i << ": " << p1.data_type(i).name() << std::endl;
  }
  std::vector<std::pair<std::string, size_t>> expressions;
  expressions.push_back(std::make_pair("1", 0));
  expressions.push_back(std::make_pair("0", 1));
  expressions.push_back(std::make_pair("1", 1));
  expressions.push_back(std::make_pair("Red", 2));
  expressions.push_back(std::make_pair("Yellow", 2));

  for(auto ei = expressions.begin(); ei != expressions.end(); ++ei)
  {
    auto e = *ei;
    std::string s = e.first;
    size_t type = e.second;

    data::data_expression d1 = data::parse_data_expression(s, spec.data());
    std::string serialised = p1.data_type(type).serialize(p1.data_type(type)[d1]);
    data::data_expression d2(p2.data_type(type).get(p2.data_type(type).deserialize(serialised)));
    std::cout << "string:     " << s << std::endl;
    std::cout << "expression: " << d1 << std::endl;
    std::cout << "serialised: " << serialised << std::endl;
    std::cout << "result:     " << d2 << std::endl;
    BOOST_CHECK(d1==d2);
  }
}

int test_main(int, char**)
{
  log::mcrl2_logger::set_reporting_level(log::debug);

  test_dependency_matrix();
  test_serialisation();
  test_ltsmin("jitty");
#ifdef MCRL2_JITTYC_AVAILABLE
  test_ltsmin("jittyc");
#endif // MCRL2_JITTYC_AVAILABLE

  return 0;
}