state_machine.h
Go to the documentation of this file.00001 #ifndef STATE_MACHINE_CLASS
00002 #define STATE_MACHINE_CLASS
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <basis/contracts.h>
00019 #include <timely/time_stamp.h>
00020
00021 namespace processes {
00022
00023 class state_machine_override_array;
00024 class state_machine_state_array;
00025
00027
00044 class state_machine : public virtual basis::root_object
00045 {
00046 public:
00047 state_machine();
00049
00050 state_machine(const state_machine &to_copy);
00052
00053 virtual ~state_machine();
00054
00055 DEFINE_CLASS_NAME("state_machine");
00056
00057 state_machine &operator =(const state_machine &to_copy);
00059
00060 virtual int update();
00062
00079 int current() const { return _current; }
00081
00083 int last() const { return _last; }
00085
00086 int trigger() const { return _trig; }
00088
00091 enum transition_types { SIMPLE, RANGE, TIMED };
00093
00094 bool simple() const { return _type == SIMPLE; }
00096 bool ranged() const { return _type == RANGE; }
00098 bool timed() const { return _type == TIMED; }
00100
00101 void set_state(int new_current, int new_last, int trigger,
00102 transition_types type);
00104
00110 timely::time_stamp start() const;
00112
00114 void set_name(const basis::astring &name);
00116
00117 basis::astring get_name() const;
00119
00120 void override_timing(int current, int next, int duration);
00122
00127 int duration_override(int current, int next) const;
00129
00134 private:
00135 friend class transition_map;
00137 int _current;
00138 int _last;
00139 int _trig;
00140 transition_types _type;
00141 timely::time_stamp *_start;
00142 basis::astring *_name;
00143 state_machine_override_array *_overrides;
00144
00145 int duration_index(int current, int next) const;
00147
00150 };
00151
00153
00155
00190 class transition_map : public virtual basis::root_object
00191 {
00192 public:
00193 transition_map();
00194 virtual ~transition_map();
00195
00196
00197
00198 DEFINE_CLASS_NAME("transition_map");
00199
00200 bool valid() const { return _valid; }
00202
00206 int states() const;
00208
00209
00210
00211 enum outcomes {
00212 OKAY = basis::common::OKAY,
00213 DEFINE_OUTCOME(BAD_START, -49, "The start state has not been properly "
00214 "specified"),
00215 DEFINE_OUTCOME(OVERLAPPING_RANGES, -50, "The ranges overlap for two "
00216 "transitions from a state"),
00217 DEFINE_OUTCOME(UNREACHABLE, -51, "There is an unreachable state in the map")
00218 };
00219 basis::outcome validate(int &examine);
00221
00229 void reconfigure();
00231
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 bool add_state(int state_number);
00246
00250 bool set_start(int starting_state);
00252
00255 bool add_simple_transition(int current, int next);
00257
00263 bool add_range_transition(int current, int next, int low, int high);
00265
00269 bool add_timed_transition(int current, int next, int duration);
00271
00280
00281
00282
00283
00284
00285 bool make_transition(state_machine &m, int next);
00287
00291 bool pulse(state_machine &m, int trigger);
00293
00300 bool time_slice(state_machine &m);
00301
00302
00303
00304
00305 bool reset(state_machine &m);
00306
00307
00308
00309
00310
00311 private:
00312 bool _valid;
00313 int _start_state;
00314 state_machine_state_array *_state_list;
00316
00317 bool check_overlapping(int &examine);
00319
00322 bool check_reachability(int &examine);
00324
00325 int state_index(int state_id) const;
00327
00328 int transition_index(int state_index, int next, int &start);
00330
00337 bool check_states();
00339
00340
00341 transition_map(const transition_map &);
00342 transition_map &operator =(const transition_map &);
00343 };
00344
00345 }
00346
00347 #endif
00348