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-2018 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 }
61 
62 void
64  const double /* frontOnLane */,
65  const double /* timeOnLane*/,
66  const double /* meanSpeedFrontOnLane */,
67  const double /*meanSpeedVehicleOnLane */,
68  const double /* travelledDistanceFrontOnLane */,
69  const double /* travelledDistanceVehicleOnLane */,
70  const double /* meanLengthOnLane */) {
71  notifyMove(const_cast<SUMOVehicle&>(veh), -1, -1, -1);
72 }
73 
74 
75 bool
76 MSDevice_Transportable::notifyMove(SUMOVehicle& veh, double /*oldPos*/, double /*newPos*/, double /*newSpeed*/) {
77  if (myStopped) {
78  if (!veh.isStopped()) {
79  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
80  (*i)->setDeparted(MSNet::getInstance()->getCurrentTimeStep());
81  }
82  myStopped = false;
83  }
84  } else {
85  if (veh.isStopped()) {
86  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end();) {
87  MSTransportable* transportable = *i;
88  if (transportable->getDestination() == veh.getEdge()) {
89  if (!transportable->proceed(MSNet::getInstance(), MSNet::getInstance()->getCurrentTimeStep())) {
90  if (myAmContainer) {
91  MSNet::getInstance()->getContainerControl().erase(transportable);
92  } else {
93  MSNet::getInstance()->getPersonControl().erase(transportable);
94  }
95  }
96  if (MSStopOut::active()) {
97  if (myAmContainer) {
99  } else {
101  }
102  }
103  i = myTransportables.erase(i);
104  } else {
105  ++i;
106  }
107  }
108  myStopped = true;
109  }
110  }
111  return true;
112 }
113 
114 
115 bool
118  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
119  (*i)->setDeparted(MSNet::getInstance()->getCurrentTimeStep());
120  }
121  }
122  return true;
123 }
124 
125 
126 bool
128  MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
129  if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
130  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
131  MSTransportable* transportable = *i;
132  if (transportable->getDestination() != veh.getEdge()) {
133  WRITE_WARNING((myAmContainer ? "Teleporting container '" : "Teleporting person '") + transportable->getID() +
134  "' from vehicle destination edge '" + veh.getEdge()->getID() +
135  "' to intended destination edge '" + transportable->getDestination()->getID() + "'");
136  }
137  if (!transportable->proceed(MSNet::getInstance(), MSNet::getInstance()->getCurrentTimeStep())) {
138  if (myAmContainer) {
139  MSNet::getInstance()->getContainerControl().erase(transportable);
140  } else {
141  MSNet::getInstance()->getPersonControl().erase(transportable);
142  }
143  }
144  }
145  }
146  return true;
147 }
148 
149 
150 void
152  myTransportables.push_back(transportable);
153  if (MSStopOut::active()) {
154  if (myAmContainer) {
156  } else {
158  }
159  }
160 }
161 
162 
163 void
165  myTransportables.erase(std::find(myTransportables.begin(), myTransportables.end(), transportable));
166  if (MSStopOut::active() && myHolder.isStopped()) {
167  if (myAmContainer) {
169  } else {
171  }
172  }
173 }
174 
175 
176 std::string
177 MSDevice_Transportable::getParameter(const std::string& key) const {
178  if (key == "IDList") {
179  std::vector<std::string> ids;
180  for (std::vector<MSTransportable*>::const_iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
181  ids.push_back((*i)->getID());
182  }
183  return toString(ids);
184  }
185  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
186 }
187 
188 
189 /****************************************************************************/
190 
static bool active()
Definition: MSStopOut.h:57
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:165
virtual bool proceed(MSNet *net, SUMOTime time)=0
const std::string & getID() const
Returns the id.
Definition: Named.h:78
bool notifyMove(SUMOVehicle &veh, double oldPos, double newPos, double newSpeed)
Checks whether the vehicle is at a stop and transportable action is needed.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:241
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:784
void unloadedPersons(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:84
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
Representation of a vehicle.
Definition: SUMOVehicle.h:60
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:776
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:263
const std::string & getID() const
returns the id of the transportable
void addTransportable(MSTransportable *transportable)
Add a passenger.
bool notifyLeave(SUMOVehicle &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Passengers leaving on arrival.
bool myAmContainer
Whether it is a container device.
The vehicle has departed (was inserted into the network)
void unloadedContainers(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:94
void notifyMoveInternal(const SUMOVehicle &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() ...
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Adds passengers on vehicle insertion.
Abstract in-vehicle device.
static MSStopOut * getInstance()
Definition: MSStopOut.h:63
virtual bool isStopped() const =0
Returns whether the vehicle is at a stop.
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:78
static MSDevice_Transportable * buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into, const bool isContainer)
Build devices for the given vehicle, if needed.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key ...