Skip to content

Commit

Permalink
improving error handling and removal #1922
Browse files Browse the repository at this point in the history
  • Loading branch information
behrisch committed Mar 21, 2024
1 parent 08612b2 commit f3f6529
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 47 deletions.
50 changes: 16 additions & 34 deletions src/microsim/devices/MSDevice_FCDReplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
// static member initializations
// ===========================================================================
MSDevice_FCDReplay::FCDHandler MSDevice_FCDReplay::myHandler;
MSDevice_FCDReplay::Removal* MSDevice_FCDReplay::myRemovalCommand = nullptr;


// ===========================================================================
Expand Down Expand Up @@ -99,7 +98,8 @@ MSDevice_FCDReplay::notifyMove(SUMOTrafficObject& veh,
double /*newPos*/,
double /*newSpeed*/) {
if (myTrajectory == nullptr || myTrajectory->empty()) {
libsumo::Vehicle::remove(veh.getID());
// removal happens via the usual MSVehicle::hasArrived mechanism
// TODO we may need to set an arrivalPos
return false;
}
MSVehicle* v = dynamic_cast<MSVehicle*>(&veh);
Expand All @@ -113,14 +113,6 @@ MSDevice_FCDReplay::notifyMove(SUMOTrafficObject& veh,
std::get<4>(p), 7);
v->setPreviousSpeed(std::get<3>(p), std::numeric_limits<double>::min());
myTrajectory->erase(myTrajectory->begin());
if (myTrajectory->empty()) {
if (myRemovalCommand == nullptr) {
myRemovalCommand = new Removal();
MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(myRemovalCommand);
}
myRemovalCommand->schedule(v);
return false;
}
return true;
}

Expand Down Expand Up @@ -148,12 +140,14 @@ MSDevice_FCDReplay::FCDHandler::myStartElement(int element, const SUMOSAXAttribu
const double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, INVALID_DOUBLE);
myTrajectories[id].push_back(std::make_tuple(Position(x, y), edgeOrLane, pos, speed, angle));
const MSEdge* const edge = MSEdge::dictionary(isPerson ? edgeOrLane : SUMOXMLDefinitions::getEdgeIDFromLane(edgeOrLane));
if (myRoutes.count(id) == 0) {
myRoutes[id] = std::make_tuple(myTime, type, isPerson, ConstMSEdgeVector{edge});
} else {
auto& route = std::get<3>(myRoutes[id]);
if (!edge->isInternal() && edge != route.back()) {
route.push_back(edge);
if (edge != nullptr) { // TODO maybe warn for unknown edge?
if (myRoutes.count(id) == 0) {
myRoutes[id] = std::make_tuple(myTime, type, isPerson, ConstMSEdgeVector{edge});
} else {
auto& route = std::get<3>(myRoutes[id]);
if (!edge->isInternal() && edge != route.back()) {
route.push_back(edge);
}
}
}
return;
Expand Down Expand Up @@ -189,7 +183,9 @@ MSDevice_FCDReplay::FCDHandler::addTrafficObjects() {
plan->push_back(new MSStageWalking(id, std::get<3>(desc.second), nullptr, -1, params->departSpeed, params->departPos, 0, 0));
MSTransportable* person = MSNet::getInstance()->getPersonControl().buildPerson(params, vehicleType, plan, nullptr);
person->getSingularType().setVClass(SVC_IGNORING);
MSNet::getInstance()->getPersonControl().add(person);
if (!MSNet::getInstance()->getPersonControl().add(person)) {
throw ProcessError("Duplicate person '" + id + "'.");
}
MSTransportableDevice_FCDReplay* device = static_cast<MSTransportableDevice_FCDReplay*>(person->getDevice(typeid(MSTransportableDevice_FCDReplay)));
if (device == nullptr) { // Person did not get a replay device
// TODO delete person
Expand All @@ -205,7 +201,9 @@ MSDevice_FCDReplay::FCDHandler::addTrafficObjects() {
throw ProcessError("Could not add route '" + dummyRouteID + "'.");
}
SUMOVehicle* vehicle = MSNet::getInstance()->getVehicleControl().buildVehicle(params, route, vehicleType, false);
MSNet::getInstance()->getVehicleControl().addVehicle(id, vehicle);
if (!MSNet::getInstance()->getVehicleControl().addVehicle(id, vehicle)) {
throw ProcessError("Duplicate vehicle '" + id + "'.");
}
MSNet::getInstance()->getInsertionControl().add(vehicle);
MSDevice_FCDReplay* device = static_cast<MSDevice_FCDReplay*>(vehicle->getDevice(typeid(MSDevice_FCDReplay)));
if (device == nullptr) { // Vehicle did not get a replay device
Expand All @@ -219,20 +217,4 @@ MSDevice_FCDReplay::FCDHandler::addTrafficObjects() {
}


SUMOTime
MSDevice_FCDReplay::Removal::execute(SUMOTime /* currentTime */) {
for (MSVehicle* veh : myToRemove) {
libsumo::Vehicle::remove(veh->getID(), libsumo::REMOVE_ARRIVED);
}
myRemovalCommand = nullptr;
return 0;
}


void
MSDevice_FCDReplay::Removal::schedule(MSVehicle* v) {
myToRemove.push_back(v);
}


/****************************************************************************/
14 changes: 1 addition & 13 deletions src/microsim/devices/MSDevice_FCDReplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#pragma once
#include <config.h>

#include <utils/common/Command.h>
#include <utils/xml/SUMOSAXHandler.h>
#include "MSVehicleDevice.h"

Expand All @@ -30,7 +29,7 @@
// ===========================================================================
/**
* @class MSDevice_FCDReplay
* @brief A device which replays a vehiucle trajectory from an fcd file
* @brief A device which replays a vehicle trajectory from an fcd file
*
* @see MSDevice
*/
Expand Down Expand Up @@ -110,19 +109,8 @@ class MSDevice_FCDReplay : public MSVehicleDevice {
std::map<std::string, std::tuple<SUMOTime, std::string, bool, ConstMSEdgeVector > > myRoutes;
};

class Removal : public Command {
public:
SUMOTime execute(SUMOTime currentTime);
void schedule(MSVehicle* v);
private:
std::vector<MSVehicle*> myToRemove;
/// @brief Invalidated assignment operator.
Removal& operator=(const Removal&) = delete;
};

private:
static FCDHandler myHandler;
static Removal* myRemovalCommand;
Trajectory* myTrajectory = nullptr;

private:
Expand Down

0 comments on commit f3f6529

Please sign in to comment.