MSPhaseDefinition.h

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // The definition of a single phase of a tls logic
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 #ifndef MSPhaseDefinition_h
00020 #define MSPhaseDefinition_h
00021 
00022 
00023 // ===========================================================================
00024 // included modules
00025 // ===========================================================================
00026 #ifdef _MSC_VER
00027 #include <windows_config.h>
00028 #else
00029 #include <config.h>
00030 #endif
00031 
00032 #include <bitset>
00033 #include <string>
00034 #include <utils/common/SUMOTime.h>
00035 #include <utils/options/OptionsCont.h>
00036 #include <microsim/MSLink.h>
00037 
00038 
00039 // ===========================================================================
00040 // class definitions
00041 // ===========================================================================
00046 class MSPhaseDefinition {
00047 public:
00049     SUMOTime duration;
00050 
00052     SUMOTime minDuration;
00053 
00055     SUMOTime maxDuration;
00056 
00058     SUMOTime myLastSwitch;
00059 
00060 
00061 public:
00069     MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg) throw()
00070             : duration(durationArg), minDuration(durationArg), maxDuration(durationArg),
00071             myLastSwitch(0), state(stateArg) {
00072         myLastSwitch = string2time(OptionsCont::getOptions().getString("begin"));
00073     }
00074 
00075 
00083     MSPhaseDefinition(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg,
00084                       const std::string &stateArg) throw()
00085             : duration(durationArg),
00086             myLastSwitch(0), state(stateArg) {
00087         myLastSwitch = string2time(OptionsCont::getOptions().getString("begin"));
00088         minDuration = minDurationArg<0 ? durationArg : minDurationArg;
00089         maxDuration = maxDurationArg<0 ? durationArg : maxDurationArg;
00090     }
00091 
00092 
00094     virtual ~MSPhaseDefinition() throw() { }
00095 
00096 
00100     const std::string &getState() const throw() {
00101         return state;
00102     }
00103 
00104 
00112     bool isGreenPhase() const throw() {
00113         if (state.find_first_of("gG")==std::string::npos) {
00114             return false;
00115         }
00116         if (state.find_first_of("yY")!=std::string::npos) {
00117             return false;
00118         }
00119         return true;
00120     }
00121 
00122 
00127     MSLink::LinkState getSignalState(unsigned int pos) const throw() {
00128         return (MSLink::LinkState) state[pos];
00129     }
00130 
00131 
00138     bool operator!=(const MSPhaseDefinition &pd) {
00139         return state!=pd.state;
00140     }
00141 
00142 
00145 
00152     static std::string old2new(const std::string &driveMask, const std::string &brakeMask, const std::string &yellowMask) throw() {
00153         std::string state;
00154         for (int i=(int) driveMask.length()-1; i>=0; --i) {
00155             if (driveMask[i]=='1') {
00156                 state += 'g';
00157             } else {
00158                 if (yellowMask[i]=='1') {
00159                     state += 'y';
00160                 } else {
00161                     state += 'r';
00162                 }
00163             }
00164         }
00165         //  brake needs then
00166         int j = 0;
00167         for (int i=(int) driveMask.length()-1; i>=0; --i, ++j) {
00168             if (brakeMask[i]=='0') {
00169                 if (state[j]=='g') {
00170                     state[j] = 'G';
00171                 }
00172                 if (state[j]=='y') {
00173                     state[j] = 'Y';
00174                 }
00175             }
00176         }
00177         return state;
00178     }
00179 
00180 
00185     static std::string new2driveMask(const std::string &state) throw() {
00186         std::string mask;
00187         for (int i=(int) state.length()-1; i>=0; --i) {
00188             if (state[i]=='g'||state[i]=='G') {
00189                 mask += '1';
00190             } else {
00191                 mask += '0';
00192             }
00193         }
00194         return mask;
00195     }
00196 
00197 
00202     static std::string new2brakeMask(const std::string &state) throw() {
00203         std::string mask;
00204         for (int i=(int) state.length()-1; i>=0; --i) {
00205             if (state[i]>='a'&&state[i]<='z') {
00206                 mask += '0';
00207             } else {
00208                 mask += '1';
00209             }
00210         }
00211         return mask;
00212     }
00213 
00214 
00219     static std::string new2yellowMask(const std::string &state) throw() {
00220         std::string mask;
00221         for (int i=(int) state.length()-1; i>=0; --i) {
00222             if (state[i]=='y'||state[i]=='Y') {
00223                 mask += '1';
00224             } else {
00225                 mask += '0';
00226             }
00227         }
00228         return mask;
00229     }
00230 
00232 
00233 
00234 private:
00236     std::string state;
00237 
00238 
00239 };
00240 
00241 #endif
00242 
00243 /****************************************************************************/
00244 

Generated on Wed May 5 00:06:32 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6