spot  1.99.4
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
taatgba.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2009, 2011, 2012, 2013, 2014, 2015 Laboratoire de
3 // Recherche et Développement de l'Epita (LRDE).
4 //
5 // This file is part of Spot, a model checking library.
6 //
7 // Spot is free software; you can redistribute it and/or modify it
8 // 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 // (at your option) any later version.
11 //
12 // Spot is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 // License for more details.
16 //
17 // 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 #pragma once
21 
22 #include <set>
23 #include <iosfwd>
24 #include <vector>
25 #include <string>
26 #include "misc/hash.hh"
27 #include "tl/formula.hh"
28 #include "bdddict.hh"
29 #include "twa.hh"
30 
31 namespace spot
32 {
35  class SPOT_API taa_tgba: public twa
36  {
37  public:
38  taa_tgba(const bdd_dict_ptr& dict);
39 
40  struct transition;
41  typedef std::list<transition*> state;
42  typedef std::set<state*> state_set;
43 
45  struct transition
46  {
47  bdd condition;
48  acc_cond::mark_t acceptance_conditions;
49  const state_set* dst;
50  };
51 
52  void add_condition(transition* t, formula f);
53 
55  virtual ~taa_tgba();
56  virtual spot::state* get_init_state() const final;
57  virtual twa_succ_iterator* succ_iter(const spot::state* state) const final;
58  virtual std::string format_state(const spot::state* state) const = 0;
59 
60  protected:
61  virtual bdd compute_support_conditions(const spot::state* state)
62  const final;
63 
64  typedef std::vector<taa_tgba::state_set*> ss_vec;
65 
66  taa_tgba::state_set* init_;
67  ss_vec state_set_vec_;
68 
69  std::map<formula, acc_cond::mark_t> acc_map_;
70 
71  private:
72  // Disallow copy.
73  taa_tgba(const taa_tgba& other) SPOT_DELETED;
74  taa_tgba& operator=(const taa_tgba& other) SPOT_DELETED;
75  };
76 
78  class SPOT_API set_state final: public spot::state
79  {
80  public:
81  set_state(const taa_tgba::state_set* s, bool delete_me = false)
82  : s_(s), delete_me_(delete_me)
83  {
84  }
85 
86  virtual int compare(const spot::state*) const;
87  virtual size_t hash() const;
88  virtual set_state* clone() const;
89 
90  virtual ~set_state()
91  {
92  if (delete_me_)
93  delete s_;
94  }
95 
96  const taa_tgba::state_set* get_state() const;
97  private:
98  const taa_tgba::state_set* s_;
99  bool delete_me_;
100  };
101 
102  class SPOT_API taa_succ_iterator final: public twa_succ_iterator
103  {
104  public:
105  taa_succ_iterator(const taa_tgba::state_set* s, const acc_cond& acc);
106  virtual ~taa_succ_iterator();
107 
108  virtual bool first();
109  virtual bool next();
110  virtual bool done() const;
111 
112  virtual set_state* current_state() const;
113  virtual bdd current_condition() const;
114  virtual acc_cond::mark_t current_acceptance_conditions() const;
115 
116  private:
119  typedef taa_tgba::state::const_iterator iterator;
120  typedef std::pair<iterator, iterator> iterator_pair;
121  typedef std::vector<iterator_pair> bounds_t;
122  typedef std::unordered_map<const spot::set_state*,
123  std::vector<taa_tgba::transition*>,
124  state_ptr_hash, state_ptr_equal> seen_map;
125 
126  struct distance_sort :
127  public std::binary_function<const iterator_pair&,
128  const iterator_pair&, bool>
129  {
130  bool
131  operator()(const iterator_pair& lhs, const iterator_pair& rhs) const
132  {
133  return std::distance(lhs.first, lhs.second) <
134  std::distance(rhs.first, rhs.second);
135  }
136  };
137 
138  std::vector<taa_tgba::transition*>::const_iterator i_;
139  std::vector<taa_tgba::transition*> succ_;
140  seen_map seen_;
141  const acc_cond& acc_;
142  };
143 
146  template<typename label>
147  class SPOT_API taa_tgba_labelled: public taa_tgba
148  {
149  public:
150  taa_tgba_labelled(const bdd_dict_ptr& dict) : taa_tgba(dict) {};
151 
153  {
154  for (auto i: name_state_map_)
155  {
156  for (auto i2: *i.second)
157  delete i2;
158  delete i.second;
159  }
160  }
161 
162  void set_init_state(const label& s)
163  {
164  std::vector<label> v(1);
165  v[0] = s;
166  set_init_state(v);
167  }
168  void set_init_state(const std::vector<label>& s)
169  {
170  init_ = add_state_set(s);
171  }
172 
173  transition*
174  create_transition(const label& s,
175  const std::vector<label>& d)
176  {
177  state* src = add_state(s);
178  state_set* dst = add_state_set(d);
179  transition* t = new transition;
180  t->dst = dst;
181  t->condition = bddtrue;
182  t->acceptance_conditions = 0U;
183  src->push_back(t);
184  return t;
185  }
186 
187  transition*
188  create_transition(const label& s, const label& d)
189  {
190  std::vector<std::string> vec;
191  vec.push_back(d);
192  return create_transition(s, vec);
193  }
194 
195  void add_acceptance_condition(transition* t, formula f)
196  {
197  auto p = acc_map_.emplace(f, 0);
198  if (p.second)
199  p.first->second = acc_.marks({acc_.add_set()});
200  t->acceptance_conditions |= p.first->second;
201  }
202 
211  virtual std::string format_state(const spot::state* s) const
212  {
213  const spot::set_state* se = down_cast<const spot::set_state*>(s);
214  assert(se);
215  const state_set* ss = se->get_state();
216  return format_state_set(ss);
217  }
218 
220  void output(std::ostream& os) const
221  {
222  typename ns_map::const_iterator i;
223  for (i = name_state_map_.begin(); i != name_state_map_.end(); ++i)
224  {
225  taa_tgba::state::const_iterator i2;
226  os << "State: " << label_to_string(i->first) << std::endl;
227  for (i2 = i->second->begin(); i2 != i->second->end(); ++i2)
228  {
229  os << ' ' << format_state_set((*i2)->dst)
230  << ", C:" << (*i2)->condition
231  << ", A:" << (*i2)->acceptance_conditions << std::endl;
232  }
233  }
234  }
235 
236  protected:
237  typedef label label_t;
238 
239  typedef std::unordered_map<label, taa_tgba::state*> ns_map;
240  typedef std::unordered_map<const taa_tgba::state*, label,
241  ptr_hash<taa_tgba::state> > sn_map;
242 
243  ns_map name_state_map_;
244  sn_map state_name_map_;
245 
247  virtual std::string label_to_string(const label_t& lbl) const = 0;
248 
249  private:
252  taa_tgba::state* add_state(const label& name)
253  {
254  typename ns_map::iterator i = name_state_map_.find(name);
255  if (i == name_state_map_.end())
256  {
257  taa_tgba::state* s = new taa_tgba::state;
258  name_state_map_[name] = s;
259  state_name_map_[s] = name;
260  return s;
261  }
262  return i->second;
263  }
264 
266  taa_tgba::state_set* add_state_set(const std::vector<label>& names)
267  {
268  state_set* ss = new state_set;
269  for (unsigned i = 0; i < names.size(); ++i)
270  ss->insert(add_state(names[i]));
271  state_set_vec_.push_back(ss);
272  return ss;
273  }
274 
275  std::string format_state_set(const taa_tgba::state_set* ss) const
276  {
277  state_set::const_iterator i1 = ss->begin();
278  typename sn_map::const_iterator i2;
279  if (ss->empty())
280  return std::string("{}");
281  if (ss->size() == 1)
282  {
283  i2 = state_name_map_.find(*i1);
284  assert(i2 != state_name_map_.end());
285  return "{" + label_to_string(i2->second) + "}";
286  }
287  else
288  {
289  std::string res("{");
290  while (i1 != ss->end())
291  {
292  i2 = state_name_map_.find(*i1++);
293  assert(i2 != state_name_map_.end());
294  res += label_to_string(i2->second);
295  res += ",";
296  }
297  res[res.size() - 1] = '}';
298  return res;
299  }
300  }
301  };
302 
303  class SPOT_API taa_tgba_string final:
304 #ifndef SWIG
305  public taa_tgba_labelled<std::string>
306 #else
307  public taa_tgba
308 #endif
309  {
310  public:
311  taa_tgba_string(const bdd_dict_ptr& dict) :
313  ~taa_tgba_string()
314  {}
315  protected:
316  virtual std::string label_to_string(const std::string& label) const;
317  };
318 
319  typedef std::shared_ptr<taa_tgba_string> taa_tgba_string_ptr;
320  typedef std::shared_ptr<const taa_tgba_string> const_taa_tgba_string_ptr;
321 
322  inline taa_tgba_string_ptr make_taa_tgba_string(const bdd_dict_ptr& dict)
323  {
324  return std::make_shared<taa_tgba_string>(dict);
325  }
326 
327  class SPOT_API taa_tgba_formula final:
328 #ifndef SWIG
329  public taa_tgba_labelled<formula>
330 #else
331  public taa_tgba
332 #endif
333  {
334  public:
335  taa_tgba_formula(const bdd_dict_ptr& dict) :
338  {}
339  protected:
340  virtual std::string label_to_string(const label_t& label) const;
341  };
342 
343  typedef std::shared_ptr<taa_tgba_formula> taa_tgba_formula_ptr;
344  typedef std::shared_ptr<const taa_tgba_formula> const_taa_tgba_formula_ptr;
345 
346  inline taa_tgba_formula_ptr make_taa_tgba_formula(const bdd_dict_ptr& dict)
347  {
348  return std::make_shared<taa_tgba_formula>(dict);
349  }
350 }
Definition: graph.hh:31
An Equivalence Relation for state*.
Definition: twa.hh:140
A Transition-based ω-Automaton.
Definition: twa.hh:482
LTL/PSL formula interface.
A self-loop Transition-based Alternating Automaton (TAA) which is seen as a TGBA (abstract class...
Definition: taatgba.hh:35
Definition: taatgba.hh:102
Definition: formula.hh:1539
Abstract class for states.
Definition: twa.hh:42
A hash function for pointers.
Definition: hash.hh:39
Main class for temporal logic formula.
Definition: formula.hh:574
Explicit transitions.
Definition: taatgba.hh:45
Definition: taatgba.hh:327
Iterate over the successors of a state.
Definition: twa.hh:331
Definition: acc.hh:31
virtual std::string format_state(const spot::state *s) const
Format the state as a string for printing.
Definition: taatgba.hh:211
Hash Function for state*.
Definition: twa.hh:164
Definition: taatgba.hh:303
Set of states deriving from spot::state.
Definition: taatgba.hh:78
Definition: taatgba.hh:147
void output(std::ostream &os) const
Output a TAA in a stream.
Definition: taatgba.hh:220
Definition: acc.hh:34

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Thu Oct 1 2015 05:49:14 for spot by doxygen 1.8.8