ltl2tgba.cc 5.47 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
37
#include "tl/formula.hh"
#include "tl/print.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
58
    { nullptr, 0, nullptr, 0, "Output automaton type:", 2 },
    { "tgba", OPT_TGBA, nullptr, 0,
59
      "Transition-based Generalized Büchi Automaton (default)", 0 },
60
61
62
    { "ba", 'B', nullptr, 0,
      "Büchi Automaton (with state-based acceptance)", 0 },
    { "monitor", 'M', nullptr, 0, "Monitor (accepts all finite prefixes "
63
      "of the given formula)", 0 },
64
    /**************************************************/
65
    { "%f", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
66
      "the formula, in Spot's syntax", 4 },
67
    /**************************************************/
68
    { "unambiguous", 'U', nullptr, 0, "output unambiguous automata "
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
69
      "(combine with other intents)", 20 },
70
    { nullptr, 0, nullptr, 0, "Miscellaneous options:", -1 },
71
    { "extra-options", 'x', "OPTS", 0,
72
      "fine-tuning options (see spot-x (7))", 0 },
73
    { nullptr, 0, nullptr, 0, nullptr, 0 }
74
75
  };

76
const struct argp_child children[] =
77
  {
78
79
80
81
82
83
    { &finput_argp, 0, nullptr, 1 },
    { &aoutput_argp, 0, nullptr, 0 },
    { &aoutput_o_format_argp, 0, nullptr, 0 },
    { &post_argp, 0, nullptr, 0 },
    { &misc_argp, 0, nullptr, -1 },
    { nullptr, 0, nullptr, 0 }
84
  };
85

86
static spot::option_map extra_options;
87
static spot::postprocessor::output_pref unambig = 0;
88
89
90
91
92
93
94
95
96
97

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;
98
99
100
    case 'M':
      type = spot::postprocessor::Monitor;
      break;
101
    case 'U':
102
      unambig = spot::postprocessor::Unambiguous;
103
      break;
104
105
106
107
108
109
110
    case 'x':
      {
	const char* opt = extra_options.parse_options(arg);
	if (opt)
	  error(2, 0, "failed to parse --options near '%s'", opt);
      }
      break;
111
    case OPT_TGBA:
112
      if (automaton_format == Spin)
113
114
115
116
117
	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?
118
      jobs.emplace_back(arg, false);
119
120
121
122
123
124
125
126
127
128
129
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}


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

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

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

157
158
159
160
161
162
      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);
163
164
165
166
167
168
169
170
      return 0;
    }
  };
}

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

174
  setup(argv);
175
176

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

179
180
  simplification_level = 3;

181
  if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
182
183
184
185
186
187
    exit(err);

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

188
  spot::translator trans(&extra_options);
189
  trans.set_pref(pref | comp | sbacc | unambig);
190
191
  trans.set_type(type);
  trans.set_level(level);
192

193
194
  try
    {
195
      trans_processor processor(trans);
196
197
198
199
200
201
202
      if (processor.run())
	return 2;
    }
  catch (const std::runtime_error& e)
    {
      error(2, 0, "%s", e.what());
    }
203
  return 0;
204
}