complementation.cc 6.94 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 "parseaut/public.hh"
25
#include "twa/twaproduct.hh"
26 27
#include "twaalgos/gtec/gtec.hh"
#include "twaalgos/ltl2tgba_fm.hh"
28
#include "tl/parse.hh"
29 30 31 32 33
#include "twaalgos/stats.hh"
#include "twaalgos/emptiness.hh"
#include "twaalgos/stats.hh"
#include "twaalgos/emptiness_stats.hh"
#include "twaalgos/degen.hh"
34

35
#include "twa/twasafracomplement.hh"
36

37
static void usage(const char* prog)
38 39 40
{
  std::cout << "usage: " << prog << " [options]" << std::endl;
  std::cout << "with options" << std::endl
41
            << "-H                      Output in HOA\n"
42 43 44 45 46 47
            << "-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";
48 49 50 51
}

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

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

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

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

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

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

113
  if (!file)
114 115 116 117 118
  {
    usage(argv[0]);
    return 1;
  }

119
  auto dict = spot::make_bdd_dict();
120 121
  if (print_automaton || print_safra)
  {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
122
    spot::environment& env(spot::default_environment::instance());
123 124 125
    spot::parse_aut_error_list pel;
    auto h = spot::parse_aut(file, pel, dict, env);
    if (spot::format_parse_aut_errors(std::cerr, file, pel))
126
      return 2;
127
    spot::twa_graph_ptr a = h->aut;
128

129
    spot::twa_ptr complement = nullptr;
130

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

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

    if (print_safra)
142
    {
143 144
      auto safra_complement =
	std::dynamic_pointer_cast<spot::tgba_safra_complement>(complement);
145 146 147 148 149
      spot::display_safra(safra_complement);
    }
  }
  else if (print_formula)
  {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
150 151
    spot::parse_error_list p1;
    auto f1 = spot::parse_infix_psl(file, p1);
152

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
153
    if (spot::format_parse_errors(std::cerr, file, p1))
154 155
      return 2;

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

160
    spot::print_dot(std::cout, complement);
161 162 163
  }
  else if (stats)
  {
164
    spot::twa_graph_ptr a;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
165
    spot::formula f1 = nullptr;
166 167 168

    if (formula)
    {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
169 170
      spot::parse_error_list p1;
      f1 = spot::parse_infix_psl(file, p1);
171

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
172
      if (spot::format_parse_errors(std::cerr, file, p1))
173 174 175 176 177 178
        return 2;

      a = spot::ltl_to_tgba_fm(f1, dict);
    }
    else
    {
179
      spot::parse_aut_error_list pel;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
180
      spot::environment& env(spot::default_environment::instance());
181 182
      auto h = spot::parse_aut(file, pel, dict, env);
      if (spot::format_parse_aut_errors(std::cerr, file, pel))
183
        return 2;
184
      a = h->aut;
185 186
    }

187
    auto safra_complement = spot::make_safra_complement(a);
188

189
    spot::twa_statistics a_size =  spot::stats_reachable(a);
190 191
    std::cout << "Original: "
              << a_size.states << ", "
192
              << a_size.edges << ", "
193
              << a->acc().num_sets()
194
              << std::endl;
195

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

203
    spot::twa_statistics b_size =  spot::stats_reachable(safra_complement);
204 205
    std::cout << "Safra Complement: "
              << b_size.states << ", "
206
              << b_size.edges << ", "
207
              << safra_complement->acc().num_sets()
208
              << std::endl;
209 210 211

    if (formula)
    {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
212
      auto a2 = spot::ltl_to_tgba_fm(spot::formula::Not(f1), dict);
213
      spot::twa_statistics a_size = spot::stats_reachable(a2);
214 215
      std::cout << "Not Formula: "
                << a_size.states << ", "
216
                << a_size.edges << ", "
217
                << a2->acc().num_sets()
218
                << std::endl;
219 220 221 222
    }
  }
  else
  {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
223 224
    spot::parse_error_list p1;
    auto f1 = spot::parse_infix_psl(file, p1);
225

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
226
    if (spot::format_parse_errors(std::cerr, file, p1))
227 228
      return 2;

229
    auto Af = spot::ltl_to_tgba_fm(f1, dict);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
230
    auto nf1 = spot::formula::Not(f1);
231 232 233
    auto Anf = spot::ltl_to_tgba_fm(nf1, dict);
    auto nAf = spot::make_safra_complement(Af);
    auto nAnf = spot::make_safra_complement(Anf);
234
    auto ec = spot::couvreur99(spot::otf_product(nAf, nAnf));
235
    auto res = ec->check();
236
    spot::twa_statistics a_size =  spot::stats_reachable(ec->automaton());
237 238 239
    std::cout << "States: "
              << a_size.states << std::endl
              << "Transitions: "
240
              << a_size.edges << std::endl
241
              << "Acc Cond: "
242
              << ec->automaton()->acc().num_sets()
243
              << std::endl;
244
    if (res)
245 246 247 248 249
      {
	std::cout << "FAIL\n";
	return_value = 1;
	if (auto run = res->accepting_run())
	  {
250
	    spot::print_dot(std::cout, ec->automaton());
251
	    spot::print_twa_run(std::cout, ec->automaton(), run);
252 253
	  }
      }
254
    else
255 256 257
      {
	std::cout << "OK\n";
      }
258 259 260 261
  }

  return return_value;
}