ltl2tgba.cc 5.09 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 "tgbaalgos/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
49
50
51
52
53
in GraphViz's format.\n\
If multiple formulas are supplied, several automata will be output.";

#define OPT_TGBA 1

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

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

80
static spot::option_map extra_options;
81
82
83
84
85
86
87
88
89
90

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;
91
92
93
    case 'M':
      type = spot::postprocessor::Monitor;
      break;
94
95
96
97
98
99
100
    case 'x':
      {
	const char* opt = extra_options.parse_options(arg);
	if (opt)
	  error(2, 0, "failed to parse --options near '%s'", opt);
      }
      break;
101
    case OPT_TGBA:
102
      if (automaton_format == Spin)
103
104
105
106
107
	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?
108
      jobs.emplace_back(arg, false);
109
110
111
112
113
114
115
116
117
118
119
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}


namespace
{
120
  class trans_processor: public job_processor
121
122
  {
  public:
123
    spot::translator& trans;
124
    automaton_printer printer;
125

126
    trans_processor(spot::translator& trans)
127
      : trans(trans), printer(ltl_input)
128
129
130
131
    {
    }

    int
132
    process_formula(const spot::ltl::formula* f,
133
134
		    const char* filename = 0, int linenum = 0)
    {
135
136
137
138
139
140
141
142
143
144
145
146
      // 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());
	}

147
148
149
150
151
152
      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);
153
      f->destroy();
154
155
156
157
158
159
160
161
      return 0;
    }
  };
}

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

165
  setup(argv);
166
167

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

170
171
  simplification_level = 3;

172
  if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, 0, 0))
173
174
175
176
177
178
    exit(err);

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

179
  spot::translator trans(&extra_options);
180
  trans.set_pref(pref | comp);
181
182
  trans.set_type(type);
  trans.set_level(level);
183

184
185
  try
    {
186
      trans_processor processor(trans);
187
188
189
190
191
192
193
      if (processor.run())
	return 2;
    }
  catch (const std::runtime_error& e)
    {
      error(2, 0, "%s", e.what());
    }
194
  return 0;
195
}