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-sim.org/
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 #include <sstream>
37 #include <microsim/MSEdge.h>
38 #include <microsim/MSLane.h>
39 #include <microsim/MSGlobals.h>
40 #include <microsim/MSNet.h>
42 #include <microsim/MSRoute.h>
43 #include "MSStateHandler.h"
44 
45 #ifdef HAVE_INTERNAL
46 #include <mesosim/MESegment.h>
47 #include <mesosim/MELoop.h>
48 #endif
49 
50 
51 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
59 MSStateHandler::MSStateHandler(const std::string& file, const SUMOTime offset) :
60  SUMOSAXHandler(file), myOffset(offset),
61 #ifdef HAVE_INTERNAL
62  mySegment(0),
63 #endif
64  myEdgeAndLane(0, -1),
65  myCurrentVType(0) {
66 }
67 
68 
70 }
71 
72 
73 void
74 MSStateHandler::saveState(const std::string& file, SUMOTime step) {
81 #ifdef HAVE_INTERNAL
82  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
83  for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(i)); s != 0; s = s->getNextSegment()) {
84  s->saveState(out);
85  }
86  }
87 #endif
88  } else {
89  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
90  const std::vector<MSLane*>& lanes = MSEdge::dictionary(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: {
104  myTime = attrs.getInt(SUMO_ATTR_TIME);
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: {
114  break;
115  }
116  case SUMO_TAG_ROUTE: {
117  const std::string id = attrs.getString(SUMO_ATTR_ID);
118  if (MSRoute::dictionary(id) == 0) {
119  MSEdgeVector edges;
121  MSRoute* r = new MSRoute(id, edges, attrs.getBool(SUMO_ATTR_STATE),
122  0, std::vector<SUMOVehicleParameter::Stop>());
123  MSRoute::dictionary(id, r);
124  }
125  break;
126  }
128  const std::string id = attrs.getString(SUMO_ATTR_ID);
129  if (MSRoute::dictionary(id) == 0) {
131  std::vector<std::string> routeIDs;
132  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
134  for (std::vector<std::string>::const_iterator it = routeIDs.begin(); it != routeIDs.end(); ++it) {
135  SUMOReal prob;
136  iss >> prob;
137  const MSRoute* r = MSRoute::dictionary(*it);
138  assert(r != 0);
139  dist->add(prob, r, false);
140  r->addReference();
141  }
142  MSRoute::dictionary(id, dist, attrs.getBool(SUMO_ATTR_STATE));
143  }
144  break;
145  }
146  case SUMO_TAG_VTYPE: {
148  break;
149  }
151  const std::string id = attrs.getString(SUMO_ATTR_ID);
152  if (vc.getVType(id) == 0) {
154  std::vector<std::string> typeIDs;
155  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
157  for (std::vector<std::string>::const_iterator it = typeIDs.begin(); it != typeIDs.end(); ++it) {
158  SUMOReal prob;
159  iss >> prob;
160  MSVehicleType* t = vc.getVType(*it);
161  assert(t != 0);
162  dist->add(prob, t, false);
163  }
164  vc.addVTypeDistribution(id, dist);
165  }
166  break;
167  }
168  case SUMO_TAG_VEHICLE: {
170  p->id = attrs.getString(SUMO_ATTR_ID);
171  p->depart = attrs.getInt(SUMO_ATTR_DEPART) - myOffset;
172  p->routeid = attrs.getString(SUMO_ATTR_ROUTE);
173  p->vtypeid = attrs.getString(SUMO_ATTR_TYPE);
174  const MSRoute* route = MSRoute::dictionary(p->routeid);
175  const MSVehicleType* type = vc.getVType(p->vtypeid);
176  assert(route != 0);
177  assert(type != 0);
178  assert(vc.getVehicle(p->id) == 0);
179 
180  SUMOVehicle* v = vc.buildVehicle(p, route, type);
181  v->loadState(attrs, myOffset);
182  if (!vc.addVehicle(p->id, v)) {
183  throw ProcessError("Error: Could not build vehicle " + p->id + "!");
184  }
185  if (!v->hasDeparted()) {
186  // !!! the save did not keep the order in which the vehicles are checked for insertion
188  }
189  break;
190  }
191 #ifdef HAVE_INTERNAL
192  case SUMO_TAG_SEGMENT: {
193  if (mySegment == 0) {
194  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(0));
195  } else if (mySegment->getNextSegment() == 0) {
196  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(mySegment->getEdge().getNumericalID() + 1));
197  } else {
198  mySegment = mySegment->getNextSegment();
199  }
200  myQueIndex = 0;
201  break;
202  }
203 #endif
204  case SUMO_TAG_LANE: {
205  myEdgeAndLane.second++;
206  if (myEdgeAndLane.second == (int)MSEdge::dictionary(myEdgeAndLane.first)->getLanes().size()) {
207  myEdgeAndLane.first++;
208  myEdgeAndLane.second = 0;
209  }
210  break;
211  }
213  std::vector<std::string> vehIDs;
216 #ifdef HAVE_INTERNAL
217  mySegment->loadState(vehIDs, MSNet::getInstance()->getVehicleControl(), attrs.getInt(SUMO_ATTR_TIME) - myOffset, myQueIndex++);
218 #endif
219  } else {
220  MSEdge::dictionary(myEdgeAndLane.first)->getLanes()[myEdgeAndLane.second]->loadState(
221  vehIDs, MSNet::getInstance()->getVehicleControl());
222  }
223  break;
224  }
225  default:
226  // parse embedded vtype information
227  if (myCurrentVType != 0) {
229  }
230  break;
231  }
232 }
233 
234 
235 void
237  switch (element) {
238  case SUMO_TAG_VTYPE:
240  delete myCurrentVType;
241  myCurrentVType = 0;
242  break;
243  default:
244  break;
245  }
246 }
247 
248 
249 /****************************************************************************/
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
unsigned int myQueIndex
std::string vtypeid
The vehicle'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.
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID)
Returns the named vehicle type or a sample from the named distribution.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
std::pair< int, int > myEdgeAndLane
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn't already in the dictionary...
Definition: MSEdge.cpp:473
virtual bool addVehicle(const std::string &id, SUMOVehicle *v)
Tries to insert the vehicle into the internal vehicle container.
SAX-handler base for SUMO-files.
SUMOVTypeParameter * myCurrentVType
The currently parsed vehicle type.
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:238
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.
virtual SUMOVehicle * buildVehicle(SUMOVehicleParameter *defs, const MSRoute *route, const MSVehicleType *type)
Builds a vehicle, increases the number of built vehicles.
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:189
A road/street connecting two junctions.
Definition: MSEdge.h:74
static void parseEdgesList(const std::string &desc, std::vector< const MSEdge * > &into, const std::string &rid)
Parses the given string assuming it contains a list of edge ids divided by spaces.
Definition: MSEdge.cpp:536
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
MSStateHandler(const std::string &file, const SUMOTime offset)
standard constructor
the edges of a route
void setState(int runningVehNo, int endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime)
Sets the current state variables as loaded from the stream.
std::string routeid
The vehicle's route id.
Representation of a vehicle.
Definition: SUMOVehicle.h:64
Encapsulated SAX-Attributes.
std::vector< const MSEdge * > MSEdgeVector
Definition: MSPerson.h:57
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:269
SUMOTime depart
The vehicle's departure time.
void saveState(OutputDevice &out)
Saves the current state into the given stream.
const SUMOTime myOffset
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:100
#define VERSION_STRING
Definition: config.h:227
bool addVType(MSVehicleType *vehType)
Adds a vehicle type.
Structure representing possible vehicle parameter.
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:300
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
virtual ~MSStateHandler()
standard destructor
void myEndElement(int element)
Called when a closing tag occurs.
static size_t dictSize()
Returns the number of edges.
Definition: MSEdge.cpp:507
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.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
#define SUMOReal
Definition: config.h:215
static const bool gUseMesoSim
Definition: MSGlobals.h:99
The class responsible for building and deletion of vehicles.
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.
virtual void loadState(const SUMOSAXAttributes &attrs, const SUMOTime offset)=0
Loads the state of this vehicle from the given description.
static void saveState(const std::string &file, SUMOTime step)
Saves the current state.
std::string id
The vehicle's id.
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:116