hierarchy.cc 11.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// -*- coding: utf-8 -*-
// Copyright (C) 2017 Laboratoire de Recherche et Développement de
// l'Epita (LRDE)
//
// 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
// the Free Software Foundation; either version 3 of the License, or
// (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
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

20
#include <sstream>
21
#include <spot/tl/hierarchy.hh>
22
#include <spot/tl/nenoform.hh>
23
24
25
26
27
28
29
#include <spot/twaalgos/isdet.hh>
#include <spot/twaalgos/ltl2tgba_fm.hh>
#include <spot/twaalgos/minimize.hh>
#include <spot/twaalgos/postproc.hh>
#include <spot/twaalgos/remfin.hh>
#include <spot/twaalgos/strength.hh>
#include <spot/twaalgos/totgba.hh>
30
#include <spot/twaalgos/cobuchi.hh>
31
32
33
34


namespace spot
{
35
  namespace
36
  {
37
38
    static bool
    cobuchi_realizable(spot::formula f,
39
                       const const_twa_graph_ptr& aut)
40
41
42
43
    {
      twa_graph_ptr cobuchi = nullptr;
      std::vector<acc_cond::rs_pair> pairs;
      if (aut->acc().is_streett_like(pairs) || aut->acc().is_parity())
44
        cobuchi = nsa_to_nca(aut, false);
45
      else if (aut->get_acceptance().is_dnf())
46
        cobuchi = dnf_to_nca(aut, false);
47
48
49
50
51
52
53
54
55
56
57
58
59
60
      else
        throw std::runtime_error("cobuchi_realizable() only works with "
                                 "Streett-like, Parity or any "
                                 "acceptance condition in DNF");

      return !cobuchi->intersects(ltl_to_tgba_fm(formula::Not(f),
                                                 cobuchi->get_dict(),
                                                 true));
    }

    static bool
    detbuchi_realizable(const twa_graph_ptr& aut)
    {
      if (is_universal(aut))
61
        return true;
62
63
64
65
66
67
68
69

      // if aut is a non-deterministic TGBA, we do
      // TGBA->DPA->DRA->(D?)BA.  The conversion from DRA to
      // BA will preserve determinism if possible.
      spot::postprocessor p;
      p.set_type(spot::postprocessor::Generic);
      p.set_pref(spot::postprocessor::Deterministic);
      p.set_level(spot::postprocessor::Low);
70
71
      auto dpa = p.run(aut);
      if (dpa->acc().is_generalized_buchi())
72
        {
73
          assert(is_deterministic(dpa));
74
75
76
77
          return true;
        }
      else
        {
78
79
          auto dra = to_generalized_rabin(dpa);
          return rabin_is_buchi_realizable(dra);
80
81
82
83
        }
    }
  }

84
85
86
87
88
89
  [[noreturn]] static void invalid_spot_pr_check()
  {
    throw std::runtime_error("invalid value for SPOT_PR_CHECK "
                             "(should be 1 or 2)");
  }

90
  static prcheck
91
  algo_to_perform(bool is_persistence, bool aut_given)
92
  {
93
    static prcheck env_algo = [&]()
94
      {
95
96
        int val;
        try
97
          {
98
99
            auto s = getenv("SPOT_PR_CHECK");
            val = s ? std::stoi(s) : 0;
100
          }
101
        catch (const std::exception& e)
102
          {
103
            invalid_spot_pr_check();
104
          }
105
        if (val == 0)
106
107
          {
            if (aut_given && !is_persistence)
108
              return prcheck::via_Rabin;
109
            else if (is_persistence || !aut_given)
110
              return prcheck::via_CoBuchi;
111
112
113
            else
              SPOT_UNREACHABLE();
          }
114
115
116
117
118
119
120
121
        else if (val == 1)
          {
            return prcheck::via_CoBuchi;
          }
        else if (val == 2)
          {
            return prcheck::via_Rabin;
          }
122
        else
Alexandre GBAGUIDI AISSE's avatar
Alexandre GBAGUIDI AISSE committed
123
          {
124
            invalid_spot_pr_check();
Alexandre GBAGUIDI AISSE's avatar
Alexandre GBAGUIDI AISSE committed
125
          }
126
127
      }();
    return env_algo;
128
129
130
131
132
133
134
135
  }

  bool
  is_persistence(formula f, twa_graph_ptr aut, prcheck algo)
  {
    if (f.is_syntactic_persistence())
      return true;

136
137
138
139
    if (algo == prcheck::Auto)
      algo = algo_to_perform(true, aut != nullptr);

    switch (algo)
140
      {
141
      case prcheck::via_CoBuchi:
142
143
        return cobuchi_realizable(f, aut ? aut :
                                  ltl_to_tgba_fm(f, make_bdd_dict(), true));
144

145
      case prcheck::via_Rabin:
146
147
        return detbuchi_realizable(ltl_to_tgba_fm(formula::Not(f),
                                                  make_bdd_dict(), true));
148

149
      case prcheck::Auto:
150
        SPOT_UNREACHABLE();
151
      }
152
153
154
155
156
157
158
159
160
161

    SPOT_UNREACHABLE();
  }

  bool
  is_recurrence(formula f, twa_graph_ptr aut, prcheck algo)
  {
    if (f.is_syntactic_recurrence())
      return true;

162
163
164
165
    if (algo == prcheck::Auto)
      algo = algo_to_perform(true, aut != nullptr);

    switch (algo)
166
      {
167
      case prcheck::via_CoBuchi:
168
        return cobuchi_realizable(formula::Not(f),
169
170
                                  ltl_to_tgba_fm(formula::Not(f),
                                                 make_bdd_dict(), true));
171

172
      case prcheck::via_Rabin:
173
174
        return detbuchi_realizable(aut ? aut :
                                   ltl_to_tgba_fm(f, make_bdd_dict(), true));
175

176
      case prcheck::Auto:
177
        SPOT_UNREACHABLE();
178
      }
179
180

    SPOT_UNREACHABLE();
181
182
  }

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

  [[noreturn]] static void invalid_spot_o_check()
  {
    throw std::runtime_error("invalid value for SPOT_O_CHECK "
                             "(should be 1, 2, or 3)");
  }

  // This private function is defined in minimize.cc for technical
  // reasons.
  SPOT_LOCAL bool is_wdba_realizable(formula f, twa_graph_ptr aut = nullptr);

  bool
  is_obligation(formula f, twa_graph_ptr aut, ocheck algo)
  {
    if (algo == ocheck::Auto)
      {
        static ocheck env_algo = []()
          {
            int val;
            try
              {
                auto s = getenv("SPOT_O_CHECK");
                val = s ? std::stoi(s) : 0;
              }
            catch (const std::exception& e)
              {
                invalid_spot_o_check();
              }
            if (val == 0)
              return ocheck::via_WDBA;
            else if (val == 1)
              return ocheck::via_CoBuchi;
            else if (val == 2)
              return ocheck::via_Rabin;
            else if (val == 3)
              return ocheck::via_WDBA;
            else
              invalid_spot_o_check();
          }();
        algo = env_algo;
      }
    switch (algo)
      {
      case ocheck::via_WDBA:
        return is_wdba_realizable(f, aut);
      case ocheck::via_CoBuchi:
229
230
        return (is_persistence(f, aut, prcheck::via_CoBuchi)
                && is_recurrence(f, aut, prcheck::via_CoBuchi));
231
      case ocheck::via_Rabin:
232
233
        return (is_persistence(f, aut, prcheck::via_Rabin)
                && is_recurrence(f, aut, prcheck::via_Rabin));
234
235
236
237
238
239
240
      case ocheck::Auto:
        SPOT_UNREACHABLE();
      }
    SPOT_UNREACHABLE();
  }


241
242
243
244
245
246
247
248
249
  char mp_class(formula f)
  {
    if (f.is_syntactic_safety() && f.is_syntactic_guarantee())
      return 'B';
    auto dict = make_bdd_dict();
    auto aut = ltl_to_tgba_fm(f, dict, true);
    auto min = minimize_obligation(aut, f);
    if (aut != min) // An obligation.
      {
250
251
252
253
254
        scc_info si(min);
        // The minimba WDBA can have some trivial accepting SCCs
        // that we should ignore in is_terminal_automaton().
        bool g = is_terminal_automaton(min, &si, true);
        bool s = is_safety_automaton(min, &si);
255
256
257
258
259
260
261
262
        if (g)
          return s ? 'B' : 'G';
        else
          return s ? 'S' : 'O';
      }
    // Not an obligation.  Could by 'P', 'R', or 'T'.
    if (is_recurrence(f, aut))
      return 'R';
263
    if (is_persistence(f, aut))
264
265
266
      return 'P';
    return 'T';
  }
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370

  std::string mp_class(formula f, const char* opt)
  {
    return mp_class(mp_class(f), opt);
  }

  std::string mp_class(char mpc, const char* opt)
  {
    bool verbose = false;
    bool wide = false;
    if (opt)
      for (;;)
        switch (int o = *opt++)
          {
          case 'v':
            verbose = true;
            break;
          case 'w':
            wide = true;
            break;
          case ' ':
          case '\t':
          case '\n':
          case ',':
            break;
          case '\0':
          case ']':
            goto break2;
          default:
            {
              std::ostringstream err;
              err << "unknown option '" << o << "' for mp_class()";
              throw std::runtime_error(err.str());
            }
          }
  break2:
    std::string c(1, mpc);
    if (wide)
      {
        switch (mpc)
          {
          case 'B':
            c = "GSOPRT";
            break;
          case 'G':
            c = "GOPRT";
            break;
          case 'S':
            c = "SOPRT";
            break;
          case 'O':
            c = "OPRT";
            break;
          case 'P':
            c = "PT";
            break;
          case 'R':
            c = "RT";
            break;
          case 'T':
            break;
          default:
            throw std::runtime_error("mp_class() called with unknown class");
          }
      }
    if (!verbose)
      return c;

    std::ostringstream os;
    bool first = true;
    for (char ch: c)
      {
        if (first)
          first = false;
        else
          os << ' ';
        switch (ch)
          {
          case 'B':
            os << "guarantee safety";
            break;
          case 'G':
            os << "guarantee";
            break;
          case 'S':
            os << "safety";
            break;
          case 'O':
            os << "obligation";
            break;
          case 'P':
            os << "persistence";
            break;
          case 'R':
            os << "recurrence";
            break;
          case 'T':
            os << "reactivity";
            break;
          }
      }
    return os.str();
  }

371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
  unsigned nesting_depth(formula f, op oper)
  {
    unsigned max_depth = 0;
    for (formula child: f)
      max_depth = std::max(max_depth, nesting_depth(child, oper));
    return max_depth + f.is(oper);
  }

  unsigned nesting_depth(formula f, const op* begin, const op* end)
  {
    unsigned max_depth = 0;
    for (formula child: f)
      max_depth = std::max(max_depth, nesting_depth(child, begin, end));
    bool matched = std::find(begin, end, f.kind()) != end;
    return max_depth + matched;
  }

  unsigned nesting_depth(formula f, const char* opers)
  {
    bool want_nnf = false;
    std::vector<op> v;
    for (;;)
      switch (char c = *opers++)
        {
        case '~':
          want_nnf = true;
          break;
        case '!':
          v.push_back(op::Not);
          break;
        case '&':
          v.push_back(op::And);
          break;
        case '|':
          v.push_back(op::Or);
          break;
        case 'e':
          v.push_back(op::Equiv);
          break;
        case 'F':
          v.push_back(op::F);
          break;
        case 'G':
          v.push_back(op::G);
          break;
        case 'i':
          v.push_back(op::Implies);
          break;
        case 'M':
          v.push_back(op::M);
          break;
        case 'R':
          v.push_back(op::R);
          break;
        case 'U':
          v.push_back(op::U);
          break;
        case 'W':
          v.push_back(op::W);
          break;
        case 'X':
          v.push_back(op::X);
          break;
        case '\0':
        case ']':
          goto break2;
        default:
          throw std::runtime_error
            (std::string("nesting_depth(): unknown operator '") + c + '\'');
        }
  break2:
    if (want_nnf)
      f = negative_normal_form(f);
    const op* vd = v.data();
    return nesting_depth(f, vd, vd + v.size());
  }
447
}