genltl.cc 9.95 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2012, 2013, 2015-2017 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
#include "common_sys.hh"
22
23
24
25
26
27
28
29

#include <iostream>
#include <fstream>
#include <argp.h>
#include <cstdlib>
#include "error.h"
#include <vector>

30
#include "common_setup.hh"
31
#include "common_output.hh"
32
#include "common_range.hh"
33
#include "common_cout.hh"
34
35
36
37
38
39
40
41

#include <cassert>
#include <iostream>
#include <sstream>
#include <set>
#include <string>
#include <cstdlib>
#include <cstring>
42
43
#include <spot/tl/formula.hh>
#include <spot/tl/relabel.hh>
44
#include <spot/gen/formulas.hh>
45
46
47
48

using namespace spot;

const char argp_program_doc[] ="\
49
Generate temporal logic formulas from predefined patterns.";
50

51
52
// We reuse the values from gen::ltl_pattern_id as option keys.
// Additional options should therefore start after gen::LTL_END.
53
enum {
54
  OPT_POSITIVE = gen::LTL_END,
55
  OPT_NEGATIVE,
56
};
57

58

59
#define OPT_ALIAS(o) { #o, 0, nullptr, OPTION_ALIAS, nullptr, 0 }
60
61
62
63
64

static const argp_option options[] =
  {
    /**************************************************/
    // Keep this alphabetically sorted (expect for aliases).
65
    { nullptr, 0, nullptr, 0, "Pattern selection:", 1},
66
67
    // J. Geldenhuys and H. Hansen (Spin'06): Larger automata and less
    // work for LTL model checking.
68
    { "and-f", gen::LTL_AND_F, "RANGE", 0, "F(p1)&F(p2)&...&F(pn)", 0 },
69
    OPT_ALIAS(gh-e),
70
71
    { "and-fg", gen::LTL_AND_FG, "RANGE", 0, "FG(p1)&FG(p2)&...&FG(pn)", 0 },
    { "and-gf", gen::LTL_AND_GF, "RANGE", 0, "GF(p1)&GF(p2)&...&GF(pn)", 0 },
72
73
    OPT_ALIAS(ccj-phi),
    OPT_ALIAS(gh-c2),
74
    { "ccj-alpha", gen::LTL_CCJ_ALPHA, "RANGE", 0,
75
      "F(p1&F(p2&F(p3&...F(pn)))) & F(q1&F(q2&F(q3&...F(qn))))", 0 },
76
    { "ccj-beta", gen::LTL_CCJ_BETA, "RANGE", 0,
77
      "F(p&X(p&X(p&...X(p)))) & F(q&X(q&X(q&...X(q))))", 0 },
78
    { "ccj-beta-prime", gen::LTL_CCJ_BETA_PRIME, "RANGE", 0,
79
      "F(p&(Xp)&(XXp)&...(X...X(p))) & F(q&(Xq)&(XXq)&...(X...X(q)))", 0 },
80
    { "dac-patterns", gen::LTL_DAC_PATTERNS, "RANGE", OPTION_ARG_OPTIONAL,
81
      "Dwyer et al. [FMSP'98] Spec. Patterns for LTL "
82
      "(range should be included in 1..55)", 0 },
83
    OPT_ALIAS(spec-patterns),
84
    { "eh-patterns", gen::LTL_EH_PATTERNS, "RANGE", OPTION_ARG_OPTIONAL,
85
86
      "Etessami and Holzmann [Concur'00] patterns "
      "(range should be included in 1..12)", 0 },
87
88
    { "fxg-or", gen::LTL_FXG_OR, "RANGE", 0,
      "F(p0 | XG(p1 | XG(p2 | ... XG(pn))))", 0},
89
    { "gh-q", gen::LTL_GH_Q, "RANGE", 0,
90
      "(F(p1)|G(p2))&(F(p2)|G(p3))&...&(F(pn)|G(p{n+1}))", 0 },
91
    { "gh-r", gen::LTL_GH_R, "RANGE", 0,
92
      "(GF(p1)|FG(p2))&(GF(p2)|FG(p3))&... &(GF(pn)|FG(p{n+1}))", 0 },
93
    { "go-theta", gen::LTL_GO_THETA, "RANGE", 0,
94
      "!((GF(p1)&GF(p2)&...&GF(pn)) -> G(q->F(r)))", 0 },
95
96
    { "gxf-and", gen::LTL_GXF_AND, "RANGE", 0,
      "G(p0 & XF(p1 & XF(p2 & ... XF(pn))))", 0},
97
98
    { "hkrss-patterns", gen::LTL_HKRSS_PATTERNS,
      "RANGE", OPTION_ARG_OPTIONAL,
99
100
101
      "Holeček et al. patterns from the Liberouter project "
      "(range should be included in 1..55)", 0 },
    OPT_ALIAS(liberouter-patterns),
102
    { "kr-n", gen::LTL_KR_N, "RANGE", 0,
103
      "linear formula with doubly exponential DBA", 0 },
104
    { "kr-nlogn", gen::LTL_KR_NLOGN, "RANGE", 0,
105
      "quasilinear formula with doubly exponential DBA", 0 },
106
    { "kv-psi", gen::LTL_KV_PSI, "RANGE", 0,
107
      "quadratic formula with doubly exponential DBA", 0 },
108
    OPT_ALIAS(kr-n2),
109
110
111
112
113
114
115
116
    { "ms-example", gen::LTL_MS_EXAMPLE, "RANGE", 0,
      "GF(a1&X(a2&X(a3&...)))&F(b1&F(b2&F(b3&...)))", 0 },
    { "ms-phi-h", gen::LTL_MS_PHI_H, "RANGE", 0,
      "FG(a|b)|FG(!a|Xb)|FG(a|XXb)|FG(!a|XXXb)|...", 0 },
    { "ms-phi-r", gen::LTL_MS_PHI_R, "RANGE", 0,
      "(FGa{n}&GFb{n})|((FGa{n-1}|GFb{n-1})&(...))", 0 },
    { "ms-phi-s", gen::LTL_MS_PHI_S, "RANGE", 0,
      "(FGa{n}|GFb{n})&((FGa{n-1}&GFb{n-1})|(...))", 0 },
117
    { "or-fg", gen::LTL_OR_FG, "RANGE", 0, "FG(p1)|FG(p2)|...|FG(pn)", 0 },
118
    OPT_ALIAS(ccj-xi),
119
    { "or-g", gen::LTL_OR_G, "RANGE", 0, "G(p1)|G(p2)|...|G(pn)", 0 },
120
    OPT_ALIAS(gh-s),
121
    { "or-gf", gen::LTL_OR_GF, "RANGE", 0, "GF(p1)|GF(p2)|...|GF(pn)", 0 },
122
    OPT_ALIAS(gh-c1),
123
    { "p-patterns", gen::LTL_P_PATTERNS, "RANGE", OPTION_ARG_OPTIONAL,
124
125
126
127
      "Pelánek [Spin'07] patterns from BEEM "
      "(range should be included in 1..20)", 0 },
    OPT_ALIAS(beem-patterns),
    OPT_ALIAS(p),
128
129
130
131
    { "r-left", gen::LTL_R_LEFT, "RANGE", 0, "(((p1 R p2) R p3) ... R pn)", 0 },
    { "r-right", gen::LTL_R_RIGHT, "RANGE", 0, "(p1 R (p2 R (... R pn)))", 0 },
    { "rv-counter", gen::LTL_RV_COUNTER, "RANGE", 0, "n-bit counter", 0 },
    { "rv-counter-carry", gen::LTL_RV_COUNTER_CARRY, "RANGE", 0,
132
      "n-bit counter w/ carry", 0 },
133
134
135
    { "rv-counter-carry-linear", gen::LTL_RV_COUNTER_CARRY_LINEAR,
      "RANGE", 0, "n-bit counter w/ carry (linear size)", 0 },
    { "rv-counter-linear", gen::LTL_RV_COUNTER_LINEAR, "RANGE", 0,
136
      "n-bit counter (linear size)", 0 },
137
    { "sb-patterns", gen::LTL_SB_PATTERNS, "RANGE", OPTION_ARG_OPTIONAL,
138
139
      "Somenzi and Bloem [CAV'00] patterns "
      "(range should be included in 1..27)", 0 },
140
    { "tv-f1", gen::LTL_TV_F1, "RANGE", 0,
141
      "G(p -> (q | Xq | ... | XX...Xq)", 0 },
142
    { "tv-f2", gen::LTL_TV_F2, "RANGE", 0,
143
      "G(p -> (q | X(q | X(... | Xq)))", 0 },
144
    { "tv-g1", gen::LTL_TV_G1, "RANGE", 0,
145
      "G(p -> (q & Xq & ... & XX...Xq)", 0 },
146
    { "tv-g2", gen::LTL_TV_G2, "RANGE", 0,
147
      "G(p -> (q & X(q & X(... & Xq)))", 0 },
148
    { "tv-uu", gen::LTL_TV_UU, "RANGE", 0,
149
      "G(p1 -> (p1 U (p2 & (p2 U (p3 & (p3 U ...))))))", 0 },
150
    { "u-left", gen::LTL_U_LEFT, "RANGE", 0, "(((p1 U p2) U p3) ... U pn)", 0 },
151
    OPT_ALIAS(gh-u),
152
    { "u-right", gen::LTL_U_RIGHT, "RANGE", 0, "(p1 U (p2 U (... U pn)))", 0 },
153
154
    OPT_ALIAS(gh-u2),
    OPT_ALIAS(go-phi),
155
    RANGE_DOC,
156
  /**************************************************/
157
    { nullptr, 0, nullptr, 0, "Output options:", -20 },
158
159
160
161
162
163
    { "negative", OPT_NEGATIVE, nullptr, 0,
      "output the negated versions of all formulas", 0 },
    OPT_ALIAS(negated),
    { "positive", OPT_POSITIVE, nullptr, 0,
      "output the positive versions of all formulas (done by default, unless"
      " --negative is specified without --positive)", 0 },
164
    { nullptr, 0, nullptr, 0, "The FORMAT string passed to --format may use "
165
      "the following interpreted sequences:", -19 },
166
    { "%f", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
167
      "the formula (in the selected syntax)", 0 },
168
    { "%F", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
169
      "the name of the pattern", 0 },
170
    { "%L", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
171
      "the argument of the pattern", 0 },
172
    { "%%", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
173
      "a single %", 0 },
174
    COMMON_LTL_OUTPUT_SPECS,
175
176
    { nullptr, 0, nullptr, 0, "Miscellaneous options:", -1 },
    { nullptr, 0, nullptr, 0, nullptr, 0 }
177
178
179
180
  };

struct job
{
181
  gen::ltl_pattern_id pattern;
182
  struct range range;
183
184
185
186
};

typedef std::vector<job> jobs_t;
static jobs_t jobs;
187
188
bool opt_positive = false;
bool opt_negative = false;
189
190
191

const struct argp_child children[] =
  {
192
193
194
    { &output_argp, 0, nullptr, -20 },
    { &misc_argp, 0, nullptr, -1 },
    { nullptr, 0, nullptr, 0 }
195
  };
196
197

static void
198
enqueue_job(int pattern, const char* range_str = nullptr)
199
200
{
  job j;
201
  j.pattern = static_cast<gen::ltl_pattern_id>(pattern);
202
203
204
205
206
207
208
  if (range_str)
    {
      j.range = parse_range(range_str);
    }
  else
    {
      j.range.min = 1;
209
      j.range.max = gen::ltl_pattern_max(j.pattern);
210
211
      if (j.range.max == 0)
        error(2, 0, "missing range for %s",
212
              gen::ltl_pattern_name(j.pattern));
213
    }
214
215
216
  jobs.push_back(j);
}

217
static int
218
parse_opt(int key, char* arg, struct argp_state*)
219
{
220
  if (key >= gen::LTL_BEGIN && key < gen::LTL_END)
221
222
223
224
    {
      enqueue_job(key, arg);
      return 0;
    }
225
226
227
  // This switch is alphabetically-ordered.
  switch (key)
    {
228
229
230
231
232
233
    case OPT_POSITIVE:
      opt_positive = true;
      break;
    case OPT_NEGATIVE:
      opt_negative = true;
      break;
234
    default:
235
      return ARGP_ERR_UNKNOWN;
236
    }
237
  return 0;
238
239
240
}


241
static void
242
output_pattern(gen::ltl_pattern_id pattern, int n)
243
{
244
  formula f = gen::ltl_pattern(pattern, n);
245

246
247
  // Make sure we use only "p42"-style of atomic propositions
  // in lbt's output.
248
249
  if (output_format == lbt_output && !f.has_lbt_atomic_props())
    f = relabel(f, Pnn);
250

251
252
  if (opt_positive || !opt_negative)
    {
253
      output_formula_checked(f, nullptr, gen::ltl_pattern_name(pattern), n);
254
255
256
257
    }
  if (opt_negative)
    {
      std::string tmp = "!";
258
      tmp += gen::ltl_pattern_name(pattern);
259
      output_formula_checked(formula::Not(f), nullptr, tmp.c_str(), n);
260
    }
261
262
263
264
265
}

static void
run_jobs()
{
266
  for (auto& j: jobs)
267
    {
268
269
      int inc = (j.range.max < j.range.min) ? -1 : 1;
      int n = j.range.min;
270
      for (;;)
271
272
273
274
275
276
        {
          output_pattern(j.pattern, n);
          if (n == j.range.max)
            break;
          n += inc;
        }
277
278
279
280
281
282
283
    }
}


int
main(int argc, char** argv)
{
284
  setup(argv);
285

286
  const argp ap = { options, parse_opt, nullptr, argp_program_doc,
287
                    children, nullptr, nullptr };
288

289
  if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
290
291
292
293
    exit(err);

  if (jobs.empty())
    error(1, 0, "Nothing to do.  Try '%s --help' for more information.",
294
          program_name);
295

296
297
298
299
300
301
302
303
304
  try
    {
      run_jobs();
    }
  catch (const std::runtime_error& e)
    {
      error(2, 0, "%s", e.what());
    }

305
  flush_cout();
306
307
  return 0;
}