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

# /Users/fpy/titan-1-5-MBARI/Mers/Estimation/Discrete/CompiledTransition.cpp

    00001 //==============================================================================
    00002 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00003 // All Rights Reserved          Massachusetts Institute of Technology
    00004 //
    00005 // This software is provided as is. Model-based Embedded and Robotic Systems
    00006 // (MERS) group does not assume any responsibility.
    00007 //==============================================================================
    00008
    00009 #include "CompiledTransition.h"
    00010
    00011 #ifdef _MSC_VER
    00012 #pragma warning( disable : 4786 )
    00013 #endif
    00014
    00015 #include <Mers/Models/Base/BaseModelVariable.h>
    00016 #include <Mers/Models/Base/BaseModelAssignment.h>
    00017 #include <Mers/Models/Base/BaseModelAssignmentArray.h>
    00018
    00019 using namespace Mers::Estimation::Discrete;
    00020 using Mers::Models::Base::BaseModelVariable;
    00021 using Mers::Models::Base::BaseModelAssignment;
    00022 using Mers::Models::Base::BaseModelAssignmentArray;
    00023 using std::ostream;
    00024 using std::cerr;
    00025 using std::endl;
    00026
    00027         //-----------------------------------------------
    00028         //      CONSTRUCTOR
    00029         //-----------------------------------------------
    00030
    00031
    00032 CompiledTransition::CompiledTransition(const BaseModelAssignment * sourceAssign,
    00033                 const BaseModelAssignmentArray * transGuards, const BaseModelAssignment * targetAssign,
    00034                 const double prob)
    00035 : sourceMode(sourceAssign), guards(transGuards), targetMode(targetAssign),
    00036         transitionProbability(prob), debug(0)
    00037 {
    00038         // Check to make sure all assigments have the correct variable types.
    00039         BaseModelAssignmentArray::ConstantIterator bmaaItr;
    00040
    00041
    00042         if(targetAssign->getVariable()->getVariableType() != BaseModelVariable::STATE &&
    00043                 targetAssign->getVariable()->getVariableType() != BaseModelVariable::State)
    00044                 cerr << "Invalid Variable Type! Target assigment must be a mode variable." << endl;
    00045
    00046         if(sourceAssign->getVariable()->getVariableType() != BaseModelVariable::STATE &&
    00047                 sourceAssign->getVariable()->getVariableType() != BaseModelVariable::State)
    00048                 cerr << "Invalid Variable Type! Source assigment must be a mode variable." << endl;
    00049
    00050         for(bmaaItr = guards->begin(); bmaaItr != guards->end(); bmaaItr++)
    00051         {
    00052                 const BaseModelVariable var = *bmaaItr->getVariable();
    00053                 if(var.getVariableType() != BaseModelVariable::STATE && var.getVariableType() != BaseModelVariable::CONTROL &&
    00054                         var.getVariableType() != BaseModelVariable::OBSERVABLE)
    00055                         cerr << "Invalid Variable Type! Compiled Transition guards should be modes, commands, or observables." << endl;
    00056         }
    00057 }
    00058
    00059
    00060         //-----------------------------------------------
    00061         //      COPY CONSTRUCTOR
    00062         //-----------------------------------------------
    00063
    00064
    00065 CompiledTransition::CompiledTransition(CompiledTransition const & originalCT)
    00066 : sourceMode(originalCT.sourceMode), guards(originalCT.guards), targetMode(originalCT.targetMode),
    00067         transitionProbability(originalCT.transitionProbability), debug(originalCT.debug)
    00068 {}
    00069
    00070
    00071 CompiledTransition::CompiledTransition(CompiledTransition const * originalCT)
    00072 : sourceMode(originalCT->sourceMode), guards(originalCT->guards), targetMode(originalCT->targetMode),
    00073         transitionProbability(originalCT->transitionProbability), debug(originalCT->debug)
    00074 {}
    00075
    00076         //-----------------------------------------------
    00077         //      DESTRUCTOR
    00078         //-----------------------------------------------
    00079
    00080
    00081 CompiledTransition::~CompiledTransition()
    00082 {
    00083         // Compiled Transitions assume ownership over the assignments so they are deleted
    00084         //  along with the compiled transition
    00085         delete sourceMode;
    00086         delete guards;
    00087         delete targetMode;
    00088 }
    00089
    00090
    00091
    00092         //-----------------------------------------------
    00093         //      PUBLIC METHODS
    00094         //-----------------------------------------------
    00095
    00096 const BaseModelAssignment *
    00097 CompiledTransition::getTargetMode(void) const
    00098 {
    00099         return targetMode;
    00100 }
    00101
    00102 const double
    00103 CompiledTransition::getTransitionProbability(void) const
    00104 {
    00105         return transitionProbability;
    00106 }
    00107
    00108
    00109 const unsigned int
    00110 CompiledTransition::getAntecedentLength(void) const
    00111 //  This includes all guards and source mode assigment
    00112 {
    00113         return 1 + guards->getLength();
    00114 }
    00115
    00116
    00117 const unsigned int
    00118 CompiledTransition::getAntecedentVariableUID(unsigned int vectorPos) const
    00119 {
    00120         // Antecendent order is [sourceAssigment, guards]
    00121         if(vectorPos >= getAntecedentLength())
    00122         {
    00123                 cerr << "ERROR: antecedent index is out-of-bounds" << endl;
    00124                 return 0;
    00125         }
    00126         if(vectorPos == 0)
    00127                 return sourceMode->getVariable()->getUID();
    00128         vectorPos--;
    00129         if(vectorPos < guards->getLength())
    00130                 return (*guards)[vectorPos]->getVariable()->getUID();
    00131
    00132         // Should never get here
    00133         cerr << "BAD ERROR in CompiledTransition::getAntecedentVariableUID!" << endl;
    00134         return 0;
    00135 }
    00136
    00137 const unsigned short
    00138 CompiledTransition::getAntecedentValueUID(unsigned int vectorPos) const
    00139 {
    00140         // Antecendent order is [sourceAssigment, guards]
    00141         if(vectorPos >= getAntecedentLength())
    00142         {
    00143                 cerr << "ERROR: antecedent index is out-of-bounds" << endl;
    00144                 return 0;
    00145         }
    00146         if(vectorPos == 0)
    00147                 return sourceMode->getValue()->getUID();
    00148         vectorPos--;
    00149         if(vectorPos < guards->getLength())
    00150                 return (*guards)[vectorPos]->getValue()->getUID();
    00151
    00152         // Should never get here
    00153         cerr << "BAD ERROR in CompiledTransition::getAntecedentVariableUID!" << endl;
    00154         return 0;
    00155 }
    00156
    00157
    00158 void
    00159 CompiledTransition::setDebug(const unsigned short debugVal)
    00160 // Sets debug to debugVal
    00161 {
    00162         debug = debugVal;
    00163         return;
    00164 }
    00165
    00166
    00167 ostream&
    00168 Mers::Estimation::Discrete::operator<<(ostream& os, CompiledTransition const & ct)
    00169 {
    00170         BaseModelAssignmentArray::ConstantIterator bmaaItr;
    00171
    00172         os << "{";
    00173         for(bmaaItr = ct.guards->begin(); bmaaItr != ct.guards->end(); bmaaItr++)
    00174                 os << *bmaaItr;
    00175         os << "} => {" << *ct.sourceMode << " -> " << *ct.targetMode << ", " << ct.transitionProbability << "}";
    00176
    00177         return os;
    00178 }
    00179         //-----------------------------------------------
    00180         //      PRIVATE METHODS
    00181         //-----------------------------------------------
    00182