complementation.cc 7.26 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2008, 2009, 2010, 2011, 2012, 2014, 2015 Laboratoire
// de Recherche et Développement de l'Epita (LRDE).
Guillaume Sadegh's avatar
Guillaume Sadegh committed
4
5
6
7
8
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 3 of the License, or
Guillaume Sadegh's avatar
Guillaume Sadegh committed
10
11
12
13
14
15
16
17
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
18
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
Guillaume Sadegh's avatar
Guillaume Sadegh committed
19

20
21
#include <iomanip>
#include <iostream>
22
#include "twaalgos/dot.hh"
23
#include "twaalgos/hoa.hh"
24
#include "hoaparse/public.hh"
25
#include "twa/twaproduct.hh"
26
27
#include "twaalgos/gtec/gtec.hh"
#include "twaalgos/ltl2tgba_fm.hh"
28
#include "ltlparse/public.hh"
29
30
#include "twaalgos/stats.hh"
#include "twaalgos/emptiness.hh"
31
#include "ltlast/unop.hh"
32
33
34
#include "twaalgos/stats.hh"
#include "twaalgos/emptiness_stats.hh"
#include "twaalgos/degen.hh"
35

36
#include "twa/twasafracomplement.hh"
37
38
39
40
41

void usage(const char* prog)
{
  std::cout << "usage: " << prog << " [options]" << std::endl;
  std::cout << "with options" << std::endl
42
            << "-H                      Output in HOA\n"
43
44
45
46
47
48
            << "-s     buchi_automaton  display the safra automaton\n"
            << "-a     buchi_automaton  display the complemented automaton\n"
            << "-astat buchi_automaton  statistics for !a\n"
            << "-fstat formula          statistics for !A_f\n"
            << "-f     formula          test !A_f and !A_!f\n"
            << "-p     formula          print the automaton for f\n";
49
50
51
52
53
54
55
}

int main(int argc, char* argv[])
{
  char *file = 0;
  bool print_safra = false;
  bool print_automaton = false;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
56
  //bool check = false;
57
58
59
  int return_value = 0;
  bool stats = false;
  bool formula = false;
60
  bool print_formula = false;
61
  bool save_hoa = false;
62
63
64
65
66
67
68
69
70
71
72

  if (argc < 3)
  {
    usage(argv[0]);
    return 1;
  }

  for (int i = 1; i < argc; ++i)
  {
    if (argv[i][0] == '-')
    {
73
      if (strcmp(argv[i] + 1, "H") == 0)
74
      {
75
	save_hoa = true;
76
77
78
	continue;
      }

79
80
81
      if (strcmp(argv[i] + 1, "astat") == 0)
      {
        stats = true;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
82
        formula = false;
83
84
85
86
87
88
89
90
91
92
93
94
        continue;
      }

      if (strcmp(argv[i] + 1, "fstat") == 0)
      {
        stats = true;
        formula = true;
        continue;
      }

      switch (argv[i][1])
      {
95
        case 's':
96
          print_safra = true; break;
97
98
99
        case 'a':
          print_automaton = true; break;
        case 'f':
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
100
101
          //check = true;
	  break;
102
103
        case 'p':
          print_formula = true; break;
104
105
        default:
          std::cerr << "unrecognized option `-" << argv[i][1]
106
                    << '\'' << std::endl;
107
108
109
110
111
112
113
114
115
116
117
118
119
          return 2;
      }
    }
    else
      file = argv[i];
  }

  if (file == 0)
  {
    usage(argv[0]);
    return 1;
  }

120
  auto dict = spot::make_bdd_dict();
121
122
123
  if (print_automaton || print_safra)
  {
    spot::ltl::environment& env(spot::ltl::default_environment::instance());
124
125
126
    spot::hoa_parse_error_list pel;
    auto h = spot::hoa_parse(file, pel, dict, env);
    if (spot::format_hoa_parse_errors(std::cerr, file, pel))
127
      return 2;
128
    spot::twa_graph_ptr a = h->aut;
129

130
    spot::twa_ptr complement = 0;
131

132
    complement = spot::make_safra_complement(a);
133
134

    if (print_automaton)
135
      {
136
	if (save_hoa)
137
	  spot::print_hoa(std::cout, complement, nullptr);
138
	else
139
	  spot::print_dot(std::cout, complement);
140
      }
141
142

    if (print_safra)
143
    {
144
145
      auto safra_complement =
	std::dynamic_pointer_cast<spot::tgba_safra_complement>(complement);
146
147
148
149
150
151
      spot::display_safra(safra_complement);
    }
  }
  else if (print_formula)
  {
    spot::ltl::parse_error_list p1;
152
    auto* f1 = spot::ltl::parse_infix_psl(file, p1);
153
154
155
156

    if (spot::ltl::format_parse_errors(std::cerr, file, p1))
      return 2;

157
    auto a = spot::ltl_to_tgba_fm(f1, dict);
158
    spot::twa_ptr complement = 0;
159
    complement = spot::make_safra_complement(a);
160

161
    spot::print_dot(std::cout, complement);
162
    f1->destroy();
163
164
165
  }
  else if (stats)
  {
166
    spot::twa_graph_ptr a;
167
    const spot::ltl::formula* f1 = 0;
168
169
170
171

    if (formula)
    {
      spot::ltl::parse_error_list p1;
172
      f1 = spot::ltl::parse_infix_psl(file, p1);
173
174
175
176
177
178
179
180

      if (spot::ltl::format_parse_errors(std::cerr, file, p1))
        return 2;

      a = spot::ltl_to_tgba_fm(f1, dict);
    }
    else
    {
181
      spot::hoa_parse_error_list pel;
182
      spot::ltl::environment& env(spot::ltl::default_environment::instance());
183
184
      auto h = spot::hoa_parse(file, pel, dict, env);
      if (spot::format_hoa_parse_errors(std::cerr, file, pel))
185
        return 2;
186
      a = h->aut;
187
188
    }

189
    auto safra_complement = spot::make_safra_complement(a);
190
191
192
193

    spot::tgba_statistics a_size =  spot::stats_reachable(a);
    std::cout << "Original: "
              << a_size.states << ", "
194
              << a_size.transitions << ", "
195
              << a->acc().num_sets()
196
              << std::endl;
197

198
    auto buchi = spot::degeneralize(a);
199
    std::cout << "Buchi: "
200
201
              << buchi->num_states()
	      << buchi->num_transitions()
202
              << buchi->acc().num_sets()
203
              << std::endl;
204

205
206
207
208
    spot::tgba_statistics b_size =  spot::stats_reachable(safra_complement);
    std::cout << "Safra Complement: "
              << b_size.states << ", "
              << b_size.transitions << ", "
209
              << safra_complement->acc().num_sets()
210
              << std::endl;
211
212
213

    if (formula)
    {
214
215
      auto nf1 = spot::ltl::unop::instance(spot::ltl::unop::Not, f1->clone());
      auto a2 = spot::ltl_to_tgba_fm(nf1, dict);
216
217
218
      spot::tgba_statistics a_size =  spot::stats_reachable(a2);
      std::cout << "Not Formula: "
                << a_size.states << ", "
219
                << a_size.transitions << ", "
220
                << a2->acc().num_sets()
221
                << std::endl;
222

223
224
      f1->destroy();
      nf1->destroy();
225
226
227
228
229
    }
  }
  else
  {
    spot::ltl::parse_error_list p1;
230
    auto* f1 = spot::ltl::parse_infix_psl(file, p1);
231
232
233
234

    if (spot::ltl::format_parse_errors(std::cerr, file, p1))
      return 2;

235
236
237
238
239
    auto Af = spot::ltl_to_tgba_fm(f1, dict);
    auto nf1 = spot::ltl::unop::instance(spot::ltl::unop::Not, f1->clone());
    auto Anf = spot::ltl_to_tgba_fm(nf1, dict);
    auto nAf = spot::make_safra_complement(Af);
    auto nAnf = spot::make_safra_complement(Anf);
240
    auto ec = spot::couvreur99(spot::otf_product(nAf, nAnf));
241
    auto res = ec->check();
242
    spot::tgba_statistics a_size =  spot::stats_reachable(ec->automaton());
243
244
245
246
247
    std::cout << "States: "
              << a_size.states << std::endl
              << "Transitions: "
              << a_size.transitions << std::endl
              << "Acc Cond: "
248
              << ec->automaton()->acc().num_sets()
249
              << std::endl;
250
    if (res)
251
252
253
254
255
      {
	std::cout << "FAIL\n";
	return_value = 1;
	if (auto run = res->accepting_run())
	  {
256
	    spot::print_dot(std::cout, ec->automaton());
257
258
259
	    spot::print_tgba_run(std::cout, ec->automaton(), run);
	  }
      }
260
    else
261
262
263
      {
	std::cout << "OK\n";
      }
264
265
    nf1->destroy();
    f1->destroy();
266
267
268
269
  }

  return return_value;
}