genltl.cc 9.32 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
    { "gh-q", gen::LTL_GH_Q, "RANGE", 0,
88
      "(F(p1)|G(p2))&(F(p2)|G(p3))&...&(F(pn)|G(p{n+1}))", 0 },
89
    { "gh-r", gen::LTL_GH_R, "RANGE", 0,
90
      "(GF(p1)|FG(p2))&(GF(p2)|FG(p3))&... &(GF(pn)|FG(p{n+1}))", 0 },
91
    { "go-theta", gen::LTL_GO_THETA, "RANGE", 0,
92
      "!((GF(p1)&GF(p2)&...&GF(pn)) -> G(q->F(r)))", 0 },
93
94
    { "hkrss-patterns", gen::LTL_HKRSS_PATTERNS,
      "RANGE", OPTION_ARG_OPTIONAL,
95
96
97
      "Holeček et al. patterns from the Liberouter project "
      "(range should be included in 1..55)", 0 },
    OPT_ALIAS(liberouter-patterns),
98
    { "kr-n", gen::LTL_KR_N, "RANGE", 0,
99
      "linear formula with doubly exponential DBA", 0 },
100
    { "kr-nlogn", gen::LTL_KR_NLOGN, "RANGE", 0,
101
      "quasilinear formula with doubly exponential DBA", 0 },
102
    { "kv-psi", gen::LTL_KV_PSI, "RANGE", 0,
103
      "quadratic formula with doubly exponential DBA", 0 },
104
    OPT_ALIAS(kr-n2),
105
    { "or-fg", gen::LTL_OR_FG, "RANGE", 0, "FG(p1)|FG(p2)|...|FG(pn)", 0 },
106
    OPT_ALIAS(ccj-xi),
107
    { "or-g", gen::LTL_OR_G, "RANGE", 0, "G(p1)|G(p2)|...|G(pn)", 0 },
108
    OPT_ALIAS(gh-s),
109
    { "or-gf", gen::LTL_OR_GF, "RANGE", 0, "GF(p1)|GF(p2)|...|GF(pn)", 0 },
110
    OPT_ALIAS(gh-c1),
111
    { "p-patterns", gen::LTL_P_PATTERNS, "RANGE", OPTION_ARG_OPTIONAL,
112
113
114
115
      "Pelánek [Spin'07] patterns from BEEM "
      "(range should be included in 1..20)", 0 },
    OPT_ALIAS(beem-patterns),
    OPT_ALIAS(p),
116
117
118
119
    { "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,
120
      "n-bit counter w/ carry", 0 },
121
122
123
    { "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,
124
      "n-bit counter (linear size)", 0 },
125
    { "sb-patterns", gen::LTL_SB_PATTERNS, "RANGE", OPTION_ARG_OPTIONAL,
126
127
      "Somenzi and Bloem [CAV'00] patterns "
      "(range should be included in 1..27)", 0 },
128
    { "tv-f1", gen::LTL_TV_F1, "RANGE", 0,
129
      "G(p -> (q | Xq | ... | XX...Xq)", 0 },
130
    { "tv-f2", gen::LTL_TV_F2, "RANGE", 0,
131
      "G(p -> (q | X(q | X(... | Xq)))", 0 },
132
    { "tv-g1", gen::LTL_TV_G1, "RANGE", 0,
133
      "G(p -> (q & Xq & ... & XX...Xq)", 0 },
134
    { "tv-g2", gen::LTL_TV_G2, "RANGE", 0,
135
      "G(p -> (q & X(q & X(... & Xq)))", 0 },
136
    { "tv-uu", gen::LTL_TV_UU, "RANGE", 0,
137
      "G(p1 -> (p1 U (p2 & (p2 U (p3 & (p3 U ...))))))", 0 },
138
    { "u-left", gen::LTL_U_LEFT, "RANGE", 0, "(((p1 U p2) U p3) ... U pn)", 0 },
139
    OPT_ALIAS(gh-u),
140
    { "u-right", gen::LTL_U_RIGHT, "RANGE", 0, "(p1 U (p2 U (... U pn)))", 0 },
141
142
    OPT_ALIAS(gh-u2),
    OPT_ALIAS(go-phi),
143
    RANGE_DOC,
144
  /**************************************************/
145
    { nullptr, 0, nullptr, 0, "Output options:", -20 },
146
147
148
149
150
151
    { "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 },
152
    { nullptr, 0, nullptr, 0, "The FORMAT string passed to --format may use "
153
      "the following interpreted sequences:", -19 },
154
    { "%f", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
155
      "the formula (in the selected syntax)", 0 },
156
    { "%F", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
157
      "the name of the pattern", 0 },
158
    { "%L", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
159
      "the argument of the pattern", 0 },
160
    { "%%", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
161
      "a single %", 0 },
162
    COMMON_LTL_OUTPUT_SPECS,
163
164
    { nullptr, 0, nullptr, 0, "Miscellaneous options:", -1 },
    { nullptr, 0, nullptr, 0, nullptr, 0 }
165
166
167
168
  };

struct job
{
169
  gen::ltl_pattern_id pattern;
170
  struct range range;
171
172
173
174
};

typedef std::vector<job> jobs_t;
static jobs_t jobs;
175
176
bool opt_positive = false;
bool opt_negative = false;
177
178
179

const struct argp_child children[] =
  {
180
181
182
    { &output_argp, 0, nullptr, -20 },
    { &misc_argp, 0, nullptr, -1 },
    { nullptr, 0, nullptr, 0 }
183
  };
184
185

static void
186
enqueue_job(int pattern, const char* range_str = nullptr)
187
188
{
  job j;
189
  j.pattern = static_cast<gen::ltl_pattern_id>(pattern);
190
191
192
193
194
195
196
  if (range_str)
    {
      j.range = parse_range(range_str);
    }
  else
    {
      j.range.min = 1;
197
      j.range.max = gen::ltl_pattern_max(j.pattern);
198
199
      if (j.range.max == 0)
        error(2, 0, "missing range for %s",
200
              gen::ltl_pattern_name(j.pattern));
201
    }
202
203
204
  jobs.push_back(j);
}

205
static int
206
parse_opt(int key, char* arg, struct argp_state*)
207
{
208
  if (key >= gen::LTL_BEGIN && key < gen::LTL_END)
209
210
211
212
    {
      enqueue_job(key, arg);
      return 0;
    }
213
214
215
  // This switch is alphabetically-ordered.
  switch (key)
    {
216
217
218
219
220
221
    case OPT_POSITIVE:
      opt_positive = true;
      break;
    case OPT_NEGATIVE:
      opt_negative = true;
      break;
222
    default:
223
      return ARGP_ERR_UNKNOWN;
224
    }
225
  return 0;
226
227
228
}


229
static void
230
output_pattern(gen::ltl_pattern_id pattern, int n)
231
{
232
  formula f = gen::ltl_pattern(pattern, n);
233

234
235
  // Make sure we use only "p42"-style of atomic propositions
  // in lbt's output.
236
237
  if (output_format == lbt_output && !f.has_lbt_atomic_props())
    f = relabel(f, Pnn);
238

239
240
  if (opt_positive || !opt_negative)
    {
241
      output_formula_checked(f, gen::ltl_pattern_name(pattern), n);
242
243
244
245
    }
  if (opt_negative)
    {
      std::string tmp = "!";
246
247
      tmp += gen::ltl_pattern_name(pattern);
      output_formula_checked(formula::Not(f), tmp.c_str(), n);
248
    }
249
250
251
252
253
}

static void
run_jobs()
{
254
  for (auto& j: jobs)
255
    {
256
257
      int inc = (j.range.max < j.range.min) ? -1 : 1;
      int n = j.range.min;
258
      for (;;)
259
260
261
262
263
264
        {
          output_pattern(j.pattern, n);
          if (n == j.range.max)
            break;
          n += inc;
        }
265
266
267
268
269
270
271
    }
}


int
main(int argc, char** argv)
{
272
  setup(argv);
273

274
  const argp ap = { options, parse_opt, nullptr, argp_program_doc,
275
                    children, nullptr, nullptr };
276

277
  if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))
278
279
280
281
    exit(err);

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

284
285
286
287
288
289
290
291
292
  try
    {
      run_jobs();
    }
  catch (const std::runtime_error& e)
    {
      error(2, 0, "%s", e.what());
    }

293
  flush_cout();
294
295
  return 0;
}