|
SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // A class that stores and controls tls and switching of their programs 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright (C) 2001-2011 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 MSTLLogicControl_h 00020 #define MSTLLogicControl_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 <vector> 00033 #include <map> 00034 #include "MSTrafficLightLogic.h" 00035 #include "MSSimpleTrafficLightLogic.h" 00036 #include <utils/common/Command.h> 00037 #include <utils/common/UtilExceptions.h> 00038 00039 00040 // =========================================================================== 00041 // class definitions 00042 // =========================================================================== 00055 class MSTLLogicControl { 00056 public: 00061 class OnSwitchAction { 00062 public: 00064 virtual ~OnSwitchAction() {}; 00065 00066 00069 virtual void execute() = 0; 00070 00071 }; 00072 00073 00074 00081 class TLSLogicVariants { 00082 public: 00084 TLSLogicVariants(); 00085 00086 00088 ~TLSLogicVariants(); 00089 00090 00097 bool checkOriginalTLS() const; 00098 00099 00107 bool addLogic(const std::string& programID, MSTrafficLightLogic* logic, bool netWasLoaded, 00108 bool isNewDefault=true); 00109 00110 00111 00112 MSTrafficLightLogic* getLogic(const std::string& programID) const; 00113 void addSwitchCommand(OnSwitchAction* c); 00114 std::vector<MSTrafficLightLogic*> getAllLogics() const; 00115 void saveInitialStates(); 00116 bool isActive(const MSTrafficLightLogic* tl) const; 00117 MSTrafficLightLogic* getActive() const; 00118 void switchTo(MSTLLogicControl& tlc, const std::string& programID); 00119 MSTrafficLightLogic* getLogicInstantiatingOff(MSTLLogicControl& tlc, 00120 const std::string& programID); 00121 void executeOnSwitchActions() const; 00122 void addLink(MSLink* link, MSLane* lane, unsigned int pos); 00123 00124 00125 00126 private: 00128 MSTrafficLightLogic* myCurrentProgram; 00129 00131 std::map<std::string, MSTrafficLightLogic*> myVariants; 00132 00134 std::map<MSLink*, LinkState> myOriginalLinkStates; 00135 00137 std::vector<OnSwitchAction*> mySwitchActions; 00138 00139 00140 private: 00142 TLSLogicVariants(const TLSLogicVariants&); 00143 00145 TLSLogicVariants& operator=(const TLSLogicVariants&); 00146 00147 00148 }; 00149 00150 00151 00153 MSTLLogicControl(); 00154 00155 00157 ~MSTLLogicControl(); 00158 00159 00177 bool closeNetworkReading(); 00178 00179 00186 void setTrafficLightSignals(SUMOTime t) const; 00187 00188 00194 std::vector<MSTrafficLightLogic*> getAllLogics() const; 00195 00196 00203 TLSLogicVariants& get(const std::string& id) const; 00204 00205 00212 MSTrafficLightLogic* get(const std::string& id, const std::string& programID) const; 00213 00214 00220 MSTrafficLightLogic* getActive(const std::string& id) const; 00221 00222 00228 std::vector<std::string> getAllTLIds() const; 00229 00230 00252 bool add(const std::string& id, const std::string& programID, 00253 MSTrafficLightLogic* logic, bool newDefault=true); 00254 00255 00256 00261 bool knows(const std::string& id) const; 00262 00263 00268 bool isActive(const MSTrafficLightLogic* tl) const; 00269 00270 00279 void switchTo(const std::string& id, const std::string& programID); 00280 00281 00282 00285 00294 void addWAUT(SUMOTime refTime, const std::string& id, 00295 const std::string& startProg); 00296 00297 00306 void addWAUTSwitch(const std::string& wautid, SUMOTime when, 00307 const std::string& to); 00308 00309 00321 void addWAUTJunction(const std::string& wautid, const std::string& tls, 00322 const std::string& proc, bool synchron); 00323 00324 00335 void closeWAUT(const std::string& wautid); 00337 00338 00339 00344 void check2Switch(SUMOTime step); 00345 00346 00354 std::pair<SUMOTime, MSPhaseDefinition> getPhaseDef(const std::string& tlid) const; 00355 00356 00357 00358 protected: 00366 class SwitchInitCommand : public Command { 00367 public: 00373 SwitchInitCommand(MSTLLogicControl& p, const std::string& wautid, unsigned int index) 00374 : myParent(p), myWAUTID(wautid), myIndex(index) { } 00375 00376 00378 ~SwitchInitCommand() { } 00379 00380 00381 00384 00399 SUMOTime execute(SUMOTime) { 00400 return myParent.initWautSwitch(*this); 00401 } 00403 00404 00405 00409 const std::string& getWAUTID() const { 00410 return myWAUTID; 00411 } 00412 00413 00417 unsigned int& getIndex() { 00418 return myIndex; 00419 } 00420 00421 00422 protected: 00424 MSTLLogicControl& myParent; 00425 00427 std::string myWAUTID; 00428 00430 unsigned int myIndex; 00431 00432 00433 private: 00435 SwitchInitCommand(const SwitchInitCommand&); 00436 00438 SwitchInitCommand& operator=(const SwitchInitCommand&); 00439 00440 }; 00441 00442 00443 00444 public: 00451 SUMOTime initWautSwitch(SwitchInitCommand& cmd); 00452 00453 00454 protected: 00458 struct WAUTSwitch { 00460 SUMOTime when; 00462 std::string to; 00463 }; 00464 00465 00469 struct WAUTJunction { 00471 std::string junction; 00473 std::string procedure; 00475 bool synchron; 00476 }; 00477 00478 00482 struct WAUT { 00484 std::string id; 00486 std::string startProg; 00488 SUMOTime refTime; 00490 std::vector<WAUTSwitch> switches; 00492 std::vector<WAUTJunction> junctions; 00493 }; 00494 00495 00499 class WAUTSwitchProcedure { 00500 public: 00508 WAUTSwitchProcedure(MSTLLogicControl& control, WAUT& waut, 00509 MSTrafficLightLogic* from, MSTrafficLightLogic* to, 00510 bool synchron) 00511 : myFrom(from), myTo(to), mySwitchSynchron(synchron), myWAUT(waut), myControl(control) { } 00512 00513 00515 virtual ~WAUTSwitchProcedure() { } 00516 00517 00522 virtual bool trySwitch(SUMOTime step) = 0; 00523 00524 00525 protected: 00536 bool isPosAtGSP(SUMOTime currentTime, const MSTrafficLightLogic& logic); 00537 00538 00544 SUMOTime getDiffToStartOfPhase(MSTrafficLightLogic& logic, SUMOTime toTime); 00545 00546 00552 void switchToPos(SUMOTime simStep, MSTrafficLightLogic& logic, SUMOTime toTime); 00553 00554 00562 unsigned int getGSPValue(const MSTrafficLightLogic& logic) const; 00563 00564 00565 protected: 00567 MSTrafficLightLogic* myFrom; 00568 00570 MSTrafficLightLogic* myTo; 00571 00573 bool mySwitchSynchron; 00574 00576 WAUT& myWAUT; 00577 00579 MSTLLogicControl& myControl; 00580 00581 00582 private: 00584 WAUTSwitchProcedure(const WAUTSwitchProcedure&); 00585 00587 WAUTSwitchProcedure& operator=(const WAUTSwitchProcedure&); 00588 00589 }; 00590 00591 00596 class WAUTSwitchProcedure_JustSwitch : public WAUTSwitchProcedure { 00597 public: 00605 WAUTSwitchProcedure_JustSwitch(MSTLLogicControl& control, WAUT& waut, 00606 MSTrafficLightLogic* from, MSTrafficLightLogic* to, 00607 bool synchron); 00608 00609 00611 ~WAUTSwitchProcedure_JustSwitch(); 00612 00613 00618 bool trySwitch(SUMOTime step); 00619 00620 00621 private: 00623 WAUTSwitchProcedure_JustSwitch(const WAUTSwitchProcedure_JustSwitch&); 00624 00626 WAUTSwitchProcedure_JustSwitch& operator=(const WAUTSwitchProcedure_JustSwitch&); 00627 00628 }; 00629 00630 00631 00636 class WAUTSwitchProcedure_GSP : public WAUTSwitchProcedure { 00637 public: 00645 WAUTSwitchProcedure_GSP(MSTLLogicControl& control, WAUT& waut, 00646 MSTrafficLightLogic* from, MSTrafficLightLogic* to, 00647 bool synchron); 00648 00650 ~WAUTSwitchProcedure_GSP(); 00651 00652 00657 bool trySwitch(SUMOTime step); 00658 00659 00660 protected: 00663 void adaptLogic(SUMOTime step); 00664 00665 00666 private: 00668 WAUTSwitchProcedure_GSP(const WAUTSwitchProcedure_GSP&); 00669 00671 WAUTSwitchProcedure_GSP& operator=(const WAUTSwitchProcedure_GSP&); 00672 00673 }; 00674 00675 00680 class WAUTSwitchProcedure_Stretch : public WAUTSwitchProcedure { 00681 public: 00689 WAUTSwitchProcedure_Stretch(MSTLLogicControl& control, WAUT& waut, 00690 MSTrafficLightLogic* from, MSTrafficLightLogic* to, 00691 bool synchron); 00692 00693 00695 ~WAUTSwitchProcedure_Stretch(); 00696 00697 00702 bool trySwitch(SUMOTime step); 00703 00704 00705 protected: 00711 void adaptLogic(SUMOTime step); 00712 00713 00719 void stretchLogic(SUMOTime step, SUMOTime startPos, SUMOTime allStretchTime); 00720 00721 00727 void cutLogic(SUMOTime step, SUMOTime startPos, SUMOTime allCutTime); 00728 00729 00730 protected: 00734 struct StretchBereichDef { 00736 SUMOReal begin; 00738 SUMOReal end; 00740 SUMOReal fac; 00741 00742 }; 00743 00744 00749 int getStretchAreaNo(MSTrafficLightLogic* from) const; 00750 00751 00759 StretchBereichDef getStretchBereichDef(MSTrafficLightLogic* from, int index) const; 00760 00761 00762 private: 00764 WAUTSwitchProcedure_Stretch(const WAUTSwitchProcedure_Stretch&); 00765 00767 WAUTSwitchProcedure_Stretch& operator=(const WAUTSwitchProcedure_Stretch&); 00768 00769 }; 00770 00771 00776 struct WAUTSwitchProcess { 00778 std::string junction; 00780 MSTrafficLightLogic* from; 00782 MSTrafficLightLogic* to; 00784 WAUTSwitchProcedure* proc; 00785 }; 00786 00787 00789 std::map<std::string, WAUT*> myWAUTs; 00790 00792 std::vector<WAUTSwitchProcess> myCurrentlySwitched; 00793 00795 std::map<std::string, TLSLogicVariants*> myLogics; 00796 00798 bool myNetWasLoaded; 00799 00800 00801 private: 00803 MSTLLogicControl(const MSTLLogicControl&); 00804 00806 MSTLLogicControl& operator=(const MSTLLogicControl&); 00807 00808 }; 00809 00810 00811 #endif 00812 00813 /****************************************************************************/ 00814