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>
41 #include <microsim/MSEdge.h>
42 #include <microsim/MSLane.h>
43 #include <microsim/MSGlobals.h>
44 #include <microsim/MSNet.h>
46 #include <microsim/MSRoute.h>
47 #include "MSStateHandler.h"
48 
49 #ifdef HAVE_INTERNAL
50 #include <mesosim/MESegment.h>
51 #include <mesosim/MELoop.h>
52 #endif
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 #ifdef HAVE_INTERNAL
65  mySegment(0),
66 #endif
67  myEdgeAndLane(0, -1),
68  myCurrentVType(0) {
69 }
70 
71 
73 }
74 
75 
76 void
77 MSStateHandler::saveState(const std::string& file, SUMOTime step) {
84 #ifdef HAVE_INTERNAL
85  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
86  for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(i)); s != 0; s = s->getNextSegment()) {
87  s->saveState(out);
88  }
89  }
90 #endif
91  } else {
92  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
93  const std::vector<MSLane*>& lanes = MSEdge::dictionary(i)->getLanes();
94  for (std::vector<MSLane*>::const_iterator it = lanes.begin(); it != lanes.end(); ++it) {
95  (*it)->saveState(out);
96  }
97  }
98  }
99 }
100 
101 
102 void
105  switch (element) {
106  case SUMO_TAG_SNAPSHOT: {
107  myTime = attrs.getInt(SUMO_ATTR_TIME);
108  const std::string& version = attrs.getString(SUMO_ATTR_VERSION);
109  if (version != VERSION_STRING) {
110  WRITE_WARNING("State was written with sumo version " + version + " (present: " + VERSION_STRING + ")!");
111  }
112  break;
113  }
114  case SUMO_TAG_DELAY: {
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);
174  p->depart = attrs.getInt(SUMO_ATTR_DEPART) - myOffset;
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  v->loadState(attrs, myOffset);
185  if (!vc.addVehicle(p->id, v)) {
186  throw ProcessError("Error: Could not build vehicle " + p->id + "!");
187  }
188  if (!v->hasDeparted()) {
189  // !!! the save did not keep the order in which the vehicles are checked for insertion
191  }
192  break;
193  }
194 #ifdef HAVE_INTERNAL
195  case SUMO_TAG_SEGMENT: {
196  if (mySegment == 0) {
197  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(0));
198  } else if (mySegment->getNextSegment() == 0) {
199  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(mySegment->getEdge().getNumericalID() + 1));
200  } else {
201  mySegment = mySegment->getNextSegment();
202  }
203  myQueIndex = 0;
204  break;
205  }
206 #endif
207  case SUMO_TAG_LANE: {
208  myEdgeAndLane.second++;
209  if (myEdgeAndLane.second == (int)MSEdge::dictionary(myEdgeAndLane.first)->getLanes().size()) {
210  myEdgeAndLane.first++;
211  myEdgeAndLane.second = 0;
212  }
213  break;
214  }
216  std::vector<std::string> vehIDs;
219 #ifdef HAVE_INTERNAL
220  mySegment->loadState(vehIDs, MSNet::getInstance()->getVehicleControl(), attrs.getInt(SUMO_ATTR_TIME) - myOffset, myQueIndex++);
221 #endif
222  } else {
223  MSEdge::dictionary(myEdgeAndLane.first)->getLanes()[myEdgeAndLane.second]->loadState(
224  vehIDs, MSNet::getInstance()->getVehicleControl());
225  }
226  break;
227  }
228  default:
229  // parse embedded vtype information
230  if (myCurrentVType != 0) {
232  }
233  break;
234  }
235 }
236 
237 
238 void
240  switch (element) {
241  case SUMO_TAG_VTYPE:
243  delete myCurrentVType;
244  myCurrentVType = 0;
245  break;
246  default:
247  break;
248  }
249 }
250 
251 
252 /****************************************************************************/
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.
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:531
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:79
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.
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:189
A road/street connecting two junctions.
Definition: MSEdge.h:81
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: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:288
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'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:230
bool addVType(MSVehicleType *vehType)
Adds a vehicle type.
Structure representing possible vehicle parameter.
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:329
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:565
int SUMOTime
Definition: SUMOTime.h:43
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:218
static const bool gUseMesoSim
Definition: MSGlobals.h:102
The class responsible for building and deletion of vehicles.
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 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:594
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