SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSContainer.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // The class for modelling container-movements
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <vector>
36 #include <utils/common/ToString.h>
37 #include "MSNet.h"
38 #include "MSEdge.h"
39 #include "MSLane.h"
40 #include "MSContainer.h"
42 #include "MSContainerControl.h"
43 #include "MSInsertionControl.h"
44 #include "MSVehicle.h"
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 /* -------------------------------------------------------------------------
52  * static member definitions
53  * ----------------------------------------------------------------------- */
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
59 /* -------------------------------------------------------------------------
60  * MSContainer::MSContainerStage_Driving - methods
61  * ----------------------------------------------------------------------- */
63  MSStoppingPlace* toCS, const std::vector<std::string>& lines)
64  : MSTransportable::Stage(destination, DRIVING), myLines(lines.begin(), lines.end()),
65  myVehicle(0), myDestinationContainerStop(toCS) {}
66 
67 
69 
70 void
72  MSEdge* previousEdge, const SUMOReal at) {
73  myWaitingEdge = previousEdge;
74  myWaitingPos = at;
75  myWaitingSince = now;
76  SUMOVehicle* availableVehicle = net->getVehicleControl().getWaitingVehicle(previousEdge, myLines, myWaitingPos, container->getID());
77  if (availableVehicle != 0 && availableVehicle->getParameter().departProcedure == DEPART_CONTAINER_TRIGGERED) {
78  myVehicle = availableVehicle;
79  previousEdge->removeContainer(container);
80  myVehicle->addContainer(container);
81  net->getInsertionControl().add(myVehicle);
82  net->getVehicleControl().removeWaiting(previousEdge, myVehicle);
84  } else {
85  net->getContainerControl().addWaiting(previousEdge, container);
86  previousEdge->addContainer(container);
87  }
88 }
89 
90 const MSEdge*
92  if (myVehicle != 0) {
93  return myVehicle->getEdge();
94  }
95  return myWaitingEdge;
96 }
97 
98 
99 const MSEdge*
101  return myWaitingEdge;
102 }
103 
104 
105 SUMOReal
107  if (myVehicle != 0) {
108  // vehicle may already have passed the lane (check whether this is correct)
109  return MIN2(myVehicle->getPositionOnLane(), getEdge()->getLength());
110  }
111  return myWaitingPos;
112 }
113 
114 Position
116  if (myVehicle != 0) {
118  return myVehicle->getEdge()->getLanes()[0]->getShape().positionAtOffset(myVehicle->getPositionOnLane());
119  }
120  return getEdgePosition(myWaitingEdge, myWaitingPos, ROADSIDE_OFFSET);
121 }
122 
123 SUMOReal
125  if (myVehicle != 0) {
126  MSVehicle* veh = dynamic_cast<MSVehicle*>(myVehicle);
127  if (veh != 0) {
128  return veh->getAngle();
129  } else {
130  return 0;
131  }
132  }
133  return getEdgeAngle(myWaitingEdge, myWaitingPos) + 90;
134 }
135 
136 bool
138  return myLines.count(line) > 0;
139 }
140 
141 bool
143  return myVehicle == 0;
144 }
145 
146 SUMOTime
148  return isWaiting4Vehicle() ? now - myWaitingSince : 0;
149 }
150 
151 SUMOReal
153  return myVehicle == 0 ? 0 : myVehicle->getSpeed();
154 }
155 
156 std::string
158  return isWaiting4Vehicle() ? "waiting for " + joinToString(myLines, ",") : "transport";
159 }
160 
163  return myDepartContainerStop;
164 }
165 
166 void
168  os.openTag("transport").writeAttr("depart", time2string(myDeparted)).writeAttr("arrival", time2string(myArrived)).closeTag();
169 }
170 
171 void
174  os.writeAttr(SUMO_ATTR_LINES, myLines).closeTag();
175 }
176 
177 void
179  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival").writeAttr("agent", container.getID()).writeAttr("link", getEdge()->getID()).closeTag();
180 }
181 
182 void
184  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival").writeAttr("agent", container.getID()).writeAttr("link", getEdge()->getID()).closeTag();
185 }
186 
187 
188 
189 /* -------------------------------------------------------------------------
190  * MSContainer::MSContainerStage_Waiting - methods
191  * ----------------------------------------------------------------------- */
193  SUMOTime duration, SUMOTime until, SUMOReal pos, const std::string& actType) :
194  MSTransportable::Stage(destination, WAITING),
195  myWaitingDuration(duration),
196  myWaitingUntil(until),
197  myActType(actType),
198  myStartPos(pos) {
200  myStartPos, myDestination.getLength(), SUMO_ATTR_DEPARTPOS, "container stopping at " + myDestination.getID());
201 }
202 
204 
205 const MSEdge*
207  return &myDestination;
208 }
209 
210 const MSEdge*
212  return &myDestination;
213 }
214 
215 SUMOReal
217  return myStartPos;
218 }
219 
220 SUMOTime
222  return myWaitingUntil;
223 }
224 
225 Position
227  return getEdgePosition(&myDestination, myStartPos, ROADSIDE_OFFSET);
228 }
229 
230 SUMOReal
232  return getEdgeAngle(&myDestination, myStartPos) - 180;
233 }
234 
235 SUMOTime
237  return now - myWaitingStart;
238 }
239 
240 SUMOReal
242  return 0;
243 }
244 
247  return myCurrentContainerStop;
248 }
249 
250 void
252  MSEdge* previousEdge, const SUMOReal /* at */) {
253  previousEdge->addContainer(container);
254  myWaitingStart = now;
255  const SUMOTime until = MAX3(now, now + myWaitingDuration, myWaitingUntil);
256  net->getContainerControl().setWaitEnd(until, container);
257 }
258 
259 void
261  os.openTag("stop").writeAttr("arrival", time2string(myArrived)).closeTag();
262 }
263 
264 void
267  if (myWaitingDuration >= 0) {
268  os.writeAttr(SUMO_ATTR_DURATION, time2string(myWaitingDuration));
269  }
270  if (myWaitingUntil >= 0) {
271  os.writeAttr(SUMO_ATTR_UNTIL, time2string(myWaitingUntil));
272  }
273  os.closeTag();
274 }
275 
276 void
278  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "actstart " + myActType)
279  .writeAttr("agent", container.getID()).writeAttr("link", getEdge()->getID()).closeTag();
280 }
281 
282 void
284  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "actend " + myActType).writeAttr("agent", container.getID())
285  .writeAttr("link", getEdge()->getID()).closeTag();
286 }
287 
288 /* -------------------------------------------------------------------------
289  * MSContainer::MSContainerStage_Tranship - methods
290  * ----------------------------------------------------------------------- */
292  MSStoppingPlace* toCS,
293  SUMOReal speed,
294  SUMOReal departPos, SUMOReal arrivalPos) :
295  MSTransportable::Stage(*route.back(), MOVING_WITHOUT_VEHICLE), myRoute(route),
296  myDestinationContainerStop(toCS),
297  mySpeed(speed), myContainerState(0), myCurrentInternalEdge(0) {
299  departPos, myRoute.front()->getLength(), SUMO_ATTR_DEPARTPOS, "container getting transhipped from " + myRoute.front()->getID());
301  arrivalPos, myRoute.back()->getLength(), SUMO_ATTR_ARRIVALPOS, "container getting transhipped to " + myRoute.back()->getID());
302 }
303 
305 }
306 
307 void
308 MSContainer::MSContainerStage_Tranship::proceed(MSNet* /* net */, MSTransportable* container, SUMOTime now, MSEdge* previousEdge, const SUMOReal at) {
309  previousEdge->removeContainer(container);
310  myRouteStep = myRoute.end() - 1; //define that the container is already on its destination edge
312  if (at >= 0) {
313  myDepartPos = at;
314  }
315  myContainerState = MSCModel_NonInteracting::getModel()->add(container, this, now);
316  (*myRouteStep)->addContainer(container);
317 }
318 
319 const MSEdge*
321  if (myCurrentInternalEdge != 0) {
322  return myCurrentInternalEdge;
323  } else {
324  return *myRouteStep;
325  }
326 }
327 
328 const MSEdge*
330  return myRoute.front();
331 }
332 
333 const MSEdge*
335  return myRoute.back();
336 }
337 
338 SUMOReal
340  return myContainerState->getEdgePos(*this, now);
341 }
342 
343 Position
345  return myContainerState->getPosition(*this, now);
346 }
347 
348 SUMOReal
350  return myContainerState->getAngle(*this, now);
351 }
352 
353 SUMOTime
355  return 0;
356 }
357 
358 SUMOReal
360  return myContainerState->getSpeed(*this);
361 }
362 
365  return myDepartContainerStop;
366 }
367 
368 void
370  os.openTag("tranship").writeAttr("arrival", time2string(myArrived)).closeTag();
371 }
372 
373 
374 void
376  os.openTag("tranship").writeAttr(SUMO_ATTR_EDGES, myRoute);
377  os.writeAttr(SUMO_ATTR_SPEED, mySpeed);
378  os.closeTag();
379 }
380 
381 
382 void
384  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "departure")
385  .writeAttr("agent", c.getID()).writeAttr("link", myRoute.front()->getID()).closeTag();
386 }
387 
388 
389 void
391  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival")
392  .writeAttr("agent", c.getID()).writeAttr("link", myRoute.back()->getID()).closeTag();
393 }
394 
395 bool
397  ((MSEdge*)getEdge())->removeContainer(container);
398  if (myRouteStep == myRoute.end() - 1) {
400  if (myDestinationContainerStop != 0) {
401  myDestinationContainerStop->addTransportable(container); //jakob
402  }
403  if (!container->proceed(MSNet::getInstance(), currentTime)) {
405  }
406  return true;
407  } else {
408  if (nextInternal == 0) {
409  ++myRouteStep;
410  myCurrentInternalEdge = 0;
411  } else {
412  myCurrentInternalEdge = nextInternal;
413  }
414  ((MSEdge*) getEdge())->addContainer(container);
415  return false;
416  }
417 }
418 
419 /* -------------------------------------------------------------------------
420  * MSContainer - methods
421  * ----------------------------------------------------------------------- */
423  : MSTransportable(pars, vtype, plan) {
424 }
425 
427 }
428 
429 bool
431  MSEdge* arrivedAt = (MSEdge*)(*myStep)->getEdge();
432  SUMOReal atPos = (*myStep)->getEdgePos(time);
433  (*myStep)->setArrived(time);
434  myStep++;
435  if (myStep != myPlan->end()) {
436  (*myStep)->proceed(net, this, time, arrivedAt, atPos);
437  return true;
438  } else {
439  arrivedAt->removeContainer(this);
440  return false;
441  }
442 }
443 
444 
445 void
447  MSTransportablePlan::const_iterator i = myPlan->begin();
448  if ((*i)->getStageType() == WAITING && getDesiredDepart() == static_cast<MSContainerStage_Waiting*>(*i)->getUntil()) {
449  ++i;
450  }
451  for (; i != myPlan->end(); ++i) {
452  (*i)->routeOutput(os);
453  }
454 }
455 
456 
457 /****************************************************************************/
virtual ~MSContainer()
destructor
SUMOReal getEdgePos(SUMOTime now) const
Returns the offset from the start of the current edge measured in its natural direction.
SUMOReal myDepartPos
the depart position
Definition: MSContainer.h:396
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
virtual void beginEventOutput(const MSTransportable &c, SUMOTime t, OutputDevice &os) const
Called for writing the events output.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
SUMOReal getAngle(SUMOTime now) const
the angle of the edge minus 90deg
A lane area vehicles can halt at.
bool isWaiting4Vehicle() const
Whether the container waits for a vehicle.
The departure is container triggered.
MSContainerStage_Tranship(const std::vector< const MSEdge * > &route, MSStoppingPlace *toCS, SUMOReal speed, SUMOReal departPos, SUMOReal arrivalPos)
constructor
bool isWaitingFor(const std::string &line) const
Whether the container waits for a vehicle of the line specified.
SUMOTime getWaitingTime(SUMOTime now) const
time spent waiting for a ride
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
virtual void addContainer(MSTransportable *container) const
Add a container to myContainers.
Definition: MSEdge.h:566
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
SUMOTime getWaitingTime(SUMOTime now) const
the time this transportable spent waiting
const MSEdge * getEdge() const
Returns the current edge.
SUMOReal getSpeed() const
Returns the speed of the container which is always zero in that stage.
MSContainerStage_Waiting(const MSEdge &destination, SUMOTime duration, SUMOTime until, SUMOReal pos, const std::string &actType)
constructor
void addWaiting(const MSEdge *edge, MSTransportable *container)
adds a container to the list of containers waiting for a vehicle on the specified edge ...
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:162
const std::string & getID() const
returns the id of the transportable
SUMOVehicle * getWaitingVehicle(const MSEdge *const edge, const std::set< std::string > &lines, const SUMOReal position, const std::string ridingID)
void removeWaiting(const MSEdge *const edge, SUMOVehicle *vehicle)
Removes a vehicle from the list of waiting vehicles to a given edge.
virtual bool proceed(MSNet *net, SUMOTime time)=0
SUMOReal getEdgePos(SUMOTime now) const
virtual void proceed(MSNet *net, MSTransportable *container, SUMOTime now, MSEdge *previousEdge, const SUMOReal at)
proceeds to the next step
Definition: MSContainer.cpp:71
MSTransportablePlan::iterator myStep
the iterator over the route
virtual void proceed(MSNet *net, MSTransportable *container, SUMOTime now, MSEdge *previousEdge, const SUMOReal at)
proceeds to the next step
virtual void beginEventOutput(const MSTransportable &container, SUMOTime t, OutputDevice &os) const
Called for writing the events output.
virtual void proceed(MSNet *net, MSTransportable *container, SUMOTime now, MSEdge *previousEdge, const SUMOReal at)
proceeds to the next step
T MAX3(T a, T b, T c)
Definition: StdDefs.h:88
virtual void removeContainer(MSTransportable *container) const
Remove container from myContainers.
Definition: MSEdge.h:571
The simulated network and simulation perfomer.
Definition: MSNet.h:94
The car-following model and parameter.
Definition: MSVehicleType.h:74
SUMOTime getDesiredDepart() const
Returns the desired departure time.
CState * add(MSTransportable *container, MSContainer::MSContainerStage_Tranship *stage, SUMOTime now)
register the given container as a transhiped container
void unregisterOneWaitingForContainer()
decreases the count of vehicles waiting for a container to allow recogniztion of container related de...
const MSEdge * getEdge() const
Returns the current edge.
static SUMOReal interpretEdgePos(SUMOReal pos, SUMOReal maximumValue, SumoXMLAttr attr, const std::string &id)
Interprets negative edge positions and fits them onto a given edge.
std::vector< const MSEdge * > myRoute
The route of the container.
Definition: MSContainer.h:390
virtual void beginEventOutput(const MSTransportable &container, SUMOTime t, OutputDevice &os) const
Called for writing the events output.
const std::string & getID() const
Returns the id.
Definition: Named.h:60
A road/street connecting two junctions.
Definition: MSEdge.h:81
std::vector< MSTransportable::Stage * > MSTransportablePlan
the structure holding the plan of a container
SUMOReal myArrivalPos
the arrival position
Definition: MSContainer.h:399
const MSEdge * getEdge() const
Returns the current edge.
Definition: MSContainer.cpp:91
SUMOReal getLength() const
return the length of the edge
Definition: MSEdge.h:535
void setWaitEnd(SUMOTime time, MSTransportable *container)
sets the arrival time for a waiting container
the edges of a route
SUMOTime getUntil() const
Returns time until the container waits.
const MSEdge * getEdge() const
Returns the current edge.
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
Representation of a vehicle.
Definition: SUMOVehicle.h:65
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
virtual MSContainerControl & getContainerControl()
Returns the container control.
Definition: MSNet.cpp:693
SUMOReal getSpeed() const
the speed of the container
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:304
Position getPosition(SUMOTime now) const
Returns the position of the container.
void setTranship(MSTransportable *c)
adds a container to myTranship
DepartDefinition departProcedure
Information how the vehicle shall choose the depart time.
SUMOReal getSpeed() const
Returns the speed of the container.
T MIN2(T a, T b)
Definition: StdDefs.h:68
static const SUMOReal ROADSIDE_OFFSET
the offset for computing container positions when standing at an edge
Definition: MSContainer.h:71
Position getPosition(SUMOTime now) const
returns the position of the transportable
MSContainerStage_Driving(const MSEdge &destination, MSStoppingPlace *toCS, const std::vector< std::string > &lines)
constructor
Definition: MSContainer.cpp:62
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
virtual void endEventOutput(const MSTransportable &c, SUMOTime t, OutputDevice &os) const
Called for writing the events output (end of an action)
const MSEdge * getFromEdge() const
Returns first edge of the containers route.
bool moveToNextEdge(MSTransportable *container, SUMOTime currentTime, MSEdge *nextInternal=0)
move forward and return whether the container arrived
const MSEdge & getDestination() const
Returns the current destination.
Position getPosition(SUMOTime now) const
returns the position of the transportable
const MSEdge * getFromEdge() const
Returns the current edge.
std::string getStageDescription() const
returns the stage description as a string
const MSEdge * getFromEdge() const
Returns the departure edge.
Structure representing possible vehicle parameter.
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:345
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
bool proceed(MSNet *net, SUMOTime time)
SUMOReal getAngle(SUMOTime now) const
the angle of the vehicle or the angle of the edge + 90deg
void unsetTranship(MSTransportable *c)
removes a container from myTranship
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
MSStoppingPlace * getDepartContainerStop() const
const MSEdge * getFromEdge() const
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:159
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
SUMOTime getWaitingTime(SUMOTime now) const
Returns the time the container spent waiting.
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:215
MSStoppingPlace * getDepartContainerStop() const
returns the container stop from which the container departs
MSTransportablePlan * myPlan
the plan of the container
MSContainer(const SUMOVehicleParameter *pars, const MSVehicleType *vtype, MSTransportablePlan *plan)
constructor
const MSEdge * getToEdge() const
Returns last edge of the containers route.
SUMOReal getAngle(SUMOTime now) const
Returns the angle of the container.
bool isWaiting4Vehicle() const
Whether the container waits for a vehicle.
MSStoppingPlace * getDepartContainerStop() const
returns the container stop from which the container departs
const MSEdge & myDestination
the next edge to reach by getting transported
void add(SUMOVehicle *veh)
Adds a single vehicle for departure.
virtual void endEventOutput(const MSTransportable &container, SUMOTime t, OutputDevice &os) const
Called for writing the events output (end of an action)
virtual void erase(MSTransportable *container)
removes a single container
virtual void endEventOutput(const MSTransportable &container, SUMOTime t, OutputDevice &os) const
Called for writing the events output (end of an action)
static MSCModel_NonInteracting * getModel()
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
SUMOReal getEdgePos(SUMOTime now) const
SUMOReal getAngle() const
Returns the vehicle&#39;s direction in degrees.
Definition: MSVehicle.cpp:640