complementation.cc 8.9 KB
Newer Older
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1 2
// Copyright (C) 2008, 2009, 2010, 2011 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
Guillaume Sadegh's avatar
Guillaume Sadegh committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//
// 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
// the Free Software Foundation; either version 2 of the License, or
// (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
// along with Spot; see the file COPYING.  If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.

21 22 23
#include <iomanip>
#include <iostream>
#include "tgbaalgos/dotty.hh"
24
#include "tgbaalgos/save.hh"
25 26 27 28 29 30 31 32 33 34 35 36
#include "tgbaparse/public.hh"
#include "tgba/tgbaproduct.hh"
#include "tgbaalgos/gtec/gtec.hh"
#include "tgbaalgos/ltl2tgba_fm.hh"
#include "ltlparse/public.hh"
#include "tgbaalgos/stats.hh"
#include "tgbaalgos/emptiness.hh"
#include "ltlast/unop.hh"
#include "tgbaalgos/stats.hh"
#include "tgbaalgos/emptiness_stats.hh"
#include "tgba/tgbatba.hh"

37
#include "tgba/tgbasafracomplement.hh"
38
#include "tgba/tgbakvcomplement.hh"
39 40 41 42 43

void usage(const char* prog)
{
  std::cout << "usage: " << prog << " [options]" << std::endl;
  std::cout << "with options" << std::endl
44
            << "-b                      Output in spot's format" << std::endl
45 46
            << "-S                      Use Safra's complementation "
	    << "instead of Kupferman&Vardi's" << std::endl
47 48 49 50 51 52
            << "-s     buchi_automaton  display the safra automaton"
            << std::endl
            << "-a     buchi_automaton  display the complemented automaton"
            << std::endl
            << "-astat buchi_automaton  statistics for !a" << std::endl
            << "-fstat formula          statistics for !A_f" << std::endl
53 54
            << "-f     formula          test !A_f and !A_!f" << std::endl
            << "-p     formula          print the automaton for f" << std::endl;
55 56 57 58 59 60 61
}

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
62
  //bool check = false;
63 64 65
  int return_value = 0;
  bool stats = false;
  bool formula = false;
66 67
  bool safra = false;
  bool print_formula = false;
68
  bool save_spot = false;
69 70 71 72 73 74 75 76 77 78 79

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

  for (int i = 1; i < argc; ++i)
  {
    if (argv[i][0] == '-')
    {
80 81 82 83 84 85
      if (strcmp(argv[i] + 1, "b") == 0)
      {
	save_spot = true;
	continue;
      }

86 87 88
      if (strcmp(argv[i] + 1, "astat") == 0)
      {
        stats = true;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
89
        formula = false;
90 91 92 93 94 95 96 97 98 99 100 101
        continue;
      }

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

      switch (argv[i][1])
      {
102 103
        case 'S':
          safra = true; break;
104
        case 's':
105
          safra = true; print_safra = true; break;
106 107 108
        case 'a':
          print_automaton = true; break;
        case 'f':
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
109 110
          //check = true;
	  break;
111 112
        case 'p':
          print_formula = true; break;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        default:
          std::cerr << "unrecognized option `-" << argv[i][1]
                    << "'" << std::endl;
          return 2;
      }
    }
    else
      file = argv[i];
  }

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

  spot::bdd_dict* dict = new spot::bdd_dict();
  if (print_automaton || print_safra)
  {
    spot::ltl::environment& env(spot::ltl::default_environment::instance());
    spot::tgba_parse_error_list pel;
Pierre PARUTTO's avatar
Pierre PARUTTO committed
134
    spot::tgba_explicit_string* a = spot::tgba_parse(file, pel, dict, env);
135 136 137
    if (spot::format_tgba_parse_errors(std::cerr, file, pel))
      return 2;

138 139 140 141 142
    spot::tgba* complement = 0;

    if (safra)
      complement = new spot::tgba_safra_complement(a);
    else
143
      complement = new spot::tgba_kv_complement(a);
144 145

    if (print_automaton)
146 147 148 149 150 151
      {
	if (save_spot)
	  spot::tgba_save_reachable(std::cout, complement);
	else
	  spot::dotty_reachable(std::cout, complement);
      }
152 153

    if (print_safra)
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
    {
      spot::tgba_safra_complement* safra_complement =
        dynamic_cast<spot::tgba_safra_complement*>(complement);
      spot::display_safra(safra_complement);
    }
    delete complement;
    delete a;
  }
  else if (print_formula)
  {
    spot::tgba* a;
    spot::ltl::formula* f1 = 0;

    spot::ltl::parse_error_list p1;
    f1 = spot::ltl::parse(file, p1);

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

    a = spot::ltl_to_tgba_fm(f1, dict);

    spot::tgba* complement = 0;
    if (safra)
      complement = new spot::tgba_safra_complement(a);
    else
179
      complement = new spot::tgba_kv_complement(a);
180 181

    spot::dotty_reachable(std::cout, complement);
182
    f1->destroy();
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
    delete complement;
    delete a;
  }
  else if (stats)
  {
    spot::tgba* a;
    spot::ltl::formula* f1 = 0;

    if (formula)
    {
      spot::ltl::parse_error_list p1;
      f1 = spot::ltl::parse(file, p1);

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

      a = spot::ltl_to_tgba_fm(f1, dict);
    }
    else
    {
      spot::tgba_parse_error_list pel;
      spot::ltl::environment& env(spot::ltl::default_environment::instance());
      a = spot::tgba_parse(file, pel, dict, env);
      if (spot::format_tgba_parse_errors(std::cerr, file, pel))
        return 2;
    }

210
    spot::tgba_safra_complement* safra_complement =
211
      new spot::tgba_safra_complement(a);
212 213 214 215

    spot::tgba_statistics a_size =  spot::stats_reachable(a);
    std::cout << "Original: "
              << a_size.states << ", "
216 217 218
              << a_size.transitions << ", "
              << a->number_of_acceptance_conditions()
              << std::endl;
219 220 221 222 223

    spot::tgba *buchi = new spot::tgba_sba_proxy(a);
    a_size =  spot::stats_reachable(buchi);
    std::cout << "Buchi: "
              << a_size.states << ", "
224 225 226
              << a_size.transitions << ", "
              << buchi->number_of_acceptance_conditions()
              << std::endl;
227 228
    delete buchi;

229 230 231 232 233 234 235
    spot::tgba_statistics b_size =  spot::stats_reachable(safra_complement);
    std::cout << "Safra Complement: "
              << b_size.states << ", "
              << b_size.transitions << ", "
              << safra_complement->number_of_acceptance_conditions()
              << std::endl;

236 237
    spot::tgba_kv_complement* complement =
      new spot::tgba_kv_complement(a);
238 239 240

    b_size =  spot::stats_reachable(complement);
    std::cout << "GBA Complement: "
241
              << b_size.states << ", "
242 243 244
              << b_size.transitions << ", "
              << complement->number_of_acceptance_conditions()
              << std::endl;
245 246 247 248 249 250 251

    delete complement;
    delete a;
    if (formula)
    {
      spot::ltl::formula* nf1 =
        spot::ltl::unop::instance(spot::ltl::unop::Not,
252
                                  f1->clone());
253 254 255 256
      spot::tgba* a2 = spot::ltl_to_tgba_fm(nf1, dict);
      spot::tgba_statistics a_size =  spot::stats_reachable(a2);
      std::cout << "Not Formula: "
                << a_size.states << ", "
257 258 259
                << a_size.transitions << ", "
                << a2->number_of_acceptance_conditions()
                << std::endl;
260 261

      delete a2;
262 263
      f1->destroy();
      nf1->destroy();
264 265 266 267 268 269 270 271 272 273 274 275
    }
  }
  else
  {
    spot::ltl::parse_error_list p1;
    spot::ltl::formula* f1 = spot::ltl::parse(file, p1);

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

    spot::tgba* Af = spot::ltl_to_tgba_fm(f1, dict);
    spot::ltl::formula* nf1 = spot::ltl::unop::instance(spot::ltl::unop::Not,
276
                                                        f1->clone());
277
    spot::tgba* Anf = spot::ltl_to_tgba_fm(nf1, dict);
278 279 280 281 282 283 284 285 286 287

    spot::tgba* nAf;
    spot::tgba* nAnf;
    if (safra)
    {
      nAf = new spot::tgba_safra_complement(Af);
      nAnf = new spot::tgba_safra_complement(Anf);
    }
    else
    {
288 289
      nAf = new spot::tgba_kv_complement(Af);
      nAnf = new spot::tgba_kv_complement(Anf);
290
    }
291 292 293 294
    spot::tgba* prod = new spot::tgba_product(nAf, nAnf);
    spot::emptiness_check* ec = spot::couvreur99(prod);
    spot::emptiness_check_result* res = ec->check();
    spot::tgba_statistics a_size =  spot::stats_reachable(ec->automaton());
295 296 297 298 299 300 301
    std::cout << "States: "
              << a_size.states << std::endl
              << "Transitions: "
              << a_size.transitions << std::endl
              << "Acc Cond: "
              << ec->automaton()->number_of_acceptance_conditions()
              << std::endl;
302 303
    if (res)
    {
304
      std::cout << "FAIL";
305 306 307
      return_value = 1;
    }
    else
308
      std::cout << "OK";
309 310 311 312 313 314 315 316 317 318
    std::cout << std::endl;

    delete res;
    delete ec;
    delete prod;
    delete nAf;
    delete Af;
    delete nAnf;
    delete Anf;

319 320
    nf1->destroy();
    f1->destroy();
321 322 323 324 325 326 327

  }

  delete dict;

  return return_value;
}