00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00034
00035
00036
00037
00038
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
00064
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);
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