00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef MSTLLogicControl_h
00020 #define MSTLLogicControl_h
00021
00022
00023
00024
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
00042
00055 class MSTLLogicControl {
00056 public:
00062 class OnSwitchAction {
00063 public:
00065 virtual ~OnSwitchAction() throw() {};
00066
00067
00071 virtual void execute() throw() = 0;
00072
00073 };
00074
00075
00076
00083 class TLSLogicVariants {
00084 public:
00086 TLSLogicVariants() throw();
00087
00088
00090 ~TLSLogicVariants() throw();
00091
00092
00099 bool checkOriginalTLS() const throw();
00100
00101
00109 bool addLogic(const std::string &subID, MSTrafficLightLogic*logic, bool netWasLoaded,
00110 bool isNewDefault=true) throw(ProcessError);
00111
00112
00113
00114 MSTrafficLightLogic* getLogic(const std::string &subid) const;
00115 void addSwitchCommand(OnSwitchAction *c);
00116 std::vector<MSTrafficLightLogic*> getAllLogics() const;
00117 void saveInitialStates();
00118 bool isActive(const MSTrafficLightLogic *tl) const;
00119 MSTrafficLightLogic* getActive() const;
00120 bool switchTo(MSTLLogicControl &tlc, const std::string &subid);
00121 bool setTrafficLightSignals();
00122 MSTrafficLightLogic*getLogicInstantiatingOff(MSTLLogicControl &tlc,
00123 const std::string &subid);
00124 void executeOnSwitchActions() const;
00125 void addLink(MSLink *link, MSLane *lane, unsigned int pos) throw();
00126
00127
00128
00129 private:
00131 MSTrafficLightLogic *myCurrentProgram;
00132
00134 std::map<std::string, MSTrafficLightLogic*> myVariants;
00135
00137 std::map<MSLink*, std::pair<MSLink::LinkState, bool> > myOriginalLinkStates;
00138
00140 std::vector<OnSwitchAction*> mySwitchActions;
00141
00142
00143 private:
00145 TLSLogicVariants(const TLSLogicVariants&);
00146
00148 TLSLogicVariants& operator=(const TLSLogicVariants&);
00149
00150
00151 };
00152
00153
00155 MSTLLogicControl() throw();
00156
00157
00159 ~MSTLLogicControl() throw();
00160
00161
00179 bool closeNetworkReading() throw();
00180
00181
00184 void setTrafficLightSignals();
00185
00186
00192 std::vector<MSTrafficLightLogic*> getAllLogics() const;
00193
00194
00200 TLSLogicVariants &get(const std::string &id) const throw(InvalidArgument);
00201
00202
00209 MSTrafficLightLogic * const get(const std::string &id, const std::string &subid) const;
00210
00211
00217 MSTrafficLightLogic * const getActive(const std::string &id) const;
00218
00224 std::vector<std::string> getAllTLIds() const;
00225
00226
00248 bool add(const std::string &id, const std::string &subID,
00249 MSTrafficLightLogic *logic, bool newDefault=true) throw(ProcessError);
00250
00251
00253 bool knows(const std::string &id) const;
00254
00256 bool isActive(const MSTrafficLightLogic *tl) const;
00257
00263 bool switchTo(const std::string &id, const std::string &subid);
00264
00265
00268
00278 void addWAUT(SUMOTime refTime, const std::string &id,
00279 const std::string &startProg) throw(InvalidArgument);
00280
00281
00291 void addWAUTSwitch(const std::string &wautid, SUMOTime when,
00292 const std::string &to) throw(InvalidArgument);
00293
00294
00307 void addWAUTJunction(const std::string &wautid, const std::string &tls,
00308 const std::string &proc, bool synchron) throw(InvalidArgument, ProcessError);
00309
00310
00322 void closeWAUT(const std::string &wautid) throw(InvalidArgument);
00324
00325
00326
00331 void check2Switch(SUMOTime step);
00332
00333
00336 std::pair<SUMOTime, MSPhaseDefinition> getPhaseDef(const std::string &tlid) const;
00337
00338
00339 protected:
00347 class SwitchInitCommand : public Command {
00348 public:
00349
00350 SwitchInitCommand(MSTLLogicControl &p, const std::string &wautid, unsigned int index) throw()
00351 : myParent(p), myWAUTID(wautid), myIndex(index) { }
00352
00354 ~SwitchInitCommand() throw() { }
00355
00356
00359
00374 SUMOTime execute(SUMOTime) throw(ProcessError) {
00375 return myParent.initWautSwitch(*this);
00376 }
00378
00379
00380
00382 const std::string &getWAUTID() const {
00383 return myWAUTID;
00384 }
00385
00387 unsigned int &getIndex() {
00388 return myIndex;
00389 }
00390
00391 protected:
00393 MSTLLogicControl &myParent;
00394
00396 std::string myWAUTID;
00397
00399 unsigned int myIndex;
00400
00401 private:
00403 SwitchInitCommand(const SwitchInitCommand&);
00404
00406 SwitchInitCommand& operator=(const SwitchInitCommand&);
00407
00408 };
00409
00410 public:
00415 SUMOTime initWautSwitch(SwitchInitCommand &cmd);
00416
00417
00418 protected:
00423 struct WAUTSwitch {
00425 SUMOTime when;
00427 std::string to;
00428 };
00429
00434 struct WAUTJunction {
00436 std::string junction;
00438 std::string procedure;
00440 bool synchron;
00441 };
00442
00447 struct WAUT {
00449 std::string id;
00451 std::string startProg;
00453 SUMOTime refTime;
00455 std::vector<WAUTSwitch> switches;
00457 std::vector<WAUTJunction> junctions;
00458 };
00459
00464 class WAUTSwitchProcedure {
00465 public:
00467 WAUTSwitchProcedure(MSTLLogicControl &control, WAUT &waut,
00468 MSTrafficLightLogic *from, MSTrafficLightLogic *to,
00469 bool synchron)
00470 : myFrom(from), myTo(to), mySwitchSynchron(synchron), myWAUT(waut), myControl(control) { }
00471
00473 virtual ~WAUTSwitchProcedure() { }
00474
00479 virtual bool trySwitch(SUMOTime step) = 0;
00480
00481
00482 bool isPosAtGSP(SUMOTime step, MSSimpleTrafficLightLogic *testLogic);
00483
00485 unsigned int getDiffToStartOfPhase(MSSimpleTrafficLightLogic *givenLogic, unsigned int pos);
00486
00487
00488 void switchToPos(SUMOTime simStep, MSSimpleTrafficLightLogic *givenLogic, unsigned int toPos);
00489
00490 protected:
00492 MSTrafficLightLogic *myFrom;
00493
00495 MSTrafficLightLogic *myTo;
00496
00498 bool mySwitchSynchron;
00499
00501 WAUT &myWAUT;
00502
00504 MSTLLogicControl &myControl;
00505
00507 SUMOReal getGSPValue(MSTrafficLightLogic *from) const;
00508
00509 private:
00511 WAUTSwitchProcedure(const WAUTSwitchProcedure&);
00512
00514 WAUTSwitchProcedure& operator=(const WAUTSwitchProcedure&);
00515
00516 };
00517
00518
00523 class WAUTSwitchProcedure_JustSwitch : public WAUTSwitchProcedure {
00524 public:
00526 WAUTSwitchProcedure_JustSwitch(MSTLLogicControl &control, WAUT &waut,
00527 MSTrafficLightLogic *from, MSTrafficLightLogic *to,
00528 bool synchron);
00529
00531 ~WAUTSwitchProcedure_JustSwitch();
00532
00537 bool trySwitch(SUMOTime step);
00538
00539 private:
00541 WAUTSwitchProcedure_JustSwitch(const WAUTSwitchProcedure_JustSwitch&);
00542
00544 WAUTSwitchProcedure_JustSwitch& operator=(const WAUTSwitchProcedure_JustSwitch&);
00545
00546 };
00547
00548
00553 class WAUTSwitchProcedure_GSP : public WAUTSwitchProcedure {
00554 public:
00556 WAUTSwitchProcedure_GSP(MSTLLogicControl &control, WAUT &waut,
00557 MSTrafficLightLogic *from, MSTrafficLightLogic *to,
00558 bool synchron);
00559
00561 ~WAUTSwitchProcedure_GSP();
00562
00565 bool trySwitch(SUMOTime step);
00566
00567 void adaptLogic(SUMOTime step);
00568
00569 private:
00571 WAUTSwitchProcedure_GSP(const WAUTSwitchProcedure_GSP&);
00572
00574 WAUTSwitchProcedure_GSP& operator=(const WAUTSwitchProcedure_GSP&);
00575
00576 };
00577
00578
00583 class WAUTSwitchProcedure_Stretch : public WAUTSwitchProcedure {
00584 public:
00586 WAUTSwitchProcedure_Stretch(MSTLLogicControl &control, WAUT &waut,
00587 MSTrafficLightLogic *from, MSTrafficLightLogic *to,
00588 bool synchron);
00589
00591 ~WAUTSwitchProcedure_Stretch();
00592
00595 bool trySwitch(SUMOTime step);
00596
00597 void adaptLogic(SUMOTime step, SUMOReal position);
00598
00599
00600
00601 void cutLogic(SUMOTime step, unsigned int pos, size_t deltaToCut);
00602
00603
00604 void stretchLogic(SUMOTime step, unsigned int startPos, size_t deltaToStretch);
00605
00606 protected:
00610 struct StretchBereichDef {
00612 SUMOReal begin;
00614 SUMOReal end;
00616 SUMOReal fac;
00617
00618 };
00619
00621 int getStretchBereicheNo(MSTrafficLightLogic *from) const;
00622
00627 StretchBereichDef getStretchBereichDef(MSTrafficLightLogic *from, int index) const;
00628
00629 private:
00631 WAUTSwitchProcedure_Stretch(const WAUTSwitchProcedure_Stretch&);
00632
00634 WAUTSwitchProcedure_Stretch& operator=(const WAUTSwitchProcedure_Stretch&);
00635
00636 };
00637
00638
00643 struct WAUTSwitchProcess {
00645 std::string junction;
00647 MSTrafficLightLogic *from;
00649 MSTrafficLightLogic *to;
00651 WAUTSwitchProcedure *proc;
00652 };
00653
00655 std::map<std::string, WAUT*> myWAUTs;
00656
00658 std::vector<WAUTSwitchProcess> myCurrentlySwitched;
00659
00661 std::map<std::string, TLSLogicVariants*> myLogics;
00662
00664 bool myNetWasLoaded;
00665
00666
00667 private:
00669 MSTLLogicControl(const MSTLLogicControl&);
00670
00672 MSTLLogicControl& operator=(const MSTLLogicControl&);
00673
00674 };
00675
00676
00677 #endif
00678
00679
00680