tgbaexplicit.hh

Go to the documentation of this file.
00001 // Copyright (C) 2009, 2010, 2011 Laboratoire de Recherche et Développement
00002 // de l'Epita (LRDE).
00003 // Copyright (C) 2003, 2004, 2006 Laboratoire d'Informatique de
00004 // Paris 6 (LIP6), département Systèmes Répartis Coopératifs (SRC),
00005 // Université Pierre et Marie Curie.
00006 //
00007 // This file is part of Spot, a model checking library.
00008 //
00009 // Spot is free software; you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation; either version 2 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // Spot is distributed in the hope that it will be useful, but WITHOUT
00015 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00016 // or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
00017 // License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with Spot; see the file COPYING.  If not, write to the Free
00021 // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00022 // 02111-1307, USA.
00023 
00024 #ifndef SPOT_TGBA_TGBAEXPLICIT_HH
00025 # define SPOT_TGBA_TGBAEXPLICIT_HH
00026 
00027 #include "misc/hash.hh"
00028 #include <list>
00029 #include "tgba.hh"
00030 #include "ltlast/formula.hh"
00031 #include <cassert>
00032 
00033 namespace spot
00034 {
00035   // Forward declarations.  See below.
00036   class state_explicit;
00037   class tgba_explicit_succ_iterator;
00038   class tgba_explicit;
00039 
00042   class tgba_explicit: public tgba
00043   {
00044   public:
00045     tgba_explicit(bdd_dict* dict);
00046 
00047     struct transition;
00048     typedef std::list<transition*> state;
00049 
00051     struct transition
00052     {
00053       bdd condition;
00054       bdd acceptance_conditions;
00055       const state* dest;
00056     };
00057 
00059     virtual state* add_default_init() = 0;
00060 
00061     transition*
00062     create_transition(state* source, const state* dest);
00063 
00064     void add_condition(transition* t, const ltl::formula* f);
00066     void add_conditions(transition* t, bdd f);
00067 
00072     void copy_acceptance_conditions_of(const tgba *a);
00073 
00075     void set_acceptance_conditions(bdd acc);
00076 
00077     bool has_acceptance_condition(const ltl::formula* f) const;
00078     void add_acceptance_condition(transition* t, const ltl::formula* f);
00080     void add_acceptance_conditions(transition* t, bdd f);
00081 
00082     // tgba interface
00083     virtual ~tgba_explicit();
00084     virtual spot::state* get_init_state() const;
00085     virtual tgba_succ_iterator*
00086     succ_iter(const spot::state* local_state,
00087               const spot::state* global_state = 0,
00088               const tgba* global_automaton = 0) const;
00089     virtual bdd_dict* get_dict() const;
00090 
00091     virtual bdd all_acceptance_conditions() const;
00092     virtual bdd neg_acceptance_conditions() const;
00093 
00094     virtual std::string format_state(const spot::state* s) const = 0;
00095 
00096   protected:
00097     virtual bdd compute_support_conditions(const spot::state* state) const;
00098     virtual bdd compute_support_variables(const spot::state* state) const;
00099 
00100     bdd get_acceptance_condition(const ltl::formula* f);
00101 
00102     bdd_dict* dict_;
00103     tgba_explicit::state* init_;
00104     mutable bdd all_acceptance_conditions_;
00105     bdd neg_acceptance_conditions_;
00106     mutable bool all_acceptance_conditions_computed_;
00107 
00108   private:
00109     // Disallow copy.
00110     tgba_explicit(const tgba_explicit& other);
00111     tgba_explicit& operator=(const tgba_explicit& other);
00112   };
00113 
00114 
00115 
00118   class state_explicit : public spot::state
00119   {
00120   public:
00121     state_explicit(const tgba_explicit::state* s)
00122       : state_(s)
00123     {
00124     }
00125 
00126     virtual int compare(const spot::state* other) const;
00127     virtual size_t hash() const;
00128     virtual state_explicit* clone() const;
00129 
00130     virtual ~state_explicit()
00131     {
00132     }
00133 
00134     const tgba_explicit::state* get_state() const;
00135   private:
00136     const tgba_explicit::state* state_;
00137   };
00138 
00139 
00142   class tgba_explicit_succ_iterator: public tgba_succ_iterator
00143   {
00144   public:
00145     tgba_explicit_succ_iterator(const tgba_explicit::state* s, bdd all_acc);
00146 
00147     virtual void first();
00148     virtual void next();
00149     virtual bool done() const;
00150 
00151     virtual state_explicit* current_state() const;
00152     virtual bdd current_condition() const;
00153     virtual bdd current_acceptance_conditions() const;
00154 
00155   private:
00156     const tgba_explicit::state* s_;
00157     tgba_explicit::state::const_iterator i_;
00158     bdd all_acceptance_conditions_;
00159   };
00160 
00162   template<typename label, typename label_hash>
00163   class tgba_explicit_labelled: public tgba_explicit
00164   {
00165   protected:
00166     typedef label label_t;
00167     typedef Sgi::hash_map<label, tgba_explicit::state*,
00168                           label_hash> ns_map;
00169     typedef Sgi::hash_map<const tgba_explicit::state*, label,
00170                           ptr_hash<tgba_explicit::state> > sn_map;
00171     ns_map name_state_map_;
00172     sn_map state_name_map_;
00173   public:
00174     tgba_explicit_labelled(bdd_dict* dict) : tgba_explicit(dict) {};
00175 
00176     bool has_state(const label& name)
00177     {
00178       return name_state_map_.find(name) != name_state_map_.end();
00179     }
00180 
00181     const label& get_label(const tgba_explicit::state* s) const
00182     {
00183       typename sn_map::const_iterator i = state_name_map_.find(s);
00184       assert(i != state_name_map_.end());
00185       return i->second;
00186     }
00187 
00188     const label& get_label(const spot::state* s) const
00189     {
00190       const state_explicit* se = dynamic_cast<const state_explicit*>(s);
00191       assert(se);
00192       return get_label(se->get_state());
00193     }
00194 
00197     state* add_state(const label& name)
00198     {
00199       typename ns_map::iterator i = name_state_map_.find(name);
00200       if (i == name_state_map_.end())
00201         {
00202           tgba_explicit::state* s = new tgba_explicit::state;
00203           name_state_map_[name] = s;
00204           state_name_map_[s] = name;
00205 
00206           // The first state we add is the inititial state.
00207           // It can also be overridden with set_init_state().
00208           if (!init_)
00209             init_ = s;
00210 
00211           return s;
00212         }
00213       return i->second;
00214     }
00215 
00216     state*
00217     set_init_state(const label& state)
00218     {
00219       tgba_explicit::state* s = add_state(state);
00220       init_ = s;
00221       return s;
00222     }
00223 
00224 
00225     transition*
00226     create_transition(state* source, const state* dest)
00227     {
00228       return tgba_explicit::create_transition(source, dest);
00229     }
00230 
00231     transition*
00232     create_transition(const label& source, const label& dest)
00233     {
00234       // It's important that the source be created before the
00235       // destination, so the first source encountered becomes the
00236       // default initial state.
00237       state* s = add_state(source);
00238       return tgba_explicit::create_transition(s, add_state(dest));
00239     }
00240 
00241     void
00242     complement_all_acceptance_conditions()
00243     {
00244       bdd all = all_acceptance_conditions();
00245       typename ns_map::iterator i;
00246       for (i = name_state_map_.begin(); i != name_state_map_.end(); ++i)
00247         {
00248           tgba_explicit::state::iterator i2;
00249           for (i2 = i->second->begin(); i2 != i->second->end(); ++i2)
00250             {
00251               (*i2)->acceptance_conditions = all - (*i2)->acceptance_conditions;
00252             }
00253         }
00254     }
00255 
00256     void
00257     declare_acceptance_condition(const ltl::formula* f)
00258     {
00259       int v = dict_->register_acceptance_variable(f, this);
00260       f->destroy();
00261       bdd neg = bdd_nithvar(v);
00262       neg_acceptance_conditions_ &= neg;
00263 
00264       // Append neg to all acceptance conditions.
00265       typename ns_map::iterator i;
00266       for (i = name_state_map_.begin(); i != name_state_map_.end(); ++i)
00267         {
00268           tgba_explicit::state::iterator i2;
00269           for (i2 = i->second->begin(); i2 != i->second->end(); ++i2)
00270             (*i2)->acceptance_conditions &= neg;
00271         }
00272 
00273       all_acceptance_conditions_computed_ = false;
00274     }
00275 
00276 
00277     void
00278     merge_transitions()
00279     {
00280       typename ns_map::iterator i;
00281       for (i = name_state_map_.begin(); i != name_state_map_.end(); ++i)
00282         {
00283           state::iterator t1;
00284           for (t1 = i->second->begin(); t1 != i->second->end(); ++t1)
00285             {
00286               bdd acc = (*t1)->acceptance_conditions;
00287               const state* dest = (*t1)->dest;
00288 
00289               // Find another transition with the same destination and
00290               // acceptance conditions.
00291               state::iterator t2 = t1;
00292               ++t2;
00293               while (t2 != i->second->end())
00294                 {
00295                   state::iterator t2copy = t2++;
00296                   if ((*t2copy)->acceptance_conditions == acc
00297                       && (*t2copy)->dest == dest)
00298                     {
00299                       (*t1)->condition |= (*t2copy)->condition;
00300                       delete *t2copy;
00301                       i->second->erase(t2copy);
00302                     }
00303                 }
00304             }
00305         }
00306     }
00307 
00308 
00309     virtual
00310     ~tgba_explicit_labelled()
00311     {
00312     }
00313 
00314   };
00315 
00316 #ifndef SWIG
00317   class tgba_explicit_string:
00318     public tgba_explicit_labelled<std::string, string_hash>
00319   {
00320   public:
00321     tgba_explicit_string(bdd_dict* dict):
00322       tgba_explicit_labelled<std::string, string_hash>(dict)
00323     {};
00324     virtual ~tgba_explicit_string();
00325     virtual state* add_default_init();
00326     virtual std::string format_state(const spot::state* s) const;
00327 
00330     virtual
00331     void add_state_alias(const std::string& alias_name,
00332                          const std::string& real_name)
00333     {
00334       name_state_map_[alias_name] = add_state(real_name);
00335     }
00336   };
00337 #else
00338   class tgba_explicit_string: public tgba
00339   {
00340   };
00341 #endif
00342 
00343 #ifndef SWIG
00344   class tgba_explicit_formula:
00345     public tgba_explicit_labelled<const ltl::formula*, ltl::formula_ptr_hash>
00346   {
00347   public:
00348     tgba_explicit_formula(bdd_dict* dict):
00349       tgba_explicit_labelled<const ltl::formula*, ltl::formula_ptr_hash>(dict)
00350     {};
00351     virtual ~tgba_explicit_formula();
00352     virtual state* add_default_init();
00353     virtual std::string format_state(const spot::state* s) const;
00354   };
00355 #else
00356   class tgba_explicit_formula: public tgba
00357   {
00358   };
00359 #endif
00360 
00361 #ifndef SWIG
00362   class tgba_explicit_number:
00363     public tgba_explicit_labelled<int, identity_hash<int> >
00364   {
00365   public:
00366     tgba_explicit_number(bdd_dict* dict):
00367       tgba_explicit_labelled<int, identity_hash<int> >(dict)
00368     {};
00369     virtual ~tgba_explicit_number();
00370     virtual state* add_default_init();
00371     virtual std::string format_state(const spot::state* s) const;
00372   };
00373 #else
00374   class tgba_explicit_number: public tgba
00375   {
00376   };
00377 #endif
00378 }
00379 
00380 #endif // SPOT_TGBA_TGBAEXPLICIT_HH

Please comment this page and report errors about it on the RefDocComments page.
Generated on Mon Feb 7 2011 14:29:29 for spot by doxygen 1.7.1