state_machine.h

Go to the documentation of this file.
00001 #ifndef STATE_MACHINE_CLASS
00002 #define STATE_MACHINE_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : state_machine                                                     *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
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   // informational functions...
00197 
00198   DEFINE_CLASS_NAME("transition_map");
00199 
00200   bool valid() const { return _valid; }
00202 
00206   int states() const;
00208 
00209   // validation functions...
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   // configuration functions...
00235 
00236   // NOTE: all of the functions below will fail if the transition_map has
00237   // already been validated previously and reconfigure() has not been called.
00238 
00239   // NOTE: a zero value for a state means that it is uninitialized.  thus, zero
00240   // is never allowed as a value for a state or a transition endpoint.  zero is
00241   // grudgingly allowed as a trigger value, although that interferes with the
00242   // state_machine object's update() method.
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   // transition functions...
00281 
00282   // NOTE: all of these actions will fail if the transition_map has not been
00283   // validated yet.
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     // allows the transition_map to process any timed transitions that may be
00302     // required for the state_machine "m".  the return value is true if such
00303     // a transition was found.
00304 
00305   bool reset(state_machine &m);
00306     // resets the state_machine to the starting state in the transition_map.
00307     // the update function is NOT invoked at the time of the reset.  true is
00308     // returned if the reset was successful; it would fail if the transition
00309     // map has not been validated yet.
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   // disallowed functions for now:
00341   transition_map(const transition_map &);
00342   transition_map &operator =(const transition_map &);
00343 };
00344 
00345 } //namespace.
00346 
00347 #endif
00348 
Generated on Sat Jan 28 04:22:24 2012 for hoople2 project by  doxygen 1.6.3