feisty meow concerns codebase  2.140
state_machine.h
Go to the documentation of this file.
1 #ifndef STATE_MACHINE_CLASS
2 #define STATE_MACHINE_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : state_machine *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 1992-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include <basis/contracts.h>
19 #include <timely/time_stamp.h>
20 
21 namespace processes {
22 
23 class state_machine_override_array;
24 class state_machine_state_array;
25 
27 
44 class state_machine : public virtual basis::root_object
45 {
46 public:
47  state_machine();
49 
50  state_machine(const state_machine &to_copy);
52 
53  virtual ~state_machine();
54 
55  DEFINE_CLASS_NAME("state_machine");
56 
57  state_machine &operator =(const state_machine &to_copy);
59 
60  virtual int update();
62 
79  int current() const { return _current; }
81 
83  int last() const { return _last; }
85 
86  int trigger() const { return _trig; }
88 
93 
94  bool simple() const { return _type == SIMPLE; }
96  bool ranged() const { return _type == RANGE; }
98  bool timed() const { return _type == TIMED; }
100 
101  void set_state(int new_current, int new_last, int trigger,
102  transition_types type);
104 
110  timely::time_stamp start() const;
112 
114  void set_name(const basis::astring &name);
116 
117  basis::astring get_name() const;
119 
120  void override_timing(int current, int next, int duration);
122 
127  int duration_override(int current, int next) const;
129 
134 private:
135  friend class transition_map;
137  int _current;
138  int _last;
139  int _trig;
140  transition_types _type;
141  timely::time_stamp *_start;
142  basis::astring *_name;
143  state_machine_override_array *_overrides;
144 
145  int duration_index(int current, int next) const;
147 
150 };
151 
153 
155 
190 class transition_map : public virtual basis::root_object
191 {
192 public:
193  transition_map();
194  virtual ~transition_map();
195 
196  // informational functions...
197 
198  DEFINE_CLASS_NAME("transition_map");
199 
200  bool valid() const { return _valid; }
202 
206  int states() const;
208 
209  // validation functions...
210 
211  enum outcomes {
212  OKAY = basis::common::OKAY,
213  DEFINE_OUTCOME(BAD_START, -49, "The start state has not been properly "
214  "specified"),
215  DEFINE_OUTCOME(OVERLAPPING_RANGES, -50, "The ranges overlap for two "
216  "transitions from a state"),
217  DEFINE_OUTCOME(UNREACHABLE, -51, "There is an unreachable state in the map")
218  };
219  basis::outcome validate(int &examine);
221 
229  void reconfigure();
231 
234  // configuration functions...
235 
236  // NOTE: all of the functions below will fail if the transition_map has
237  // already been validated previously and reconfigure() has not been called.
238 
239  // NOTE: a zero value for a state means that it is uninitialized. thus, zero
240  // is never allowed as a value for a state or a transition endpoint. zero is
241  // grudgingly allowed as a trigger value, although that interferes with the
242  // state_machine object's update() method.
243 
244  bool add_state(int state_number);
246 
250  bool set_start(int starting_state);
252 
255  bool add_simple_transition(int current, int next);
257 
263  bool add_range_transition(int current, int next, int low, int high);
265 
269  bool add_timed_transition(int current, int next, int duration);
271 
280  // transition functions...
281 
282  // NOTE: all of these actions will fail if the transition_map has not been
283  // validated yet.
284 
285  bool make_transition(state_machine &m, int next);
287 
291  bool pulse(state_machine &m, int trigger);
293 
300  bool time_slice(state_machine &m);
301  // allows the transition_map to process any timed transitions that may be
302  // required for the state_machine "m". the return value is true if such
303  // a transition was found.
304 
305  bool reset(state_machine &m);
306  // resets the state_machine to the starting state in the transition_map.
307  // the update function is NOT invoked at the time of the reset. true is
308  // returned if the reset was successful; it would fail if the transition
309  // map has not been validated yet.
310 
311 private:
312  bool _valid;
313  int _start_state;
314  state_machine_state_array *_state_list;
316 
317  bool check_overlapping(int &examine);
319 
322  bool check_reachability(int &examine);
324 
325  int state_index(int state_id) const;
327 
328  int transition_index(int state_index, int next, int &start);
330 
337  bool check_states();
339 
340  // disallowed functions for now:
342  transition_map &operator =(const transition_map &);
343 };
344 
345 } //namespace.
346 
347 #endif
348 
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
Outcomes describe the state of completion for an operation.
Definition: outcome.h:31
Monitors objects with multiple states and the transitions between states.
Definition: state_machine.h:45
void override_timing(int current, int next, int duration)
modifies the recorded timing for timed transitions.
virtual int update()
this is the main implementation function provided by derived classes.
int trigger() const
returns the trigger that caused this state.
Definition: state_machine.h:85
basis::astring get_name() const
retrieves the object's current name.
int current() const
returns the current state.
Definition: state_machine.h:79
void set_state(int new_current, int new_last, int trigger, transition_types type)
sets the current state to "new_current" and the previous state to "new_last".
timely::time_stamp start() const
start time for the current state.
bool timed() const
returns true if the last transition was a timed type.
Definition: state_machine.h:96
int last() const
returns the last active state.
Definition: state_machine.h:82
DEFINE_CLASS_NAME("state_machine")
bool simple() const
returns true if the last transition was a simple type.
Definition: state_machine.h:92
bool ranged() const
returns true if the last transition was a ranged type.
Definition: state_machine.h:94
state_machine()
sets up the machine in a blank state.
int duration_override(int current, int next) const
reports if there's a duration override for a timed transition.
void set_name(const basis::astring &name)
sets the name to be printed in any debugging information for "this".
state_machine & operator=(const state_machine &to_copy)
assigns this to a copy of the machine provided in "to_copy".
friend class transition_map
manager buddy object.
The transition_map manages state machines and causes state changes to occur.
bool add_range_transition(int current, int next, int low, int high)
adds a transition that listens to triggers in the pulse() method.
bool time_slice(state_machine &m)
bool set_start(int starting_state)
assigns a state as the first state.
bool add_simple_transition(int current, int next)
adds a transition between "current" and "next".
bool make_transition(state_machine &m, int next)
changes the state to the "next" listed for "m" given the current state.
bool pulse(state_machine &m, int trigger)
applies a "trigger" to possibly cause a range transition.
bool valid() const
returns true if the transition_map is valid and ready for operation.
bool reset(state_machine &m)
void reconfigure()
puts the transition_map back into an unvalidated state.
int states() const
returns the current number of states.
bool add_state(int state_number)
registers a legal state in the transition_map.
DEFINE_CLASS_NAME("transition_map")
basis::outcome validate(int &examine)
checks to that all required transition conditions are satisfied.
bool add_timed_transition(int current, int next, int duration)
adds a transition that occurs after a timeout with no other activity.
Represents a point in time relative to the operating system startup time.
Definition: time_stamp.h:38