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

# /Users/fpy/titan-1-5-MBARI/Mers/Models/CCA/CCAState.cpp

    00001
    00002 //
    00003 // Copyright (c) 2004           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 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #include "CCAState.h"
    00015
    00016 #include <cstdio>
    00017 #include <cassert>
    00018
    00019 #include "CCAAutomaton.h"
    00020 #include "CCAAutomata.h"
    00021 #include "CCAModel.h"
    00022
    00024
    00025 using namespace Mers::Models::CCA;
    00026 using std::ostream;
    00027
    00029 // Constructors, Copy Constructors, & Destructors
    00031
    00032 CCAState::CCAState()
    00033         : BaseModelObject(BaseModelObject::oCCAState), _constraint(CCASentence::CNF),
    00034         _in_transitionPtrs(false), _out_transitionPtrs(false)
    00035 {
    00036         parentAutomaton         = NULL;
    00037         _value_p                = NULL;
    00038         _type                   = UNKNOWN;
    00039 }
    00040
    00041 CCAState::CCAState(const CCAAutomaton &_automaton, const CCAValue &_value)
    00042 : BaseModelObject(BaseModelObject::oCCAState), _constraint(CCASentence::CNF),
    00043         _in_transitionPtrs(false), _out_transitionPtrs(false)
    00044 {
    00045         parentAutomaton = &_automaton;
    00046         if (&_value == _automaton.variable_p()->getValue(_value.getName())) {
    00047                 _value_p = &_value;
    00048         } else {
    00049                 _value_p = NULL;
    00050         }
    00051         _type = UNKNOWN;
    00052 }
    00053
    00054 CCAState::CCAState(const CCAState &other)
    00055         : BaseModelObject(other), _constraint(true),
    00056         _in_transitionPtrs(false), _out_transitionPtrs(false)
    00057 {
    00058         copy(other);
    00059 }
    00060
    00061 CCAState::~CCAState() {
    00062         destroy();
    00063 }
    00064
    00066 // Public Function Members
    00068
    00070
    00071 char const * CCAState::stateTypeToString(CCAState::StateType type)
    00072 {
    00073   switch (type) {
    00074         case NOMINAL:
    00075                 return "Nominal";
    00076         case FAILURE:
    00077                 return "Failure";
    00078         case UNKNOWN:
    00079                 return "Unknown";
    00080         default:
    00081                 return "Invalid State Type";
    00082         }
    00083 }
    00084
    00086
    00087 int CCAState::stringToStateType(char const * str, StateType &type)
    00088 {
    00089         struct{
    00090                 char const * str;
    00091                 StateType type;
    00092         } const charMap[8] =
    00093                 {
    00094                         {"NOMINAL", NOMINAL},
    00095                         {"Nominal", NOMINAL},
    00096                         {"FAULT", FAILURE},
    00097                         {"Fault", FAILURE},
    00098                         {"FAILURE", FAILURE},
    00099                         {"Failure", FAILURE},
    00100                         {"UNKNOWN", UNKNOWN},
    00101                         {"Unknown", UNKNOWN}
    00102                 };
    00103
    00104         for(unsigned int x = 0; x < sizeof(charMap) / sizeof(charMap[0]); x++){
    00105                 if(strcmp(charMap[x].str, str) == 0){
    00106                         type = charMap[x].type;
    00107                         return 0;
    00108                 }
    00109         }
    00110         return 1;
    00111 }
    00112
    00114
    00115 bool CCAState::set_automaton_p(const CCAAutomaton &_automaton)
    00116 {
    00117         parentAutomaton = &_automaton;
    00118         return true;
    00119 }
    00120
    00121 const CCAAutomaton * CCAState::automaton_p() const
    00122 {
    00123         return parentAutomaton;
    00124 }
    00125
    00126 const CCAAutomata * CCAState::ccAutomata_p() const
    00127 {
    00128         if (parentAutomaton != NULL) {
    00129                 return parentAutomaton->ccAutomata_p();
    00130         } else {
    00131                 return NULL;
    00132         }
    00133 }
    00134
    00135 const CCAModel * CCAState::model_p() const
    00136 {
    00137         if (parentAutomaton != NULL) {
    00138                 return parentAutomaton->model_p();
    00139         } else {
    00140                 return NULL;
    00141         }
    00142 }
    00143
    00144 char const * CCAState::name() const
    00145 {
    00146         return _value_p->getName();
    00147 }
    00148
    00149 char const * CCAState::getName() const
    00150 {
    00151         return _value_p->getName();
    00152 }
    00153
    00154 const CCAValue * CCAState::value_p() const
    00155 {
    00156         return _value_p;
    00157 }
    00158
    00159 const CCAValue * CCAState::getValue() const
    00160 {
    00161         return _value_p;
    00162 }
    00163
    00164 bool CCAState::set_type(const StateType new_type)
    00165 {
    00166         _type = new_type;
    00167         return true;
    00168 }
    00169
    00170 CCAState::StateType CCAState::type() const
    00171 {
    00172         return _type;
    00173 }
    00174
    00175 CCAState::StateType CCAState::getStateType() const
    00176 {
    00177         return _type;
    00178 }
    00179
    00180 bool CCAState::set_constraint(const CCASentence &new_constraint)
    00181 {
    00182         _constraint.makeEmpty();
    00183         _constraint = new_constraint;
    00184         return true;
    00185 }
    00186
    00187 const CCASentence * CCAState::constraint_p() const
    00188 {
    00189         return &_constraint;
    00190 }
    00191
    00192 CCASentence * CCAState::constraint_p()
    00193 {
    00194         return &_constraint;
    00195 }
    00196
    00198
    00199 void CCAState::setUID(unsigned int newUID)
    00200 {
    00201         uid = newUID;
    00202 }
    00203
    00205
    00206 unsigned int CCAState::getUID() const
    00207 {
    00208         return uid;
    00209 }
    00210
    00212
    00213 CCATransitions::const_iterator
    00214 CCAState::insert_in_transition(CCATransition * _transition)
    00215 {
    00216         if (_transition->to_state_p() == this) {
    00217                 return _in_transitionPtrs.insert_transition_p(_transition);
    00218         } else {
    00219                 return _in_transitionPtrs.end();
    00220         }
    00221 }
    00222
    00223 bool CCAState::clear_in_transitionPtrs()
    00224 {
    00225         _in_transitionPtrs.makeEmpty();
    00226         return true;
    00227 }
    00228
    00229 const CCATransitions * CCAState::in_transitionPtrs_p()  const
    00230 {
    00231         return &_in_transitionPtrs;
    00232 }
    00233
    00234 CCATransitions::const_iterator
    00235 CCAState::insert_out_transition(CCATransition * _transition)
    00236 {
    00237         if (_transition->from_state_p() == this) {
    00238                 return _out_transitionPtrs.insert_transition_p(_transition);
    00239         } else {
    00240                 return _out_transitionPtrs.end();
    00241         }
    00242 }
    00243
    00244 bool CCAState::clear_out_transitionPtrs()
    00245 {
    00246         _out_transitionPtrs.makeEmpty();
    00247         return true;
    00248 }
    00249
    00250 const CCATransitions * CCAState::out_transitionPtrs_p() const
    00251 {
    00252         return &_out_transitionPtrs;
    00253 }
    00254
    00255 #ifndef EMBEDDED
    00256
    00257 void CCAState::print(ostream &out) const
    00258 {
    00259         out << name() << "[" << stateTypeToString(type())
    00260                 << "] => " << *constraint_p();
    00261 }
    00262
    00263 #endif
    00264
    00266 // Public Operators
    00268
    00269 const CCAState &CCAState::operator =(const CCAState &other)
    00270 {
    00271         if(this != &other) {
    00272                 destroy();
    00273                 copy(other);
    00274         }
    00275         return *this;
    00276 }
    00277
    00278 bool CCAState::operator ==(const CCAState &other) const
    00279 {
    00280         return
    00281                 (parentAutomaton        == other.parentAutomaton        &&
    00282                 _value_p                        == other._value_p                               &&
    00283                 _constraint                     == other._constraint                    &&
    00284                 _in_transitionPtrs      == other._in_transitionPtrs     &&
    00285                 _out_transitionPtrs     == other._out_transitionPtrs);
    00286 }
    00287
    00288 bool CCAState::operator !=(const CCAState &other) const
    00289 {
    00290         return !operator ==(other);
    00291 }
    00292
    00294 // Private Function Members
    00296
    00297 void CCAState::destroy()
    00298 {
    00299         parentAutomaton = NULL;
    00300         _value_p            = NULL;
    00301         _type                 = UNKNOWN;
    00302         _constraint.clear();
    00303         _in_transitionPtrs.makeEmpty();
    00304         _out_transitionPtrs.makeEmpty();
    00305 }
    00306
    00307 void CCAState::copy(CCAState const &other)
    00308 {
    00309         unsigned int x;
    00310
    00311         parentAutomaton       = other.parentAutomaton;
    00312         _value_p              = other._value_p;
    00313         _type                 = other._type;
    00314         _constraint           = other._constraint;
    00315         // we want to copy the transitions w/o duplicating them
    00316         if(FAILED(_in_transitionPtrs.resize(other._in_transitionPtrs.getLength())))
    00317                 return;
    00318         for(x = 0; x < other._in_transitionPtrs.getLength(); x++)
    00319                 _in_transitionPtrs[x] = (CCATransition *) other._in_transitionPtrs[x];
    00320
    00321         if(FAILED(_out_transitionPtrs.resize(other._out_transitionPtrs.getLength())))
    00322                 return;
    00323         for(x = 0; x < other._out_transitionPtrs.getLength(); x++)
    00324                 _out_transitionPtrs[x] = (CCATransition *) other._out_transitionPtrs[x];
    00325 }
    00326
    00328