spot  2.3.1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
twa.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2009, 2011, 2013, 2014, 2015, 2016 Laboratoire de
3 // Recherche et Développement de l'Epita (LRDE).
4 // Copyright (C) 2003, 2004, 2005 Laboratoire d'Informatique de
5 // Paris 6 (LIP6), département Systèmes Répartis Coopératifs (SRC),
6 // Université Pierre et Marie Curie.
7 //
8 // This file is part of Spot, a model checking library.
9 //
10 // Spot is free software; you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Spot is distributed in the hope that it will be useful, but WITHOUT
16 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 // License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 #pragma once
24 
25 #include <cstddef>
26 #include <spot/twa/fwd.hh>
27 #include <spot/twa/acc.hh>
28 #include <spot/twa/bdddict.hh>
29 #include <cassert>
30 #include <memory>
31 #include <unordered_map>
32 #include <functional>
33 #include <array>
34 #include <vector>
35 #include <spot/misc/casts.hh>
36 #include <spot/misc/hash.hh>
37 #include <spot/tl/formula.hh>
38 #include <spot/misc/trival.hh>
39 
40 namespace spot
41 {
42  struct twa_run;
43  typedef std::shared_ptr<twa_run> twa_run_ptr;
44 
45  struct twa_word;
46  typedef std::shared_ptr<twa_word> twa_word_ptr;
47 
50  class SPOT_API state
51  {
52  public:
63  virtual int compare(const state* other) const = 0;
64 
84  virtual size_t hash() const = 0;
85 
87  virtual state* clone() const = 0;
88 
98  virtual void destroy() const
99  {
100  delete this;
101  }
102 
103  protected:
110  virtual ~state()
111  {
112  }
113  };
114 
128  {
129  bool
130  operator()(const state* left, const state* right) const
131  {
132  SPOT_ASSERT(left);
133  return left->compare(right) < 0;
134  }
135  };
136 
151  {
152  bool
153  operator()(const state* left, const state* right) const
154  {
155  SPOT_ASSERT(left);
156  return 0 == left->compare(right);
157  }
158  };
159 
175  {
176  size_t
177  operator()(const state* that) const
178  {
179  SPOT_ASSERT(that);
180  return that->hash();
181  }
182  };
183 
189  typedef std::unordered_set<const state*,
190  state_ptr_hash, state_ptr_equal> state_set;
191 
195  template<class val>
196  using state_map = std::unordered_map<const state*, val,
197  state_ptr_hash, state_ptr_equal>;
198 
201  class SPOT_API state_unicity_table
202  {
203  state_set m;
204  public:
205 
214  const state* operator()(const state* s)
215  {
216  auto p = m.insert(s);
217  if (!p.second)
218  s->destroy();
219  return *p.first;
220  }
221 
226  const state* is_new(const state* s)
227  {
228  auto p = m.insert(s);
229  if (!p.second)
230  {
231  s->destroy();
232  return nullptr;
233  }
234  return *p.first;
235  }
236 
238  {
239  for (state_set::iterator i = m.begin(); i != m.end();)
240  {
241  // Advance the iterator before destroying its key. This
242  // avoid issues with old g++ implementations.
243  state_set::iterator old = i++;
244  (*old)->destroy();
245  }
246  }
247 
248  size_t
249  size()
250  {
251  return m.size();
252  }
253  };
254 
255 
256 
257  // Functions related to shared_ptr.
259 
260  typedef std::shared_ptr<const state> shared_state;
261 
262  inline void shared_state_deleter(state* s) { s->destroy(); }
263 
278  {
279  bool
280  operator()(shared_state left,
281  shared_state right) const
282  {
283  SPOT_ASSERT(left);
284  return left->compare(right.get()) < 0;
285  }
286  };
287 
306  {
307  bool
308  operator()(shared_state left,
309  shared_state right) const
310  {
311  SPOT_ASSERT(left);
312  return 0 == left->compare(right.get());
313  }
314  };
315 
335  {
336  size_t
337  operator()(shared_state that) const
338  {
339  SPOT_ASSERT(that);
340  return that->hash();
341  }
342  };
343 
345  typedef std::unordered_set<shared_state,
347  state_shared_ptr_equal> shared_state_set;
348 
397  class SPOT_API twa_succ_iterator
398  {
399  public:
400  virtual
402  {
403  }
404 
407 
422  virtual bool first() = 0;
423 
434  virtual bool next() = 0;
435 
451  virtual bool done() const = 0;
452 
454 
457 
467  virtual const state* dst() const = 0;
471  virtual bdd cond() const = 0;
474  virtual acc_cond::mark_t acc() const = 0;
475 
477  };
478 
479  namespace internal
480  {
486  struct SPOT_API succ_iterator
487  {
488  protected:
489  twa_succ_iterator* it_;
490  public:
491 
493  it_(it)
494  {
495  }
496 
497  bool operator==(succ_iterator o) const
498  {
499  return it_ == o.it_;
500  }
501 
502  bool operator!=(succ_iterator o) const
503  {
504  return it_ != o.it_;
505  }
506 
507  const twa_succ_iterator* operator*() const
508  {
509  return it_;
510  }
511 
512  void operator++()
513  {
514  if (!it_->next())
515  it_ = nullptr;
516  }
517  };
518 
519 #ifndef SWIG
520  class twa_succ_iterable
526  {
527  protected:
528  const twa* aut_;
529  twa_succ_iterator* it_;
530  public:
531  twa_succ_iterable(const twa* aut, twa_succ_iterator* it)
532  : aut_(aut), it_(it)
533  {
534  }
535 
536  twa_succ_iterable(twa_succ_iterable&& other) noexcept
537  : aut_(other.aut_), it_(other.it_)
538  {
539  other.it_ = nullptr;
540  }
541 
542  ~twa_succ_iterable(); // Defined in this file after twa
543 
544  internal::succ_iterator begin()
545  {
546  return it_->first() ? it_ : nullptr;
547  }
548 
549  internal::succ_iterator end()
550  {
551  return nullptr;
552  }
553  };
554 #endif // SWIG
555  }
556 
566 
569 
622  class SPOT_API twa: public std::enable_shared_from_this<twa>
623  {
624  protected:
625  twa(const bdd_dict_ptr& d);
629  bdd_dict_ptr dict_;
630  public:
631 
632  virtual ~twa();
633 
639  virtual const state* get_init_state() const = 0;
640 
648  virtual twa_succ_iterator*
649  succ_iter(const state* local_state) const = 0;
650 
651 #ifndef SWIG
676  succ(const state* s) const
677  {
678  return {this, succ_iter(s)};
679  }
680  #endif
681 
687  {
688  if (iter_cache_)
689  delete i;
690  else
691  iter_cache_ = i;
692  }
693 
710  bdd_dict_ptr get_dict() const
711  {
712  return dict_;
713  }
714 
728  {
729  int res = dict_->has_registered_proposition(ap, this);
730  if (res < 0)
731  {
732  aps_.emplace_back(ap);
733  res = dict_->register_proposition(ap, this);
734  bddaps_ &= bdd_ithvar(res);
735  }
736  return res;
737  }
738 
739  int register_ap(std::string ap)
740  {
741  return register_ap(formula::ap(ap));
742  }
744 
748  void unregister_ap(int num);
749 
762  {
763  if (!aps_.empty())
764  throw std::runtime_error("register_ap_from_dict() may not be"
765  " called on an automaton that has already"
766  " registered some AP");
767  auto& m = get_dict()->bdd_map;
768  unsigned s = m.size();
769  for (unsigned n = 0; n < s; ++n)
770  if (m[n].refs.find(this) != m[n].refs.end())
771  {
772  aps_.emplace_back(m[n].f);
773  bddaps_ &= bdd_ithvar(n);
774  }
775  }
776 
779  const std::vector<formula>& ap() const
780  {
781  return aps_;
782  }
783 
785  bdd ap_vars() const
786  {
787  return bddaps_;
788  }
789 
796  virtual std::string format_state(const state* s) const = 0;
797 
811  virtual state* project_state(const state* s,
812  const const_twa_ptr& t) const;
813 
816  const acc_cond& acc() const
817  {
818  return acc_;
819  }
820 
822  {
823  return acc_;
824  }
826 
831  virtual bool is_empty() const;
832 
845  virtual twa_run_ptr accepting_run() const;
846 
856  virtual twa_word_ptr accepting_word() const;
857 
860  virtual bool intersects(const_twa_ptr other) const;
861 
879  virtual twa_run_ptr intersecting_run(const_twa_ptr other,
880  bool from_other = false) const;
881 
882 
889  virtual twa_word_ptr intersecting_word(const_twa_ptr other) const;
890 
891  private:
892  acc_cond acc_;
893 
894  void set_num_sets_(unsigned num)
895  {
896  if (num < acc_.num_sets())
897  {
898  acc_.~acc_cond();
899  new (&acc_) acc_cond;
900  }
901  acc_.add_sets(num - acc_.num_sets());
902  }
903 
904  public:
906  unsigned num_sets() const
907  {
908  return acc_.num_sets();
909  }
910 
913  {
914  return acc_.get_acceptance();
915  }
916 
921  void set_acceptance(unsigned num, const acc_cond::acc_code& c)
922  {
923  set_num_sets_(num);
924  acc_.set_acceptance(c);
925  }
926 
928  void copy_acceptance_of(const const_twa_ptr& a)
929  {
930  acc_ = a->acc();
931  }
932 
934  void copy_ap_of(const const_twa_ptr& a)
935  {
936  for (auto f: a->ap())
937  this->register_ap(f);
938  }
939 
952  void set_generalized_buchi(unsigned num)
953  {
954  set_num_sets_(num);
955  acc_.set_generalized_buchi();
956  }
957 
974  {
975  set_generalized_buchi(1);
976  return acc_.mark(0);
977  }
978 
979  private:
980  std::vector<formula> aps_;
981  bdd bddaps_;
982 
984  struct bprop
985  {
986  trival::repr_t state_based_acc:2; // State-based acceptance.
987  trival::repr_t inherently_weak:2; // Inherently Weak automaton.
988  trival::repr_t weak:2; // Weak automaton.
989  trival::repr_t terminal:2; // Terminal automaton.
990  trival::repr_t deterministic:2; // Deterministic automaton.
991  trival::repr_t unambiguous:2; // Unambiguous automaton.
992  trival::repr_t stutter_invariant:2; // Stutter invariant language.
993  trival::repr_t very_weak:2; // very-weak, or 1-weak
994  trival::repr_t semi_deterministic:2; // semi-deterministic automaton.
995  };
996  union
997  {
998  unsigned props;
999  bprop is;
1000  };
1001 
1002 #ifndef SWIG
1003  // Dynamic properties, are given with a name and a destructor function.
1004  std::unordered_map<std::string,
1005  std::pair<void*,
1006  std::function<void(void*)>>> named_prop_;
1007 #endif
1008  void* get_named_prop_(std::string s) const;
1009 
1010  public:
1011 
1012 #ifndef SWIG
1013  void set_named_prop(std::string s,
1029  void* val, std::function<void(void*)> destructor);
1030 
1046  template<typename T>
1047  void set_named_prop(std::string s, T* val)
1048  {
1049  set_named_prop(s, val, [](void *p) { delete static_cast<T*>(p); });
1050  }
1051 
1062  void set_named_prop(std::string s, std::nullptr_t);
1063 
1079  template<typename T>
1080  T* get_named_prop(std::string s) const
1081  {
1082  if (void* p = get_named_prop_(s))
1083  return static_cast<T*>(p);
1084  else
1085  return nullptr;
1086  }
1087 
1100  template<typename T>
1101  T* get_or_set_named_prop(std::string s)
1102  {
1103  if (void* p = get_named_prop_(s))
1104  return static_cast<T*>(p);
1105 
1106  auto tmp = new T;
1107  set_named_prop(s, tmp);
1108  return tmp;
1109  }
1110 
1111 #endif
1112 
1118  {
1119  // Destroy all named properties.
1120  for (auto& np: named_prop_)
1121  np.second.second(np.second.first);
1122  named_prop_.clear();
1123  }
1124 
1132  {
1133  if (num_sets() == 0)
1134  return true;
1135  return is.state_based_acc;
1136  }
1137 
1143  {
1144  is.state_based_acc = val.val();
1145  }
1146 
1151  trival is_sba() const
1152  {
1153  return prop_state_acc() && acc().is_buchi();
1154  }
1155 
1165  {
1166  return is.inherently_weak;
1167  }
1168 
1177  {
1178  is.inherently_weak = val.val();
1179  if (!val)
1180  is.very_weak = is.terminal = is.weak = val.val();
1181  }
1182 
1195  {
1196  return is.terminal;
1197  }
1198 
1207  {
1208  is.terminal = val.val();
1209  if (val)
1210  is.inherently_weak = is.weak = val.val();
1211  }
1212 
1221  {
1222  return is.weak;
1223  }
1224 
1233  void prop_weak(trival val)
1234  {
1235  is.weak = val.val();
1236  if (val)
1237  is.inherently_weak = val.val();
1238  if (!val)
1239  is.very_weak = is.terminal = val.val();
1240  }
1241 
1251  {
1252  return is.very_weak;
1253  }
1254 
1263  {
1264  is.very_weak = val.val();
1265  if (val)
1266  is.weak = is.inherently_weak = val.val();
1267  }
1268 
1269 
1270 
1283  {
1284  return is.deterministic;
1285  }
1286 
1295  {
1296  is.deterministic = val.val();
1297  if (val)
1298  // deterministic implies unambiguous and semi-deterministic
1299  is.unambiguous = is.semi_deterministic = val.val();
1300  }
1301 
1316  {
1317  return is.unambiguous;
1318  }
1319 
1327  {
1328  is.unambiguous = val.val();
1329  if (!val)
1330  is.deterministic = val.val();
1331  }
1332 
1346  {
1347  return is.semi_deterministic;
1348  }
1349 
1357  {
1358  is.semi_deterministic = val.val();
1359  if (!val)
1360  is.deterministic = val.val();
1361  }
1362 
1376  {
1377  return is.stutter_invariant;
1378  }
1379 
1382  {
1383  is.stutter_invariant = val.val();
1384  }
1385 
1425  struct prop_set
1426  {
1427  bool state_based;
1428  bool inherently_weak;
1429  bool deterministic;
1430  bool improve_det;
1431  bool stutter_inv;
1432 
1448  static prop_set all()
1449  {
1450  return { true, true, true, true, true };
1451  }
1452  };
1453 
1464  void prop_copy(const const_twa_ptr& other, prop_set p)
1465  {
1466  if (p.state_based)
1467  prop_state_acc(other->prop_state_acc());
1468  if (p.inherently_weak)
1469  {
1470  prop_terminal(other->prop_terminal());
1471  prop_weak(other->prop_weak());
1472  prop_very_weak(other->prop_very_weak());
1473  prop_inherently_weak(other->prop_inherently_weak());
1474  }
1475  if (p.deterministic)
1476  {
1477  prop_deterministic(other->prop_deterministic());
1478  prop_semi_deterministic(other->prop_semi_deterministic());
1479  prop_unambiguous(other->prop_unambiguous());
1480  }
1481  else if (p.improve_det)
1482  {
1483  if (other->prop_deterministic().is_true())
1484  {
1485  prop_deterministic(true);
1486  }
1487  else
1488  {
1489  if (other->prop_semi_deterministic().is_true())
1490  prop_semi_deterministic(true);
1491  if (other->prop_unambiguous().is_true())
1492  prop_unambiguous(true);
1493  }
1494  }
1495  if (p.stutter_inv)
1496  prop_stutter_invariant(other->prop_stutter_invariant());
1497  }
1498 
1504  void prop_keep(prop_set p)
1505  {
1506  if (!p.state_based)
1507  prop_state_acc(trival::maybe());
1508  if (!p.inherently_weak)
1509  {
1510  prop_terminal(trival::maybe());
1511  prop_weak(trival::maybe());
1512  prop_very_weak(trival::maybe());
1513  prop_inherently_weak(trival::maybe());
1514  }
1515  if (!p.deterministic)
1516  {
1517  if (!(p.improve_det && prop_deterministic().is_true()))
1518  prop_deterministic(trival::maybe());
1519  if (!(p.improve_det && prop_semi_deterministic().is_true()))
1520  prop_semi_deterministic(trival::maybe());
1521  if (!(p.improve_det && prop_unambiguous().is_true()))
1522  prop_unambiguous(trival::maybe());
1523  }
1524  if (!p.stutter_inv)
1525  prop_stutter_invariant(trival::maybe());
1526  }
1527 
1528  };
1529 
1530 #ifndef SWIG
1531  namespace internal
1532  {
1533  inline twa_succ_iterable::~twa_succ_iterable()
1534  {
1535  if (it_)
1536  aut_->release_iter(it_);
1537  }
1538  }
1539 #endif // SWIG
1540 
1543 
1546 
1549 
1552 
1555 
1558 
1561 
1564 }
Helper class to iterate over the successor of a state using the on-the-fly interface.
Definition: twa.hh:525
trival prop_very_weak() const
Whether the automaton is very-weak.
Definition: twa.hh:1250
virtual ~state()
Destructor.
Definition: twa.hh:110
Definition: graph.hh:33
Helper structure to iterate over the successor of a state using the on-the-fly interface.
Definition: twa.hh:486
An Equivalence Relation for state*.
Definition: twa.hh:150
Render state pointers unique via a hash table.
Definition: twa.hh:201
void prop_weak(trival val)
Set the weak property.
Definition: twa.hh:1233
bdd ap_vars() const
The set of atomic propositions as a conjunction.
Definition: twa.hh:785
void prop_inherently_weak(trival val)
Set the "inherently weak" proeprty.
Definition: twa.hh:1176
static formula ap(const std::string &name)
Build an atomic proposition.
Definition: formula.hh:816
twa_succ_iterator * iter_cache_
Any iterator returned via release_iter.
Definition: twa.hh:627
bdd_dict_ptr dict_
BDD dictionary used by the automaton.
Definition: twa.hh:629
trival prop_inherently_weak() const
Whether the automaton is inherently weak.
Definition: twa.hh:1164
A Transition-based ω-Automaton.
Definition: twa.hh:622
LTL/PSL formula interface.
void prop_stutter_invariant(trival val)
Set the stutter-invariant property.
Definition: twa.hh:1381
acc_cond & acc()
The acceptance condition of the automaton.
Definition: twa.hh:821
Abstract class for states.
Definition: twa.hh:50
Strict Weak Ordering for shared_state (shared_ptr).
Definition: twa.hh:277
T * get_or_set_named_prop(std::string s)
Create or retrieve a named property.
Definition: twa.hh:1101
trival prop_unambiguous() const
Whether the automaton is unambiguous.
Definition: twa.hh:1315
trival prop_state_acc() const
Whether the automaton uses state-based acceptance.
Definition: twa.hh:1131
Main class for temporal logic formula.
Definition: formula.hh:669
virtual bool next()=0
Jump to the next successor (if any).
void set_generalized_buchi(unsigned num)
Set generalized Büchi acceptance.
Definition: twa.hh:952
void copy_acceptance_of(const const_twa_ptr &a)
Copy the acceptance condition of another TωA.
Definition: twa.hh:928
virtual bool first()=0
Position the iterator on the first successor (if any).
void set_named_prop(std::string s, T *val)
Declare a named property.
Definition: twa.hh:1047
void set_acceptance(unsigned num, const acc_cond::acc_code &c)
Set the acceptance condition of the automaton.
Definition: twa.hh:921
void prop_state_acc(trival val)
Set the state-based-acceptance property.
Definition: twa.hh:1142
virtual void destroy() const
Release a state.
Definition: twa.hh:98
void prop_unambiguous(trival val)
Sets the unambiguous property.
Definition: twa.hh:1326
Hash Function for shared_state (shared_ptr).
Definition: twa.hh:334
Iterate over the successors of a state.
Definition: twa.hh:397
void prop_semi_deterministic(trival val)
Sets the semi-deterministic property.
Definition: twa.hh:1356
trival prop_stutter_invariant() const
Whether the automaton is stutter-invariant.
Definition: twa.hh:1375
void release_named_properties()
Destroy all named properties.
Definition: twa.hh:1117
void prop_deterministic(trival val)
Set the deterministic property.
Definition: twa.hh:1294
Definition: acc.hh:31
internal::twa_succ_iterable succ(const state *s) const
Build an iterable over the successors of s.
Definition: twa.hh:676
trival prop_terminal() const
Whether the automaton is terminal.
Definition: twa.hh:1194
const acc_cond::acc_code & get_acceptance() const
Acceptance formula used by the automaton.
Definition: twa.hh:912
trival prop_weak() const
Whether the automaton is weak.
Definition: twa.hh:1220
int register_ap(formula ap)
Register an atomic proposition designated by ap.
Definition: twa.hh:727
const state * operator()(const state *s)
Canonicalize state pointer.
Definition: twa.hh:214
void prop_terminal(trival val)
Set the terminal property.
Definition: twa.hh:1206
A class implementing Kleene's three-valued logic.
Definition: trival.hh:33
void release_iter(twa_succ_iterator *i) const
Release an iterator after usage.
Definition: twa.hh:686
const state * is_new(const state *s)
Canonicalize state pointer.
Definition: twa.hh:226
unsigned num_sets() const
Number of acceptance sets used by the automaton.
Definition: twa.hh:906
trival prop_semi_deterministic() const
Whether the automaton is semi-deterministic.
Definition: twa.hh:1345
Hash Function for state*.
Definition: twa.hh:174
const acc_cond & acc() const
The acceptance condition of the automaton.
Definition: twa.hh:816
T * get_named_prop(std::string s) const
Retrieve a named property.
Definition: twa.hh:1080
void register_aps_from_dict()
Register all atomic propositions that have already be register by the bdd_dict for this automaton...
Definition: twa.hh:761
const std::vector< formula > & ap() const
The vector of atomic propositions registered by this automaton.
Definition: twa.hh:779
Definition: twa.hh:1531
virtual int compare(const state *other) const =0
Compares two states (that come from the same automaton).
int register_ap(std::string ap)
Register an atomic proposition designated by ap.
Definition: twa.hh:739
virtual size_t hash() const =0
Hash a state.
trival prop_deterministic() const
Whether the automaton is deterministic.
Definition: twa.hh:1282
static prop_set all()
A structure for selecting a set of automaton properties to copy.
Definition: twa.hh:1448
Definition: acc.hh:300
trival is_sba() const
Whether this is a state-based Büchi automaton.
Definition: twa.hh:1151
Strict Weak Ordering for state*.
Definition: twa.hh:127
Atomic proposition.
bdd_dict_ptr get_dict() const
Get the dictionary associated to the automaton.
Definition: twa.hh:710
acc_cond::mark_t set_buchi()
Set Büchi acceptance.
Definition: twa.hh:973
Definition: acc.hh:34
void copy_ap_of(const const_twa_ptr &a)
Copy the atomic propositions of another TωA.
Definition: twa.hh:934
void prop_very_weak(trival val)
Set the very-weak property.
Definition: twa.hh:1262
An Equivalence Relation for shared_state (shared_ptr).
Definition: twa.hh:305

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Mon Feb 20 2017 07:08:25 for spot by doxygen 1.8.8