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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // The class for modelling container-movements
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <string>
32 #include <vector>
35 #include <utils/common/ToString.h>
36 #include <utils/geom/GeomHelper.h>
37 #include "MSNet.h"
38 #include "MSEdge.h"
39 #include "MSLane.h"
40 #include "MSContainer.h"
42 #include "MSTransportableControl.h"
43 #include "MSInsertionControl.h"
44 #include "MSVehicle.h"
45 #include "MSVehicleControl.h"
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 /* -------------------------------------------------------------------------
52  * MSContainer::MSContainerStage_Driving - methods
53  * ----------------------------------------------------------------------- */
55  MSStoppingPlace* toStop, const double arrivalPos, const std::vector<std::string>& lines) :
56  MSTransportable::Stage_Driving(destination, toStop,
57  SUMOVehicleParameter::interpretEdgePos(
58  arrivalPos, destination.getLength(), SUMO_ATTR_ARRIVALPOS, "container getting transported to " + destination.getID()),
59  lines) {
60 }
61 
62 
64 
65 
66 void
68  if (previous->getDestinationStop() != 0) {
69  // the arrival stop may have an access point
70  myWaitingEdge = &previous->getDestinationStop()->getLane().getEdge();
71  } else {
72  myWaitingEdge = previous->getEdge();
73  }
74  myWaitingPos = previous->getEdgePos(now);
75  myWaitingSince = now;
76  SUMOVehicle* availableVehicle = net->getVehicleControl().getWaitingVehicle(myWaitingEdge, myLines, myWaitingPos, container->getID());
77  if (availableVehicle != 0 && availableVehicle->getParameter().departProcedure == DEPART_CONTAINER_TRIGGERED && !availableVehicle->hasDeparted()) {
78  setVehicle(availableVehicle);
79  myVehicle->addContainer(container);
83  } else {
84  net->getContainerControl().addWaiting(myWaitingEdge, container);
85  myWaitingEdge->addContainer(container);
86  }
87 }
88 
89 
90 std::string
92  return isWaiting4Vehicle() ? "waiting for " + joinToString(myLines, ",") : "transport";
93 }
94 
95 
96 void
98  os.openTag("transport");
99  os.writeAttr("waitingTime", time2string(myDeparted - myWaitingSince));
100  os.writeAttr("vehicle", myVehicleID);
101  os.writeAttr("depart", time2string(myDeparted));
102  os.writeAttr("arrival", time2string(myArrived));
103  os.writeAttr("arrivalPos", toString(myArrivalPos));
104  os.writeAttr("duration", time2string(myArrived - myDeparted));
105  os.writeAttr("routeLength", myVehicleDistance);
106  os.closeTag();
107 }
108 
109 
110 void
114 }
115 
116 
117 
118 /* -------------------------------------------------------------------------
119  * MSContainer::MSContainerStage_Tranship - methods
120  * ----------------------------------------------------------------------- */
122  MSStoppingPlace* toStop,
123  double speed,
124  double departPos, double arrivalPos) :
125  MSTransportable::Stage(*route.back(), toStop, SUMOVehicleParameter::interpretEdgePos(
126  arrivalPos, route.back()->getLength(), SUMO_ATTR_ARRIVALPOS, "container getting transhipped to " + route.back()->getID()), MOVING_WITHOUT_VEHICLE), myRoute(route),
127  mySpeed(speed), myContainerState(0), myCurrentInternalEdge(0) {
129  departPos, myRoute.front()->getLength(), SUMO_ATTR_DEPARTPOS, "container getting transhipped from " + myRoute.front()->getID());
130 }
131 
133 }
134 
135 void
137  myDeparted = now;
138  myRouteStep = myRoute.end() - 1; //define that the container is already on its destination edge
139  myDepartPos = previous->getEdgePos(now);
140  myContainerState = MSCModel_NonInteracting::getModel()->add(container, this, now);
141  (*myRouteStep)->addContainer(container);
142 }
143 
144 const MSEdge*
146  if (myCurrentInternalEdge != 0) {
147  return myCurrentInternalEdge;
148  } else {
149  return *myRouteStep;
150  }
151 }
152 
153 const MSEdge*
155  return myRoute.front();
156 }
157 
158 const MSEdge*
160  return myRoute.back();
161 }
162 
163 double
165  return myContainerState->getEdgePos(*this, now);
166 }
167 
168 Position
170  return myContainerState->getPosition(*this, now);
171 }
172 
173 double
175  return myContainerState->getAngle(*this, now);
176 }
177 
178 SUMOTime
180  return 0;
181 }
182 
183 double
185  return myContainerState->getSpeed(*this);
186 }
187 
188 
191  return myRoute;
192 }
193 
194 
195 void
197  const SUMOTime duration = myArrived - myDeparted;
198  // no timeloss is possible
199  const double distance = mySpeed * STEPS2TIME(duration);
200  os.openTag("tranship");
201  os.writeAttr("depart", time2string(myDeparted));
202  os.writeAttr("departPos", myDepartPos);
203  os.writeAttr("arrival", time2string(myArrived));
204  os.writeAttr("arrivalPos", myArrivalPos);
205  os.writeAttr("duration", time2string(duration));
206  os.writeAttr("routeLength", distance);
207  os.writeAttr("maxSpeed", mySpeed);
208  os.closeTag();
209 }
210 
211 
212 void
214  os.openTag("tranship").writeAttr(SUMO_ATTR_EDGES, myRoute);
216  os.closeTag();
217 }
218 
219 
220 void
222  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "departure")
223  .writeAttr("agent", c.getID()).writeAttr("link", myRoute.front()->getID()).closeTag();
224 }
225 
226 
227 void
229  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival")
230  .writeAttr("agent", c.getID()).writeAttr("link", myRoute.back()->getID()).closeTag();
231 }
232 
233 bool
235  ((MSEdge*)getEdge())->removeContainer(container);
236  if (myRouteStep == myRoute.end() - 1) {
237  if (myDestinationStop != 0) {
238  myDestinationStop->addTransportable(container); //jakob
239  }
240  if (!container->proceed(MSNet::getInstance(), currentTime)) {
242  }
243  return true;
244  } else {
245  if (nextInternal == 0) {
246  ++myRouteStep;
248  } else {
249  myCurrentInternalEdge = nextInternal;
250  }
251  ((MSEdge*) getEdge())->addContainer(container);
252  return false;
253  }
254 }
255 
256 /* -------------------------------------------------------------------------
257  * MSContainer - methods
258  * ----------------------------------------------------------------------- */
260  : MSTransportable(pars, vtype, plan) {
261 }
262 
263 
265 }
266 
267 
268 bool
270  Stage* prior = *myStep;
271  prior->setArrived(time);
272  // must be done before increasing myStep to avoid invalid state for rendering
273  prior->getEdge()->removeContainer(this);
274  myStep++;
275  if (myStep != myPlan->end()) {
276  (*myStep)->proceed(net, this, time, prior);
277  return true;
278  } else {
279  return false;
280  }
281 }
282 
283 
284 void
286  os.openTag("containerinfo").writeAttr("id", getID()).writeAttr("depart", time2string(getDesiredDepart()));
287  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
288  (*i)->tripInfoOutput(os, transportable);
289  }
290  os.closeTag();
291 }
292 
293 
294 void
297  if (myStep == myPlan->end()) {
298  os.writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
299  }
300  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
301  (*i)->routeOutput(os);
302  }
303  os.closeTag();
304  os.lf();
305 }
306 
307 /****************************************************************************/
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 ...
double getSpeed() const
Returns the speed of the container.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:260
virtual void setArrived(SUMOTime now)
logs end of the step
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:607
virtual void beginEventOutput(const MSTransportable &c, SUMOTime t, OutputDevice &os) const
Called for writing the events output.
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
A lane area vehicles can halt at.
std::vector< const MSEdge * >::iterator myRouteStep
current step
Definition: MSContainer.h:210
virtual void proceed(MSNet *net, MSTransportable *container, SUMOTime now, Stage *previous)
proceeds to the next step
Definition: MSContainer.cpp:67
The departure is container triggered.
virtual void addContainer(MSTransportable *container) const
Add a container to myContainers.
Definition: MSEdge.h:607
virtual const MSEdge * getEdge() const =0
Returns the current edge.
const MSStoppingPlace * getDestinationStop() const
returns the destination stop (if any)
bool isWaiting4Vehicle() const
Whether the person waits for a vehicle.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
const MSEdge * getEdge() const
Returns the current edge.
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:167
const std::set< std::string > myLines
the lines to choose from
const std::string & getID() const
returns the id of the transportable
void removeWaiting(const MSEdge *const edge, SUMOVehicle *vehicle)
Removes a vehicle from the list of waiting vehicles to a given edge.
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:78
virtual bool proceed(MSNet *net, SUMOTime time)=0
double myDepartPos
the depart position
Definition: MSContainer.h:213
MSTransportablePlan::iterator myStep
the iterator over the route
virtual void removeContainer(MSTransportable *container) const
Remove container from myContainers.
Definition: MSEdge.h:612
The simulated network and simulation perfomer.
Definition: MSNet.h:90
The car-following model and parameter.
Definition: MSVehicleType.h:72
virtual void erase(MSTransportable *transportable)
removes a single transportable
void addTransportable(MSTransportable *p)
adds a transportable to this stop
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
virtual MSTransportableControl & getContainerControl()
Returns the container control.
Definition: MSNet.cpp:776
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 ...
const MSEdge & getDestination() const
returns the destination edge
std::vector< const MSEdge * > myRoute
The route of the container.
Definition: MSContainer.h:207
static double interpretEdgePos(double pos, double maximumValue, SumoXMLAttr attr, const std::string &id)
Interprets negative edge positions and fits them onto a given edge.
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.
const MSEdge * getFromEdge() const
A road/street connecting two junctions.
Definition: MSEdge.h:80
std::vector< MSTransportable::Stage * > MSTransportablePlan
the structure holding the plan of a transportable
const MSLane & getLane() const
Returns the lane this stop is located at.
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:55
SUMOTime myDeparted
the time at which this stage started
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
Representation of a vehicle.
Definition: SUMOVehicle.h:66
MSStoppingPlace *const myDestinationStop
the stop to reach by getting transported (if any)
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
virtual double getEdgePos(SUMOTime now) const =0
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:306
double getEdgePos(SUMOTime now) const
Returns the offset from the start of the current edge measured in its natural direction.
Position getPosition(SUMOTime now) const
Returns the position of the container.
double mySpeed
the speed of the container
Definition: MSContainer.h:216
virtual void tripInfoOutput(OutputDevice &os, MSTransportable *transportable) const
Called on writing tripinfo output.
Definition: MSContainer.cpp:97
#define STEPS2TIME(x)
Definition: SUMOTime.h:64
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 tripInfoOutput(OutputDevice &os, MSTransportable *transportable) const
Called on writing tripinfo output.
double getAngle(const MSContainer::MSContainerStage_Tranship &stage, SUMOTime now) const
return the direction in which the container heading to
SUMOVehicle * myVehicle
The taken vehicle.
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.
CState * myContainerState
state that is to be manipulated by MSCModel
Definition: MSContainer.h:219
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
std::string getStageDescription() const
returns the stage description as a string
Definition: MSContainer.cpp:91
Structure representing possible vehicle parameter.
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:359
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
Position getPosition(const MSContainer::MSContainerStage_Tranship &stage, SUMOTime now) const
return the network coordinate of the container
virtual bool proceed(MSNet *net, SUMOTime time)
MSContainerStage_Tranship(const std::vector< const MSEdge * > &route, MSStoppingPlace *toStop, double speed, double departPos, double arrivalPos)
constructor
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:70
SUMOTime getWaitingTime(SUMOTime now) const
Returns the time the container spent waiting.
bool closeTag()
Closes the most recently opened tag.
MSContainerStage_Driving(const MSEdge &destination, MSStoppingPlace *toStop, const double arrivalPos, const std::vector< std::string > &lines)
constructor
Definition: MSContainer.cpp:54
MSTransportablePlan * myPlan
the plan of the transportable
long long int SUMOTime
Definition: TraCIDefs.h:51
const MSEdge * getToEdge() const
Returns last edge of the containers route.
void add(SUMOVehicle *veh)
Adds a single vehicle for departure.
double getAngle(SUMOTime now) const
Returns the angle 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:236
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
void lf()
writes a line feed if applicable
Definition: OutputDevice.h:238
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
ConstMSEdgeVector getEdges() const
the edges of the current stage
double myArrivalPos
the position at which we want to arrive
double getSpeed(const MSContainer::MSContainerStage_Tranship &stage) const
return the current speed of the container
MSEdge * myCurrentInternalEdge
The current internal edge this container is on or 0.
Definition: MSContainer.h:222