Eclipse SUMO - Simulation of Urban MObility
MSRailSignalControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // Centralized services for rail signal control (Singleton)
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <cassert>
23 #include <utility>
24 #include <vector>
25 #include <bitset>
26 #include <microsim/MSNet.h>
27 #include <microsim/MSRoute.h>
28 #include <microsim/MSEdge.h>
29 #include "MSRailSignal.h"
30 #include "MSRailSignalControl.h"
31 
32 
33 //#define DEBUG_REGISTER_DRIVEWAY
34 
35 // ===========================================================================
36 // static value definitions
37 // ===========================================================================
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
44 {}
45 
48  if (myInstance == nullptr) {
51  }
52  return *myInstance;
53 }
54 
55 void
57  delete myInstance;
58  myInstance = nullptr;
59 }
60 
62 }
63 
64 void
65 MSRailSignalControl::vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& /*info*/) {
67  if (isRailway(vehicle->getVClass())) {
68  for (const MSEdge* edge : vehicle->getRoute().getEdges()) {
69  myUsedEdges.insert(edge);
70  if (myProtectedDriveways.count(edge) != 0) {
71  updateDriveways(edge);
72  }
73  }
74  }
75  }
76 }
77 
78 
79 void
80 MSRailSignalControl::registerProtectedDriveway(MSRailSignal* rs, int driveWayID, const MSEdge* protectedBidi) {
81  myProtectedDriveways[protectedBidi].push_back(std::make_pair(rs, driveWayID));
82 #ifdef DEBUG_REGISTER_DRIVEWAY
83  std::cout << "MSRailSignalControl edge=" << protectedBidi->getID() << " assumed secure by driveway " << driveWayID << " at signal " << rs->getID() << "\n";
84 #endif
85 }
86 
87 void
89  for (auto item : myProtectedDriveways[used]) {
90  item.first->updateDriveway(item.second);
91  }
92  myProtectedDriveways.erase(used);
93 }
94 
95 /****************************************************************************/
bool isRailway(SVCPermissions permissions)
Returns whether an edge with the given permission is a railway edge.
A road/street connecting two junctions.
Definition: MSEdge.h:77
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:587
@ VEHICLE_STATE_NEWROUTE
The vehicle got a new route.
Definition: MSNet.h:599
@ VEHICLE_STATE_DEPARTED
The vehicle has departed (was inserted into the network)
Definition: MSNet.h:591
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
void addVehicleStateListener(VehicleStateListener *listener)
Adds a vehicle states listener.
Definition: MSNet.cpp:1054
A signal for rails.
void vehicleStateChanged(const SUMOVehicle *const vehicle, MSNet::VehicleState to, const std::string &info="")
Called if a vehicle changes its state.
std::map< const MSEdge *, std::vector< std::pair< MSRailSignal *, int > > > myProtectedDriveways
map of driveways that must perform additional checks if the key edge is used by a train route
MSRailSignalControl()
Constructor.
std::set< const MSEdge * > myUsedEdges
all rail edges that are part of a known route
static MSRailSignalControl * myInstance
static MSRailSignalControl & getInstance()
void updateDriveways(const MSEdge *used)
compute additioanl deadlock-check requirements for registered driveways
void registerProtectedDriveway(MSRailSignal *rs, int driveWayID, const MSEdge *protectedBidi)
mark driveway that must receive additional checks if protectedBidi is ever used by a train route
A signal for rails.
Definition: MSRailSignal.h:46
const ConstMSEdgeVector & getEdges() const
Definition: MSRoute.h:120
const std::string & getID() const
Returns the id.
Definition: Named.h:73
virtual SUMOVehicleClass getVClass() const =0
Returns the object's access class.
Representation of a vehicle.
Definition: SUMOVehicle.h:58
virtual const MSRoute & getRoute() const =0
Returns the current route.