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

# /Users/fpy/titan-1-5-MBARI/Mers/Interfaces/ObservationInterface.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 "ObservationInterface.h"
    00015
    00016 #include "ObservationUpdateListener.h"
    00017 #include "ContinuousVariable.h"
    00018 #include "DiscreteVariable.h"
    00019 #include "DiscreteValue.h"
    00020
    00022
    00023 using namespace Mers::Interfaces;
    00024 using Mers::Utils::Exceptions::RuntimeException;
    00025 using Mers::Utils::Exceptions::IllegalArgumentException;
    00026 using std::set;
    00027 using std::map;
    00028
    00030
    00031 ObservationInterface::ObservationInterface() : mListeners()
    00032 {
    00033 }
    00034
    00036
    00037 ObservationInterface::~ObservationInterface()
    00038 {
    00039 }
    00040
    00042
    00043 namespace Mers {
    00044         namespace Interfaces {
    00045                 template<class V> V * findNamedSetObject(set<V *> &vSet, const char * vName)
    00046                 {
    00047                         typename set<V *>::iterator iSet;
    00048                         for(iSet = vSet.begin(); iSet != vSet.end(); ++iSet) {
    00049                                 V * v = *iSet;
    00050                                 if(strcmp(v->getName(), vName) == 0) {
    00051                                         return v;
    00052                                 }
    00053                         }
    00054                         return NULL;
    00055                 }
    00056         }
    00057 }
    00058
    00060
    00061 ObservationInterface::DiscreteVariableIdentifier
    00062 ObservationInterface::getDiscreteVariable(const char * varName)
    00063 {
    00064         return findNamedSetObject<DiscreteVariable>(mDiscreteVars, varName);
    00065 }
    00066
    00068
    00069 ObservationInterface::ContinuousVariableIdentifier
    00070 ObservationInterface::getContinuousVariable(const char * varName)
    00071 {
    00072         return findNamedSetObject<ContinuousVariable>(mContinuousVars, varName);
    00073 }
    00074
    00076
    00077 ObservationInterface::ValueIdentifier
    00078 ObservationInterface::getValue(DiscreteVariableIdentifier varID, const char * value)
    00079 {
    00080         DiscreteValueMap::iterator iMap = mDiscreteValueMap.find(varID);
    00081         if(iMap != mDiscreteValueMap.end()) {
    00082                 set<DiscreteValue *> * values = (*iMap).second;
    00083
    00084                 return findNamedSetObject<DiscreteValue>(*values, value);
    00085         }
    00086         return NULL;
    00087 }
    00088
    00090
    00091 bool ObservationInterface::isValid(DiscreteVariableIdentifier varID)
    00092 {
    00093         return varID != NULL;
    00094 }
    00095
    00097
    00098 bool ObservationInterface::isValid(ContinuousVariableIdentifier varID)
    00099 {
    00100         return varID != NULL;
    00101 }
    00102
    00104
    00105 bool ObservationInterface::isValid(ValueIdentifier value)
    00106 {
    00107         return value != NULL;
    00108 }
    00109
    00111
    00112 bool ObservationInterface::isCurrentState(ValueIdentifier value)
    00113 {
    00114         ValueIdentifier curState = getCurrentState(value->getVariable());
    00115         return value == curState;
    00116 }
    00117
    00119
    00120 void ObservationInterface::addUpdateListener(ObservationUpdateListener * listener)
    00121 {
    00122         mListeners.insert(listener);
    00123 }
    00124
    00126
    00127 void ObservationInterface::updateListeners(ValueIdentifier value)
    00128 {
    00129         set<ObservationUpdateListener *>::iterator iSet;
    00130         for(iSet = mListeners.begin(); iSet != mListeners.end(); ++iSet) {
    00131                 ObservationUpdateListener * listener = *iSet;
    00132                 listener->update(value);
    00133         }
    00134 }
    00135
    00137
    00138 void ObservationInterface::updateListeners(ContinuousVariableIdentifier varID, double value)
    00139 {
    00140         set<ObservationUpdateListener *>::iterator iSet;
    00141         for(iSet = mListeners.begin(); iSet != mListeners.end(); ++iSet) {
    00142                 ObservationUpdateListener * listener = *iSet;
    00143                 listener->update(varID, value);
    00144         }
    00145 }
    00146
    00148
    00149 void ObservationInterface::addDiscreteVariable(DiscreteVariable * var)
    00150 {
    00151         mDiscreteVars.insert(var);
    00152         // Add an empty value map to the discrete value map.
    00153         DiscreteValueMap::value_type mapPair(var, new set<DiscreteValue *>);
    00154         mDiscreteValueMap.insert(mapPair);
    00155 }
    00156
    00158
    00159 void ObservationInterface::addContinuousVariable(ContinuousVariable * var)
    00160 {
    00161         mContinuousVars.insert(var);
    00162 }
    00163
    00165
    00166 void ObservationInterface::addDiscreteValue(DiscreteValue * value)
    00167         throw(IllegalArgumentException, RuntimeException)
    00168 {
    00169         // Verify the value is not NULL
    00170         if(value == NULL)
    00171                 throw new IllegalArgumentException("Value is NULL", __FILE__, __LINE__);
    00172
    00173         DiscreteVariable * var = value->getVariable();
    00174
    00175         // Verify that the variable already exists.
    00176         set<DiscreteVariable *>::iterator iSet =
    00177                 mDiscreteVars.find(var);
    00178
    00179         if(iSet == mDiscreteVars.end())
    00180                 throw new IllegalArgumentException("The variable of the value was not found.", __FILE__, __LINE__);
    00181
    00182         // Add the value to the list of available values.
    00183         mDiscreteValues.insert(value);
    00184
    00185         // Get the map
    00186         DiscreteValueMap::iterator iMap = mDiscreteValueMap.find(var);
    00187         if(iMap == mDiscreteValueMap.end())
    00188                 throw new IllegalArgumentException("The variable map of the value was not found.", __FILE__, __LINE__);
    00189
    00190         set<DiscreteValue *> * values = (*iMap).second;
    00191         if(values == NULL)
    00192                 throw new RuntimeException("Unexpected NULL value pointer.", __FILE__, __LINE__);
    00193
    00194         values->insert(value);
    00195 }
    00196
    00198