Eclipse SUMO - Simulation of Urban MObility
MSContainer.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 /****************************************************************************/
16 // The class for modelling container-movements
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include <vector>
29 #include <utils/common/ToString.h>
30 #include <utils/geom/GeomHelper.h>
31 #include "MSNet.h"
32 #include "MSEdge.h"
33 #include "MSLane.h"
34 #include "MSContainer.h"
36 #include "MSTransportableControl.h"
37 #include "MSInsertionControl.h"
38 #include "MSVehicle.h"
39 #include "MSVehicleControl.h"
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
45 /* -------------------------------------------------------------------------
46  * MSContainer::MSContainerStage_Driving - methods
47  * ----------------------------------------------------------------------- */
49  MSStoppingPlace* toStop, const double arrivalPos, const std::vector<std::string>& lines) :
50  MSTransportable::Stage_Driving(destination, toStop,
51  SUMOVehicleParameter::interpretEdgePos(
52  arrivalPos, destination->getLength(), SUMO_ATTR_ARRIVALPOS, "container getting transported to " + destination->getID()),
53  lines) {
54 }
55 
56 
58 
61  return new MSContainerStage_Driving(myDestination, myDestinationStop, myArrivalPos, std::vector<std::string>(myLines.begin(), myLines.end()));
62 }
63 
64 void
66  if (previous->getDestinationStop() != nullptr) {
67  // the arrival stop may have an access point
68  myWaitingEdge = &previous->getDestinationStop()->getLane().getEdge();
69  } else {
70  myWaitingEdge = previous->getEdge();
71  }
72  myWaitingPos = previous->getEdgePos(now);
73  myWaitingSince = now;
74  SUMOVehicle* availableVehicle = net->getVehicleControl().getWaitingVehicle(container, myWaitingEdge, myWaitingPos);
75  if (availableVehicle != nullptr && availableVehicle->getParameter().departProcedure == DEPART_CONTAINER_TRIGGERED && !availableVehicle->hasDeparted()) {
76  setVehicle(availableVehicle);
77  myVehicle->addContainer(container);
81  } else {
82  net->getContainerControl().addWaiting(myWaitingEdge, container);
83  myWaitingEdge->addContainer(container);
84  }
85 }
86 
87 
88 std::string
90  return isWaiting4Vehicle() ? "waiting for " + joinToString(myLines, ",") : "transport";
91 }
92 
93 
94 std::string
96  const std::string dest = (getDestinationStop() == nullptr ?
97  " edge '" + getDestination()->getID() + "'" :
98  " stop '" + getDestinationStop()->getID() + "'");
99  return isWaiting4Vehicle() ?
100  "waiting for " + joinToString(myLines, ",") + " then transported to " + dest :
101  "transported to " + dest;
102 }
103 
104 
105 
106 void
109  const SUMOTime departed = myDeparted >= 0 ? myDeparted : now;
110  os.openTag("transport");
111  os.writeAttr("waitingTime", time2string(departed - myWaitingSince));
112  os.writeAttr("vehicle", myVehicleID);
113  os.writeAttr("depart", myDeparted >= 0 ? time2string(myDeparted) : "-1");
114  os.writeAttr("arrival", myArrived >= 0 ? time2string(myArrived) : "-1");
115  os.writeAttr("arrivalPos", toString(myArrivalPos));
116  os.writeAttr("duration", myArrived >= 0 ? time2string(myArrived - myDeparted) :
117  (myDeparted >= 0 ? time2string(now - myDeparted) : "-1"));
118  os.writeAttr("routeLength", myVehicleDistance);
119  os.closeTag();
120 }
121 
122 
123 void
124 MSContainer::MSContainerStage_Driving::routeOutput(OutputDevice& os, const bool withRouteLength) const {
127  if (withRouteLength) {
128  os.writeAttr("routeLength", myVehicleDistance);
129  }
130 }
131 
132 
133 
134 /* -------------------------------------------------------------------------
135  * MSContainer::MSContainerStage_Tranship - methods
136  * ----------------------------------------------------------------------- */
138  MSStoppingPlace* toStop,
139  double speed,
140  double departPos, double arrivalPos) :
141  MSTransportable::Stage(route.back(), toStop, SUMOVehicleParameter::interpretEdgePos(
142  arrivalPos, route.back()->getLength(), SUMO_ATTR_ARRIVALPOS,
143  "container getting transhipped to " + route.back()->getID()),
144  MOVING_WITHOUT_VEHICLE), myRoute(route),
145  mySpeed(speed), myContainerState(nullptr), myCurrentInternalEdge(nullptr) {
147  departPos, myRoute.front()->getLength(), SUMO_ATTR_DEPARTPOS,
148  "container getting transhipped from " + myRoute.front()->getID());
149 }
150 
152 }
153 
157 }
158 
159 void
161  myDeparted = now;
162  //MSCModel_NonInteracting moves the container straight from start to end in
163  //a single step and assumes that moveToNextEdge is only called once)
164  //therefor we define that the container is already on its destination edge
165  myRouteStep = myRoute.end() - 1;
166  myDepartPos = previous->getEdgePos(now);
167  myContainerState = MSCModel_NonInteracting::getModel()->add(container, this, now);
168  (*myRouteStep)->addContainer(container);
169 }
170 
171 const MSEdge*
173  if (myCurrentInternalEdge != nullptr) {
174  return myCurrentInternalEdge;
175  } else {
176  return *myRouteStep;
177  }
178 }
179 
180 const MSEdge*
182  return myRoute.front();
183 }
184 
185 const MSEdge*
187  return myRoute.back();
188 }
189 
190 double
192  return myContainerState->getEdgePos(*this, now);
193 }
194 
195 Position
197  return myContainerState->getPosition(*this, now);
198 }
199 
200 double
202  return myContainerState->getAngle(*this, now);
203 }
204 
205 SUMOTime
207  return 0;
208 }
209 
210 double
212  return myContainerState->getSpeed(*this);
213 }
214 
215 
218  return myRoute;
219 }
220 
221 double
223  if (myArrived >= 0) {
224  const SUMOTime duration = myArrived - myDeparted;
225  return mySpeed * STEPS2TIME(duration);
226  } else {
227  return -1;
228  }
229 }
230 
231 void
233  os.openTag("tranship");
234  os.writeAttr("depart", time2string(myDeparted));
235  os.writeAttr("departPos", myDepartPos);
236  os.writeAttr("arrival", time2string(myArrived));
237  os.writeAttr("arrivalPos", myArrivalPos);
238  os.writeAttr("duration", myArrived >= 0 ? time2string(myArrived - myDeparted) : "-1");
239  os.writeAttr("routeLength", getDistance());
240  os.writeAttr("maxSpeed", mySpeed);
241  os.closeTag();
242 }
243 
244 
245 void
247  os.openTag("tranship").writeAttr(SUMO_ATTR_EDGES, myRoute);
249  if (withRouteLength) {
250  os.writeAttr("routeLength", mySpeed * (myArrived - myDeparted));
251  }
252  os.closeTag();
253 }
254 
255 
256 void
258  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "departure")
259  .writeAttr("agent", c.getID()).writeAttr("link", myRoute.front()->getID()).closeTag();
260 }
261 
262 
263 void
265  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival")
266  .writeAttr("agent", c.getID()).writeAttr("link", myRoute.back()->getID()).closeTag();
267 }
268 
269 bool
271  ((MSEdge*)getEdge())->removeContainer(container);
272  if (myRouteStep == myRoute.end() - 1) {
273  if (myDestinationStop != nullptr) {
274  myDestinationStop->addTransportable(container); //jakob
275  }
276  if (!container->proceed(MSNet::getInstance(), currentTime)) {
278  }
279  return true;
280  } else {
281  if (nextInternal == nullptr) {
282  ++myRouteStep;
283  myCurrentInternalEdge = nullptr;
284  } else {
285  myCurrentInternalEdge = nextInternal;
286  }
287  ((MSEdge*) getEdge())->addContainer(container);
288  return false;
289  }
290 }
291 
292 std::string
294  const std::string dest = (getDestinationStop() == nullptr ?
295  " edge '" + getDestination()->getID() + "'" :
296  " stop '" + getDestinationStop()->getID() + "'");
297  return "transhipped to " + dest;
298 }
299 
300 /* -------------------------------------------------------------------------
301  * MSContainer - methods
302  * ----------------------------------------------------------------------- */
304  : MSTransportable(pars, vtype, plan) {
305 }
306 
307 
309 }
310 
311 
312 bool
314  Stage* prior = *myStep;
315  prior->setArrived(net, this, time);
316  // must be done before increasing myStep to avoid invalid state for rendering
317  prior->getEdge()->removeContainer(this);
318  myStep++;
319  if (myStep != myPlan->end()) {
320  (*myStep)->proceed(net, this, time, prior);
321  return true;
322  } else {
323  // cleanup
324  if (prior->getDestinationStop() != nullptr) {
325  prior->getDestinationStop()->removeTransportable(this);
326  }
327  return false;
328  }
329 }
330 
331 
332 void
334  os.openTag("containerinfo").writeAttr("id", getID()).writeAttr("depart", time2string(getDesiredDepart()));
335  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
336  (*i)->tripInfoOutput(os, this);
337  }
338  os.closeTag();
339 }
340 
341 
342 void
343 MSContainer::routeOutput(OutputDevice& os, const bool withRouteLength) const {
345  if (myStep == myPlan->end()) {
346  os.writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
347  }
348  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
349  (*i)->routeOutput(os, withRouteLength);
350  }
351  os.closeTag();
352  os.lf();
353 }
354 
355 
356 /****************************************************************************/
Position getPosition(SUMOTime now) const
Returns the position of the container.
virtual ~MSContainer()
destructor
void addWaiting(const MSEdge *edge, MSTransportable *person)
adds a transportable to the list of transportables waiting for a vehicle on the specified edge ...
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:670
long long int SUMOTime
Definition: SUMOTime.h:35
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
virtual void setArrived(MSNet *net, MSTransportable *transportable, SUMOTime now)
logs end of the step
MSContainerStage_Tranship(const std::vector< const MSEdge *> &route, MSStoppingPlace *toStop, double speed, double departPos, double arrivalPos)
constructor
A lane area vehicles can halt at.
std::vector< const MSEdge * >::iterator myRouteStep
current step
Definition: MSContainer.h:213
ConstMSEdgeVector getEdges() const
the edges of the current stage
virtual void proceed(MSNet *net, MSTransportable *container, SUMOTime now, Stage *previous)
proceeds to the next step
Definition: MSContainer.cpp:65
virtual void routeOutput(OutputDevice &os, const bool withRouteLength) const
Called on writing vehroute output.
The departure is container triggered.
const MSEdge * getDestination() const
returns the destination edge
const MSEdge * getToEdge() const
Returns last edge of the containers route.
virtual const MSEdge * getEdge() const
Returns the current edge.
virtual double getEdgePos(SUMOTime now) const
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
MSContainer(const SUMOVehicleParameter *pars, MSVehicleType *vtype, MSTransportablePlan *plan)
constructor
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
const MSEdge * getFromEdge() const
Returns first edge of the containers route.
const std::set< std::string > myLines
the lines to choose from
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:73
virtual bool proceed(MSNet *net, SUMOTime time)=0
double myDepartPos
the depart position
Definition: MSContainer.h:216
double getEdgePos(SUMOTime now) const
Returns the offset from the start of the current edge measured in its natural direction.
const std::string & getID() const
Returns the id.
Definition: Named.h:77
const MSEdge * myDestination
the next edge to reach by getting transported
MSStoppingPlace * myDestinationStop
the stop to reach by getting transported (if any)
MSTransportablePlan::iterator myStep
the iterator over the route
const MSEdge * getFromEdge() const
virtual void endEventOutput(const MSTransportable &c, SUMOTime t, OutputDevice &os) const
Called for writing the events output (end of an action)
virtual void routeOutput(OutputDevice &os, const bool withRouteLength) const
Called on writing vehroute output.
The simulated network and simulation perfomer.
Definition: MSNet.h:92
The car-following model and parameter.
Definition: MSVehicleType.h:66
virtual void erase(MSTransportable *transportable)
removes a single transportable
virtual void tripInfoOutput(OutputDevice &os, const MSTransportable *const transportable) const
Called on writing tripinfo output.
CState * add(MSTransportable *container, MSContainer::MSContainerStage_Tranship *stage, SUMOTime now)
register the given container as a transhiped container
virtual MSTransportableControl & getContainerControl()
Returns the container control.
Definition: MSNet.cpp:806
std::vector< const MSEdge * > myRoute
The route of the container.
Definition: MSContainer.h:210
static double interpretEdgePos(double pos, double maximumValue, SumoXMLAttr attr, const std::string &id)
Interprets negative edge positions and fits them onto a given edge.
bool isWaiting4Vehicle() const
Whether the person waits for a vehicle.
virtual void addContainer(MSTransportable *container)=0
Adds a container to this vehicle.
SUMOTime myWaitingSince
The time since which this person is waiting for a ride.
A road/street connecting two junctions.
Definition: MSEdge.h:76
std::vector< MSTransportable::Stage * > MSTransportablePlan
the structure holding the plan of a transportable
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
SUMOTime myArrived
the time at which this stage ended
the edges of a route
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
SUMOTime myDeparted
the time at which this stage started
Representation of a vehicle.
Definition: SUMOVehicle.h:61
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
SUMOTime getDesiredDepart() const
Returns the desired departure time.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:337
double mySpeed
the speed of the container
Definition: MSContainer.h:219
#define STEPS2TIME(x)
Definition: SUMOTime.h:57
DepartDefinition departProcedure
Information how the vehicle shall choose the depart time.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:284
std::string myVehicleID
cached vehicle data for output after the vehicle has been removed
virtual void routeOutput(OutputDevice &os, const bool withRouteLength) const
Called on writing vehroute output.
const std::string & getID() const
returns the id of the transportable
SUMOVehicle * myVehicle
The taken vehicle.
void removeTransportable(MSTransportable *p)
Removes a transportable from this stop.
CState * myContainerState
state that is to be manipulated by MSCModel
Definition: MSContainer.h:222
virtual void removeContainer(MSTransportable *container) const
Remove container from myContainers.
Definition: MSEdge.h:626
double getAngle(const MSContainer::MSContainerStage_Tranship &stage, SUMOTime now) const
return the direction in which the container heading to
bool moveToNextEdge(MSTransportable *container, SUMOTime currentTime, MSEdge *nextInternal=0)
move forward and return whether the container arrived
bool addTransportable(MSTransportable *p)
adds a transportable to this stop
virtual void proceed(MSNet *net, MSTransportable *container, SUMOTime now, Stage *previous)
proceeds to the next step
virtual void beginEventOutput(const MSTransportable &c, SUMOTime t, OutputDevice &os) const
Called for writing the events output.
MSStoppingPlace * getDestinationStop() const
returns the destination stop (if any)
MSContainerStage_Driving(const MSEdge *destination, MSStoppingPlace *toStop, const double arrivalPos, const std::vector< std::string > &lines)
constructor
Definition: MSContainer.cpp:48
Structure representing possible vehicle parameter.
SUMOVehicle * getWaitingVehicle(MSTransportable *transportable, const MSEdge *const edge, const double position)
std::string getStageSummary() const
return string summary of the current stage
Definition: MSContainer.cpp:95
std::string getStageDescription() const
returns the stage description as a string
Definition: MSContainer.cpp:89
virtual void addContainer(MSTransportable *container) const
Add a container to myContainers.
Definition: MSEdge.h:621
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:390
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
virtual bool proceed(MSNet *net, SUMOTime time)
SUMOTime getWaitingTime(SUMOTime now) const
Returns the time the container spent waiting.
double getDistance() const
get travel distance in this stage
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
virtual void tripInfoOutput(OutputDevice &os, const MSTransportable *const transportable) const
Called on writing tripinfo output.
MSTransportablePlan * myPlan
the plan of the transportable
const MSEdge * getEdge() const
Returns the current edge.
double getSpeed(const MSContainer::MSContainerStage_Tranship &stage) const
return the current speed of the container
const MSLane & getLane() const
Returns the lane this stop is located at.
Position getPosition(const MSContainer::MSContainerStage_Tranship &stage, SUMOTime now) const
return the network coordinate of the container
double getEdgePos(const MSContainer::MSContainerStage_Tranship &stage, SUMOTime now) const
return the offset from the start of the current edge measured in its natural direction ...
void add(SUMOVehicle *veh)
Adds a single vehicle for departure.
double getSpeed() const
Returns the speed of the container.
void unregisterOneWaiting(const bool isPerson)
decreases the count of vehicles waiting for a transport to allow recognition of person / container re...
static MSCModel_NonInteracting * getModel()
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
Definition: ToString.h:247
std::string getStageSummary() const
return string summary of the current stage
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:234
void removeWaiting(const MSEdge *const edge, const SUMOVehicle *vehicle)
Removes a vehicle from the list of waiting vehicles for the given edge.
double getAngle(SUMOTime now) const
Returns the angle of the container.
double myArrivalPos
the position at which we want to arrive
MSEdge * myCurrentInternalEdge
The current internal edge this container is on or 0.
Definition: MSContainer.h:225