spot 2.11.6.dev
taatgba.hh
1// -*- coding: utf-8 -*-
2// Copyright (C) 2009, 2011-2019, 2022 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#pragma once
21
22#include <set>
23#include <iosfwd>
24#include <vector>
25#include <string>
26#include <spot/misc/hash.hh>
27#include <spot/tl/formula.hh>
28#include <spot/twa/bdddict.hh>
29#include <spot/twa/twa.hh>
30
31namespace 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
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 override final;
57 virtual twa_succ_iterator* succ_iter(const spot::state* state)
58 const override final;
59
60 protected:
61
62 typedef std::vector<taa_tgba::state_set*> ss_vec;
63
64 taa_tgba::state_set* init_;
65 ss_vec state_set_vec_;
66
67 std::map<formula, acc_cond::mark_t> acc_map_;
68
69 private:
70 // Disallow copy.
71 taa_tgba(const taa_tgba& other) = delete;
72 taa_tgba& operator=(const taa_tgba& other) = delete;
73 };
74
76 class SPOT_API set_state final: public spot::state
77 {
78 public:
79 set_state(const taa_tgba::state_set* s, bool delete_me = false)
80 : s_(s), delete_me_(delete_me)
81 {
82 }
83
84 virtual int compare(const spot::state*) const override;
85 virtual size_t hash() const override;
86 virtual set_state* clone() const override;
87
88 virtual ~set_state()
89 {
90 if (delete_me_)
91 delete s_;
92 }
93
94 const taa_tgba::state_set* get_state() const;
95 private:
96 const taa_tgba::state_set* s_;
97 bool delete_me_;
98 };
99
100 class SPOT_API taa_succ_iterator final: public twa_succ_iterator
101 {
102 public:
103 taa_succ_iterator(const taa_tgba::state_set* s, const acc_cond& acc);
104 virtual ~taa_succ_iterator();
105
106 virtual bool first() override;
107 virtual bool next() override;
108 virtual bool done() const override;
109
110 virtual set_state* dst() const override;
111 virtual bdd cond() const override;
112 virtual acc_cond::mark_t acc() const override;
113
114 private:
117 typedef taa_tgba::state::const_iterator iterator;
118 typedef std::pair<iterator, iterator> iterator_pair;
119 typedef std::vector<iterator_pair> bounds_t;
120 typedef std::unordered_map<const spot::set_state*,
121 std::vector<taa_tgba::transition*>,
123
124 struct distance_sort
125 {
126 bool
127 operator()(const iterator_pair& lhs, const iterator_pair& rhs) const
128 {
129 return std::distance(lhs.first, lhs.second) <
130 std::distance(rhs.first, rhs.second);
131 }
132 };
133
134 std::vector<taa_tgba::transition*>::const_iterator i_;
135 std::vector<taa_tgba::transition*> succ_;
136 seen_map seen_;
137 const acc_cond& acc_;
138 };
139
142 template<typename label>
143 class SPOT_API taa_tgba_labelled: public taa_tgba
144 {
145 public:
146 taa_tgba_labelled(const bdd_dict_ptr& dict) : taa_tgba(dict) {};
147
149 {
150 for (auto i: name_state_map_)
151 {
152 for (auto i2: *i.second)
153 delete i2;
154 delete i.second;
155 }
156 }
157
158 void set_init_state(const label& s)
159 {
160 std::vector<label> v(1);
161 v[0] = s;
162 set_init_state(v);
163 }
164 void set_init_state(const std::vector<label>& s)
165 {
166 init_ = add_state_set(s);
167 }
168
170 create_transition(const label& s,
171 const std::vector<label>& d)
172 {
173 state* src = add_state(s);
174 state_set* dst = add_state_set(d);
175 transition* t = new transition;
176 t->dst = dst;
177 t->condition = bddtrue;
178 t->acceptance_conditions = {};
179 src->emplace_back(t);
180 return t;
181 }
182
184 create_transition(const label& s, const label& d)
185 {
186 std::vector<std::string> vec;
187 vec.emplace_back(d);
188 return create_transition(s, vec);
189 }
190
191 void add_acceptance_condition(transition* t, formula f)
192 {
193 auto p = acc_map_.emplace(f, acc_cond::mark_t({}));
194 if (p.second)
195 p.first->second = acc_cond::mark_t({acc().add_set()});
196 t->acceptance_conditions |= p.first->second;
197 }
198
207 virtual std::string format_state(const spot::state* s) const override
208 {
209 const spot::set_state* se = down_cast<const spot::set_state*>(s);
210 const state_set* ss = se->get_state();
211 return format_state_set(ss);
212 }
213
215 void output(std::ostream& os) const
216 {
217 typename ns_map::const_iterator i;
218 for (i = name_state_map_.begin(); i != name_state_map_.end(); ++i)
219 {
220 taa_tgba::state::const_iterator i2;
221 os << "State: " << label_to_string(i->first) << std::endl;
222 for (i2 = i->second->begin(); i2 != i->second->end(); ++i2)
223 {
224 os << ' ' << format_state_set((*i2)->dst)
225 << ", C:" << (*i2)->condition
226 << ", A:" << (*i2)->acceptance_conditions << std::endl;
227 }
228 }
229 }
230
231 protected:
232 typedef label label_t;
233
234 typedef std::unordered_map<label, taa_tgba::state*> ns_map;
235 typedef std::unordered_map<const taa_tgba::state*, label,
237
238 ns_map name_state_map_;
239 sn_map state_name_map_;
240
242 virtual std::string label_to_string(const label_t& lbl) const = 0;
243
244 private:
247 taa_tgba::state* add_state(const label& name)
248 {
249 typename ns_map::iterator i = name_state_map_.find(name);
250 if (i == name_state_map_.end())
251 {
252 taa_tgba::state* s = new taa_tgba::state;
253 name_state_map_[name] = s;
254 state_name_map_[s] = name;
255 return s;
256 }
257 return i->second;
258 }
259
261 taa_tgba::state_set* add_state_set(const std::vector<label>& names)
262 {
263 state_set* ss = new state_set;
264 for (unsigned i = 0; i < names.size(); ++i)
265 ss->insert(add_state(names[i]));
266 state_set_vec_.emplace_back(ss);
267 return ss;
268 }
269
270 std::string format_state_set(const taa_tgba::state_set* ss) const
271 {
272 state_set::const_iterator i1 = ss->begin();
273 typename sn_map::const_iterator i2;
274 if (ss->empty())
275 return std::string("{}");
276 if (ss->size() == 1)
277 {
278 i2 = state_name_map_.find(*i1);
279 SPOT_ASSERT(i2 != state_name_map_.end());
280 return "{" + label_to_string(i2->second) + "}";
281 }
282 else
283 {
284 std::string res("{");
285 while (i1 != ss->end())
286 {
287 i2 = state_name_map_.find(*i1++);
288 SPOT_ASSERT(i2 != state_name_map_.end());
289 res += label_to_string(i2->second);
290 res += ",";
291 }
292 res[res.size() - 1] = '}';
293 return res;
294 }
295 }
296 };
297
298 class SPOT_API taa_tgba_string final:
299#ifndef SWIG
300 public taa_tgba_labelled<std::string>
301#else
302 public taa_tgba
303#endif
304 {
305 public:
306 taa_tgba_string(const bdd_dict_ptr& dict) :
309 {}
310 protected:
311 virtual std::string label_to_string(const std::string& label)
312 const override;
313 };
314
315 typedef std::shared_ptr<taa_tgba_string> taa_tgba_string_ptr;
316 typedef std::shared_ptr<const taa_tgba_string> const_taa_tgba_string_ptr;
317
318 inline taa_tgba_string_ptr make_taa_tgba_string(const bdd_dict_ptr& dict)
319 {
320 return SPOT_make_shared_enabled__(taa_tgba_string, dict);
321 }
322
323 class SPOT_API taa_tgba_formula final:
324#ifndef SWIG
325 public taa_tgba_labelled<formula>
326#else
327 public taa_tgba
328#endif
329 {
330 public:
331 taa_tgba_formula(const bdd_dict_ptr& dict) :
334 {}
335 protected:
336 virtual std::string label_to_string(const label_t& label)
337 const override;
338 };
339
340 typedef std::shared_ptr<taa_tgba_formula> taa_tgba_formula_ptr;
341 typedef std::shared_ptr<const taa_tgba_formula> const_taa_tgba_formula_ptr;
342
343 inline taa_tgba_formula_ptr make_taa_tgba_formula(const bdd_dict_ptr& dict)
344 {
345 return SPOT_make_shared_enabled__(taa_tgba_formula, dict);
346 }
347}
An acceptance condition.
Definition: acc.hh:62
Main class for temporal logic formula.
Definition: formula.hh:728
Set of states deriving from spot::state.
Definition: taatgba.hh:77
virtual size_t hash() const override
Hash a state.
virtual set_state * clone() const override
Duplicate a state.
virtual int compare(const spot::state *) const override
Compares two states (that come from the same automaton).
Abstract class for states.
Definition: twa.hh:51
Definition: taatgba.hh:101
virtual bool next() override
Jump to the next successor (if any).
virtual set_state * dst() const override
Get the destination state of the current edge.
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.
virtual bool done() const override
Check whether the iteration is finished.
virtual bool first() override
Position the iterator on the first successor (if any).
Definition: taatgba.hh:329
virtual std::string label_to_string(const label_t &label) const override
Return a label as a string.
Definition: taatgba.hh:144
virtual std::string format_state(const spot::state *s) const override
Format the state as a string for printing.
Definition: taatgba.hh:207
void output(std::ostream &os) const
Output a TAA in a stream.
Definition: taatgba.hh:215
virtual std::string label_to_string(const label_t &lbl) const =0
Return a label as a string.
Definition: taatgba.hh:304
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
virtual spot::state * get_init_state() const override final
Get the initial state of the automaton.
virtual ~taa_tgba()
TGBA interface.
Iterate over the successors of a state.
Definition: twa.hh:398
A Transition-based ω-Automaton.
Definition: twa.hh:623
LTL/PSL formula interface.
Definition: automata.hh:27
std::unordered_set< const state *, state_ptr_hash, state_ptr_equal > state_set
Unordered set of abstract states.
Definition: twa.hh:190
An acceptance mark.
Definition: acc.hh:85
A hash function for pointers.
Definition: hash.hh:40
An Equivalence Relation for state*.
Definition: twa.hh:151
Hash Function for state*.
Definition: twa.hh:175
Explicit transitions.
Definition: taatgba.hh:46

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.4