Eclipse SUMO - Simulation of Urban MObility
Command_RouteReplacement.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-2022 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/****************************************************************************/
19// Writes the state of the tls to a file (in each second)
20/****************************************************************************/
21#include <config.h>
22
24#include <microsim/MSNet.h>
26#include <microsim/MSRoute.h>
27#include <microsim/MSGlobals.h>
29
30
31// ===========================================================================
32// method definitions
33// ===========================================================================
34Command_RouteReplacement::Command_RouteReplacement(const std::string& vehID, const MSRoute* route) :
35 myVehID(vehID),
36 myRoute(route) {
38}
39
40
42
43
47 // if the vehicle is not available anymore, silently ignore replacement
48 if (veh != nullptr) {
49 std::string errorPrefix = ("Replayed route replacement failed for vehicle '"
50 + veh->getID() + "' route=" + myRoute->getID() + " time=" + time2string(SIMSTEP));
51 std::string msg;
52 if (!veh->hasValidRoute(msg, myRoute)) {
53 WRITE_WARNING("Invalid route replacement for vehicle '" + veh->getID() + "'. " + msg);
55 throw ProcessError(errorPrefix + ".");
56 }
57 }
58 std::string errorMsg;
59 if (!veh->replaceRoute(myRoute, "replayRerouting", veh->getLane() == nullptr,
60 veh->getRoute().getReplacedIndex(), true, true, &errorMsg)) {
61 throw ProcessError(errorPrefix + " (" + errorMsg + ").");
62 }
63 }
65 return 0;
66}
67
68/****************************************************************************/
long long int SUMOTime
Definition: GUI.h:36
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:265
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
#define SIMSTEP
Definition: SUMOTime.h:60
SUMOTime execute(SUMOTime currentTime)
toggles override
Command_RouteReplacement(const std::string &vehID, const MSRoute *route)
Constructor.
const MSRoute * myRoute
The replacement route.
std::string myVehID
The id of the vehicle to be rerouted.
static bool gCheckRoutes
Definition: MSGlobals.h:88
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:183
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:379
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:99
int getReplacedIndex() const
Returns the index at which this route was replaced.
Definition: MSRoute.h:186
void release() const
deletes the route if there are no further references to it
Definition: MSRoute.cpp:105
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
const std::string & getID() const
Returns the id.
Definition: Named.h:74
virtual const MSLane * getLane() const =0
Returns the lane the object is currently at.
Representation of a vehicle.
Definition: SUMOVehicle.h:60
virtual bool hasValidRoute(std::string &msg, const MSRoute *route=0) const =0
Validates the current or given route.
virtual bool replaceRoute(const MSRoute *route, const std::string &info, bool onInit=false, int offset=0, bool addStops=true, bool removeStops=true, std::string *msgReturn=nullptr)=0
Replaces the current route by the given one.
virtual const MSRoute & getRoute() const =0
Returns the current route.