50 : myCurrentProgram(nullptr) {
55 std::map<std::string, MSTrafficLightLogic*>::const_iterator j;
56 for (std::map<std::string, MSTrafficLightLogic*>::iterator j = myVariants.begin(); j != myVariants.end(); ++j) {
59 for (std::vector<OnSwitchAction*>::iterator i = mySwitchActions.begin(); i != mySwitchActions.end(); ++i) {
67 bool hadErrors =
false;
68 for (std::map<std::string, MSTrafficLightLogic*>::const_iterator j = myVariants.begin(); j != myVariants.end(); ++j) {
70 int linkNo = (int)(*j).second->getLinks().size();
71 bool hadProgramErrors =
false;
72 for (MSTrafficLightLogic::Phases::const_iterator i = phases.begin(); i != phases.end(); ++i) {
73 if ((
int)(*i)->getState().length() < linkNo) {
74 hadProgramErrors =
true;
77 if (hadProgramErrors) {
78 WRITE_ERROR(
"Mismatching phase size in tls '" + (*j).second->getID() +
"', program '" + (*j).first +
"'.");
88 myOriginalLinkStates = myCurrentProgram->collectLinkStates();
95 if (myVariants.find(programID) != myVariants.end()) {
101 if (myCurrentProgram ==
nullptr) {
102 throw ProcessError(
"No initial signal plan loaded for tls '" + logic->
getID() +
"'.");
106 throw ProcessError(
"Mismatching phase size in tls '" + logic->
getID() +
"', program '" + programID +
"'.");
110 if (myVariants.size() == 0 || isNewDefault) {
111 myCurrentProgram = logic;
114 myVariants[programID] = logic;
115 if (myVariants.size() == 1 || isNewDefault) {
117 executeOnSwitchActions();
125 if (myVariants.find(programID) == myVariants.end()) {
128 return myVariants.find(programID)->second;
134 const std::string& programID) {
135 if (myVariants.find(programID) == myVariants.end()) {
136 if (programID ==
"off") {
140 throw ProcessError(
"Could not build an off-state for tls '" + myCurrentProgram->getID() +
"'.");
144 throw ProcessError(
"Can not switch tls '" + myCurrentProgram->getID() +
"' to program '" + programID +
"';\n The program is not known.");
147 return getLogic(programID);
153 const std::string& state) {
156 if (logic ==
nullptr) {
158 std::vector<MSPhaseDefinition*> phases;
159 phases.push_back(phase);
162 std::map<std::string, std::string>());
163 addLogic(
"online", logic,
true,
true);
167 *(dynamic_cast<MSSimpleTrafficLightLogic*>(logic)->getPhases()[0]) = nphase;
175 mySwitchActions.push_back(c);
179 std::vector<MSTrafficLightLogic*>
181 std::vector<MSTrafficLightLogic*> ret;
182 std::map<std::string, MSTrafficLightLogic*>::const_iterator i;
183 for (i = myVariants.begin(); i != myVariants.end(); ++i) {
184 ret.push_back((*i).second);
192 return tl == myCurrentProgram;
198 return myCurrentProgram;
205 myCurrentProgram = getLogicInstantiatingOff(tlc, programID);
207 executeOnSwitchActions();
213 for (std::vector<OnSwitchAction*>::const_iterator i = mySwitchActions.begin(); i != mySwitchActions.end(); ++i) {
221 for (std::map<std::string, MSTrafficLightLogic*>::iterator i = myVariants.begin(); i != myVariants.end(); ++i) {
222 (*i).second->addLink(link, lane, pos);
228 for (std::map<std::string, MSTrafficLightLogic*>::iterator i = myVariants.begin(); i != myVariants.end(); ++i) {
229 (*i).second->ignoreLinkIndex(pos);
243 if (val.length() == 0) {
255 return gspTime == programTime;
263 assert(toTime >= startOfPhase);
264 return toTime - startOfPhase;
271 SUMOTime diff = getDiffToStartOfPhase(logic, toTime);
313 if (isPosAtGSP(step, *myFrom)) {
315 if (mySwitchSynchron) {
318 switchToPos(step, *myTo,
TIME2STEPS(getGSPValue(*myTo)));
331 int stepTo = myTo->getIndexFromOffset(gspTo);
332 SUMOTime cycleTimeTo = myTo->getDefaultCycleTime();
333 if (gspTo == cycleTimeTo) {
337 SUMOTime currentPosTo = myTo->getOffsetFromIndex(myTo->getCurrentPhaseIndex());
338 currentPosTo += (myTo->getCurrentPhaseDef().duration - (myTo->getNextSwitchTime() - step));
339 SUMOTime diff = getDiffToStartOfPhase(*myTo, gspTo);
342 if (gspTo >= currentPosTo) {
343 deltaToStretch = (gspTo - currentPosTo);
345 deltaToStretch = (cycleTimeTo - currentPosTo + gspTo);
347 const SUMOTime newdur = myTo->getPhase(stepTo).duration - diff + deltaToStretch;
348 myTo->changeStepAndDuration(myControl, step, stepTo, newdur);
368 if (isPosAtGSP(step, *myFrom)) {
370 if (mySwitchSynchron) {
373 switchToPos(step, *myTo,
TIME2STEPS(getGSPValue(*myTo)));
386 SUMOTime cycleTime = myTo->getDefaultCycleTime();
388 SUMOTime posAfterSyn = myTo->getPhaseIndexAtTime(step);
391 if (posAfterSyn < gspTo) {
392 deltaToCut = posAfterSyn + cycleTime - gspTo;
394 deltaToCut = posAfterSyn - gspTo;
398 int areasNo = getStretchAreaNo(myTo);
399 for (
int i = 0; i < areasNo; i++) {
405 deltaPossible = stretchUmlaufAnz * deltaPossible;
406 if ((deltaPossible > deltaToCut) && (deltaToCut < (cycleTime / 2))) {
407 cutLogic(step, gspTo, deltaToCut);
409 SUMOTime deltaToStretch = (cycleTime - deltaToCut) % cycleTime;
410 stretchLogic(step, gspTo, deltaToStretch);
417 int actStep = myTo->getIndexFromOffset(startPos);
419 int areasNo = getStretchAreaNo(myTo);
421 for (
int i = 0; i < areasNo; i++) {
425 int stepOfBegin = myTo->getIndexFromOffset(begin);
426 if (stepOfBegin == actStep) {
427 if (begin < startPos) {
428 toCut = end - startPos;
432 toCut =
MIN2(allCutTime, toCut);
433 allCutTime = allCutTime - toCut;
436 SUMOTime remainingDur = myTo->getPhase(actStep).duration - getDiffToStartOfPhase(*myTo, startPos);
437 SUMOTime newDur = remainingDur - toCut;
438 myTo->changeStepAndDuration(myControl, step, actStep, newDur);
441 int currStep = (actStep + 1) % (
int)myTo->getPhases().size();
442 while (allCutTime > 0) {
443 for (
int i = currStep; i < (int) myTo->getPhases().size(); i++) {
444 SUMOTime beginOfPhase = myTo->getOffsetFromIndex(i);
445 SUMOTime durOfPhase = myTo->getPhase(i).duration;
446 SUMOTime endOfPhase = beginOfPhase + durOfPhase;
447 for (
int i = 0; i < areasNo; i++) {
451 if ((beginOfPhase <= begin) && (endOfPhase >= end)) {
453 allCutTime = allCutTime - maxCutOfPhase;
454 durOfPhase = durOfPhase - maxCutOfPhase;
457 myTo->addOverridingDuration(durOfPhase);
465 int currStep = myTo->getIndexFromOffset(startPos);
466 SUMOTime durOfPhase = myTo->getPhase(currStep).duration;
467 SUMOTime remainingStretchTime = allStretchTime;
471 int areasNo = getStretchAreaNo(myTo);
472 for (
int x = 0; x < areasNo; x++) {
476 facSum *= stretchUmlaufAnz;
479 SUMOTime diffToStart = getDiffToStartOfPhase(*myTo, startPos);
480 for (
int x = 0; x < areasNo; x++) {
483 SUMOTime endOfPhase = (startPos + durOfPhase - diffToStart);
484 if (end <= endOfPhase && end >= startPos) {
485 double fac = def.
fac;
486 double actualfac = fac / facSum;
487 facSum = facSum - fac;
489 remainingStretchTime = allStretchTime - StretchTimeOfPhase;
493 WRITE_WARNING(
"The computed factor sum in WAUT '" + myWAUT.id +
"' at time '" +
toString(
STEPS2TIME(step)) +
"' equals zero;\n assuming an error in WAUT definition.");
496 durOfPhase = durOfPhase - diffToStart + StretchTimeOfPhase;
497 myTo->changeStepAndDuration(myControl, step, currStep, durOfPhase);
499 currStep = (currStep + 1) % (
int)myTo->getPhases().size();
501 while (remainingStretchTime > 0) {
502 for (
int i = currStep; i < (int)myTo->getPhases().size() && remainingStretchTime > 0; i++) {
503 durOfPhase = myTo->getPhase(i).duration;
504 SUMOTime beginOfPhase = myTo->getOffsetFromIndex(i);
505 SUMOTime endOfPhase = beginOfPhase + durOfPhase;
506 for (
int j = 0; j < areasNo && remainingStretchTime > 0; j++) {
509 double fac = def.
fac;
510 if ((beginOfPhase <= end) && (endOfPhase >= end)) {
511 double actualfac = fac / facSum;
514 durOfPhase += StretchTimeOfPhase;
515 remainingStretchTime -= StretchTimeOfPhase;
518 myTo->addOverridingDuration(durOfPhase);
554 for (std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {
558 for (std::map<std::string, WAUT*>::const_iterator i =
myWAUTs.begin(); i !=
myWAUTs.end(); ++i) {
566 for (std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {
567 (*i).second->getActive()->setTrafficLightSignals(t);
572 std::vector<MSTrafficLightLogic*>
574 std::vector<MSTrafficLightLogic*> ret;
575 std::map<std::string, TLSLogicVariants*>::const_iterator i;
577 std::vector<MSTrafficLightLogic*> s = (*i).second->getAllLogics();
578 copy(s.begin(), s.end(), back_inserter(ret));
585 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(
id);
595 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(
id);
599 return (*i).second->getLogic(programID);
603 std::vector<std::string>
605 std::vector<std::string> ret;
606 for (std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {
607 ret.push_back((*i).first);
619 std::map<std::string, TLSLogicVariants*>::iterator i =
myLogics.find(
id);
627 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(
id);
637 bool hadErrors =
false;
638 for (std::map<std::string, TLSLogicVariants*>::iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {
639 hadErrors |= !(*i).second->checkOriginalTLS();
640 (*i).second->saveInitialStates();
649 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(tl->
getID());
653 return (*i).second->isActive(tl);
659 std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.find(
id);
663 return (*i).second->getActive();
670 std::map<std::string, TLSLogicVariants*>::iterator i =
myLogics.find(
id);
673 throw ProcessError(
"Could not switch tls '" +
id +
"' to program '" + programID +
"': No such tls exists.");
675 (*i).second->switchTo(*
this, programID);
681 const std::string& startProg) {
697 SUMOTime when,
const std::string& to) {
706 s.
when = (
myWAUTs[wautid]->refTime + when) % 86400000;
707 myWAUTs[wautid]->switches.push_back(s);
713 const std::string& tls,
714 const std::string& proc,
724 throw InvalidArgument(
"TLS '" + tls +
"' to switch in WAUT '" + wautid +
"' was not yet defined.");
730 myWAUTs[wautid]->junctions.push_back(j);
732 std::string initProg =
myWAUTs[wautid]->startProg;
733 std::vector<WAUTSwitch>::const_iterator first =
myWAUTs[wautid]->switches.end();
735 for (std::vector<WAUTSwitch>::const_iterator i =
myWAUTs[wautid]->switches.begin(); i !=
myWAUTs[wautid]->switches.end(); ++i) {
737 minExecTime = (*i).when;
740 if (first !=
myWAUTs[wautid]->switches.begin()) {
741 initProg = (*(first - 1)).to;
757 std::string initProg =
myWAUTs[wautid]->startProg;
759 std::vector<WAUTSwitch>::const_iterator first = w->
switches.end();
761 for (std::vector<WAUTSwitch>::const_iterator i = w->
switches.begin(); i != w->
switches.end(); ++i) {
763 minExecTime = (*i).when;
769 std::vector<WAUTSwitch>::const_iterator mbegin = w->
switches.begin();
785 const std::string& wautid = cmd.
getWAUTID();
788 for (std::vector<WAUTJunction>::iterator i =
myWAUTs[wautid]->junctions.begin(); i !=
myWAUTs[wautid]->junctions.end(); ++i) {
794 if ((*i).procedure ==
"GSP") {
796 }
else if ((*i).procedure ==
"Stretch") {
811 if (index == static_cast<int>(
myWAUTs[wautid]->switches.size())) {
824 switchTo((*i).to->getID(), (*i).to->getProgramID());
833 std::pair<SUMOTime, MSPhaseDefinition>
842 for (std::map<std::string, TLSLogicVariants*>::const_iterator i =
myLogics.begin(); i !=
myLogics.end(); ++i) {