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-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 /****************************************************************************/
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 
59 
60 void
62  if (previous->getDestinationStop() != nullptr) {
63  // the arrival stop may have an access point
64  myWaitingEdge = &previous->getDestinationStop()->getLane().getEdge();
65  } else {
66  myWaitingEdge = previous->getEdge();
67  }
68  myWaitingPos = previous->getEdgePos(now);
69  myWaitingSince = now;
70  SUMOVehicle* availableVehicle = net->getVehicleControl().getWaitingVehicle(myWaitingEdge, myLines, myWaitingPos, container->getID());
71  if (availableVehicle != nullptr && availableVehicle->getParameter().departProcedure == DEPART_CONTAINER_TRIGGERED && !availableVehicle->hasDeparted()) {
72  setVehicle(availableVehicle);
73  myVehicle->addContainer(container);
77  } else {
78  net->getContainerControl().addWaiting(myWaitingEdge, container);
79  myWaitingEdge->addContainer(container);
80  }
81 }
82 
83 
84 std::string
86  return isWaiting4Vehicle() ? "waiting for " + joinToString(myLines, ",") : "transport";
87 }
88 
89 
90 std::string
92  const std::string dest = (getDestinationStop() == nullptr ?
93  " edge '" + getDestination()->getID() + "'" :
94  " stop '" + getDestinationStop()->getID() + "'");
95  return isWaiting4Vehicle() ?
96  "waiting for " + joinToString(myLines, ",") + " then transported to " + dest :
97  "transported to " + dest;
98 }
99 
100 
101 
102 void
104  os.openTag("transport");
105  os.writeAttr("waitingTime", time2string(myDeparted - myWaitingSince));
106  os.writeAttr("vehicle", myVehicleID);
107  os.writeAttr("depart", time2string(myDeparted));
108  os.writeAttr("arrival", time2string(myArrived));
109  os.writeAttr("arrivalPos", toString(myArrivalPos));
110  os.writeAttr("duration", myArrived > 0 ? time2string(myArrived - myDeparted) : "-1");
111  os.writeAttr("routeLength", myVehicleDistance);
112  os.closeTag();
113 }
114 
115 
116 void
117 MSContainer::MSContainerStage_Driving::routeOutput(OutputDevice& os, const bool withRouteLength) const {
120  if (withRouteLength) {
121  os.writeAttr("routeLength", myVehicleDistance);
122  }
123 }
124 
125 
126 
127 /* -------------------------------------------------------------------------
128  * MSContainer::MSContainerStage_Tranship - methods
129  * ----------------------------------------------------------------------- */
131  MSStoppingPlace* toStop,
132  double speed,
133  double departPos, double arrivalPos) :
134  MSTransportable::Stage(route.back(), toStop, SUMOVehicleParameter::interpretEdgePos(
135  arrivalPos, route.back()->getLength(), SUMO_ATTR_ARRIVALPOS, "container getting transhipped to " + route.back()->getID()), MOVING_WITHOUT_VEHICLE), myRoute(route),
136  mySpeed(speed), myContainerState(nullptr), myCurrentInternalEdge(nullptr) {
138  departPos, myRoute.front()->getLength(), SUMO_ATTR_DEPARTPOS, "container getting transhipped from " + myRoute.front()->getID());
139 }
140 
142 }
143 
144 void
146  myDeparted = now;
147  myRouteStep = myRoute.end() - 1; //define that the container is already on its destination edge
148  myDepartPos = previous->getEdgePos(now);
149  myContainerState = MSCModel_NonInteracting::getModel()->add(container, this, now);
150  (*myRouteStep)->addContainer(container);
151 }
152 
153 const MSEdge*
155  if (myCurrentInternalEdge != nullptr) {
156  return myCurrentInternalEdge;
157  } else {
158  return *myRouteStep;
159  }
160 }
161 
162 const MSEdge*
164  return myRoute.front();
165 }
166 
167 const MSEdge*
169  return myRoute.back();
170 }
171 
172 double
174  return myContainerState->getEdgePos(*this, now);
175 }
176 
177 Position
179  return myContainerState->getPosition(*this, now);
180 }
181 
182 double
184  return myContainerState->getAngle(*this, now);
185 }
186 
187 SUMOTime
189  return 0;
190 }
191 
192 double
194  return myContainerState->getSpeed(*this);
195 }
196 
197 
200  return myRoute;
201 }
202 
203 
204 void
206  const SUMOTime duration = myArrived - myDeparted;
207  // no timeloss is possible
208  const double distance = mySpeed * STEPS2TIME(duration);
209  os.openTag("tranship");
210  os.writeAttr("depart", time2string(myDeparted));
211  os.writeAttr("departPos", myDepartPos);
212  os.writeAttr("arrival", time2string(myArrived));
213  os.writeAttr("arrivalPos", myArrivalPos);
214  os.writeAttr("duration", time2string(duration));
215  os.writeAttr("routeLength", distance);
216  os.writeAttr("maxSpeed", mySpeed);
217  os.closeTag();
218 }
219 
220 
221 void
223  os.openTag("tranship").writeAttr(SUMO_ATTR_EDGES, myRoute);
225  if (withRouteLength) {
226  os.writeAttr("routeLength", mySpeed * (myArrived - myDeparted));
227  }
228  os.closeTag();
229 }
230 
231 
232 void
234  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "departure")
235  .writeAttr("agent", c.getID()).writeAttr("link", myRoute.front()->getID()).closeTag();
236 }
237 
238 
239 void
241  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival")
242  .writeAttr("agent", c.getID()).writeAttr("link", myRoute.back()->getID()).closeTag();
243 }
244 
245 bool
247  ((MSEdge*)getEdge())->removeContainer(container);
248  if (myRouteStep == myRoute.end() - 1) {
249  if (myDestinationStop != nullptr) {
250  myDestinationStop->addTransportable(container); //jakob
251  }
252  if (!container->proceed(MSNet::getInstance(), currentTime)) {
254  }
255  return true;
256  } else {
257  if (nextInternal == nullptr) {
258  ++myRouteStep;
259  myCurrentInternalEdge = nullptr;
260  } else {
261  myCurrentInternalEdge = nextInternal;
262  }
263  ((MSEdge*) getEdge())->addContainer(container);
264  return false;
265  }
266 }
267 
268 std::string
270  const std::string dest = (getDestinationStop() == nullptr ?
271  " edge '" + getDestination()->getID() + "'" :
272  " stop '" + getDestinationStop()->getID() + "'");
273  return "transhipped to " + dest;
274 }
275 
276 /* -------------------------------------------------------------------------
277  * MSContainer - methods
278  * ----------------------------------------------------------------------- */
280  : MSTransportable(pars, vtype, plan) {
281 }
282 
283 
285 }
286 
287 
288 bool
290  Stage* prior = *myStep;
291  prior->setArrived(net, this, time);
292  // must be done before increasing myStep to avoid invalid state for rendering
293  prior->getEdge()->removeContainer(this);
294  myStep++;
295  if (myStep != myPlan->end()) {
296  (*myStep)->proceed(net, this, time, prior);
297  return true;
298  } else {
299  return false;
300  }
301 }
302 
303 
304 void
306  os.openTag("containerinfo").writeAttr("id", getID()).writeAttr("depart", time2string(getDesiredDepart()));
307  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
308  (*i)->tripInfoOutput(os, this);
309  }
310  os.closeTag();
311 }
312 
313 
314 void
315 MSContainer::routeOutput(OutputDevice& os, const bool withRouteLength) const {
317  if (myStep == myPlan->end()) {
318  os.writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
319  }
320  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
321  (*i)->routeOutput(os, withRouteLength);
322  }
323  os.closeTag();
324  os.lf();
325 }
326 
327 
328 /****************************************************************************/
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:640
long long int SUMOTime
Definition: SUMOTime.h:36
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:206
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:61
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:165
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:72
virtual bool proceed(MSNet *net, SUMOTime time)=0
double myDepartPos
the depart position
Definition: MSContainer.h:209
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:78
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:84
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.
void addTransportable(MSTransportable *p)
adds a transportable to this stop
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:784
std::vector< const MSEdge * > myRoute
The route of the container.
Definition: MSContainer.h:203
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:75
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:49
SUMOTime myDeparted
the time at which this stage started
Representation of a vehicle.
Definition: SUMOVehicle.h:60
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:316
double mySpeed
the speed of the container
Definition: MSContainer.h:212
#define STEPS2TIME(x)
Definition: SUMOTime.h:58
DepartDefinition departProcedure
Information how the vehicle shall choose the depart time.
SUMOVehicle * getWaitingVehicle(const MSEdge *const edge, const std::set< std::string > &lines, const double position, const std::string ridingID)
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.
CState * myContainerState
state that is to be manipulated by MSCModel
Definition: MSContainer.h:215
virtual void removeContainer(MSTransportable *container) const
Remove container from myContainers.
Definition: MSEdge.h:612
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
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.
std::string getStageSummary() const
return string summary of the current stage
Definition: MSContainer.cpp:91
std::string getStageDescription() const
returns the stage description as a string
Definition: MSContainer.cpp:85
virtual void addContainer(MSTransportable *container) const
Add a container to myContainers.
Definition: MSEdge.h:607
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:369
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.
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:237
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 to a 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:218