SUMO - Simulation of Urban MObility
MSStateHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Parser and output filter for routes and vehicles state saving and loading
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 200122014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifdef HAVE_VERSION_H
34 #include <version.h>
35 #endif
36 
37 #include <sstream>
43 #include <microsim/MSEdge.h>
44 #include <microsim/MSLane.h>
45 #include <microsim/MSGlobals.h>
46 #include <microsim/MSNet.h>
48 #include <microsim/MSRoute.h>
49 #include "MSStateHandler.h"
50 
51 #include <mesosim/MESegment.h>
52 #include <mesosim/MELoop.h>
53 
54 #ifdef CHECK_MEMORY_LEAKS
55 #include <foreign/nvwa/debug_new.h>
56 #endif // CHECK_MEMORY_LEAKS
57 
58 
59 // ===========================================================================
60 // method definitions
61 // ===========================================================================
62 MSStateHandler::MSStateHandler(const std::string& file, const SUMOTime offset) :
63  SUMOSAXHandler(file), myOffset(offset),
64  mySegment(0),
65  myEdgeAndLane(0, -1),
66  myCurrentVType(0) {
67 }
68 
69 
71  delete myCurrentVType;
72 }
73 
74 
75 void
76 MSStateHandler::saveState(const std::string& file, SUMOTime step) {
83  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
84  for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::getAllEdges()[i]); s != 0; s = s->getNextSegment()) {
85  s->saveState(out);
86  }
87  }
88  } else {
89  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
90  const std::vector<MSLane*>& lanes = MSEdge::getAllEdges()[i]->getLanes();
91  for (std::vector<MSLane*>::const_iterator it = lanes.begin(); it != lanes.end(); ++it) {
92  (*it)->saveState(out);
93  }
94  }
95  }
96 }
97 
98 
99 void
102  switch (element) {
103  case SUMO_TAG_SNAPSHOT: {
105  const std::string& version = attrs.getString(SUMO_ATTR_VERSION);
106  if (version != VERSION_STRING) {
107  WRITE_WARNING("State was written with sumo version " + version + " (present: " + VERSION_STRING + ")!");
108  }
109  break;
110  }
111  case SUMO_TAG_DELAY: {
112  vc.setState(attrs.getInt(SUMO_ATTR_NUMBER),
113  attrs.getInt(SUMO_ATTR_BEGIN),
114  attrs.getInt(SUMO_ATTR_END),
115  attrs.getFloat(SUMO_ATTR_DEPART),
116  attrs.getFloat(SUMO_ATTR_TIME));
117  break;
118  }
119  case SUMO_TAG_ROUTE: {
120  const std::string id = attrs.getString(SUMO_ATTR_ID);
121  if (MSRoute::dictionary(id) == 0) {
122  ConstMSEdgeVector edges;
124  MSRoute* r = new MSRoute(id, edges, attrs.getBool(SUMO_ATTR_STATE),
125  0, std::vector<SUMOVehicleParameter::Stop>());
126  MSRoute::dictionary(id, r);
127  }
128  break;
129  }
131  const std::string id = attrs.getString(SUMO_ATTR_ID);
132  if (MSRoute::dictionary(id) == 0) {
134  std::vector<std::string> routeIDs;
135  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
137  for (std::vector<std::string>::const_iterator it = routeIDs.begin(); it != routeIDs.end(); ++it) {
138  SUMOReal prob;
139  iss >> prob;
140  const MSRoute* r = MSRoute::dictionary(*it);
141  assert(r != 0);
142  dist->add(prob, r, false);
143  r->addReference();
144  }
145  MSRoute::dictionary(id, dist, attrs.getBool(SUMO_ATTR_STATE));
146  }
147  break;
148  }
149  case SUMO_TAG_VTYPE: {
151  break;
152  }
154  const std::string id = attrs.getString(SUMO_ATTR_ID);
155  if (vc.getVType(id) == 0) {
157  std::vector<std::string> typeIDs;
158  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
160  for (std::vector<std::string>::const_iterator it = typeIDs.begin(); it != typeIDs.end(); ++it) {
161  SUMOReal prob;
162  iss >> prob;
163  MSVehicleType* t = vc.getVType(*it);
164  assert(t != 0);
165  dist->add(prob, t, false);
166  }
167  vc.addVTypeDistribution(id, dist);
168  }
169  break;
170  }
171  case SUMO_TAG_VEHICLE: {
173  p->id = attrs.getString(SUMO_ATTR_ID);
175  p->routeid = attrs.getString(SUMO_ATTR_ROUTE);
176  p->vtypeid = attrs.getString(SUMO_ATTR_TYPE);
177  const MSRoute* route = MSRoute::dictionary(p->routeid);
178  const MSVehicleType* type = vc.getVType(p->vtypeid);
179  assert(route != 0);
180  assert(type != 0);
181  assert(vc.getVehicle(p->id) == 0);
182 
183  SUMOVehicle* v = vc.buildVehicle(p, route, type, true);
184  vc.discountStateLoaded(); // already included (see SUMO_TAG_DELAY)
185  v->loadState(attrs, myOffset);
186  if (!vc.addVehicle(p->id, v)) {
187  throw ProcessError("Error: Could not build vehicle " + p->id + "!");
188  }
189  if (!v->hasDeparted()) {
190  // !!! the save did not keep the order in which the vehicles are checked for insertion
192  } else {
193  // vehicle already departed: disable pre-insertion rerouting and enable regular routing behavior
194  MSDevice_Routing* routingDevice = static_cast<MSDevice_Routing*>(v->getDevice(typeid(MSDevice_Routing)));
195  if (routingDevice != 0) {
197  }
198  }
199  break;
200  }
201  case SUMO_TAG_SEGMENT: {
202  if (mySegment == 0) {
204  } else if (mySegment->getNextSegment() == 0) {
206  } else {
208  }
209  myQueIndex = 0;
210  break;
211  }
212  case SUMO_TAG_LANE: {
213  myEdgeAndLane.second++;
214  if (myEdgeAndLane.second == (int)MSEdge::getAllEdges()[myEdgeAndLane.first]->getLanes().size()) {
215  myEdgeAndLane.first++;
216  myEdgeAndLane.second = 0;
217  }
218  break;
219  }
221  std::vector<std::string> vehIDs;
224  mySegment->loadState(vehIDs, MSNet::getInstance()->getVehicleControl(), TplConvert::_2long(attrs.getString(SUMO_ATTR_TIME).c_str()) - myOffset, myQueIndex++);
225  } else {
226  MSEdge::getAllEdges()[myEdgeAndLane.first]->getLanes()[myEdgeAndLane.second]->loadState(
227  vehIDs, MSNet::getInstance()->getVehicleControl());
228  }
229  break;
230  }
231  default:
232  // parse embedded vtype information
233  if (myCurrentVType != 0) {
235  }
236  break;
237  }
238 }
239 
240 
241 void
243  switch (element) {
244  case SUMO_TAG_VTYPE:
246  delete myCurrentVType;
247  myCurrentVType = 0;
248  break;
249  default:
250  break;
251  }
252 }
253 
254 
255 /****************************************************************************/
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
bool addVTypeDistribution(const std::string &id, RandomDistributor< MSVehicleType * > *vehTypeDistribution)
Adds a vehicle type distribution.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
long long int SUMOTime
Definition: SUMOTime.h:43
unsigned int myQueIndex
std::string vtypeid
The vehicle&#39;s type id.
static void parseVTypeEmbedded(SUMOVTypeParameter &into, int element, const SUMOSAXAttributes &attrs, bool fromVType=false)
Parses an element embedded in vtype definition.
bool add(SUMOReal prob, T val, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:59
A device that performs vehicle rerouting based on current edge speeds.
MESegment * mySegment
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:160
std::pair< int, int > myEdgeAndLane
static long long int _2long(const E *const data)
Definition: TplConvert.h:161
virtual bool addVehicle(const std::string &id, SUMOVehicle *v)
Tries to insert the vehicle into the internal vehicle container.
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:78
SAX-handler base for SUMO-files.
SUMOVTypeParameter * myCurrentVType
The currently parsed vehicle type.
const MSEdge & getEdge() const
Returns the edge this segment belongs to.
Definition: MESegment.h:272
static MSVehicleType * build(SUMOVTypeParameter &from)
Builds the microsim vehicle type described by the given parameter.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
The car-following model and parameter.
Definition: MSVehicleType.h:74
static void dict_saveState(OutputDevice &out)
Saves all known routes into the given stream.
Definition: MSRoute.cpp:265
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
The state of a link.
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:189
A road/street connecting two junctions.
Definition: MSEdge.h:80
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
const std::string & getFileName() const
returns the current file name
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:158
MSStateHandler(const std::string &file, const SUMOTime offset)
standard constructor
the edges of a route
std::string routeid
The vehicle&#39;s route id.
Representation of a vehicle.
Definition: SUMOVehicle.h:65
Encapsulated SAX-Attributes.
virtual SUMOReal getFloat(int id) const =0
Returns the SUMOReal-value of the named (by its enum-value) attribute.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:308
void loadState(std::vector< std::string > &vehIDs, MSVehicleControl &vc, const SUMOTime blockTime, const unsigned int queIdx)
Loads the state of this segment with the given parameters.
Definition: MESegment.cpp:607
virtual SUMOVehicle * buildVehicle(SUMOVehicleParameter *defs, const MSRoute *route, const MSVehicleType *type, const bool ignoreStopErrors, const bool fromRouteFile=true)
Builds a vehicle, increases the number of built vehicles.
SUMOTime depart
The vehicle&#39;s departure time.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
void saveState(OutputDevice &out)
Saves the current state into the given stream.
const SUMOTime myOffset
void setState(int runningVehNo, int loadedVehNo, int endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime)
Sets the current state variables as loaded from the stream.
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:103
#define VERSION_STRING
Definition: config.h:225
bool addVType(MSVehicleType *vehType)
Adds a vehicle type.
The vehicle has departed (was inserted into the network)
Structure representing possible vehicle parameter.
int getNumericalID() const
Returns the numerical id of the edge.
Definition: MSEdge.h:264
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:349
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
static const MSEdgeVector & getAllEdges()
Returns all edges with a numerical id.
Definition: MSEdge.cpp:611
virtual ~MSStateHandler()
standard destructor
A single mesoscopic segment (cell)
Definition: MESegment.h:57
void myEndElement(int element)
Called when a closing tag occurs.
static size_t dictSize()
Returns the number of edges.
Definition: MSEdge.cpp:605
MESegment * getSegmentForEdge(const MSEdge &e, SUMOReal pos=0)
Get the segment for a given edge at a given position.
Definition: MELoop.cpp:288
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition: MSGlobals.h:99
virtual int getInt(int id) const =0
Returns the int-value of the named (by its enum-value) attribute.
virtual bool getBool(int id) const =0
Returns the bool-value of the named (by its enum-value) attribute.
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason)
Computes a new route on vehicle insertion.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
#define SUMOReal
Definition: config.h:213
The class responsible for building and deletion of vehicles.
void discountStateLoaded()
avoid counting a vehicle twice if it was loaded from state and route input
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID, MTRand *rng=0)
Returns the named vehicle type or a sample from the named distribution.
static SUMOVTypeParameter * beginVTypeParsing(const SUMOSAXAttributes &attrs, const std::string &file)
Starts to parse a vehicle type.
void add(SUMOVehicle *veh)
Adds a single vehicle for departure.
static bool gUseMesoSim
Definition: MSGlobals.h:87
static void parseEdgesList(const std::string &desc, ConstMSEdgeVector &into, const std::string &rid)
Parses the given string assuming it contains a list of edge ids divided by spaces.
Definition: MSEdge.cpp:634
static void saveState(const std::string &file, SUMOTime step)
Saves the current state.
std::string id
The vehicle&#39;s id.
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:122