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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
22 // A device which is used to keep track of persons and containers riding with a vehicle
23 /****************************************************************************/
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
35 #include <microsim/MSNet.h>
36 #include <microsim/MSEdge.h>
39 #include <microsim/MSContainer.h>
40 #include "MSDevice_Transportable.h"
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 // ---------------------------------------------------------------------------
47 // static initialisation methods
48 // ---------------------------------------------------------------------------
50 MSDevice_Transportable::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into, const bool isContainer) {
51  MSDevice_Transportable* device = new MSDevice_Transportable(v, isContainer ? "container_" + v.getID() : "person_" + v.getID(), isContainer);
52  into.push_back(device);
53  return device;
54 }
55 
56 
57 // ---------------------------------------------------------------------------
58 // MSDevice_Transportable-methods
59 // ---------------------------------------------------------------------------
60 MSDevice_Transportable::MSDevice_Transportable(SUMOVehicle& holder, const std::string& id, const bool isContainer)
61  : MSDevice(holder, id), myAmContainer(isContainer), myTransportables(), myStopped(holder.isStopped()) {
62 }
63 
64 
66 }
67 
68 
69 bool
70 MSDevice_Transportable::notifyMove(SUMOVehicle& veh, double /*oldPos*/, double /*newPos*/, double /*newSpeed*/) {
71  if (myStopped) {
72  if (!veh.isStopped()) {
73  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
74  (*i)->setDeparted(MSNet::getInstance()->getCurrentTimeStep());
75  }
76  myStopped = false;
77  }
78  } else {
79  if (veh.isStopped()) {
80  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end();) {
81  MSTransportable* transportable = *i;
82  if (&(transportable->getDestination()) == veh.getEdge()) {
83  if (!transportable->proceed(MSNet::getInstance(), MSNet::getInstance()->getCurrentTimeStep())) {
84  if (myAmContainer) {
85  MSNet::getInstance()->getContainerControl().erase(transportable);
86  } else {
87  MSNet::getInstance()->getPersonControl().erase(transportable);
88  }
89  }
90  if (MSStopOut::active()) {
91  if (myAmContainer) {
93  } else {
95  }
96  }
97  i = myTransportables.erase(i);
98  } else {
99  ++i;
100  }
101  }
102  myStopped = true;
103  }
104  }
105  return true;
106 }
107 
108 
109 bool
112  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
113  (*i)->setDeparted(MSNet::getInstance()->getCurrentTimeStep());
114  }
115  }
116  return true;
117 }
118 
119 
120 bool
122  MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
123  if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
124  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
125  MSTransportable* transportable = *i;
126  if (&(transportable->getDestination()) != veh.getEdge()) {
127  WRITE_WARNING((myAmContainer ? "Teleporting container '" : "Teleporting person '") + transportable->getID() +
128  "' from vehicle destination edge '" + veh.getEdge()->getID() +
129  "' to intended destination edge '" + transportable->getDestination().getID() + "'");
130  }
131  if (!transportable->proceed(MSNet::getInstance(), MSNet::getInstance()->getCurrentTimeStep())) {
132  if (myAmContainer) {
133  MSNet::getInstance()->getContainerControl().erase(transportable);
134  } else {
135  MSNet::getInstance()->getPersonControl().erase(transportable);
136  }
137  }
138  }
139  }
140  return true;
141 }
142 
143 
144 void
146  myTransportables.push_back(transportable);
147  if (MSStopOut::active()) {
148  if (myAmContainer) {
150  } else {
152  }
153  }
154 }
155 
156 
157 void
159  myTransportables.erase(std::find(myTransportables.begin(), myTransportables.end(), transportable));
160  if (MSStopOut::active() && myHolder.isStopped()) {
161  if (myAmContainer) {
163  } else {
165  }
166  }
167 }
168 
169 
170 std::string
171 MSDevice_Transportable::getParameter(const std::string& key) const {
172  if (key == "IDList") {
173  std::vector<std::string> ids;
174  for (std::vector<MSTransportable*>::const_iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
175  ids.push_back((*i)->getID());
176  }
177  return toString(ids);
178  }
179  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
180 }
181 
182 
183 /****************************************************************************/
184 
static bool active()
Definition: MSStopOut.h:63
const MSEdge & getDestination() const
Returns the current destination.
SUMOVehicle & myHolder
The vehicle that stores the device.
Definition: MSDevice.h:188
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
Notification
Definition of a vehicle state.
static MSDevice_Transportable * buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into, const bool isContainer)
Build devices for the given vehicle, if needed.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
virtual bool proceed(MSNet *net, SUMOTime time)=0
const std::string & getID() const
Returns the id.
Definition: Named.h:65
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:199
virtual void erase(MSTransportable *transportable)
removes a single transportable
void loadedContainers(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:83
virtual MSTransportableControl & getContainerControl()
Returns the container control.
Definition: MSNet.cpp:776
void unloadedPersons(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:78
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
Representation of a vehicle.
Definition: SUMOVehicle.h:66
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:768
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:253
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.
Abstract in-vehicle device.
Definition: MSDevice.h:70
The vehicle has departed (was inserted into the network)
void unloadedContainers(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:88
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Adds passengers on vehicle insertion.
static MSStopOut * getInstance()
Definition: MSStopOut.h:67
virtual bool isStopped() const =0
Returns whether the vehicle is at a stop.
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:73
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:77
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 ...