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 
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Checks whether the vehicle is at a stop and transportable action is needed.
static bool active()
Definition: MSStopOut.h:57
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
bool notifyLeave(SUMOTrafficObject &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Passengers leaving on arrival.
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
Notification
Definition of a vehicle state.
SUMOVehicle & myHolder
The vehicle that stores the device.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
virtual bool proceed(MSNet *net, SUMOTime time)=0
const std::string & getID() const
Returns the id.
Definition: Named.h:77
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
virtual void erase(MSTransportable *transportable)
removes a single transportable
void loadedContainers(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:89
virtual MSTransportableControl & getContainerControl()
Returns the container control.
Definition: MSNet.cpp:806
void unloadedPersons(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:84
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
Representation of a vehicle.
Definition: SUMOVehicle.h:61
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() ...
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:798
const std::string deviceName() const
return the name for this type of device
The vehicle arrived at its destination (is deleted)
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:284
const std::string & getID() const
returns the id of the transportable
void addTransportable(MSTransportable *transportable)
Add a passenger.
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Adds passengers on vehicle insertion.
bool myAmContainer
Whether it is a container device.
The vehicle has departed (was inserted into the network)
Representation of a vehicle or person.
void unloadedContainers(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:94
Abstract in-vehicle device.
static MSStopOut * getInstance()
Definition: MSStopOut.h:63
const MSEdge * getDestination() const
Returns the current destination.
MSDevice_Transportable(SUMOVehicle &holder, const std::string &id, const bool isContainer)
Constructor.
bool myStopped
Whether the vehicle is at a stop.
void loadedPersons(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:76
std::vector< MSTransportable * > myTransportables
The passengers of the vehicle.
void removeTransportable(MSTransportable *transportable)
Remove a passenger (TraCI)
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
virtual bool isStopped() const =0
Returns whether the vehicle is at a stop.
static MSDevice_Transportable * buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into, const bool isContainer)
Build devices for the given vehicle, if needed.
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key ...