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