Line data Source code
1 : #include "Dynamic/Env/Event/EventUpdateTrafficLight.hpp" 2 : 3 : #include <functional> 4 : #include <stdexcept> 5 : 6 : #include "Dynamic/Env/Env.hpp" 7 : #include "Dynamic/Env/Event/EventPopQueue.hpp" 8 : #include "Dynamic/Env/Lane.hpp" 9 : #include "Dynamic/Env/Position.hpp" 10 : #include "Dynamic/Env/TrafficLight.hpp" 11 : #include "utils/alg.hpp" 12 : 13 : using namespace std; 14 : using namespace Dynamic::Env; 15 : 16 0 : EventUpdateTrafficLight::EventUpdateTrafficLight( 17 : Time t_, 18 : TrafficLight &trafficLight_, 19 : const TrafficLight::Phase &phase_ 20 0 : ): 21 0 : Event(t_), trafficLight(trafficLight_), phase(phase_) {} 22 : 23 0 : void EventUpdateTrafficLight::process(Env &env) { 24 0 : trafficLight.currentPhase = &phase; 25 : 26 0 : const TrafficLight::Phase &next = phase.next(); 27 : 28 0 : env.pushEvent(make_shared<EventUpdateTrafficLight>( 29 0 : getTime() + phase.duration, 30 : trafficLight, 31 : next 32 : )); 33 : 34 0 : set<reference_wrapper<Lane>, less<Lane>> lanes; 35 0 : for(const Connection &connection: trafficLight.connections) { 36 0 : if(connection.isRed()) continue; 37 0 : lanes.insert(connection.fromLane); 38 : } 39 : 40 0 : vector<reference_wrapper<Connection>> connections; 41 0 : for(const Lane &lane: lanes) { 42 0 : if(lane.stopped.empty()) continue; 43 : 44 0 : Connection &connection = lane.stopped.front().second->connection; 45 : 46 0 : if(connection.isRed()) continue; 47 : 48 0 : connections.push_back(connection); 49 : } 50 : 51 0 : try { 52 0 : utils::alg::topological_sort( 53 : connections, 54 : [](const Connection &lhs, const Connection &rhs) -> bool { 55 : return rhs.yieldsTo(lhs); 56 : } 57 : ); 58 0 : } catch(const std::invalid_argument &e) { 59 0 : stringstream ss; 60 0 : ss << "e.what(): " << e.what() << "; " 61 0 : << "exception happened in traffic light " << trafficLight.id << " " 62 0 : << " with connections"; 63 0 : for(const Connection &connection: trafficLight.connections) { 64 0 : ss << " " 65 0 : << connection.fromLane.idAsString() << "→" 66 0 : << connection.toLane.idAsString(); 67 : } 68 0 : throw std::invalid_argument(ss.str()); 69 : } 70 : 71 0 : for(Connection &connection: connections) { 72 0 : env.pushEvent(make_shared<EventPopQueue>( 73 0 : env.getTime(), 74 : connection.fromLane 75 : )); 76 : } 77 0 : }