spot  2.16
taatgba.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) by the Spot authors, see the AUTHORS file for details.
3 //
4 // This file is part of Spot, a model checking library.
5 //
6 // Spot is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Spot is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 #pragma once
20 
21 #include <set>
22 #include <iosfwd>
23 #include <vector>
24 #include <string>
25 #include <spot/misc/hash.hh>
26 #include <spot/tl/formula.hh>
27 #include <spot/twa/bdddict.hh>
28 #include <spot/twa/twa.hh>
29 
30 namespace spot
31 {
35  class SPOT_API taa_tgba: public twa
36  {
37  public:
39  taa_tgba(const bdd_dict_ptr& dict);
40 
41  struct transition;
43  typedef std::list<transition*> state;
45  typedef std::set<state*> state_set;
46 
48  struct transition
49  {
50  bdd condition;
52  const state_set* dst;
53  };
54 
57 
59  virtual ~taa_tgba();
60  virtual spot::state* get_init_state() const override final;
61  virtual twa_succ_iterator* succ_iter(const spot::state* state)
62  const override final;
63 
64  protected:
66  typedef std::vector<taa_tgba::state_set*> ss_vec;
67 
68  taa_tgba::state_set* init_;
69  ss_vec state_set_vec_;
70 
72  std::map<formula, acc_cond::mark_t> acc_map_;
73 
74  private:
75  // Disallow copy.
76  taa_tgba(const taa_tgba& other) = delete;
77  taa_tgba& operator=(const taa_tgba& other) = delete;
78  };
79 
82  class SPOT_API set_state final: public spot::state
83  {
84  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 override;
95  virtual size_t hash() const override;
96  virtual set_state* clone() const override;
97 
98  virtual ~set_state()
99  {
100  if (delete_me_)
101  delete s_;
102  }
103 
106  private:
107  const taa_tgba::state_set* s_;
108  bool delete_me_;
109  };
110 
114  class SPOT_API taa_succ_iterator final: public twa_succ_iterator
115  {
116  public:
119  virtual ~taa_succ_iterator();
120 
121  virtual bool first() override;
122  virtual bool next() override;
123  virtual bool done() const override;
124 
125  virtual set_state* dst() const override;
126  virtual bdd cond() const override;
127  virtual acc_cond::mark_t acc() const override;
128 
129  private:
132  typedef taa_tgba::state::const_iterator iterator;
133  typedef std::pair<iterator, iterator> iterator_pair;
134  typedef std::vector<iterator_pair> bounds_t;
135  typedef std::unordered_map<const spot::set_state*,
136  std::vector<taa_tgba::transition*>,
137  state_ptr_hash, state_ptr_equal> seen_map;
138 
139  struct distance_sort
140  {
141  bool
142  operator()(const iterator_pair& lhs, const iterator_pair& rhs) const
143  {
144  return std::distance(lhs.first, lhs.second) <
145  std::distance(rhs.first, rhs.second);
146  }
147  };
148 
149  std::vector<taa_tgba::transition*>::const_iterator i_;
150  std::vector<taa_tgba::transition*> succ_;
151  seen_map seen_;
152  const acc_cond& acc_;
153  };
154 
157  template<typename label>
158  class SPOT_API taa_tgba_labelled: public taa_tgba
159  {
160  public:
162  taa_tgba_labelled(const bdd_dict_ptr& dict) : taa_tgba(dict) {};
163 
165  {
166  for (auto i: name_state_map_)
167  {
168  for (auto i2: *i.second)
169  delete i2;
170  delete i.second;
171  }
172  }
173 
175  void set_init_state(const label& s)
176  {
177  std::vector<label> v(1);
178  v[0] = s;
179  set_init_state(v);
180  }
182  void set_init_state(const std::vector<label>& s)
183  {
184  init_ = add_state_set(s);
185  }
186 
188  transition*
189  create_transition(const label& s,
190  const std::vector<label>& d)
191  {
192  state* src = add_state(s);
193  state_set* dst = add_state_set(d);
194  transition* t = new transition;
195  t->dst = dst;
196  t->condition = bddtrue;
197  t->acceptance_conditions = {};
198  src->emplace_back(t);
199  return t;
200  }
201 
203  transition*
204  create_transition(const label& s, const label& d)
205  {
206  std::vector<std::string> vec;
207  vec.emplace_back(d);
208  return create_transition(s, vec);
209  }
210 
213  {
214  auto p = acc_map_.emplace(f, acc_cond::mark_t({}));
215  if (p.second)
216  p.first->second = acc_cond::mark_t({acc().add_set()});
217  t->acceptance_conditions |= p.first->second;
218  }
219 
228  virtual std::string format_state(const spot::state* s) const override
229  {
230  const spot::set_state* se = down_cast<const spot::set_state*>(s);
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:
254  typedef label label_t;
255 
257  typedef std::unordered_map<label, taa_tgba::state*> ns_map;
259  typedef std::unordered_map<const taa_tgba::state*, label,
261 
264 
266  virtual std::string label_to_string(const label_t& lbl) const = 0;
267 
268  private:
271  taa_tgba::state* add_state(const label& name)
272  {
273  typename ns_map::iterator i = name_state_map_.find(name);
274  if (i == name_state_map_.end())
275  {
277  name_state_map_[name] = s;
278  state_name_map_[s] = name;
279  return s;
280  }
281  return i->second;
282  }
283 
285  taa_tgba::state_set* add_state_set(const std::vector<label>& names)
286  {
287  state_set* ss = new state_set;
288  for (unsigned i = 0; i < names.size(); ++i)
289  ss->insert(add_state(names[i]));
290  state_set_vec_.emplace_back(ss);
291  return ss;
292  }
293 
294  std::string format_state_set(const taa_tgba::state_set* ss) const
295  {
296  state_set::const_iterator i1 = ss->begin();
297  typename sn_map::const_iterator i2;
298  if (ss->empty())
299  return std::string("{}");
300  if (ss->size() == 1)
301  {
302  i2 = state_name_map_.find(*i1);
303  SPOT_ASSERT(i2 != state_name_map_.end());
304  return "{" + label_to_string(i2->second) + "}";
305  }
306  else
307  {
308  std::string res("{");
309  while (i1 != ss->end())
310  {
311  i2 = state_name_map_.find(*i1++);
312  SPOT_ASSERT(i2 != state_name_map_.end());
313  res += label_to_string(i2->second);
314  res += ",";
315  }
316  res[res.size() - 1] = '}';
317  return res;
318  }
319  }
320  };
321 
324  class SPOT_API taa_tgba_string final:
325 #ifndef SWIG
326  public taa_tgba_labelled<std::string>
327 #else
328  public taa_tgba
329 #endif
330  {
331  public:
334  taa_tgba_labelled<std::string>(dict) {}
335  ~taa_tgba_string()
336  {}
337  protected:
338  virtual std::string label_to_string(const std::string& label)
339  const override;
340  };
341 
344  typedef std::shared_ptr<taa_tgba_string> taa_tgba_string_ptr;
347  typedef std::shared_ptr<const taa_tgba_string> const_taa_tgba_string_ptr;
348 
352  {
353  return SPOT_make_shared_enabled__(taa_tgba_string, dict);
354  }
355 
358  class SPOT_API taa_tgba_formula final:
359 #ifndef SWIG
360  public taa_tgba_labelled<formula>
361 #else
362  public taa_tgba
363 #endif
364  {
365  public:
368  taa_tgba_labelled<formula>(dict) {}
370  {}
371  protected:
372  virtual std::string label_to_string(const label_t& label)
373  const override;
374  };
375 
378  typedef std::shared_ptr<taa_tgba_formula> taa_tgba_formula_ptr;
381  typedef std::shared_ptr<const taa_tgba_formula> const_taa_tgba_formula_ptr;
382 
386  {
387  return SPOT_make_shared_enabled__(taa_tgba_formula, dict);
388  }
389 }
An acceptance condition.
Definition: acc.hh:54
Main class for temporal logic formula.
Definition: formula.hh:847
Definition: taatgba.hh:83
virtual set_state * clone() const override
Duplicate a state.
set_state(const taa_tgba::state_set *s, bool delete_me=false)
Construct a set_state wrapping state set s.
Definition: taatgba.hh:89
virtual size_t hash() const override
Hash a state.
virtual int compare(const spot::state *) const override
Compares two states (that come from the same automaton).
const taa_tgba::state_set * get_state() const
Return the underlying TAA state set.
Abstract class for states.
Definition: twa.hh:49
Successor iterator for alternating automata with tree-and-automata (TAA) transitions.
Definition: taatgba.hh:115
virtual bool next() override
Jump to the next successor (if any).
virtual bdd cond() const override
Get the condition on the edge leading to this successor.
virtual acc_cond::mark_t acc() const override
Get the acceptance mark of the edge leading to this successor.
taa_succ_iterator(const taa_tgba::state_set *s, const acc_cond &acc)
Construct an iterator over the successors of state s.
virtual set_state * dst() const override
Get the destination state of the current edge.
virtual bool done() const override
Check whether the iteration is finished.
virtual bool first() override
Position the iterator on the first successor (if any).
A TAA-TGBA automaton where states are labelled with formulas.
Definition: taatgba.hh:364
taa_tgba_formula(const bdd_dict_ptr &dict)
Construct a formula-labelled TAA automaton.
Definition: taatgba.hh:367
virtual std::string label_to_string(const label_t &label) const override
Return a label as a string.
Definition: taatgba.hh:159
virtual std::string format_state(const spot::state *s) const override
Format the state as a string for printing.
Definition: taatgba.hh:228
void set_init_state(const label &s)
Set the initial state to the singleton s.
Definition: taatgba.hh:175
std::unordered_map< label, taa_tgba::state * > ns_map
Map from label to taa_tgba::state*.
Definition: taatgba.hh:257
sn_map state_name_map_
Map from state pointer to label.
Definition: taatgba.hh:263
label label_t
The label type for this automaton.
Definition: taatgba.hh:254
void set_init_state(const std::vector< label > &s)
Set the initial state to the conjunction of states in s.
Definition: taatgba.hh:182
ns_map name_state_map_
Map from label to state pointer.
Definition: taatgba.hh:262
taa_tgba_labelled(const bdd_dict_ptr &dict)
Construct a labelled TAA using the given dictionary.
Definition: taatgba.hh:162
std::unordered_map< const taa_tgba::state *, label, ptr_hash< taa_tgba::state > > sn_map
Map from taa_tgba::state* to label.
Definition: taatgba.hh:260
void add_acceptance_condition(transition *t, formula f)
Add acceptance condition f to transition t.
Definition: taatgba.hh:212
transition * create_transition(const label &s, const std::vector< label > &d)
Create a transition from s to the conjunction of d.
Definition: taatgba.hh:189
transition * create_transition(const label &s, const label &d)
Create a transition from s to singleton destination d.
Definition: taatgba.hh:204
void output(std::ostream &os) const
Output a TAA in a stream.
Definition: taatgba.hh:236
virtual std::string label_to_string(const label_t &lbl) const =0
Return a label as a string.
A TAA-TGBA automaton where states are labelled with strings.
Definition: taatgba.hh:330
taa_tgba_string(const bdd_dict_ptr &dict)
Construct a string-labelled TAA automaton.
Definition: taatgba.hh:333
virtual std::string label_to_string(const std::string &label) const override
Return a label as a string.
A self-loop Transition-based Alternating Automaton (TAA) which is seen as a TGBA (abstract class,...
Definition: taatgba.hh:36
void add_condition(transition *t, formula f)
Add a Boolean condition f to transition t.
std::set< state * > state_set
Type of a set of TAA states.
Definition: taatgba.hh:45
taa_tgba(const bdd_dict_ptr &dict)
Construct a TAA automaton using the given dictionary.
std::vector< taa_tgba::state_set * > ss_vec
Type of a vector of state sets (used internally).
Definition: taatgba.hh:66
virtual spot::state * get_init_state() const override final
Get the initial state of the automaton.
virtual ~taa_tgba()
TGBA interface.
std::list< transition * > state
Type of a TAA state: a list of outgoing transitions.
Definition: taatgba.hh:41
Class for representing a transition.
Definition: twacube.hh:53
Iterate over the successors of a state.
Definition: twa.hh:425
A Transition-based ω-Automaton.
Definition: twa.hh:648
LTL/PSL formula interface.
std::shared_ptr< bdd_dict > bdd_dict_ptr
Shared pointer to a bdd_dict.
Definition: bdddict.hh:304
std::shared_ptr< taa_tgba_string > taa_tgba_string_ptr
Shared pointer to a taa_tgba_string automaton.
Definition: taatgba.hh:344
std::shared_ptr< const taa_tgba_string > const_taa_tgba_string_ptr
Shared pointer to a const taa_tgba_string automaton.
Definition: taatgba.hh:347
std::shared_ptr< const taa_tgba_formula > const_taa_tgba_formula_ptr
Shared pointer to a const taa_tgba_formula automaton.
Definition: taatgba.hh:381
taa_tgba_formula_ptr make_taa_tgba_formula(const bdd_dict_ptr &dict)
Construct a taa_tgba_formula automaton.
Definition: taatgba.hh:385
std::shared_ptr< taa_tgba_formula > taa_tgba_formula_ptr
Shared pointer to a taa_tgba_formula automaton.
Definition: taatgba.hh:378
taa_tgba_string_ptr make_taa_tgba_string(const bdd_dict_ptr &dict)
Construct a taa_tgba_string automaton.
Definition: taatgba.hh:351
Definition: automata.hh:26
std::unordered_set< const state *, state_ptr_hash, state_ptr_equal > state_set
Unordered set of abstract states.
Definition: twa.hh:197
An acceptance mark.
Definition: acc.hh:76
A hash function for pointers.
Definition: hash.hh:36
An Equivalence Relation for state pointers.
Definition: twa.hh:154
Hash Function for state pointers.
Definition: twa.hh:181
An explicit TAA transition.
Definition: taatgba.hh:49
bdd condition
Boolean label of the edge.
Definition: taatgba.hh:50
const state_set * dst
Destination state set.
Definition: taatgba.hh:52
acc_cond::mark_t acceptance_conditions
Acceptance marks.
Definition: taatgba.hh:51

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.9.1