SUMO - Simulation of Urban MObility
MSTLLogicControl.h
Go to the documentation of this file.
00001 /****************************************************************************/
00011 // A class that stores and controls tls and switching of their programs
00012 /****************************************************************************/
00013 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00014 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00015 /****************************************************************************/
00016 //
00017 //   This file is part of SUMO.
00018 //   SUMO is free software: you can redistribute it and/or modify
00019 //   it under the terms of the GNU General Public License as published by
00020 //   the Free Software Foundation, either version 3 of the License, or
00021 //   (at your option) any later version.
00022 //
00023 /****************************************************************************/
00024 #ifndef MSTLLogicControl_h
00025 #define MSTLLogicControl_h
00026 
00027 
00028 // ===========================================================================
00029 // included modules
00030 // ===========================================================================
00031 #ifdef _MSC_VER
00032 #include <windows_config.h>
00033 #else
00034 #include <config.h>
00035 #endif
00036 
00037 #include <vector>
00038 #include <map>
00039 #include "MSTrafficLightLogic.h"
00040 #include "MSSimpleTrafficLightLogic.h"
00041 #include <utils/common/Command.h>
00042 #include <utils/common/UtilExceptions.h>
00043 
00044 
00045 // ===========================================================================
00046 // class definitions
00047 // ===========================================================================
00060 class MSTLLogicControl {
00061 public:
00066     class OnSwitchAction {
00067     public:
00069         virtual ~OnSwitchAction() {};
00070 
00071 
00074         virtual void execute() = 0;
00075 
00076     };
00077 
00078 
00079 
00086     class TLSLogicVariants {
00087     public:
00089         TLSLogicVariants();
00090 
00091 
00093         ~TLSLogicVariants();
00094 
00095 
00102         bool checkOriginalTLS() const;
00103 
00104 
00112         bool addLogic(const std::string& programID, MSTrafficLightLogic* logic, bool netWasLoaded,
00113                       bool isNewDefault = true);
00114 
00115 
00116 
00117         MSTrafficLightLogic* getLogic(const std::string& programID) const;
00118         void addSwitchCommand(OnSwitchAction* c);
00119         std::vector<MSTrafficLightLogic*> getAllLogics() const;
00120         void saveInitialStates();
00121         bool isActive(const MSTrafficLightLogic* tl) const;
00122         MSTrafficLightLogic* getActive() const;
00123         void switchTo(MSTLLogicControl& tlc, const std::string& programID);
00124         MSTrafficLightLogic* getLogicInstantiatingOff(MSTLLogicControl& tlc,
00125                 const std::string& programID);
00126         void executeOnSwitchActions() const;
00127         void addLink(MSLink* link, MSLane* lane, unsigned int pos);
00128 
00129 
00130 
00131     private:
00133         MSTrafficLightLogic* myCurrentProgram;
00134 
00136         std::map<std::string, MSTrafficLightLogic*> myVariants;
00137 
00139         std::map<MSLink*, LinkState> myOriginalLinkStates;
00140 
00142         std::vector<OnSwitchAction*> mySwitchActions;
00143 
00144 
00145     private:
00147         TLSLogicVariants(const TLSLogicVariants&);
00148 
00150         TLSLogicVariants& operator=(const TLSLogicVariants&);
00151 
00152 
00153     };
00154 
00155 
00156 
00158     MSTLLogicControl();
00159 
00160 
00162     ~MSTLLogicControl();
00163 
00164 
00182     bool closeNetworkReading();
00183 
00184 
00191     void setTrafficLightSignals(SUMOTime t) const;
00192 
00193 
00199     std::vector<MSTrafficLightLogic*> getAllLogics() const;
00200 
00201 
00208     TLSLogicVariants& get(const std::string& id) const;
00209 
00210 
00217     MSTrafficLightLogic* get(const std::string& id, const std::string& programID) const;
00218 
00219 
00225     MSTrafficLightLogic* getActive(const std::string& id) const;
00226 
00227 
00233     std::vector<std::string> getAllTLIds() const;
00234 
00235 
00257     bool add(const std::string& id, const std::string& programID,
00258              MSTrafficLightLogic* logic, bool newDefault = true);
00259 
00260 
00261 
00266     bool knows(const std::string& id) const;
00267 
00268 
00273     bool isActive(const MSTrafficLightLogic* tl) const;
00274 
00275 
00284     void switchTo(const std::string& id, const std::string& programID);
00285 
00286 
00287 
00290 
00299     void addWAUT(SUMOTime refTime, const std::string& id,
00300                  const std::string& startProg);
00301 
00302 
00311     void addWAUTSwitch(const std::string& wautid, SUMOTime when,
00312                        const std::string& to);
00313 
00314 
00326     void addWAUTJunction(const std::string& wautid, const std::string& tls,
00327                          const std::string& proc, bool synchron);
00328 
00329 
00340     void closeWAUT(const std::string& wautid);
00342 
00343 
00344 
00349     void check2Switch(SUMOTime step);
00350 
00351 
00359     std::pair<SUMOTime, MSPhaseDefinition> getPhaseDef(const std::string& tlid) const;
00360 
00361 
00362 
00363 protected:
00371     class SwitchInitCommand : public Command {
00372     public:
00378         SwitchInitCommand(MSTLLogicControl& p, const std::string& wautid, unsigned int index)
00379             : myParent(p), myWAUTID(wautid), myIndex(index) { }
00380 
00381 
00383         ~SwitchInitCommand() { }
00384 
00385 
00386 
00389 
00404         SUMOTime execute(SUMOTime) {
00405             return myParent.initWautSwitch(*this);
00406         }
00408 
00409 
00410 
00414         const std::string& getWAUTID() const {
00415             return myWAUTID;
00416         }
00417 
00418 
00422         unsigned int& getIndex() {
00423             return myIndex;
00424         }
00425 
00426 
00427     protected:
00429         MSTLLogicControl& myParent;
00430 
00432         std::string myWAUTID;
00433 
00435         unsigned int myIndex;
00436 
00437 
00438     private:
00440         SwitchInitCommand(const SwitchInitCommand&);
00441 
00443         SwitchInitCommand& operator=(const SwitchInitCommand&);
00444 
00445     };
00446 
00447 
00448 
00449 public:
00456     SUMOTime initWautSwitch(SwitchInitCommand& cmd);
00457 
00458 
00459 protected:
00463     struct WAUTSwitch {
00465         SUMOTime when;
00467         std::string to;
00468     };
00469 
00470 
00474     struct WAUTJunction {
00476         std::string junction;
00478         std::string procedure;
00480         bool synchron;
00481     };
00482 
00483 
00487     struct WAUT {
00489         std::string id;
00491         std::string startProg;
00493         SUMOTime refTime;
00495         std::vector<WAUTSwitch> switches;
00497         std::vector<WAUTJunction> junctions;
00498     };
00499 
00500 
00504     class WAUTSwitchProcedure {
00505     public:
00513         WAUTSwitchProcedure(MSTLLogicControl& control, WAUT& waut,
00514                             MSTrafficLightLogic* from, MSTrafficLightLogic* to,
00515                             bool synchron)
00516             : myFrom(from), myTo(to), mySwitchSynchron(synchron), myWAUT(waut), myControl(control) { }
00517 
00518 
00520         virtual ~WAUTSwitchProcedure() { }
00521 
00522 
00527         virtual bool trySwitch(SUMOTime step) = 0;
00528 
00529 
00530     protected:
00541         bool isPosAtGSP(SUMOTime currentTime, const MSTrafficLightLogic& logic);
00542 
00543 
00549         SUMOTime getDiffToStartOfPhase(MSTrafficLightLogic& logic, SUMOTime toTime);
00550 
00551 
00557         void switchToPos(SUMOTime simStep, MSTrafficLightLogic& logic, SUMOTime toTime);
00558 
00559 
00567         unsigned int getGSPValue(const MSTrafficLightLogic& logic) const;
00568 
00569 
00570     protected:
00572         MSTrafficLightLogic* myFrom;
00573 
00575         MSTrafficLightLogic* myTo;
00576 
00578         bool mySwitchSynchron;
00579 
00581         WAUT& myWAUT;
00582 
00584         MSTLLogicControl& myControl;
00585 
00586 
00587     private:
00589         WAUTSwitchProcedure(const WAUTSwitchProcedure&);
00590 
00592         WAUTSwitchProcedure& operator=(const WAUTSwitchProcedure&);
00593 
00594     };
00595 
00596 
00601     class WAUTSwitchProcedure_JustSwitch : public WAUTSwitchProcedure {
00602     public:
00610         WAUTSwitchProcedure_JustSwitch(MSTLLogicControl& control, WAUT& waut,
00611                                        MSTrafficLightLogic* from, MSTrafficLightLogic* to,
00612                                        bool synchron);
00613 
00614 
00616         ~WAUTSwitchProcedure_JustSwitch();
00617 
00618 
00623         bool trySwitch(SUMOTime step);
00624 
00625 
00626     private:
00628         WAUTSwitchProcedure_JustSwitch(const WAUTSwitchProcedure_JustSwitch&);
00629 
00631         WAUTSwitchProcedure_JustSwitch& operator=(const WAUTSwitchProcedure_JustSwitch&);
00632 
00633     };
00634 
00635 
00636 
00641     class WAUTSwitchProcedure_GSP : public WAUTSwitchProcedure {
00642     public:
00650         WAUTSwitchProcedure_GSP(MSTLLogicControl& control, WAUT& waut,
00651                                 MSTrafficLightLogic* from, MSTrafficLightLogic* to,
00652                                 bool synchron);
00653 
00655         ~WAUTSwitchProcedure_GSP();
00656 
00657 
00662         bool trySwitch(SUMOTime step);
00663 
00664 
00665     protected:
00668         void adaptLogic(SUMOTime step);
00669 
00670 
00671     private:
00673         WAUTSwitchProcedure_GSP(const WAUTSwitchProcedure_GSP&);
00674 
00676         WAUTSwitchProcedure_GSP& operator=(const WAUTSwitchProcedure_GSP&);
00677 
00678     };
00679 
00680 
00685     class WAUTSwitchProcedure_Stretch : public WAUTSwitchProcedure {
00686     public:
00694         WAUTSwitchProcedure_Stretch(MSTLLogicControl& control, WAUT& waut,
00695                                     MSTrafficLightLogic* from, MSTrafficLightLogic* to,
00696                                     bool synchron);
00697 
00698 
00700         ~WAUTSwitchProcedure_Stretch();
00701 
00702 
00707         bool trySwitch(SUMOTime step);
00708 
00709 
00710     protected:
00716         void adaptLogic(SUMOTime step);
00717 
00718 
00724         void stretchLogic(SUMOTime step, SUMOTime startPos, SUMOTime allStretchTime);
00725 
00726 
00732         void cutLogic(SUMOTime step, SUMOTime startPos, SUMOTime allCutTime);
00733 
00734 
00735     protected:
00739         struct StretchBereichDef {
00741             SUMOReal begin;
00743             SUMOReal end;
00745             SUMOReal fac;
00746 
00747         };
00748 
00749 
00754         int getStretchAreaNo(MSTrafficLightLogic* from) const;
00755 
00756 
00764         StretchBereichDef getStretchBereichDef(MSTrafficLightLogic* from, int index) const;
00765 
00766 
00767     private:
00769         WAUTSwitchProcedure_Stretch(const WAUTSwitchProcedure_Stretch&);
00770 
00772         WAUTSwitchProcedure_Stretch& operator=(const WAUTSwitchProcedure_Stretch&);
00773 
00774     };
00775 
00776 
00781     struct WAUTSwitchProcess {
00783         std::string junction;
00785         MSTrafficLightLogic* from;
00787         MSTrafficLightLogic* to;
00789         WAUTSwitchProcedure* proc;
00790     };
00791 
00792 
00794     std::map<std::string, WAUT*> myWAUTs;
00795 
00797     std::vector<WAUTSwitchProcess> myCurrentlySwitched;
00798 
00800     std::map<std::string, TLSLogicVariants*> myLogics;
00801 
00803     bool myNetWasLoaded;
00804 
00805 
00806 private:
00808     MSTLLogicControl(const MSTLLogicControl&);
00809 
00811     MSTLLogicControl& operator=(const MSTLLogicControl&);
00812 
00813 };
00814 
00815 
00816 #endif
00817 
00818 /****************************************************************************/
00819 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines