- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Planning/Reactive/CCA/rpState.cpp

    00001 //==============================================================================
    00002 //
    00003 // Copyright (c) 2001           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Author:                      Seung H. Chung
    00010 // Organization:        Model-based Embedded and Robotic Systems Group
    00011 //                                      Massachusetts Institute of Technology
    00012 //
    00013 // rpState class
    00014 //
    00015 //==============================================================================
    00016
    00017 #include <cstdio>
    00018 #include <cassert>
    00019
    00020 #include "rpCCAutomata.h"
    00021 #include "rpAutomaton.h"
    00022 #include "rpTransition.h"
    00023 #include "rpState.h"
    00024
    00026
    00027 using Mers::Models::CCA::CCAState;
    00028 using Mers::Models::CCA::CCAModel;
    00029 using Mers::Models::CCA::CCAValue;
    00030 using Mers::Utils::Lang::String;
    00031
    00033 // Constructors, Copy Constructors, & Destructors
    00035
    00036 rpState::rpState() {
    00037         _rpAutomaton_p                  = NULL;
    00038         _state_p                                = NULL;
    00039         _in_rpTransitionPtrs    = rpTransitionPtrs();
    00040         _out_rpTransitionPtrs   = rpTransitionPtrs();
    00041         _index                                  = 0;
    00042         _reachability                   = UNKNOWN;
    00043         _SCC_root_p                             = NULL;
    00044         _lowlink_numb                   = 0;
    00045         _depth_first_numb               = 0;
    00046         _predecessor_p                  = NULL;
    00047         _marked                                 = false;
    00048 }
    00049 rpState::rpState(const CCAState &_state, const indexType index_) {
    00050         _rpAutomaton_p                  = NULL;
    00051         _state_p                                = &_state;
    00052         _in_rpTransitionPtrs    = rpTransitionPtrs(_state.in_transitionPtrs_p()->
    00053                                                                                            getLength());
    00054         _out_rpTransitionPtrs   = rpTransitionPtrs(_state.out_transitionPtrs_p()->
    00055                                                                                            getLength());
    00056         _index                                  = index_;
    00057         _reachability                   = UNKNOWN;
    00058         _SCC_root_p                             = NULL;
    00059         _lowlink_numb                   = 0;
    00060         _depth_first_numb               = 0;
    00061         _predecessor_p                  = NULL;
    00062         _marked                                 = false;
    00063 }
    00064 rpState::rpState(const rpState &other) {
    00065         copy(other);
    00066 }
    00067 rpState::~rpState() {
    00068         destroy();
    00069 }
    00070 //------------------------------------------------------------------------------
    00071 // Public Function Members
    00072 //------------------------------------------------------------------------------
    00073 const CCAModel *const rpState::model_p() const {
    00074         if (_rpAutomaton_p != NULL) {
    00075                 return _rpAutomaton_p->model_p();
    00076         } else {
    00077                 return NULL;
    00078         }
    00079 }
    00080 const rpCCAutomata *const rpState::rpCCAutomata_p() const {
    00081         if (_rpAutomaton_p != NULL) {
    00082                 return _rpAutomaton_p->rpCCAutomata_p();
    00083         } else {
    00084                 return NULL;
    00085         }
    00086 }
    00087 const rpAutomaton *const rpState::rpAutomaton_p() const {
    00088         return _rpAutomaton_p;
    00089 }
    00090 //------------------------------------------------------------------------------
    00091 const String rpState::name() const {
    00092         if (_state_p != NULL) {
    00093                 return _state_p->name();
    00094         } else {
    00095                 return "";
    00096         }
    00097 }
    00098 const CCAValue *const rpState::value_p() const {
    00099         if (_state_p != NULL) {
    00100                 return _state_p->value_p();
    00101         } else {
    00102                 return NULL;
    00103         }
    00104 }
    00105 const unsigned int rpState::index() const {
    00106         return _index;
    00107 }
    00108 const CCAState::StateType rpState::type() const {
    00109         if (_state_p != NULL) {
    00110                 return _state_p->type();
    00111         } else {
    00112                 return CCAState::UNKNOWN;
    00113         }
    00114 }
    00115 const rpTransitionPtrs * rpState::in_rpTransitionPtrs_p()       const {
    00116         return &_in_rpTransitionPtrs;
    00117 }
    00118 const rpTransitionPtrs * rpState::out_rpTransitionPtrs_p() const {
    00119         return &_out_rpTransitionPtrs;
    00120 }
    00121 const rpTransition * rpState::in_rpTransitionPtrs(const unsigned int i) const {
    00122         if (i < _in_rpTransitionPtrs.size()) {
    00123                 return _in_rpTransitionPtrs[i];
    00124         } else {
    00125                 return NULL;
    00126         }
    00127 }
    00128 const rpTransition * rpState::out_rpTransitionPtrs(const unsigned int i) const {
    00129         if (i < _out_rpTransitionPtrs.size()) {
    00130                 return _out_rpTransitionPtrs[i];
    00131         } else {
    00132                 return NULL;
    00133         }
    00134 }
    00135 const rpState::reachabilityType rpState::reachability() const {
    00136         return _reachability;
    00137 }
    00138 const rpState *const rpState::SCC_root_p() const {
    00139         return _SCC_root_p;
    00140 }
    00141 const rpState *const rpState::predecessor_p() const {
    00142         return _predecessor_p;
    00143 }
    00144 const unsigned int rpState::lowlink_numb() const {
    00145         return _lowlink_numb;
    00146 }
    00147 const unsigned int rpState::depth_first_numb() const {
    00148         return _depth_first_numb;
    00149 }
    00150 const bool rpState::marked() const {
    00151         return _marked;
    00152 }
    00153 //------------------------------------------------------------------------------
    00154 void rpState::set_rpAutomaton_p(const rpAutomaton &_rpAutomaton) {
    00155         _rpAutomaton_p = &_rpAutomaton;
    00156 }
    00157 const bool rpState::insert_in_rpTransition(const rpTransition &_rpTransition) {
    00158         if (_rpTransition.to_rpState_p() == this &&
    00159                 _in_rpTransitionPtrs.size() > 0) {
    00160                 for (unsigned int i = 0; i < _in_rpTransitionPtrs.size(); i++) {
    00161                         if (_in_rpTransitionPtrs[i] == NULL) {
    00162                                 _in_rpTransitionPtrs[i] = &_rpTransition;
    00163                                 return true;
    00164                         }
    00165                 }
    00166                 return false;
    00167         } else {
    00168                 return false;
    00169         }
    00170 }
    00171 const bool rpState::insert_out_rpTransition(const rpTransition &_rpTransition) {
    00172         if (_rpTransition.from_rpState_p() == this &&
    00173                 _out_rpTransitionPtrs.size() > 0) {
    00174                 for (unsigned int i = 0; i < _out_rpTransitionPtrs.size(); i++) {
    00175                         if (_out_rpTransitionPtrs[i] == NULL) {
    00176                                 _out_rpTransitionPtrs[i] = &_rpTransition;
    00177                                 return true;
    00178                         }
    00179                 }
    00180                 return false;
    00181         } else {
    00182                 return false;
    00183         }
    00184 }
    00185 void rpState::clear_in_rpTransitionPtrs() {
    00186         _in_rpTransitionPtrs.clear();
    00187 }
    00188 void rpState::clear_out_rpTransitionPtrs() {
    00189         _out_rpTransitionPtrs.clear();
    00190 }
    00191 void rpState::set_reachability(const reachabilityType &reachability_) {
    00192         _reachability = reachability_;
    00193 }
    00194 void rpState::set_SCC_root(const rpState &_rpState) {
    00195         _SCC_root_p = &_rpState;
    00196 }
    00197 void rpState::reset_SCC_root() {
    00198         _SCC_root_p = NULL;
    00199 }
    00200 void rpState::set_lowlink_numb(const unsigned int numb) {
    00201         _lowlink_numb = numb;
    00202 }
    00203 void rpState::set_depth_first_numb(const unsigned int numb) {
    00204         _depth_first_numb = numb;
    00205 }
    00206 void rpState::set_predecessor(const rpState &_rpState) {
    00207         _predecessor_p = &_rpState;
    00208 }
    00209 void rpState::reset_predecessor() {
    00210         _predecessor_p = NULL;
    00211 }
    00212 void rpState::mark() {
    00213         _marked = true;
    00214 }
    00215 void rpState::unmark() {
    00216         _marked = false;
    00217 }
    00218 void rpState::clear() {
    00219         _rpAutomaton_p                          = NULL;
    00220         _state_p                                        = NULL;
    00221         _in_rpTransitionPtrs.clear();
    00222         _out_rpTransitionPtrs.clear();
    00223         _index                                          = 0;
    00224         _reachability                           = UNKNOWN;
    00225         _SCC_root_p                                     = NULL;
    00226         _lowlink_numb                           = 0;
    00227         _depth_first_numb                       = 0;
    00228         _predecessor_p                          = NULL;
    00229         _marked                                         = false;
    00230 }
    00231 //------------------------------------------------------------------------------
    00232 // Public Operators
    00233 //------------------------------------------------------------------------------
    00234 const rpState &rpState::operator =(const rpState &other) {
    00235         if(this != &other) {
    00236                 destroy();
    00237                 copy(other);
    00238         }
    00239         return *this;
    00240 }
    00241 const bool rpState::operator ==(const rpState &other) const {
    00242         if (
    00243                 //_rpAutomaton_p                                == other._rpAutomaton_p                 &&
    00244                 _state_p                                == other._state_p                               &&
    00245                 _in_rpTransitionPtrs    == other._in_rpTransitionPtrs   &&
    00246                 _out_rpTransitionPtrs   == other._out_rpTransitionPtrs  &&
    00247                 _reachability                   == other._reachability                  &&
    00248                 _SCC_root_p                             == other._SCC_root_p                    &&
    00249                 _lowlink_numb                   == other._lowlink_numb                  &&
    00250                 _depth_first_numb               == other._depth_first_numb              &&
    00251                 _predecessor_p                  == other._predecessor_p                 &&
    00252                 _marked                                 == other._marked) {
    00253                 return true;
    00254         } else {
    00255                 return false;
    00256         }
    00257 }
    00258 const bool rpState::operator !=(const rpState &other) const {
    00259         return !(*this == other);
    00260 }
    00261 const bool rpState::operator >(const rpState &other) const {
    00262         if (*_rpAutomaton_p > *other.rpAutomaton_p()) {
    00263                 return true;
    00264         } else if (*_rpAutomaton_p < *other.rpAutomaton_p()) {
    00265                 return false;
    00266         } else if (_depth_first_numb > other.depth_first_numb()) {
    00267                 return true;
    00268         } else {
    00269                 return false;
    00270         }
    00271 }
    00272 const bool rpState::operator <(const rpState &other) const {
    00273         if (*_rpAutomaton_p < *other.rpAutomaton_p()) {
    00274                 return true;
    00275         } else if (*_rpAutomaton_p > *other.rpAutomaton_p()) {
    00276                 return false;
    00277         } else if (_depth_first_numb < other.depth_first_numb()) {
    00278                 return true;
    00279         } else {
    00280                 return false;
    00281         }
    00282 }
    00283 const bool rpState::operator >=(const rpState &other) const {
    00284         if (*_rpAutomaton_p > *other.rpAutomaton_p()) {
    00285                 return true;
    00286         } else if (*_rpAutomaton_p < *other.rpAutomaton_p()) {
    00287                 return false;
    00288         } else if (_depth_first_numb >= other.depth_first_numb()) {
    00289                 return true;
    00290         } else {
    00291                 return false;
    00292         }
    00293 }
    00294 const bool rpState::operator <=(const rpState &other) const {
    00295         if (*_rpAutomaton_p < *other.rpAutomaton_p()) {
    00296                 return true;
    00297         } else if (*_rpAutomaton_p > *other.rpAutomaton_p()) {
    00298                 return false;
    00299         } else if (_depth_first_numb <= other.depth_first_numb()) {
    00300                 return true;
    00301         } else {
    00302                 return false;
    00303         }
    00304 }
    00305 std::ostream &operator <<(std::ostream &_ostream, const rpState &_rpState) {
    00306         _ostream << _rpState.name() << " [";
    00307         switch (_rpState.type()) {
    00308         case CCAState::NOMINAL:
    00309                 _ostream << "nominal";
    00310                 break;
    00311         case CCAState::FAILURE:
    00312                 _ostream << "failure";
    00313                 break;
    00314         case CCAState::UNKNOWN:
    00315                 _ostream << "unknown";
    00316                 break;
    00317         default:
    00318                 break;
    00319         }
    00320         _ostream << "]: index(" << _rpState.index() << ")" << std::endl;
    00321         _ostream << "Reachability: ";
    00322         switch (_rpState.reachability()) {
    00323         case rpState::ALWAYS:
    00324                 _ostream << "ALWAYS";
    00325                 break;
    00326         case rpState::INITIALLY:
    00327                 _ostream << "INITIALLY";
    00328                 break;
    00329         case rpState::NEVER:
    00330                 _ostream << "NEVER";
    00331                 break;
    00332         case rpState::UNKNOWN:
    00333                 _ostream << "UNKNOWN";
    00334                 break;
    00335         default:
    00336                 _ostream << "Error!";
    00337                 break;
    00338         }
    00339         return _ostream;
    00340 }
    00341 //------------------------------------------------------------------------------
    00342 // Private Function Members
    00343 //------------------------------------------------------------------------------
    00344 void rpState::destroy() {
    00345         _rpAutomaton_p                  = NULL;
    00346         _state_p                                = NULL;
    00347         _in_rpTransitionPtrs.clear();
    00348         _out_rpTransitionPtrs.clear();
    00349         _index                                  = 0;
    00350         _reachability                   = UNKNOWN;
    00351         _SCC_root_p                             = NULL;
    00352         _lowlink_numb                   = 0;
    00353         _depth_first_numb               = 0;
    00354         _predecessor_p                  = NULL;
    00355         _marked                                 = false;
    00356 }
    00357 void rpState::copy(const rpState &other) {
    00358         _rpAutomaton_p                  = other._rpAutomaton_p;
    00359         _state_p                                = other._state_p;
    00360         _in_rpTransitionPtrs    = other._in_rpTransitionPtrs;
    00361         _out_rpTransitionPtrs   = other._out_rpTransitionPtrs;
    00362         _index                                  = other._index;
    00363         _reachability                   = other._reachability;
    00364         _SCC_root_p                             = other._SCC_root_p;
    00365         _lowlink_numb                   = other._lowlink_numb;
    00366         _depth_first_numb               = other._depth_first_numb;
    00367         _predecessor_p                  = other._predecessor_p;
    00368         _marked                                 = other._marked;
    00369 }