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() != 0) {
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 != 0 && 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() == 0 ?
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
120 }
121 
122 
123 
124 /* -------------------------------------------------------------------------
125  * MSContainer::MSContainerStage_Tranship - methods
126  * ----------------------------------------------------------------------- */
128  MSStoppingPlace* toStop,
129  double speed,
130  double departPos, double arrivalPos) :
131  MSTransportable::Stage(route.back(), toStop, SUMOVehicleParameter::interpretEdgePos(
132  arrivalPos, route.back()->getLength(), SUMO_ATTR_ARRIVALPOS, "container getting transhipped to " + route.back()->getID()), MOVING_WITHOUT_VEHICLE), myRoute(route),
133  mySpeed(speed), myContainerState(0), myCurrentInternalEdge(0) {
135  departPos, myRoute.front()->getLength(), SUMO_ATTR_DEPARTPOS, "container getting transhipped from " + myRoute.front()->getID());
136 }
137 
139 }
140 
141 void
143  myDeparted = now;
144  myRouteStep = myRoute.end() - 1; //define that the container is already on its destination edge
145  myDepartPos = previous->getEdgePos(now);
146  myContainerState = MSCModel_NonInteracting::getModel()->add(container, this, now);
147  (*myRouteStep)->addContainer(container);
148 }
149 
150 const MSEdge*
152  if (myCurrentInternalEdge != 0) {
153  return myCurrentInternalEdge;
154  } else {
155  return *myRouteStep;
156  }
157 }
158 
159 const MSEdge*
161  return myRoute.front();
162 }
163 
164 const MSEdge*
166  return myRoute.back();
167 }
168 
169 double
171  return myContainerState->getEdgePos(*this, now);
172 }
173 
174 Position
176  return myContainerState->getPosition(*this, now);
177 }
178 
179 double
181  return myContainerState->getAngle(*this, now);
182 }
183 
184 SUMOTime
186  return 0;
187 }
188 
189 double
191  return myContainerState->getSpeed(*this);
192 }
193 
194 
197  return myRoute;
198 }
199 
200 
201 void
203  const SUMOTime duration = myArrived - myDeparted;
204  // no timeloss is possible
205  const double distance = mySpeed * STEPS2TIME(duration);
206  os.openTag("tranship");
207  os.writeAttr("depart", time2string(myDeparted));
208  os.writeAttr("departPos", myDepartPos);
209  os.writeAttr("arrival", time2string(myArrived));
210  os.writeAttr("arrivalPos", myArrivalPos);
211  os.writeAttr("duration", time2string(duration));
212  os.writeAttr("routeLength", distance);
213  os.writeAttr("maxSpeed", mySpeed);
214  os.closeTag();
215 }
216 
217 
218 void
220  os.openTag("tranship").writeAttr(SUMO_ATTR_EDGES, myRoute);
222  os.closeTag();
223 }
224 
225 
226 void
228  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "departure")
229  .writeAttr("agent", c.getID()).writeAttr("link", myRoute.front()->getID()).closeTag();
230 }
231 
232 
233 void
235  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival")
236  .writeAttr("agent", c.getID()).writeAttr("link", myRoute.back()->getID()).closeTag();
237 }
238 
239 bool
241  ((MSEdge*)getEdge())->removeContainer(container);
242  if (myRouteStep == myRoute.end() - 1) {
243  if (myDestinationStop != 0) {
244  myDestinationStop->addTransportable(container); //jakob
245  }
246  if (!container->proceed(MSNet::getInstance(), currentTime)) {
248  }
249  return true;
250  } else {
251  if (nextInternal == 0) {
252  ++myRouteStep;
254  } else {
255  myCurrentInternalEdge = nextInternal;
256  }
257  ((MSEdge*) getEdge())->addContainer(container);
258  return false;
259  }
260 }
261 
262 std::string
264  const std::string dest = (getDestinationStop() == 0 ?
265  " edge '" + getDestination()->getID() + "'" :
266  " stop '" + getDestinationStop()->getID() + "'");
267  return "transhipped to " + dest;
268 }
269 
270 /* -------------------------------------------------------------------------
271  * MSContainer - methods
272  * ----------------------------------------------------------------------- */
274  : MSTransportable(pars, vtype, plan) {
275 }
276 
277 
279 }
280 
281 
282 bool
284  Stage* prior = *myStep;
285  prior->setArrived(time);
286  // must be done before increasing myStep to avoid invalid state for rendering
287  prior->getEdge()->removeContainer(this);
288  myStep++;
289  if (myStep != myPlan->end()) {
290  (*myStep)->proceed(net, this, time, prior);
291  return true;
292  } else {
293  return false;
294  }
295 }
296 
297 
298 void
300  os.openTag("containerinfo").writeAttr("id", getID()).writeAttr("depart", time2string(getDesiredDepart()));
301  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
302  (*i)->tripInfoOutput(os, transportable);
303  }
304  os.closeTag();
305 }
306 
307 
308 void
311  if (myStep == myPlan->end()) {
312  os.writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
313  }
314  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
315  (*i)->routeOutput(os);
316  }
317  os.closeTag();
318  os.lf();
319 }
320 
321 /****************************************************************************/
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
virtual void setArrived(SUMOTime now)
logs end of the step
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:617
long long int SUMOTime
Definition: SUMOTime.h:36
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
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
The departure is container triggered.
const MSEdge * getDestination() const
returns the destination edge
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
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:67
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:160
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:69
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)
The simulated network and simulation perfomer.
Definition: MSNet.h:85
The car-following model and parameter.
Definition: MSVehicleType.h:66
virtual void erase(MSTransportable *transportable)
removes a single transportable
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:756
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
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:309
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
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:607
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)
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
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
virtual void tripInfoOutput(OutputDevice &os, MSTransportable *transportable) const
Called on writing tripinfo output.
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:602
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:362
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.
virtual void tripInfoOutput(OutputDevice &os, MSTransportable *transportable) const
Called on writing tripinfo output.
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.
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:230
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
virtual void tripInfoOutput(OutputDevice &os, MSTransportable *transportable) const
Called on writing tripinfo output.
MSEdge * myCurrentInternalEdge
The current internal edge this container is on or 0.
Definition: MSContainer.h:218