SUMO - Simulation of Urban MObility
MSDevice_Transportable.cpp
Go to the documentation of this file.
1 /****************************************************************************/
12 // A device which is used to keep track of persons and containers riding with a vehicle
13 /****************************************************************************/
14 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
15 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
16 /****************************************************************************/
17 //
18 // This file is part of SUMO.
19 // SUMO is free software: you can redistribute it and/or modify
20 // it under the terms of the GNU General Public License as published by
21 // the Free Software Foundation, either version 3 of the License, or
22 // (at your option) any later version.
23 //
24 /****************************************************************************/
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
36 #include <microsim/MSNet.h>
37 #include <microsim/MSEdge.h>
40 #include <microsim/MSContainer.h>
41 #include "MSDevice_Transportable.h"
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
47 // ---------------------------------------------------------------------------
48 // static initialisation methods
49 // ---------------------------------------------------------------------------
51 MSDevice_Transportable::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into, const bool isContainer) {
52  MSDevice_Transportable* device = new MSDevice_Transportable(v, isContainer ? "container_" + v.getID() : "person_" + v.getID(), isContainer);
53  into.push_back(device);
54  return device;
55 }
56 
57 
58 // ---------------------------------------------------------------------------
59 // MSDevice_Transportable-methods
60 // ---------------------------------------------------------------------------
61 MSDevice_Transportable::MSDevice_Transportable(SUMOVehicle& holder, const std::string& id, const bool isContainer)
62  : MSDevice(holder, id), myAmContainer(isContainer), myTransportables(), myStopped(holder.isStopped()) {
63 }
64 
65 
67 }
68 
69 
70 bool
71 MSDevice_Transportable::notifyMove(SUMOVehicle& veh, double /*oldPos*/, double /*newPos*/, double /*newSpeed*/) {
72  if (myStopped) {
73  if (!veh.isStopped()) {
74  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
75  (*i)->setDeparted(MSNet::getInstance()->getCurrentTimeStep());
76  }
77  myStopped = false;
78  }
79  } else {
80  if (veh.isStopped()) {
81  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end();) {
82  MSTransportable* transportable = *i;
83  if (&(transportable->getDestination()) == veh.getEdge()) {
84  if (!transportable->proceed(MSNet::getInstance(), MSNet::getInstance()->getCurrentTimeStep())) {
85  if (myAmContainer) {
86  MSNet::getInstance()->getContainerControl().erase(transportable);
87  } else {
88  MSNet::getInstance()->getPersonControl().erase(transportable);
89  }
90  }
91  if (MSStopOut::active()) {
92  if (myAmContainer) {
94  } else {
96  }
97  }
98  i = myTransportables.erase(i);
99  } else {
100  ++i;
101  }
102  }
103  myStopped = true;
104  }
105  }
106  return true;
107 }
108 
109 
110 bool
113  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
114  (*i)->setDeparted(MSNet::getInstance()->getCurrentTimeStep());
115  }
116  }
117  return true;
118 }
119 
120 
121 bool
123  MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
124  if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
125  for (std::vector<MSTransportable*>::iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
126  MSTransportable* transportable = *i;
127  if (&(transportable->getDestination()) != veh.getEdge()) {
128  WRITE_WARNING((myAmContainer ? "Teleporting container '" : "Teleporting person '") + transportable->getID() +
129  "' from vehicle destination edge '" + veh.getEdge()->getID() +
130  "' to intended destination edge '" + transportable->getDestination().getID() + "'");
131  }
132  if (!transportable->proceed(MSNet::getInstance(), MSNet::getInstance()->getCurrentTimeStep())) {
133  if (myAmContainer) {
134  MSNet::getInstance()->getContainerControl().erase(transportable);
135  } else {
136  MSNet::getInstance()->getPersonControl().erase(transportable);
137  }
138  }
139  }
140  }
141  return true;
142 }
143 
144 
145 void
147  myTransportables.push_back(transportable);
148  if (MSStopOut::active()) {
149  if (myAmContainer) {
151  } else {
153  }
154  }
155 }
156 
157 
158 void
160  myTransportables.erase(std::find(myTransportables.begin(), myTransportables.end(), transportable));
161  if (MSStopOut::active() && myHolder.isStopped()) {
162  if (myAmContainer) {
164  } else {
166  }
167  }
168 }
169 
170 
171 std::string
172 MSDevice_Transportable::getParameter(const std::string& key) const {
173  if (key == "IDList") {
174  std::vector<std::string> ids;
175  for (std::vector<MSTransportable*>::const_iterator i = myTransportables.begin(); i != myTransportables.end(); ++i) {
176  ids.push_back((*i)->getID());
177  }
178  return toString(ids);
179  }
180  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
181 }
182 
183 
184 /****************************************************************************/
185 
static bool active()
Definition: MSStopOut.h:64
const MSEdge & getDestination() const
Returns the current destination.
SUMOVehicle & myHolder
The vehicle that stores the device.
Definition: MSDevice.h:187
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:158
virtual bool proceed(MSNet *net, SUMOTime time)=0
const std::string & getID() const
Returns the id.
Definition: Named.h:66
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:200
virtual void erase(MSTransportable *transportable)
removes a single transportable
void loadedContainers(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:81
virtual MSTransportableControl & getContainerControl()
Returns the container control.
Definition: MSNet.cpp:738
void unloadedPersons(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:76
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
Representation of a vehicle.
Definition: SUMOVehicle.h:67
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:730
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:257
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:71
The vehicle has departed (was inserted into the network)
void unloadedContainers(const SUMOVehicle *veh, int n)
Definition: MSStopOut.cpp:86
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Adds passengers on vehicle insertion.
static MSStopOut * getInstance()
Definition: MSStopOut.h:68
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:71
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:79
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 ...