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

# /Users/fpy/titan-1-5-MBARI/Mers/Models/Base/BaseModelAssignment.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 "BaseModelAssignment.h"
    00015 #include <Mers/Utils/Exceptions/RuntimeException.h>
    00016 #include <Mers/Utils/Lang/String.h>
    00017 #include <cstdio>
    00018 #include <cassert>
    00019
    00021
    00022 using namespace Mers::Models::Base;
    00023 using Mers::Utils::Exceptions::RuntimeException;
    00024 using Mers::Utils::Lang::String;
    00025 using std::ostream;
    00026
    00028 // Construction/Destruction
    00030
    00031 BaseModelAssignment::BaseModelAssignment(BaseModelVariable const * myVariable,
    00032         BaseModelValue const * myValue, OperatorType myOpType)
    00033 : BaseModelObject(BaseModelObject::BMAssignment), operatorType(myOpType)
    00034 {
    00035         assert(myVariable->getDomain() == myValue->getDomain());
    00036         variable = myVariable;
    00037         value = myValue;
    00038 }
    00039
    00041
    00042 BaseModelAssignment::BaseModelAssignment(BaseModelAssignment const &other)
    00043 : BaseModelObject(other)
    00044 {
    00045         operatorType = other.operatorType;
    00046         variable = other.variable;
    00047         value = other.value;
    00048 }
    00049
    00051
    00052 BaseModelAssignment::~BaseModelAssignment()
    00053 {
    00054         BaseModelAssignment::destroy();
    00055 }
    00056
    00058
    00059 char const * BaseModelAssignment::operatorToSymbolString(OperatorType op)
    00060 {
    00061         switch(op){
    00062         case EqualTo:
    00063                 return "=";
    00064         case NotEqualTo:
    00065                 return "!=";
    00066         case GreaterThan:
    00067                 return ">";
    00068         case GreaterThanOrEqualTo:
    00069                 return ">=";
    00070         case LessThan:
    00071                 return "<";
    00072         case LessThanOrEqualTo:
    00073                 return "<=";
    00074         default:
    00075                 return "??? Invalid Operator ???";
    00076         }
    00077 }
    00078
    00080
    00081 BaseModelAssignment::OperatorType
    00082 BaseModelAssignment::symbolStringToOperator(char const * op)
    00083 {
    00084         switch( op[0] )
    00085         {
    00086         case '=':
    00087                 if( op[1] == '\0' )
    00088                         return EqualTo;
    00089                 break;
    00090         case '!':
    00091                 if( op[1] == '=' && op[2] == '\0' )
    00092                         return NotEqualTo;
    00093                 break;
    00094         case '>':
    00095                 if( op[1] == '\0' )
    00096                         return GreaterThan;
    00097                 else if( op[1] == '=' && op[2] == '\0' )
    00098                         return GreaterThanOrEqualTo;
    00099                 break;
    00100         case '<':
    00101                 if( op[1] == '\0' )
    00102                         return LessThan;
    00103                 else if( op[1] == '=' && op[2] == '\0' )
    00104                         return LessThanOrEqualTo;
    00105                 break;
    00106         default:
    00107                 break;
    00108         }
    00109         String error("Invalid Assignment Operator: '");
    00110         error += op;
    00111         error += '\'';
    00112         throw RuntimeException( error, __FILE__, __LINE__ );
    00113 }
    00114
    00116
    00117 BaseModelVariable const * BaseModelAssignment::getVariable() const
    00118 {
    00119         return variable;
    00120 }
    00121
    00123
    00124 BaseModelValue const * BaseModelAssignment::getValue() const
    00125 {
    00126         return value;
    00127 }
    00128
    00130
    00131 BaseModelAssignment::OperatorType BaseModelAssignment::getOperator() const
    00132 {
    00133         return operatorType;
    00134 }
    00135
    00137
    00138 bool BaseModelAssignment::assignmentHolds(BaseModelValue const &variableValue) const
    00139 {
    00140         switch(operatorType){
    00141         case EqualTo:
    00142                 return variableValue == *value;
    00143         case NotEqualTo:
    00144                 return variableValue != *value;
    00145         case LessThan:
    00146                 return variableValue < *value;
    00147         case LessThanOrEqualTo:
    00148                 return variableValue <= *value;
    00149         case GreaterThan:
    00150                 return variableValue > *value;
    00151         case GreaterThanOrEqualTo:
    00152                 return variableValue >= *value;
    00153         default:
    00154                 fprintf(stderr, "Error, Assignment Operator Type is invalid: %d\n",
    00155                                                 operatorType);
    00156                 return false;
    00157         }
    00158 }
    00159
    00161
    00162 bool BaseModelAssignment::assignmentHolds(double variableValue) const
    00163 {
    00164         switch(operatorType){
    00165         case EqualTo:
    00166                 return variableValue == value->getDouble();
    00167         case NotEqualTo:
    00168                 return variableValue != value->getDouble();
    00169         case LessThan:
    00170                 return variableValue < value->getDouble();
    00171         case LessThanOrEqualTo:
    00172                 return variableValue <= value->getDouble();
    00173         case GreaterThan:
    00174                 return variableValue > value->getDouble();
    00175         case GreaterThanOrEqualTo:
    00176                 return variableValue >= value->getDouble();
    00177         default:
    00178                 fprintf(stderr, "Error, Assignment Operator Type is invalid: %d\n",
    00179                                                 operatorType);
    00180                 return false;
    00181         }
    00182 }
    00183
    00185
    00186 BaseModelAssignment const & BaseModelAssignment::operator =(BaseModelAssignment const & other)
    00187 {
    00188         copy(other);
    00189         return other;
    00190 }
    00191
    00193
    00194 bool BaseModelAssignment::operator ==(BaseModelAssignment const & other) const
    00195 {
    00196         if(this == &other)
    00197                 return true;
    00198         if(operatorType != other.operatorType)
    00199                 return false;
    00200         if((*value) != (*other.value))
    00201                 return false;
    00202         return (*variable) == (*other.variable);
    00203 }
    00204
    00206
    00207 bool BaseModelAssignment::operator !=(BaseModelAssignment const & other) const
    00208 {
    00209         return !BaseModelAssignment::operator ==(other);
    00210 }
    00211
    00213
    00214 #ifndef EMBEDDED
    00215
    00216 void BaseModelAssignment::print(ostream &out) const
    00217 {
    00218         char const * startString, * opString, * endString;
    00219
    00220         startString = "(";
    00221         endString = ")";
    00222         switch(operatorType){
    00223         case Negative:
    00224                 startString = "(NOT (";
    00225                 endString = "))";
    00226         case Positive:
    00227                 opString = "=";
    00228                 break;
    00229         case GreaterThan:
    00230                 opString = ">";
    00231                 break;
    00232         case GreaterThanOrEqualTo:
    00233                 opString = ">=";
    00234                 break;
    00235         case LessThan:
    00236                 opString = "<";
    00237                 break;
    00238         case LessThanOrEqualTo:
    00239                 opString = "<=";
    00240                 break;
    00241         default:
    00242                 opString = "<Unknown Operator>";
    00243                 break;
    00244         }
    00245
    00246         out << startString;
    00247         if(NULL == variable)
    00248                 out << "NULL Variable";
    00249         else
    00250                 out << variable->getName();
    00251         out << ' ' << opString << ' ';
    00252         if(NULL == value)
    00253                 out << "NULL Value";
    00254         else
    00255                 out << value->getName();
    00256         out << endString;
    00257 }
    00258
    00259 #endif
    00260
    00262
    00263 void BaseModelAssignment::destroy()
    00264 {
    00265         // Nothing needs to be done, might as well set the variable & value to NULL
    00266         variable = NULL;
    00267         value = NULL;
    00268 }
    00269
    00271
    00272 int BaseModelAssignment::copy(BaseModelAssignment const &other)
    00273 {
    00274         operatorType = other.operatorType;
    00275         variable = other.variable;
    00276         value = other.value;
    00277         return 0;
    00278 }
    00279
    00281