ltl2tgba.cc 5.78 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2012, 2013, 2014, 2015 Laboratoire de Recherche et
3
// Développement de l'Epita (LRDE).
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
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/>.
19
20
21
22
23
24
25
26
27

#include "common_sys.hh"

#include <string>
#include <iostream>

#include <argp.h>
#include "error.h"

28
#include "common_setup.hh"
29
#include "common_r.hh"
30
#include "common_cout.hh"
31
#include "common_finput.hh"
32
#include "common_output.hh"
33
#include "common_aoutput.hh"
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
34
#include "common_post.hh"
35

36
#include "ltlast/formula.hh"
37
#include "ltlvisit/tostring.hh"
38
#include "twaalgos/translate.hh"
39
#include "misc/optionmap.hh"
40
#include "misc/timer.hh"
41

42
static const char argp_program_doc[] ="\
43
44
45
Translate linear-time formulas (LTL/PSL) into Büchi automata.\n\n\
By default it will apply all available optimizations to output \
the smallest Transition-based Generalized Büchi Automata, \
46
47
48
in GraphViz's format.\n\
If multiple formulas are supplied, several automata will be output.";

49
50
51
52

enum {
  OPT_TGBA = 1,
};
53
54
55
56

static const argp_option options[] =
  {
    /**************************************************/
57
    { 0, 0, 0, 0, "Output automaton type:", 2 },
58
59
60
    { "tgba", OPT_TGBA, 0, 0,
      "Transition-based Generalized Büchi Automaton (default)", 0 },
    { "ba", 'B', 0, 0, "Büchi Automaton", 0 },
61
62
    { "monitor", 'M', 0, 0, "Monitor (accepts all finite prefixes "
      "of the given formula)", 0 },
63
    /**************************************************/
64
    { "%f", 0, 0, OPTION_DOC | OPTION_NO_USAGE,
65
      "the formula, in Spot's syntax", 4 },
66
67
    /**************************************************/
    { "unambiguous", 'U', 0, 0, "produce unambiguous automata", 20 },
68
    { 0, 0, 0, 0, "Miscellaneous options:", -1 },
69
    { "extra-options", 'x', "OPTS", 0,
70
      "fine-tuning options (see spot-x (7))", 0 },
71
72
73
    { 0, 0, 0, 0, 0, 0 }
  };

74
const struct argp_child children[] =
75
  {
76
    { &finput_argp, 0, 0, 1 },
77
78
    { &aoutput_argp, 0, 0, 0 },
    { &aoutput_o_format_argp, 0, 0, 0 },
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
79
    { &post_argp, 0, 0, 20 },
80
    { &misc_argp, 0, 0, -1 },
81
82
    { 0, 0, 0, 0 }
  };
83

84
static spot::option_map extra_options;
85
bool unambiguous = false;
86
87
88
89
90
91
92
93
94
95

static int
parse_opt(int key, char* arg, struct argp_state*)
{
  // This switch is alphabetically-ordered.
  switch (key)
    {
    case 'B':
      type = spot::postprocessor::BA;
      break;
96
97
98
    case 'M':
      type = spot::postprocessor::Monitor;
      break;
99
100
101
    case 'U':
      unambiguous = true;
      break;
102
103
104
105
106
107
108
    case 'x':
      {
	const char* opt = extra_options.parse_options(arg);
	if (opt)
	  error(2, 0, "failed to parse --options near '%s'", opt);
      }
      break;
109
    case OPT_TGBA:
110
      if (automaton_format == Spin)
111
112
113
114
115
	error(2, 0, "--spin and --tgba are incompatible");
      type = spot::postprocessor::TGBA;
      break;
    case ARGP_KEY_ARG:
      // FIXME: use stat() to distinguish filename from string?
116
      jobs.emplace_back(arg, false);
117
118
119
120
121
122
123
124
125
126
127
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}


namespace
{
128
  class trans_processor: public job_processor
129
130
  {
  public:
131
    spot::translator& trans;
132
    automaton_printer printer;
133

134
    trans_processor(spot::translator& trans)
135
      : trans(trans), printer(ltl_input)
136
137
138
139
    {
    }

    int
140
    process_formula(const spot::ltl::formula* f,
141
142
		    const char* filename = 0, int linenum = 0)
    {
143
144
145
146
147
148
149
150
151
152
153
154
      // This should not happen, because the parser we use can only
      // read PSL/LTL formula, but since our ltl::formula* type can
      // represent more than PSL formula, let's make this
      // future-proof.
      if (!f->is_psl_formula())
	{
	  std::string s = spot::ltl::to_string(f);
	  error_at_line(2, 0, filename, linenum,
			"formula '%s' is not an LTL or PSL formula",
			s.c_str());
	}

155
156
157
158
159
160
      spot::stopwatch sw;
      sw.start();
      auto aut = trans.run(&f);
      const double translation_time = sw.stop();

      printer.print(aut, f, filename, linenum, translation_time, nullptr);
161
      f->destroy();
162
163
164
165
166
167
168
169
      return 0;
    }
  };
}

int
main(int argc, char** argv)
{
170
171
172
  // By default we name automata using the formula.
  opt_name = "%f";

173
  setup(argv);
174
175

  const argp ap = { options, parse_opt, "[FORMULA...]",
176
		    argp_program_doc, children, 0, 0 };
177

178
179
  simplification_level = 3;

180
  if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, 0, 0))
181
182
    exit(err);

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  // Using both --unambiguous --deterministic do not really make
  // sense.
  if (unambiguous)
    {
      if (type == spot::postprocessor::Monitor)
	{
	  // We do not now how to make unambiguous monitors, other
	  // than deterministic monitors.
	  unambiguous = false;
	  pref = spot::postprocessor::Deterministic;
	}
      else if (pref == spot::postprocessor::Deterministic)
	{
	  error(2, 0,
		"--unambiguous and --deterministic are incompatible options");
	}
    }

201
202
203
204
  if (jobs.empty())
    error(2, 0, "No formula to translate?  Run '%s --help' for usage.",
	  program_name);

205
  spot::translator trans(&extra_options);
206
  trans.set_pref(pref | comp);
207
208
  trans.set_type(type);
  trans.set_level(level);
209
210
  if (unambiguous)
    trans.set_pref(spot::translator::Unambiguous);
211

212
213
  try
    {
214
      trans_processor processor(trans);
215
216
217
218
219
220
221
      if (processor.run())
	return 2;
    }
  catch (const std::runtime_error& e)
    {
      error(2, 0, "%s", e.what());
    }
222
  return 0;
223
}