Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

state.H

Go to the documentation of this file.
00001 /*
00002  * Copyright 1997, Brown University, Providence, RI.
00003  * 
00004  *                         All Rights Reserved
00005  * 
00006  * Permission to use, copy, modify, and distribute this software and its
00007  * documentation for any purpose other than its incorporation into a
00008  * commercial product is hereby granted without fee, provided that the
00009  * above copyright notice appear in all copies and that both that
00010  * copyright notice and this permission notice appear in supporting
00011  * documentation, and that the name of Brown University not be used in
00012  * advertising or publicity pertaining to distribution of the software
00013  * without specific, written prior permission.
00014  * 
00015  * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00016  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
00017  * PARTICULAR PURPOSE.  IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR
00018  * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00019  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00020  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00021  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00022  */
00023 #ifndef GLUE_STATE_H
00024 #define GLUE_STATE_H
00025 
00026 #include "std/ref.H"
00027 #include "std/list.H"
00028 #include "std/strings.H"
00029 #include "fsa/arc.H"
00030 
00031 //
00032 //
00033 //  STATE
00034 //     is a STATE in an FSA.  The STATE may contain multiple
00035 //  Arcs.  Only one Arc out of a STATE will be traversed.
00036 //  When an Event is passed to a state, each Arc will be
00037 //  matched to the Event.  The first Arc that matches the 
00038 //  Event is traversed.
00039 //
00040 //
00041 typedef const class STATE cSTATE;
00042 class STATE {
00043    protected :
00044      ARRAY<ARC *>  _arcs;
00045      str_ptr       _name;
00046 
00047    public : 
00048                      STATE()                     { }
00049                      STATE(Cstr_ptr &n):_name(n) { }
00050      int             is_empty()                  { return !_arcs.num(); }
00051      void            operator +=(cSTATE &s) {
00052                         for (int i = 0; i < s._arcs.num(); i++) {
00053                            int j = 0;
00054                            for (; j < _arcs.num(); j++)
00055                               if (*_arcs[j] == *s._arcs[i]) {
00056                                  *_arcs[j] = *s._arcs[i]; 
00057                                  break;
00058                               }
00059                            if (j == _arcs.num())
00060                               if (!s._arcs[i]->guarded())
00061                                  _arcs += s._arcs[i]; 
00062                               else {
00063                                  // arcs w/guards come first for priority
00064                                  // over more general arcs
00065                                  _arcs.push(s._arcs[i]); 
00066                               }
00067                         }
00068                      }
00069      void            operator -=(cSTATE  &s) {
00070                         for (int i = 0; i < s._arcs.num(); i++) {
00071                            int j = _arcs.num() - 1;
00072                            for (; j >= 0; j--)
00073                               if (*_arcs[j] == *s._arcs[i]) {
00074                                  _arcs.remove(j);
00075                                  break;
00076                               }
00077                         }
00078                      }
00079      void            operator -=(ARC *a) { _arcs -= a; }
00080      void            operator +=(ARC *a) { 
00081                          if (a->guarded())
00082                               _arcs.push(a);    // first for priority
00083                          else _arcs += a; 
00084                      }
00085      int             consumes(cEVENTptr &e) const {
00086                         for (int i = 0; i < _arcs.num(); i++)
00087                             if (_arcs[i]->match(e))
00088                                return 1;
00089                         return 0;
00090                      }
00091      void            event(STATE *&next, cEVENTptr &e, STATE *start=0) const {
00092                         if (e->debug_on())
00093                            cerr << "\t-> State " << _name << endl;
00094                         next = (STATE *)this;
00095                         for (int i = 0; i < _arcs.num(); i++) {
00096                            if (_arcs[i]->match(e)) {
00097                               if (e->debug_on()) {
00098                                  cerr << "\t\tMATCH  "; 
00099                                  _arcs[i]->debug(cerr); 
00100                                  cerr << endl;
00101                               }
00102                               _arcs[i]->exec(next, e, start);
00103                               break;
00104                            } else if (e->debug_on()) {
00105                               cerr << "\t\t... "; 
00106                               _arcs[i]->debug(cerr); 
00107                               cerr << endl;
00108                            }
00109                         }
00110                      }
00111 
00112      void                 clear()      { _arcs.clear(); }
00113      const ARRAY<ARC *>  &arcs() const { return _arcs; }
00114            ARRAY<ARC *>  &arcs()       { return _arcs; }
00115 
00116      Cstr_ptr    &name()            const { return _name; }
00117      void         set_name(Cstr_ptr &n)   { _name = n; }
00118      int          operator == (cSTATE &) const
00119         { cerr << "XXX: Dummy STATE_t::operator== got called\n"; return 0; }
00120 };
00121 
00122 
00123 #endif

Generated on Mon Sep 15 16:25:57 2003 for gluebase by doxygen1.2.18