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

# /Users/fpy/titan-1-5-MBARI/Mers/Estimation/Discrete/CompiledObject.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 "CompiledObject.h"
    00010
    00011 #ifdef _MSC_VER
    00012 #pragma warning( disable : 4786 )
    00013 #endif
    00014
    00015 #include <iostream>
    00016 #include "Dissent.h"
    00017 #include "CompiledTransition.h"
    00018
    00019 using namespace Mers::Estimation::Discrete;
    00020 using std::ostream;
    00021 using std::cerr;
    00022 using std::cout;
    00023 using std::endl;
    00024
    00025
    00026         //-----------------------------------------------
    00027         //      CONSTRUCTOR
    00028         //-----------------------------------------------
    00029
    00030
    00031 CompiledObject::CompiledObject(const void * obj, const CompiledObjectType theType,
    00032                 unsigned short db)
    00033 : compiledObjectPtr(obj), theObjectType(theType), numElementsSatisfied(0), debug(db)
    00034 {}
    00035
    00036
    00037         //-----------------------------------------------
    00038         //      COPY CONSTRUCTOR
    00039         //-----------------------------------------------
    00040
    00041
    00042 CompiledObject::CompiledObject(CompiledObject const & origCO)
    00043 : compiledObjectPtr(origCO.compiledObjectPtr), theObjectType(origCO.theObjectType),
    00044         numElementsSatisfied(origCO.numElementsSatisfied), debug(origCO.debug)
    00045 {}
    00046
    00047
    00048 CompiledObject::CompiledObject(CompiledObject const * origCO)
    00049 : compiledObjectPtr(origCO->compiledObjectPtr), theObjectType(origCO->theObjectType),
    00050         numElementsSatisfied(origCO->numElementsSatisfied), debug(origCO->debug)
    00051 {}
    00052
    00053         //-----------------------------------------------
    00054         //      DESTRUCTOR
    00055         //-----------------------------------------------
    00056
    00057
    00058 CompiledObject::~CompiledObject()
    00059 {
    00060         // compiled object class does not own the dissent or compiled transition
    00061 }
    00062
    00063
    00064
    00065         //-----------------------------------------------
    00066         //      PUBLIC METHODS
    00067         //-----------------------------------------------
    00068
    00069 const Dissent *
    00070 CompiledObject::getDissent(void) const
    00071 {
    00072         // Warns if compiled object is not enabled
    00073         if(!isEnabled())
    00074                 cout << "WARNING: Compiled Object is not enabled." << endl;
    00075
    00076         if(theObjectType == oCOMPILEDTRANSITION)
    00077         {
    00078                 cerr << "<CompiledObject> ERROR: This object is not a Dissent!" << endl;
    00079                 return NULL;
    00080         }
    00081         else
    00082                 return (const Dissent *)compiledObjectPtr;
    00083 }
    00084
    00085
    00086 const CompiledTransition *
    00087 CompiledObject::getCompiledTransition(void) const
    00088 {
    00089         // Warns if compiled object is not enabled
    00090         if(!isEnabled())
    00091                 cout << "WARNING: Compiled Object is not enabled." << endl;
    00092
    00093         if(theObjectType == oDISSENT)
    00094         {
    00095                 cerr << "<CompiledObject> ERROR: This object is not a CompiledTransition!" << endl;
    00096                 return NULL;
    00097         }
    00098         else
    00099                 return (const CompiledTransition *)compiledObjectPtr;
    00100 }
    00101
    00102
    00103 bool
    00104 CompiledObject::implicantElementSatisfied(const bool isSat)
    00105 // If isSat, increment count of elements satisfied. Else, decrement count
    00106 {
    00107         if(debug > 1)
    00108         {
    00109                 if(theObjectType == oDISSENT)
    00110                         cout << "Dissent: ";
    00111                 else if(theObjectType == oCOMPILEDTRANSITION)
    00112                         cout << "Compiled transition: ";
    00113                 else
    00114                         cout << "Unknown type: ";
    00115         }
    00116
    00117         if(isSat)
    00118         {
    00119                 numElementsSatisfied++;
    00120                 if(debug > 1)
    00121                         cout << "Antecendent element satisfied count incremented to " << numElementsSatisfied << endl;
    00122         }
    00123         else
    00124         {
    00125                 numElementsSatisfied--;
    00126                 if(debug > 1)
    00127                         cout << "Antecendent element satisfied count decremented to " << numElementsSatisfied << endl;
    00128         }
    00129
    00130         // Limit checking
    00131         if(numElementsSatisfied > getAntecedentLength())
    00132         {
    00133                 cerr << "<CompiledObject> ERROR: Element count over limit!" << endl;
    00134                 numElementsSatisfied--;         // decrement back to max value
    00135                 return false;
    00136         }
    00137         else if(numElementsSatisfied < 0)
    00138         {
    00139                 cerr << "<CompiledObject> ERROR: Element count below zero!" << endl;
    00140                 numElementsSatisfied++;         // increment back to min value
    00141                 return false;
    00142         }
    00143
    00144         return true;
    00145 }
    00146
    00147
    00148 bool
    00149 CompiledObject::isEnabled(void) const
    00150 // Returns if all the elements in the antecedent have been satisfied
    00151 {
    00152         if(numElementsSatisfied == getAntecedentLength())
    00153                 return true;
    00154         return false;
    00155 }
    00156
    00157
    00158 void
    00159 CompiledObject::resetCount(void)
    00160 // Resets the satisfied element count
    00161 {
    00162         numElementsSatisfied = 0;
    00163         return;
    00164 }
    00165
    00166
    00167 void
    00168 CompiledObject::setDebug(const unsigned short debugVal)
    00169 // Sets debug to debugVal
    00170 {
    00171         debug = debugVal;
    00172         return;
    00173 }
    00174
    00175
    00176
    00177 ostream&
    00178 Mers::Estimation::Discrete::operator<<(ostream& os, CompiledObject const & co)
    00179 {
    00180         if(co.debug > 0)
    00181                 os << co.numElementsSatisfied << "/"
    00182                 << co.getAntecedentLength() << " ";
    00183
    00184
    00185         if(co.theObjectType == CompiledObject::oDISSENT)
    00186                 os << *(Dissent *)co.compiledObjectPtr;
    00187         else if(co.theObjectType == CompiledObject::oCOMPILEDTRANSITION)
    00188                 os << *(CompiledTransition *)co.compiledObjectPtr;
    00189         else
    00190                 os << "Unknown compiled object";
    00191
    00192
    00193
    00194         return os;
    00195 }
    00196         //-----------------------------------------------
    00197         //      PRIVATE METHODS
    00198         //-----------------------------------------------
    00199
    00200 const int
    00201 CompiledObject::getAntecedentLength(void) const
    00202 {
    00203         if(theObjectType == oDISSENT)
    00204                 return ((Dissent *)compiledObjectPtr)->getAntecedentLength();
    00205         else if(theObjectType == oCOMPILEDTRANSITION)
    00206                 return ((CompiledTransition *)compiledObjectPtr)->getAntecedentLength();
    00207         else
    00208         {
    00209                 cerr << "Unknown compiled object type!" << endl;
    00210                 return -1;
    00211         }
    00212 }