spot  1.2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
taatgba.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2009, 2011, 2012, 2013 Laboratoire de Recherche et
3 // 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 #ifndef SPOT_TGBA_TAATGBA_HH
21 # define SPOT_TGBA_TAATGBA_HH
22 
23 #include <set>
24 #include <iosfwd>
25 #include <vector>
26 #include "misc/hash.hh"
27 #include "ltlast/formula.hh"
28 #include "bdddict.hh"
29 #include "tgba.hh"
30 
31 namespace spot
32 {
35  class SPOT_API taa_tgba : public tgba
36  {
37  public:
38  taa_tgba(bdd_dict* 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  bdd acceptance_conditions;
49  const state_set* dst;
50  };
51 
52  void add_condition(transition* t, const ltl::formula* f);
53 
55  virtual ~taa_tgba();
56  virtual spot::state* get_init_state() const;
57  virtual tgba_succ_iterator*
58  succ_iter(const spot::state* local_state,
59  const spot::state* global_state = 0,
60  const tgba* global_automaton = 0) const;
61  virtual bdd_dict* get_dict() const;
62  virtual std::string format_state(const spot::state* state) const = 0;
63  virtual bdd all_acceptance_conditions() const;
64  virtual bdd neg_acceptance_conditions() const;
65 
66  protected:
67  virtual bdd compute_support_conditions(const spot::state* state) const;
68  virtual bdd compute_support_variables(const spot::state* state) const;
69 
70  typedef std::vector<taa_tgba::state_set*> ss_vec;
71 
72  bdd_dict* dict_;
73  mutable bdd all_acceptance_conditions_;
74  mutable bool all_acceptance_conditions_computed_;
75  bdd neg_acceptance_conditions_;
76  taa_tgba::state_set* init_;
77  ss_vec state_set_vec_;
78 
79  private:
80  // Disallow copy.
81  taa_tgba(const taa_tgba& other);
82  taa_tgba& operator=(const taa_tgba& other);
83  };
84 
86  class SPOT_API set_state : public spot::state
87  {
88  public:
89  set_state(const taa_tgba::state_set* s, bool delete_me = false)
90  : s_(s), delete_me_(delete_me)
91  {
92  }
93 
94  virtual int compare(const spot::state*) const;
95  virtual size_t hash() const;
96  virtual set_state* clone() const;
97 
98  virtual ~set_state()
99  {
100  if (delete_me_)
101  delete s_;
102  }
103 
104  const taa_tgba::state_set* get_state() const;
105  private:
106  const taa_tgba::state_set* s_;
107  bool delete_me_;
108  };
109 
110  class SPOT_API taa_succ_iterator : public tgba_succ_iterator
111  {
112  public:
113  taa_succ_iterator(const taa_tgba::state_set* s, bdd all_acc);
114  virtual ~taa_succ_iterator();
115 
116  virtual void first();
117  virtual void next();
118  virtual bool done() const;
119 
120  virtual set_state* current_state() const;
121  virtual bdd current_condition() const;
122  virtual bdd current_acceptance_conditions() const;
123 
124  private:
127  typedef taa_tgba::state::const_iterator iterator;
128  typedef std::pair<iterator, iterator> iterator_pair;
129  typedef std::vector<iterator_pair> bounds_t;
130  typedef Sgi::hash_map<
131  const spot::set_state*, std::vector<taa_tgba::transition*>,
132  state_ptr_hash, state_ptr_equal> seen_map;
133 
134  struct distance_sort :
135  public std::binary_function<const iterator_pair&,
136  const iterator_pair&, bool>
137  {
138  bool
139  operator()(const iterator_pair& lhs, const iterator_pair& rhs) const
140  {
141  return std::distance(lhs.first, lhs.second) <
142  std::distance(rhs.first, rhs.second);
143  }
144  };
145 
146  std::vector<taa_tgba::transition*>::const_iterator i_;
147  std::vector<taa_tgba::transition*> succ_;
148  bdd all_acceptance_conditions_;
149  seen_map seen_;
150  };
151 
154  template<typename label, typename label_hash>
155  class SPOT_API taa_tgba_labelled : public taa_tgba
156  {
157  public:
158  taa_tgba_labelled(bdd_dict* dict) : taa_tgba(dict) {};
159 
160  void set_init_state(const label& s)
161  {
162  std::vector<label> v(1);
163  v[0] = s;
164  set_init_state(v);
165  }
166  void set_init_state(const std::vector<label>& s)
167  {
168  init_ = add_state_set(s);
169  }
170 
171  transition*
172  create_transition(const label& s,
173  const std::vector<label>& d)
174  {
175  state* src = add_state(s);
176  state_set* dst = add_state_set(d);
177  transition* t = new transition;
178  t->dst = dst;
179  t->condition = bddtrue;
180  t->acceptance_conditions = bddfalse;
181  src->push_back(t);
182  return t;
183  }
184  transition*
185  create_transition(const label& s, const label& d)
186  {
187  std::vector<std::string> vec;
188  vec.push_back(d);
189  return create_transition(s, vec);
190  }
191 
192  void add_acceptance_condition(transition* t, const ltl::formula* f)
193  {
194  if (dict_->acc_map.find(f) == dict_->acc_map.end())
195  {
196  int v = dict_->register_acceptance_variable(f, this);
197  bdd neg = bdd_nithvar(v);
198  neg_acceptance_conditions_ &= neg;
199 
200  // Append neg to all acceptance conditions.
201  typename ns_map::iterator i;
202  for (i = name_state_map_.begin(); i != name_state_map_.end(); ++i)
203  {
204  taa_tgba::state::iterator i2;
205  for (i2 = i->second->begin(); i2 != i->second->end(); ++i2)
206  (*i2)->acceptance_conditions &= neg;
207  }
208 
209  all_acceptance_conditions_computed_ = false;
210  }
211 
212  bdd_dict::fv_map::iterator i = dict_->acc_map.find(f);
213  assert(i != dict_->acc_map.end());
214  f->destroy();
215  bdd v = bdd_ithvar(i->second);
216  t->acceptance_conditions |= v & bdd_exist(neg_acceptance_conditions_, v);
217  }
218 
227  virtual std::string format_state(const spot::state* s) const
228  {
229  const spot::set_state* se = down_cast<const spot::set_state*>(s);
230  assert(se);
231  const state_set* ss = se->get_state();
232  return format_state_set(ss);
233  }
234 
236  void output(std::ostream& os) const
237  {
238  typename ns_map::const_iterator i;
239  for (i = name_state_map_.begin(); i != name_state_map_.end(); ++i)
240  {
241  taa_tgba::state::const_iterator i2;
242  os << "State: " << label_to_string(i->first) << std::endl;
243  for (i2 = i->second->begin(); i2 != i->second->end(); ++i2)
244  {
245  os << " " << format_state_set((*i2)->dst)
246  << ", C:" << (*i2)->condition
247  << ", A:" << (*i2)->acceptance_conditions << std::endl;
248  }
249  }
250  }
251 
252  protected:
253  typedef label label_t;
254 
255  typedef Sgi::hash_map<
256  const label, taa_tgba::state*, label_hash
257  > ns_map;
258  typedef Sgi::hash_map<
259  const taa_tgba::state*, label, ptr_hash<taa_tgba::state>
260  > sn_map;
261 
262  ns_map name_state_map_;
263  sn_map state_name_map_;
264 
266  virtual std::string label_to_string(const label_t& lbl) const = 0;
267 
270  virtual label_t clone_if(const label_t& lbl) const = 0;
271 
272  private:
275  taa_tgba::state* add_state(const label& name)
276  {
277  typename ns_map::iterator i = name_state_map_.find(name);
278  if (i == name_state_map_.end())
279  {
280  const label& name_ = clone_if(name);
281  taa_tgba::state* s = new taa_tgba::state;
282  name_state_map_[name_] = s;
283  state_name_map_[s] = name_;
284  return s;
285  }
286  return i->second;
287  }
288 
290  taa_tgba::state_set* add_state_set(const std::vector<label>& names)
291  {
292  state_set* ss = new state_set;
293  for (unsigned i = 0; i < names.size(); ++i)
294  ss->insert(add_state(names[i]));
295  state_set_vec_.push_back(ss);
296  return ss;
297  }
298 
299  std::string format_state_set(const taa_tgba::state_set* ss) const
300  {
301  state_set::const_iterator i1 = ss->begin();
302  typename sn_map::const_iterator i2;
303  if (ss->empty())
304  return std::string("{}");
305  if (ss->size() == 1)
306  {
307  i2 = state_name_map_.find(*i1);
308  assert(i2 != state_name_map_.end());
309  return "{" + label_to_string(i2->second) + "}";
310  }
311  else
312  {
313  std::string res("{");
314  while (i1 != ss->end())
315  {
316  i2 = state_name_map_.find(*i1++);
317  assert(i2 != state_name_map_.end());
318  res += label_to_string(i2->second);
319  res += ",";
320  }
321  res[res.size() - 1] = '}';
322  return res;
323  }
324  }
325  };
326 
327  class SPOT_API taa_tgba_string :
328  public taa_tgba_labelled<std::string, string_hash>
329  {
330  public:
331  taa_tgba_string(bdd_dict* dict) :
333  ~taa_tgba_string();
334  protected:
335  virtual std::string label_to_string(const std::string& label) const;
336  virtual std::string clone_if(const std::string& label) const;
337  };
338 
339  class SPOT_API taa_tgba_formula :
340  public taa_tgba_labelled<const ltl::formula*, ltl::formula_ptr_hash>
341  {
342  public:
343  taa_tgba_formula(bdd_dict* dict) :
345  ~taa_tgba_formula();
346  protected:
347  virtual std::string label_to_string(const label_t& label) const;
348  virtual const ltl::formula* clone_if(const label_t& label) const;
349  };
350 }
351 
352 #endif // SPOT_TGBA_TAATGBA_HH

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Thu May 15 2014 11:04:11 for spot by doxygen 1.8.4