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

# /Users/fpy/titan-1-5-MBARI/Mers/Planning/Reactive/CCA/rpTransition.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 // rpTransition 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::CCAVariable;
    00030 using Mers::Models::CCA::CCATransition;
    00031 using Mers::Models::CCA::CCAAssignments;
    00032 using Mers::Models::CCA::CCAAssignment;
    00033 using Mers::Models::CCA::CCAClause;
    00034
    00036 // Constructors, Copy Constructors, & Destructors
    00038
    00039 rpTransition::rpTransition() : _controlConditions(false) {
    00040         _transition_p                           = NULL;
    00041         _from_rpState_p                         = NULL;
    00042         _to_rpState_p                           = NULL;
    00043         _stateConditions                        = rpStatePtrs();
    00044         _current_status                         = UNKNOWN;
    00045         _prior_status                           = UNKNOWN;
    00046 }
    00047 rpTransition::rpTransition(const CCATransition &_transition,
    00048                                                    rpState &from_rpState,
    00049                                                    rpState &to_rpState) : _controlConditions(false) {
    00050         if (from_rpState.rpAutomaton_p() == to_rpState.rpAutomaton_p()) {
    00051                 _transition_p                   = &_transition;
    00052                 _from_rpState_p                 = &from_rpState;
    00053                 _to_rpState_p                   = &to_rpState;
    00054                 _stateConditions                = rpStatePtrs();
    00055                 _current_status                 = UNKNOWN;
    00056                 _prior_status                   = UNKNOWN;
    00057         } else {
    00058                 _transition_p                   = NULL;
    00059                 _from_rpState_p                 = NULL;
    00060                 _to_rpState_p                   = NULL;
    00061                 _stateConditions                = rpStatePtrs();
    00062                 _current_status                 = UNKNOWN;
    00063                 _prior_status                   = UNKNOWN;
    00064         }
    00065 }
    00066 rpTransition::rpTransition(const rpTransition &other)
    00067 : _controlConditions(false) {
    00068         copy(other);
    00069 }
    00070 rpTransition::~rpTransition() {
    00071         destroy();
    00072 }
    00073 //------------------------------------------------------------------------------
    00074 // Public Function Members
    00075 //------------------------------------------------------------------------------
    00076 const CCAModel *const rpTransition::model_p() const {
    00077         if (_from_rpState_p != NULL && _to_rpState_p != NULL) {
    00078                 if (_from_rpState_p->model_p() == _to_rpState_p->model_p()) {
    00079                         return _from_rpState_p->model_p();
    00080                 }
    00081         }
    00082         return NULL;
    00083 }
    00084 const CCATransition *const rpTransition::transition_p() const {
    00085         return _transition_p;
    00086 }
    00087 const rpCCAutomata *const rpTransition::rpCCAutomata_p() const {
    00088         if (_from_rpState_p != NULL && _to_rpState_p != NULL) {
    00089                 if (_from_rpState_p->rpCCAutomata_p() == _to_rpState_p->rpCCAutomata_p()) {
    00090                         return _from_rpState_p->rpCCAutomata_p();
    00091                 }
    00092         }
    00093         return NULL;
    00094 }
    00095 const rpAutomaton *const rpTransition::rpAutomaton_p() const {
    00096         if (_from_rpState_p != NULL && _to_rpState_p != NULL) {
    00097                 if (_from_rpState_p->rpAutomaton_p() == _to_rpState_p->rpAutomaton_p()) {
    00098                         return _from_rpState_p->rpAutomaton_p();
    00099                 }
    00100         }
    00101         return NULL;
    00102 }
    00103 //------------------------------------------------------------------------------
    00104 rpState *const rpTransition::from_rpState_p() const {
    00105         return _from_rpState_p;
    00106 }
    00107 rpState *const rpTransition::to_rpState_p() const {
    00108         return _to_rpState_p;
    00109 }
    00110 const rpState *const rpTransition::unsatisfied_stateCondition_p() const {
    00111         for (unsigned int i = 0; i < _stateConditions.size(); i++) {
    00112                 if (_stateConditions[i]->rpAutomaton_p()->current_rpState_p() !=
    00113                         _stateConditions[i]) {
    00114                         return _stateConditions[i];
    00115                 }
    00116         }
    00117         return NULL;
    00118 }
    00119 const CCAAssignments *const rpTransition::controlConditions_p() const {
    00120         return &_controlConditions;
    00121 }
    00122 const rpTransition::statusType rpTransition::current_status() const {
    00123         return _current_status;
    00124 }
    00125 const rpTransition::statusType rpTransition::prior_status() const {
    00126         return _prior_status;
    00127 }
    00128 //------------------------------------------------------------------------------
    00129 void rpTransition::set_from_rpState_p(rpState &_rpState) {
    00130         _from_rpState_p = &_rpState;
    00131 }
    00132 void rpTransition::set_to_rpState_p(rpState &_rpState) {
    00133         _to_rpState_p = &_rpState;
    00134 }
    00135 void rpTransition::reset_conditions() {
    00136         unsigned int _stateConditions_index = 0;
    00137         unsigned int numb_of_state_conditions = 0;
    00138         if (_transition_p != NULL) {
    00139                 if (!_transition_p->guard_p()->empty()) {
    00140                         for (CCAClause::const_iterator clause_const_it =
    00141                                 _transition_p->guard_p()->begin();
    00142                         clause_const_it != _transition_p->guard_p()->end();
    00143                         clause_const_it++) {
    00144                                 if (clause_const_it->getVariable()->getVariableType() ==
    00145                                         CCAVariable::STATE && clause_const_it->getOperator() == CCAAssignment::POSITIVE) {
    00146                                         numb_of_state_conditions++;
    00147                                 }
    00148                         }
    00149                         _stateConditions        = rpStatePtrs(numb_of_state_conditions);
    00150                 } else {
    00151                         _stateConditions        = rpStatePtrs();
    00152                 }
    00153                 for (CCAClause::const_iterator clause_const_it =
    00154                         _transition_p->guard_p()->begin();
    00155                 clause_const_it !=
    00156                         _transition_p->guard_p()->end();
    00157                 clause_const_it++) {
    00158                         switch (clause_const_it->getVariable()->getVariableType()) {
    00159                         case CCAVariable::STATE:
    00160                                 if (clause_const_it->getOperator() != CCAAssignment::NEGATIVE) {
    00161                                         _stateConditions[_stateConditions_index] =
    00162                                                 rpCCAutomata_p()->
    00163                                                 rpState_p(*clause_const_it);
    00164                                         _stateConditions_index ++;
    00165                                 }
    00166                                 break;
    00167                         case CCAVariable::CONTROL:
    00168                                 if (clause_const_it->getOperator() != CCAAssignment::NEGATIVE) {
    00169                                         _controlConditions.insert_assignment(*clause_const_it);
    00170                                 }
    00171                                 break;
    00172                         default:
    00173                                 break;
    00174                         }
    00175                 }
    00176                 _stateConditions.sort();
    00177         }
    00178 }
    00179 bool rpTransition::update_status() {
    00180         unsigned int i;
    00181         if (_to_rpState_p->type() == CCAState::FAILURE)
    00182         {
    00183                 if (_current_status == DISALLOWED) {
    00184                         return false;
    00185                 }
    00186                 else
    00187                 {
    00188                         _current_status = DISALLOWED;
    00189                         return true;
    00190                 }
    00191         }
    00192         else if (_stateConditions.size() > 0)
    00193         {
    00194                 for (i = 0; i < _stateConditions.size(); i++)
    00195                 {
    00196                         if (_stateConditions[i]->reachability() != rpState::ALWAYS)
    00197                         {
    00198                                 if (_current_status == DISALLOWED)
    00199                                 {
    00200                                         return false;
    00201                                 }
    00202                                 else
    00203                                 {
    00204                                         _current_status = DISALLOWED;
    00205                                         return true;
    00206                                 }
    00207                         }
    00208                 }
    00209                 if (_current_status == ALLOWED)
    00210                 {
    00211                         return false;
    00212                 }
    00213                 else
    00214                 {
    00215                         _current_status = ALLOWED;
    00216                         return true;
    00217                 }
    00218         }
    00219         else if (_current_status == ALLOWED)
    00220         {
    00221                 return false;
    00222         }
    00223         else
    00224         {
    00225                 _current_status = ALLOWED;
    00226                 return true;
    00227         }
    00228 }
    00229 void rpTransition::set_current_status(const statusType &status) {
    00230         _current_status = status;
    00231 }
    00232 void rpTransition::set_prior_status(const statusType &status) {
    00233         _prior_status = status;
    00234 }
    00235 void rpTransition::clear() {
    00236         _transition_p                           = NULL;
    00237         _from_rpState_p                         = NULL;
    00238         _to_rpState_p                           = NULL;
    00239         _stateConditions.clear();
    00240         _controlConditions.clear();
    00241         _current_status                         = UNKNOWN;
    00242         _prior_status                           = UNKNOWN;
    00243 }
    00244 //------------------------------------------------------------------------------
    00245 // Public Operators
    00246 //------------------------------------------------------------------------------
    00247 const rpTransition &rpTransition::operator =(const rpTransition &other) {
    00248         if(this != &other) {
    00249                 destroy();
    00250                 copy(other);
    00251         }
    00252         return *this;
    00253 }
    00254 bool rpTransition::operator ==(const rpTransition &other) const {
    00255         if (_transition_p       == other._transition_p          &&
    00256                 _from_rpState_p == other._from_rpState_p        &&
    00257                 _to_rpState_p   == other._to_rpState_p          &&
    00258                 _current_status == other._current_status        &&
    00259                 _prior_status   == other._prior_status) {
    00260                 return true;
    00261         } else {
    00262                 return false;
    00263         }
    00264 }
    00265 bool rpTransition::operator !=(const rpTransition &other) const {
    00266         return !(*this == other);
    00267 }
    00268 std::ostream &operator <<(std::ostream &_ostream,
    00269                                                   const rpTransition &_rpTransition) {
    00270         _ostream << _rpTransition.from_rpState_p()->name();
    00271         _ostream << " -> ";
    00272         _ostream << _rpTransition.to_rpState_p()->name() << std::endl;
    00273         _ostream << "State Conditions: " << _rpTransition._stateConditions << std::endl;
    00274         _ostream << "Control Conditions: " << _rpTransition._controlConditions << std::endl;
    00275         _ostream << "Status: ";
    00276         switch (_rpTransition.current_status()) {
    00277         case (rpTransition::ALLOWED):
    00278                 _ostream << "ALLOWED";
    00279                 break;
    00280         case (rpTransition::DISALLOWED):
    00281                 _ostream << "DISALLOWED";
    00282                 break;
    00283         case (rpTransition::UNKNOWN):
    00284                 _ostream << "UNKNOWN";
    00285                 break;
    00286         default:
    00287                 _ostream << "Error!";
    00288                 break;
    00289         }
    00290         return _ostream;
    00291 }
    00292 //------------------------------------------------------------------------------
    00293 // Private Function Members
    00294 //------------------------------------------------------------------------------
    00295 void rpTransition::destroy() {
    00296         _transition_p   = NULL;
    00297         _from_rpState_p = NULL;
    00298         _to_rpState_p   = NULL;
    00299         _current_status = UNKNOWN;
    00300         _prior_status   = UNKNOWN;
    00301 }
    00302 void rpTransition::copy(const rpTransition &other) {
    00303         _transition_p   = other._transition_p;
    00304         _from_rpState_p = other._from_rpState_p;
    00305         _to_rpState_p   = other._to_rpState_p;
    00306         _current_status = other._current_status;
    00307         _prior_status   = other._prior_status;
    00308 }