Eclipse SUMO - Simulation of Urban MObility
MSDevice_Transportable.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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
20 // A device which is used to keep track of persons and containers riding with a vehicle
21 /****************************************************************************/
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
29 #include <microsim/MSNet.h>
30 #include <microsim/MSEdge.h>
33 #include <microsim/MSContainer.h>
34 #include "MSDevice_Transportable.h"
35 
36 
37 // ===========================================================================
38 // method definitions
39 // ===========================================================================
40 // ---------------------------------------------------------------------------
41 // static initialisation methods
42 // ---------------------------------------------------------------------------
44 MSDevice_Transportable::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into, const bool isContainer) {
45  MSDevice_Transportable* device = new MSDevice_Transportable(v, isContainer ? "container_" + v.getID() : "person_" + v.getID(), isContainer);
46  into.push_back(device);
47  return device;
48 }
49 
50 
51 // ---------------------------------------------------------------------------
52 // MSDevice_Transportable-methods
53 // ---------------------------------------------------------------------------
54 MSDevice_Transportable::MSDevice_Transportable(SUMOVehicle& holder, const std::string& id, const bool isContainer)
55  : MSVehicleDevice(holder, id), myAmContainer(isContainer), myTransportables(), myStopped(holder.isStopped()) {
56 }
57 
58 
60  // flush any unfortunate riders still remaining
61  while (!myTransportables.empty()) {
62  MSTransportable* transportable = myTransportables.front();
63  WRITE_WARNING((myAmContainer ? "Removing container '" : "Removing person '") + transportable->getID() +
64  "' at removal of vehicle '" + myHolder.getID() + "'");
65  if (myAmContainer) {
66  MSNet::getInstance()->getContainerControl().erase(transportable);
67  } else {
68  MSNet::getInstance()->getPersonControl().erase(transportable);
69  }
70  }
71 }
72 
73 void
75  const double /* frontOnLane */,
76  const double /* timeOnLane*/,
77  const double /* meanSpeedFrontOnLane */,
78  const double /*meanSpeedVehicleOnLane */,
79  const double /* travelledDistanceFrontOnLane */,
80  const double /* travelledDistanceVehicleOnLane */,
81  const double /* meanLengthOnLane */) {
82  notifyMove(const_cast<SUMOTrafficObject&>(veh), -1, -1, -1);
83 }
84 
85 
86 bool
87 MSDevice_Transportable::notifyMove(SUMOTrafficObject& veh, double /*oldPos*/, double /*newPos*/, double /*newSpeed*/) {
88  if (myStopped) {
89  if (!veh.isStopped()) {
90  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
91  (*i)->setDeparted(MSNet::getInstance()->getCurrentTimeStep());
92  }
93  myStopped = false;
94  }
95  } else {
96  if (veh.isStopped()) {
97  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end();) {
98  MSTransportable* transportable = *i;
99  if (transportable->getDestination() == veh.getEdge()) {
100  i = myTransportables.erase(i); // erase first in case proceed throws an exception
101  if (!transportable->proceed(MSNet::getInstance(), MSNet::getInstance()->getCurrentTimeStep())) {
102  if (myAmContainer) {
103  MSNet::getInstance()->getContainerControl().erase(transportable);
104  } else {
105  MSNet::getInstance()->getPersonControl().erase(transportable);
106  }
107  }
108  if (MSStopOut::active()) {
109  SUMOVehicle* vehicle = dynamic_cast<SUMOVehicle*>(&veh);
110  if (myAmContainer) {
112  } else {
114  }
115  }
116  } else {
117  ++i;
118  }
119  }
120  myStopped = true;
121  }
122  }
123  return true;
124 }
125 
126 
127 bool
130  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
131  (*i)->setDeparted(MSNet::getInstance()->getCurrentTimeStep());
132  }
133  }
134  return true;
135 }
136 
137 
138 bool
140  MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
141  if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
142  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end();) {
143  MSTransportable* transportable = *i;
144  if (transportable->getDestination() != veh.getEdge()) {
145  WRITE_WARNING((myAmContainer ? "Teleporting container '" : "Teleporting person '") + transportable->getID() +
146  "' from vehicle destination edge '" + veh.getEdge()->getID() +
147  "' to intended destination edge '" + transportable->getDestination()->getID() + "'");
148  }
149  if (!transportable->proceed(MSNet::getInstance(), MSNet::getInstance()->getCurrentTimeStep())) {
150  if (myAmContainer) {
151  MSNet::getInstance()->getContainerControl().erase(transportable);
152  } else {
153  MSNet::getInstance()->getPersonControl().erase(transportable);
154  }
155  }
156  i = myTransportables.erase(i);
157  }
158  }
159  return true;
160 }
161 
162 
163 void
165  myTransportables.push_back(transportable);
166  if (MSStopOut::active()) {
167  if (myAmContainer) {
169  } else {
171  }
172  }
173 }
174 
175 
176 void
178  auto it = std::find(myTransportables.begin(), myTransportables.end(), transportable);
179  if (it != myTransportables.end()) {
180  myTransportables.erase(it);
181  if (MSStopOut::active() && myHolder.isStopped()) {
182  if (myAmContainer) {
184  } else {
186  }
187  }
188  }
189 }
190 
191 
192 std::string
193 MSDevice_Transportable::getParameter(const std::string& key) const {
194  if (key == "IDList") {
195  std::vector<std::string> ids;
196  for (std::vector<MSTransportable*>::const_iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
197  ids.push_back((*i)->getID());
198  }
199  return toString(ids);
200  }
201  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
202 }
203 
204 
205 /****************************************************************************/
206 
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:48
MSDevice_Transportable::myTransportables
std::vector< MSTransportable * > myTransportables
The passengers of the vehicle.
Definition: MSDevice_Transportable.h:167
MSStopOut::unloadedContainers
void unloadedContainers(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:94
MSStopOut.h
MSTransportable::getDestination
const MSEdge * getDestination() const
Returns the current destination.
Definition: MSTransportable.h:618
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
MSNet.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
SUMOTrafficObject::getEdge
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
MSDevice_Transportable
Definition: MSDevice_Transportable.h:44
MSStopOut::getInstance
static MSStopOut * getInstance()
Definition: MSStopOut.h:63
MSDevice_Transportable.h
MSNet::getContainerControl
virtual MSTransportableControl & getContainerControl()
Returns the container control.
Definition: MSNet.cpp:806
SUMOTrafficObject::getID
virtual const std::string & getID() const =0
Get the vehicle's ID.
MSTransportableControl::erase
virtual void erase(MSTransportable *transportable)
removes a single transportable
Definition: MSTransportableControl.cpp:86
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:61
MSStopOut::loadedPersons
void loadedPersons(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:76
MSEdge.h
MSTransportable
Definition: MSTransportable.h:59
MSTransportable::proceed
virtual bool proceed(MSNet *net, SUMOTime time)=0
MSStopOut::loadedContainers
void loadedContainers(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:89
MSVehicleDevice::myHolder
SUMOVehicle & myHolder
The vehicle that stores the device.
Definition: MSVehicleDevice.h:85
MSDevice_Transportable::myAmContainer
bool myAmContainer
Whether it is a container device.
Definition: MSDevice_Transportable.h:164
MSTransportableControl.h
MSDevice_Transportable::myStopped
bool myStopped
Whether the vehicle is at a stop.
Definition: MSDevice_Transportable.h:170
MSDevice_Transportable::MSDevice_Transportable
MSDevice_Transportable(SUMOVehicle &holder, const std::string &id, const bool isContainer)
Constructor.
Definition: MSDevice_Transportable.cpp:54
MSNet::getCurrentTimeStep
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:284
MSDevice_Transportable::deviceName
const std::string deviceName() const
return the name for this type of device
Definition: MSDevice_Transportable.h:103
MSStopOut::active
static bool active()
Definition: MSStopOut.h:57
MSDevice_Transportable::getParameter
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key
Definition: MSDevice_Transportable.cpp:193
MSDevice_Transportable::buildVehicleDevices
static MSDevice_Transportable * buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into, const bool isContainer)
Build devices for the given vehicle, if needed.
Definition: MSDevice_Transportable.cpp:44
MSMoveReminder::NOTIFICATION_DEPARTED
The vehicle has departed (was inserted into the network)
Definition: MSMoveReminder.h:91
MSDevice_Transportable::notifyMove
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Checks whether the vehicle is at a stop and transportable action is needed.
Definition: MSDevice_Transportable.cpp:87
MSContainer.h
MSDevice_Transportable::removeTransportable
void removeTransportable(MSTransportable *transportable)
Remove a passenger (TraCI)
Definition: MSDevice_Transportable.cpp:177
MSPerson.h
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
MSDevice_Transportable::~MSDevice_Transportable
~MSDevice_Transportable()
Destructor.
Definition: MSDevice_Transportable.cpp:59
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
MSNet::getPersonControl
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:798
MSDevice_Transportable::addTransportable
void addTransportable(MSTransportable *transportable)
Add a passenger.
Definition: MSDevice_Transportable.cpp:164
MSMoveReminder::NOTIFICATION_ARRIVED
The vehicle arrived at its destination (is deleted)
Definition: MSMoveReminder.h:105
MSDevice_Transportable::notifyLeave
bool notifyLeave(SUMOTrafficObject &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Passengers leaving on arrival.
Definition: MSDevice_Transportable.cpp:139
MSStopOut::unloadedPersons
void unloadedPersons(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:84
InvalidArgument
Definition: UtilExceptions.h:57
MSTransportable::getID
const std::string & getID() const
returns the id of the transportable
Definition: MSTransportable.cpp:694
MSDevice_Transportable::notifyMoveInternal
void notifyMoveInternal(const SUMOTrafficObject &veh, const double frontOnLane, const double timeOnLane, const double meanSpeedFrontOnLane, const double meanSpeedVehicleOnLane, const double travelledDistanceFrontOnLane, const double travelledDistanceVehicleOnLane, const double)
Internal notification about the vehicle moves, see MSMoveReminder::notifyMoveInternal()
Definition: MSDevice_Transportable.cpp:74
config.h
MSDevice_Transportable::notifyEnter
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Adds passengers on vehicle insertion.
Definition: MSDevice_Transportable.cpp:128
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:77
MSMoveReminder::Notification
Notification
Definition of a vehicle state.
Definition: MSMoveReminder.h:89
SUMOTrafficObject::isStopped
virtual bool isStopped() const =0
Returns whether the vehicle is at a stop.
MSVehicleDevice
Abstract in-vehicle device.
Definition: MSVehicleDevice.h:55