49 : myCurrentProgram(nullptr) {
54 std::map<std::string, MSTrafficLightLogic*>::const_iterator j;
55 for (std::map<std::string, MSTrafficLightLogic*>::iterator j = myVariants.begin(); j != myVariants.end(); ++j) {
58 for (std::vector<OnSwitchAction*>::iterator i = mySwitchActions.begin(); i != mySwitchActions.end(); ++i) {
66 bool hadErrors =
false;
67 for (std::map<std::string, MSTrafficLightLogic*>::const_iterator j = myVariants.begin(); j != myVariants.end(); ++j) {
69 int linkNo = (int)(*j).second->getLinks().size();
70 bool hadProgramErrors =
false;
71 for (MSTrafficLightLogic::Phases::const_iterator i = phases.begin(); i != phases.end(); ++i) {
72 if ((
int)(*i)->getState().length() < linkNo) {
73 hadProgramErrors =
true;
76 if (hadProgramErrors) {
77 WRITE_ERROR(
"Mismatching phase size in tls '" + (*j).second->getID() +
"', program '" + (*j).first +
"'.");
87 myOriginalLinkStates = myCurrentProgram->collectLinkStates();
94 if (myVariants.find(programID) != myVariants.end()) {
100 if (myCurrentProgram ==
nullptr) {
101 throw ProcessError(
"No initial signal plan loaded for tls '" + logic->
getID() +
"'.");
105 throw ProcessError(
"Mismatching phase size in tls '" + logic->
getID() +
"', program '" + programID +
"'.");
109 if (myVariants.size() == 0 || isNewDefault) {
110 if (myCurrentProgram !=
nullptr) {
111 myCurrentProgram->deactivateProgram();
113 myCurrentProgram = logic;
117 myVariants[programID] = logic;
118 if (myVariants.size() == 1 || isNewDefault) {
120 executeOnSwitchActions();
128 if (myVariants.find(programID) == myVariants.end()) {
131 return myVariants.find(programID)->second;
137 const std::string& programID) {
138 if (myVariants.find(programID) == myVariants.end()) {
139 if (programID ==
"off") {
143 throw ProcessError(
"Could not build an off-state for tls '" + myCurrentProgram->getID() +
"'.");
147 throw ProcessError(
"Can not switch tls '" + myCurrentProgram->getID() +
"' to program '" + programID +
"';\n The program is not known.");
150 return getLogic(programID);
156 const std::string& state) {
159 if (logic ==
nullptr) {
161 std::vector<MSPhaseDefinition*> phases;
162 phases.push_back(phase);
165 std::map<std::string, std::string>());
166 addLogic(
"online", logic,
true,
true);
178 mySwitchActions.push_back(c);
182 std::vector<MSTrafficLightLogic*>
184 std::vector<MSTrafficLightLogic*> ret;
185 std::map<std::string, MSTrafficLightLogic*>::const_iterator i;
186 for (i = myVariants.begin(); i != myVariants.end(); ++i) {
187 ret.push_back((*i).second);
195 return tl == myCurrentProgram;
201 return myCurrentProgram;
208 myCurrentProgram->deactivateProgram();
209 myCurrentProgram = getLogicInstantiatingOff(tlc, programID);
210 myCurrentProgram->activateProgram();
212 executeOnSwitchActions();
218 for (std::vector<OnSwitchAction*>::const_iterator i = mySwitchActions.begin(); i != mySwitchActions.end(); ++i) {
226 for (std::map<std::string, MSTrafficLightLogic*>::iterator i = myVariants.begin(); i != myVariants.end(); ++i) {
227 (*i).second->addLink(link, lane, pos);
233 for (std::map<std::string, MSTrafficLightLogic*>::iterator i = myVariants.begin(); i != myVariants.end(); ++i) {
234 (*i).second->ignoreLinkIndex(pos);
248 if (val.length() == 0) {
260 return gspTime == programTime;
268 assert(toTime >= startOfPhase);
269 return toTime - startOfPhase;
276 SUMOTime diff = getDiffToStartOfPhase(logic, toTime);
318 if (isPosAtGSP(step, *myFrom)) {
320 if (mySwitchSynchron) {
323 switchToPos(step, *myTo,
TIME2STEPS(getGSPValue(*myTo)));
336 int stepTo = myTo->getIndexFromOffset(gspTo);
337 SUMOTime cycleTimeTo = myTo->getDefaultCycleTime();
338 if (gspTo == cycleTimeTo) {
342 SUMOTime currentPosTo = myTo->getOffsetFromIndex(myTo->getCurrentPhaseIndex());
343 currentPosTo += (myTo->getCurrentPhaseDef().duration - (myTo->getNextSwitchTime() - step));
344 SUMOTime diff = getDiffToStartOfPhase(*myTo, gspTo);
347 if (gspTo >= currentPosTo) {
348 deltaToStretch = (gspTo - currentPosTo);
350 deltaToStretch = (cycleTimeTo - currentPosTo + gspTo);
352 const SUMOTime newdur = myTo->getPhase(stepTo).duration - diff + deltaToStretch;
353 myTo->changeStepAndDuration(myControl, step, stepTo, newdur);
373 if (isPosAtGSP(step, *myFrom)) {
375 if (mySwitchSynchron) {
378 switchToPos(step, *myTo,
TIME2STEPS(getGSPValue(*myTo)));
391 SUMOTime cycleTime = myTo->getDefaultCycleTime();
393 SUMOTime posAfterSyn = myTo->getPhaseIndexAtTime(step);
396 if (posAfterSyn < gspTo) {
397 deltaToCut = posAfterSyn + cycleTime - gspTo;
399 deltaToCut = posAfterSyn - gspTo;
403 int areasNo = getStretchAreaNo(myTo);
404 for (
int i = 0; i < areasNo; i++) {
410 deltaPossible = stretchUmlaufAnz * deltaPossible;
411 if ((deltaPossible > deltaToCut) && (deltaToCut < (cycleTime / 2))) {
412 cutLogic(step, gspTo, deltaToCut);
414 SUMOTime deltaToStretch = (cycleTime - deltaToCut) % cycleTime;
415 stretchLogic(step, gspTo, deltaToStretch);
422 int actStep = myTo->getIndexFromOffset(startPos);
424 int areasNo = getStretchAreaNo(myTo);
426 for (
int i = 0; i < areasNo; i++) {
430 int stepOfBegin = myTo->getIndexFromOffset(begin);
431 if (stepOfBegin == actStep) {
432 if (begin < startPos) {
433 toCut = end - startPos;
437 toCut =
MIN2(allCutTime, toCut);
438 allCutTime = allCutTime - toCut;
441 SUMOTime remainingDur = myTo->getPhase(actStep).duration - getDiffToStartOfPhase(*myTo, startPos);
442 SUMOTime newDur = remainingDur - toCut;
443 myTo->changeStepAndDuration(myControl, step, actStep, newDur);
446 int currStep = (actStep + 1) % (
int)myTo->getPhases().size();
447 while (allCutTime > 0) {
448 for (
int i = currStep; i < (int) myTo->getPhases().size(); i++) {
449 SUMOTime beginOfPhase = myTo->getOffsetFromIndex(i);
450 SUMOTime durOfPhase = myTo->getPhase(i).duration;
451 SUMOTime endOfPhase = beginOfPhase + durOfPhase;
452 for (
int i = 0; i < areasNo; i++) {
456 if ((beginOfPhase <= begin) && (endOfPhase >= end)) {
458 allCutTime = allCutTime - maxCutOfPhase;
459 durOfPhase = durOfPhase - maxCutOfPhase;
462 myTo->addOverridingDuration(durOfPhase);
470 int currStep = myTo->getIndexFromOffset(startPos);
471 SUMOTime durOfPhase = myTo->getPhase(currStep).duration;
472 SUMOTime remainingStretchTime = allStretchTime;
476 int areasNo = getStretchAreaNo(myTo);
477 for (
int x = 0; x < areasNo; x++) {
481 facSum *= stretchUmlaufAnz;
484 SUMOTime diffToStart = getDiffToStartOfPhase(*myTo, startPos);
485 for (
int x = 0; x < areasNo; x++) {
488 SUMOTime endOfPhase = (startPos + durOfPhase - diffToStart);
489 if (end <= endOfPhase && end >= startPos) {
490 double fac = def.
fac;
491 double actualfac = fac / facSum;
492 facSum = facSum - fac;
494 remainingStretchTime = allStretchTime - StretchTimeOfPhase;
498 WRITE_WARNING(
"The computed factor sum in WAUT '" + myWAUT.id +
"' at time '" +
toString(
STEPS2TIME(step)) +
"' equals zero;\n assuming an error in WAUT definition.");
501 durOfPhase = durOfPhase - diffToStart + StretchTimeOfPhase;
502 myTo->changeStepAndDuration(myControl, step, currStep, durOfPhase);
504 currStep = (currStep + 1) % (
int)myTo->getPhases().size();
506 while (remainingStretchTime > 0) {
507 for (
int i = currStep; i < (int)myTo->getPhases().size() && remainingStretchTime > 0; i++) {
508 durOfPhase = myTo->getPhase(i).duration;
509 SUMOTime beginOfPhase = myTo->getOffsetFromIndex(i);
510 SUMOTime endOfPhase = beginOfPhase + durOfPhase;
511 for (
int j = 0; j < areasNo && remainingStretchTime > 0; j++) {
514 double fac = def.
fac;
515 if ((beginOfPhase <= end) && (endOfPhase >= end)) {
516 double actualfac = fac / facSum;
519 durOfPhase += StretchTimeOfPhase;
520 remainingStretchTime -= StretchTimeOfPhase;
523 myTo->addOverridingDuration(durOfPhase);
559 for (std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {
563 for (std::map<std::string, WAUT*>::const_iterator i =
myWAUTs.begin(); i !=
myWAUTs.end(); ++i) {
571 for (std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {
572 (*i).second->getActive()->setTrafficLightSignals(t);
577 std::vector<MSTrafficLightLogic*>
579 std::vector<MSTrafficLightLogic*> ret;
580 std::map<std::string, TLSLogicVariants*>::const_iterator i;
582 std::vector<MSTrafficLightLogic*> s = (*i).second->getAllLogics();
583 copy(s.begin(), s.end(), back_inserter(ret));
590 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(
id);
600 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(
id);
604 return (*i).second->getLogic(programID);
608 std::vector<std::string>
610 std::vector<std::string> ret;
611 for (std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {
612 ret.push_back((*i).first);
624 std::map<std::string, TLSLogicVariants*>::iterator i =
myLogics.find(
id);
632 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(
id);
642 bool hadErrors =
false;
643 for (std::map<std::string, TLSLogicVariants*>::iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {
644 hadErrors |= !(*i).second->checkOriginalTLS();
645 (*i).second->saveInitialStates();
654 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(tl->
getID());
658 return (*i).second->isActive(tl);
664 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(
id);
668 return (*i).second->getActive();
675 std::map<std::string, TLSLogicVariants*>::iterator i =
myLogics.find(
id);
678 throw ProcessError(
"Could not switch tls '" +
id +
"' to program '" + programID +
"': No such tls exists.");
680 (*i).second->switchTo(*
this, programID);
686 const std::string& startProg) {
702 SUMOTime when,
const std::string& to) {
711 s.
when = (
myWAUTs[wautid]->refTime + when) % 86400000;
712 myWAUTs[wautid]->switches.push_back(s);
718 const std::string& tls,
719 const std::string& proc,
729 throw InvalidArgument(
"TLS '" + tls +
"' to switch in WAUT '" + wautid +
"' was not yet defined.");
735 myWAUTs[wautid]->junctions.push_back(j);
737 std::string initProg =
myWAUTs[wautid]->startProg;
738 std::vector<WAUTSwitch>::const_iterator first =
myWAUTs[wautid]->switches.end();
740 for (std::vector<WAUTSwitch>::const_iterator i =
myWAUTs[wautid]->switches.begin(); i !=
myWAUTs[wautid]->switches.end(); ++i) {
742 minExecTime = (*i).when;
745 if (first !=
myWAUTs[wautid]->switches.begin()) {
746 initProg = (*(first - 1)).to;
762 std::string initProg =
myWAUTs[wautid]->startProg;
764 std::vector<WAUTSwitch>::const_iterator first = w->
switches.end();
766 for (std::vector<WAUTSwitch>::const_iterator i = w->
switches.begin(); i != w->
switches.end(); ++i) {
768 minExecTime = (*i).when;
774 std::vector<WAUTSwitch>::const_iterator mbegin = w->
switches.begin();
790 const std::string& wautid = cmd.
getWAUTID();
793 for (std::vector<WAUTJunction>::iterator i =
myWAUTs[wautid]->junctions.begin(); i !=
myWAUTs[wautid]->junctions.end(); ++i) {
799 if ((*i).procedure ==
"GSP") {
801 }
else if ((*i).procedure ==
"Stretch") {
816 if (index ==
static_cast<int>(
myWAUTs[wautid]->switches.size())) {
829 switchTo((*i).to->getID(), (*i).to->getProgramID());
838 std::pair<SUMOTime, MSPhaseDefinition>
847 for (std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {