compsusp.cc 12 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2012, 2013, 2014 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
// 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/>.

#include "compsusp.hh"
#include "sccfilter.hh"
22
#include "sccinfo.hh"
23
#include "tgba/tgbagraph.hh"
24
25
26
27
#include "ltl2tgba_fm.hh"
#include "minimize.hh"
#include "simulation.hh"
#include "safety.hh"
28
#include "dupexp.hh"
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "ltlast/allnodes.hh"
#include "ltlvisit/tostring.hh"
#include "ltlvisit/clone.hh"
#include <queue>
#include <sstream>
#include "ltlenv/environment.hh"

namespace spot
{
  namespace
  {
    typedef std::map<const ltl::formula*, bdd> formula_bdd_map;

    // An environment to store atomic proposition associated to
    // suspended variable.  (We don't use the default environment to
    // avoid conflicts with user-defined atomic propositions that
    // would share the same name.)
    class suspended_environment: public ltl::environment
    {
    public:
      const ltl::formula*
      require(const std::string& s)
      {
	return ltl::atomic_prop::instance(s, *this);
      }

      const std::string&
56
      name() const
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
      {
	static std::string name("suspended environment");
	return name;
      }
    };
    static suspended_environment suspenv;

    // Rewrite the suspendable subformulae "s" of an LTL formula in
    // the form Gg where "g" is an atomic proposition representing
    // "s".  At the same time, populate maps that associate "s" to "g"
    // and vice-versa.
    class ltl_suspender_visitor: public ltl::clone_visitor
    {
    public:
      typedef std::map<const ltl::formula*, const ltl::formula*> fmap_t;
      ltl_suspender_visitor(fmap_t& g2s, fmap_t& a2o, bool oblig)
	: g2s_(g2s), a2o_(a2o), oblig_(oblig)
      {
      }

      void
      visit(const ltl::multop* mo)
      {
	ltl::multop::type op = mo->op();
	switch (op)
	  {
	  case ltl::multop::Or:
	  case ltl::multop::And:
	    {
	      ltl::multop::vec* res = new ltl::multop::vec;
	      ltl::multop::vec* oblig = oblig_ ? new ltl::multop::vec : 0;
	      ltl::multop::vec* susp = new ltl::multop::vec;
	      unsigned mos = mo->size();
	      for (unsigned i = 0; i < mos; ++i)
		{
		  const ltl::formula* c = mo->nth(i);
		  if (c->is_boolean())
		    res->push_back(c->clone());
		  else if (oblig_ && c->is_syntactic_obligation())
		    oblig->push_back(c->clone());
		  else if (c->is_eventual() && c->is_universal())
		    susp->push_back(c->clone());
		  else
		    res->push_back(recurse(c));
		}
	      if (!oblig_ || oblig->empty())
		{
		  delete oblig;
		}
	      else
		{
		  const ltl::formula* o = ltl::multop::instance(op, oblig);
		  res->push_back(recurse(o));
		  o->destroy();
		}
	      if (susp->empty())
		{
		  delete susp;
		}
	      else
		{
		  const ltl::formula* o = ltl::multop::instance(op, susp);
		  // Rewrite 'o' as 'G"o"'
		  const ltl::formula* g = recurse(o);
		  o->destroy();
		  if (op == ltl::multop::And)
		    {
		      res->push_back(g);
		    }
		  else
		    {
		      // res || susp -> (res && G![susp]) || G[susp])
		      const ltl::formula* r = ltl::multop::instance(op, res);
		      const ltl::unop* u =
			down_cast<const ltl::unop*>(g);
		      const ltl::formula* gn =
			ltl::unop::instance
			(ltl::unop::G, ltl::unop::instance
			 (ltl::unop::Not, u->child()->clone()));
		      result_ = ltl::multop::instance
			(ltl::multop::Or, ltl::multop::instance
			 (ltl::multop::And, r, gn),
			 g);
		      return;
		    }
		}
	      result_ = ltl::multop::instance(op, res);
	    }
	    break;
	  case ltl::multop::OrRat:
	  case ltl::multop::AndRat:
	  case ltl::multop::AndNLM:
	  case ltl::multop::Concat:
	  case ltl::multop::Fusion:
	    this->ltl::clone_visitor::visit(mo);
	    break;
	  }
      }


      const ltl::formula*
      recurse(const ltl::formula* f)
      {
	const ltl::formula* res;
	if (f->is_boolean())
	  return f->clone();
	if (oblig_ && f->is_syntactic_obligation())
	  {
	    fmap_t::const_iterator i = assoc_.find(f);
	    if (i != assoc_.end())
	      return i->second->clone();

	    std::ostringstream s;
	    s << "〈";
	    to_string(f, s);
	    s << "〉";
	    res = suspenv.require(s.str());
	    // We have to clone f, because it is not always a sub-tree
	    // of the original formula.  (Think n-ary operators.)
	    a2o_[res] = f->clone();
	    assoc_[f] = res;
	    return res;
	  }
	if (f->is_eventual() && f->is_universal())
	  {
	    fmap_t::const_iterator i = assoc_.find(f);
	    if (i != assoc_.end())
	      return ltl::unop::instance(ltl::unop::G, i->second->clone());

	    std::ostringstream s;
187
	    s << '[';
188
	    to_string(f, s);
189
	    s << ']';
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
	    res = suspenv.require(s.str());
	    // We have to clone f, because it is not always a sub-tree
	    // of the original formula.  (Think n-ary operators.)
	    g2s_[res] = f->clone();
	    assoc_[f] = res;
	    return ltl::unop::instance(ltl::unop::G, res);
	  }
	f->accept(*this);
	return result_;
      }

    private:
      fmap_t& g2s_;
      fmap_t assoc_;		// This one is only needed by the visitor.
      fmap_t& a2o_;
      bool oblig_;
    };


    typedef std::pair<const state*, const state*> state_pair;

211
    typedef std::map<state_pair, unsigned> pair_map;
212
213
214
    typedef std::deque<state_pair> pair_queue;

    static
215
    tgba_digraph*
216
217
218
    susp_prod(tgba* left, const ltl::formula* f, bdd v)
    {
      bdd_dict* dict = left->get_dict();
219
      const tgba_digraph* a1 = ltl_to_tgba_fm(f, dict, true, true);
220

221
      const tgba_digraph* a2 = scc_filter(a1, false);
222
      delete a1;
223
      const tgba_digraph* right = iterated_simulations(a2);
224
225
      delete a2;

226
      tgba_digraph* res = new tgba_digraph(dict);
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
      dict->register_all_variables_of(left, res);
      dict->register_all_variables_of(right, res);
      dict->unregister_variable(bdd_var(v), res);

      // The left and right automata might have acceptance marker in
      // common.
      // For the example, assume left has acceptance conditions A,B,C,D
      // while right has acceptance condition C,D,E,F.

      // Negative acceptance variables...
      // !A&!B&!C&!D
      bdd lna = left->neg_acceptance_conditions();
      // !C&!D&!E&!F
      bdd rna = right->neg_acceptance_conditions();

      // Missing acceptance variables...
      // !E&!F
      bdd lma = bdd_exist(rna, bdd_support(lna));
      // !A&!B
      bdd rma = bdd_exist(lna, bdd_support(rna));

      // (A&!B&!C&!D + ... + !A&!B&!C&D) & !E&!F
      bdd lac = left->all_acceptance_conditions() & lma;
      // (C&!D&!E&!F + ... + !C&!D&!E&F) & !A&!B
      bdd rac = right->all_acceptance_conditions() & rma;
      bdd allacc = lac | rac;
      res->set_acceptance_conditions(allacc);

      // Acceptance condition to add to all transitions
      // of the left automaton.
      // !A&!B&!C&!D&E&!F | !A&!B&!C&!D&!E&F
      bdd ladd = rac - lma;
      bdd radd = lac - rma;

      pair_map seen;
      pair_queue todo;

      state_pair p(left->get_init_state(), 0);
      state* ris = right->get_init_state();
      p.second = ris;
267
268
      unsigned i = res->new_state();
      seen[p] = i;
269
      todo.push_back(p);
270
      res->set_init_state(i);
271
272
273
274
275
276
277
278
279

      while (!todo.empty())
	{
	  p = todo.front();
	  todo.pop_front();
	  const state* ls = p.first;
	  const state* rs = p.second;
	  int src = seen[p];

280
	  for (auto li: left->succ(ls))
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
	    {
	      state_pair d(li->current_state(), ris);
	      bdd lc = li->current_condition();

	      tgba_succ_iterator* ri = 0;
	      // Should we reset the right automaton?
	      if ((lc & v) == lc)
		{
		  // No.
		  ri = right->succ_iter(rs);
		  ri->first();
		}
	      // Yes.  Reset the right automaton.
	      else
		{
		  p.second = ris;
		}

	      // This loops over all the right transitions
	      // if RI is defined.  Otherwise this just makes
	      // one iteration as if the right automaton was
	      // looping in state 0 with "true".
	      while (!ri || !ri->done())
		{
		  bdd cond = lc;
		  bdd ra = allacc;
		  if (ri)
		    {
		      cond = lc & ri->current_condition();
		      // Skip incompatible transitions.
		      if (cond == bddfalse)
			{
			  ri->next();
			  continue;
			}
		      d.second = ri->current_state();
		      ra = (ri->current_acceptance_conditions() & rma) | radd;
		    }

320
		  int dest;
321
		  pair_map::const_iterator i = seen.find(d);
322
		  if (i != seen.end()) // Is this an existing state?
323
324
325
326
327
		    {
		      dest = i->second;
		    }
		  else
		    {
328
		      dest = res->new_state();
329
330
331
332
333
		      seen[d] = dest;
		      todo.push_back(d);
		    }

		  bdd la = (li->current_acceptance_conditions() & lma) | ladd;
334
		  res->new_transition(src, dest, bdd_exist(cond, v), ra & la);
335
336
337
338
339
340

		  if (ri)
		    ri->next();
		  else
		    break;
		}
341
342
	      if (ri)
		right->release_iter(ri);
343
344
345
346
347
348
349
350
	    }
	}
      delete left;
      delete right;
      return res;
    }
  }

351

352
  tgba_digraph*
353
354
355
356
357
358
359
360
361
362
  compsusp(const ltl::formula* f, bdd_dict* dict,
	   bool no_wdba, bool no_simulation,
	   bool early_susp, bool no_susp_product, bool wdba_smaller,
	   bool oblig)
  {
    ltl_suspender_visitor::fmap_t g2s;
    ltl_suspender_visitor::fmap_t a2o;
    ltl_suspender_visitor v(g2s, a2o, oblig);
    const ltl::formula* g = v.recurse(f);

363
    tgba_digraph* res;
364
365
    {
      // Translate the patched formula, and remove useless SCCs.
366
367
      tgba_digraph* aut =
	spot::ltl_to_tgba_fm(g, dict, true, true, false, false, 0, 0);
368
369
370
371
372
373
      res = scc_filter(aut, false);
      delete aut;
    }

    if (!no_wdba)
      {
374
	tgba_digraph* min = minimize_obligation(res, g, 0, wdba_smaller);
375
376
377
	if (min != res)
	  {
	    delete res;
378
	    res = min;
379
380
381
382
383
384
	    no_simulation = true;
	  }
      }

    if (!no_simulation)
      {
385
	tgba_digraph* sim = spot::iterated_simulations(res);
386
387
388
389
	delete res;
	res = sim;
      }

390
    // Create a map of suspended formulae to BDD variables.
391
    spot::formula_bdd_map susp;
392
    for (auto& it: g2s)
393
      {
394
395
396
397
398
399
400
401
402
	auto j = dict->var_map.find(it.first);
	// If no BDD variable of this suspended formula exist in the
	// BDD dict, it means the suspended subformulae was never
	// actually used in the automaton.  Just skip it.  FIXME: It
	// would be better if we had a way to check that the variable
	// is used in this automaton, and not in some automaton
	// (sharing the same dictionary.)
	if (j != dict->var_map.end())
	  susp[it.second] = bdd_ithvar(j->second);
403
404
405
406
407
408
409
410
411
      }

    // Remove suspendable formulae from non-accepting SCCs.
    bdd suspvars = bddtrue;
    for (formula_bdd_map::const_iterator i = susp.begin();
	 i != susp.end(); ++i)
      suspvars &= i->second;

    bdd allaccap = bddtrue; // set of atomic prop used in accepting SCCs.
412
    tgba_digraph* aut = res;
413
    {
414
      scc_info si(aut);
415
416
417

      // Restrict suspvars to the set of suspension labels that occur
      // in accepting SCC.
418
      unsigned sn = si.scc_count();
419
      for (unsigned n = 0; n < sn; n++)
420
421
	if (si.is_accepting_scc(n))
	  allaccap &= si.scc_ap_support(n);
422
423
424

      bdd ignored = bdd_exist(suspvars, allaccap);
      suspvars = bdd_existcomp(suspvars, allaccap);
425
      res = scc_filter_susp(aut, false, suspvars, ignored, early_susp, &si);
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
    }
    delete aut;

    // Do we need to synchronize any suspended formula?
    if (!susp.empty() && !no_susp_product)
      for (formula_bdd_map::const_iterator i = susp.begin();
	   i != susp.end(); ++i)
	if ((allaccap & i->second) == allaccap)
	  res = susp_prod(res, i->first, i->second);

    g->destroy();

    for (ltl_suspender_visitor::fmap_t::iterator i = g2s.begin();
	 i != g2s.end(); ++i)
      i->second->destroy();
    for (ltl_suspender_visitor::fmap_t::iterator i = a2o.begin();
	 i != a2o.end(); ++i)
      i->second->destroy();
    return res;
  }
}