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>
42 #include <microsim/MSEdge.h>
43 #include <microsim/MSLane.h>
44 #include <microsim/MSGlobals.h>
45 #include <microsim/MSNet.h>
47 #include <microsim/MSRoute.h>
48 #include "MSStateHandler.h"
49 
50 #ifdef HAVE_INTERNAL
51 #include <mesosim/MESegment.h>
52 #include <mesosim/MELoop.h>
53 #endif
54 
55 #ifdef CHECK_MEMORY_LEAKS
56 #include <foreign/nvwa/debug_new.h>
57 #endif // CHECK_MEMORY_LEAKS
58 
59 
60 // ===========================================================================
61 // method definitions
62 // ===========================================================================
63 MSStateHandler::MSStateHandler(const std::string& file, const SUMOTime offset) :
64  SUMOSAXHandler(file), myOffset(offset),
65 #ifdef HAVE_INTERNAL
66  mySegment(0),
67 #endif
68  myEdgeAndLane(0, -1),
69  myCurrentVType(0) {
70 }
71 
72 
74 }
75 
76 
77 void
78 MSStateHandler::saveState(const std::string& file, SUMOTime step) {
85 #ifdef HAVE_INTERNAL
86  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
87  for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(i)); s != 0; s = s->getNextSegment()) {
88  s->saveState(out);
89  }
90  }
91 #endif
92  } else {
93  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
94  const std::vector<MSLane*>& lanes = MSEdge::dictionary(i)->getLanes();
95  for (std::vector<MSLane*>::const_iterator it = lanes.begin(); it != lanes.end(); ++it) {
96  (*it)->saveState(out);
97  }
98  }
99  }
100 }
101 
102 
103 void
106  switch (element) {
107  case SUMO_TAG_SNAPSHOT: {
109  const std::string& version = attrs.getString(SUMO_ATTR_VERSION);
110  if (version != VERSION_STRING) {
111  WRITE_WARNING("State was written with sumo version " + version + " (present: " + VERSION_STRING + ")!");
112  }
113  break;
114  }
115  case SUMO_TAG_DELAY: {
118  break;
119  }
120  case SUMO_TAG_ROUTE: {
121  const std::string id = attrs.getString(SUMO_ATTR_ID);
122  if (MSRoute::dictionary(id) == 0) {
123  ConstMSEdgeVector edges;
125  MSRoute* r = new MSRoute(id, edges, attrs.getBool(SUMO_ATTR_STATE),
126  0, std::vector<SUMOVehicleParameter::Stop>());
127  MSRoute::dictionary(id, r);
128  }
129  break;
130  }
132  const std::string id = attrs.getString(SUMO_ATTR_ID);
133  if (MSRoute::dictionary(id) == 0) {
135  std::vector<std::string> routeIDs;
136  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
138  for (std::vector<std::string>::const_iterator it = routeIDs.begin(); it != routeIDs.end(); ++it) {
139  SUMOReal prob;
140  iss >> prob;
141  const MSRoute* r = MSRoute::dictionary(*it);
142  assert(r != 0);
143  dist->add(prob, r, false);
144  r->addReference();
145  }
146  MSRoute::dictionary(id, dist, attrs.getBool(SUMO_ATTR_STATE));
147  }
148  break;
149  }
150  case SUMO_TAG_VTYPE: {
152  break;
153  }
155  const std::string id = attrs.getString(SUMO_ATTR_ID);
156  if (vc.getVType(id) == 0) {
158  std::vector<std::string> typeIDs;
159  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
161  for (std::vector<std::string>::const_iterator it = typeIDs.begin(); it != typeIDs.end(); ++it) {
162  SUMOReal prob;
163  iss >> prob;
164  MSVehicleType* t = vc.getVType(*it);
165  assert(t != 0);
166  dist->add(prob, t, false);
167  }
168  vc.addVTypeDistribution(id, dist);
169  }
170  break;
171  }
172  case SUMO_TAG_VEHICLE: {
174  p->id = attrs.getString(SUMO_ATTR_ID);
176  p->routeid = attrs.getString(SUMO_ATTR_ROUTE);
177  p->vtypeid = attrs.getString(SUMO_ATTR_TYPE);
178  const MSRoute* route = MSRoute::dictionary(p->routeid);
179  const MSVehicleType* type = vc.getVType(p->vtypeid);
180  assert(route != 0);
181  assert(type != 0);
182  assert(vc.getVehicle(p->id) == 0);
183 
184  SUMOVehicle* v = vc.buildVehicle(p, route, type, true);
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  }
193  break;
194  }
195 #ifdef HAVE_INTERNAL
196  case SUMO_TAG_SEGMENT: {
197  if (mySegment == 0) {
198  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(0));
199  } else if (mySegment->getNextSegment() == 0) {
200  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(mySegment->getEdge().getNumericalID() + 1));
201  } else {
202  mySegment = mySegment->getNextSegment();
203  }
204  myQueIndex = 0;
205  break;
206  }
207 #endif
208  case SUMO_TAG_LANE: {
209  myEdgeAndLane.second++;
210  if (myEdgeAndLane.second == (int)MSEdge::dictionary(myEdgeAndLane.first)->getLanes().size()) {
211  myEdgeAndLane.first++;
212  myEdgeAndLane.second = 0;
213  }
214  break;
215  }
217  std::vector<std::string> vehIDs;
220 #ifdef HAVE_INTERNAL
221  mySegment->loadState(vehIDs, MSNet::getInstance()->getVehicleControl(), TplConvert::_2long(attrs.getString(SUMO_ATTR_TIME).c_str()) - myOffset, myQueIndex++);
222 #endif
223  } else {
224  MSEdge::dictionary(myEdgeAndLane.first)->getLanes()[myEdgeAndLane.second]->loadState(
225  vehIDs, MSNet::getInstance()->getVehicleControl());
226  }
227  break;
228  }
229  default:
230  // parse embedded vtype information
231  if (myCurrentVType != 0) {
233  }
234  break;
235  }
236 }
237 
238 
239 void
241  switch (element) {
242  case SUMO_TAG_VTYPE:
244  delete myCurrentVType;
245  myCurrentVType = 0;
246  break;
247  default:
248  break;
249  }
250 }
251 
252 
253 /****************************************************************************/
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:61
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:162
std::pair< int, int > myEdgeAndLane
static long long int _2long(const E *const data)
Definition: TplConvert.h:161
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn&#39;t already in the dictionary...
Definition: MSEdge.cpp:579
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&#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
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:48
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:226
bool addVType(MSVehicleType *vehType)
Adds a vehicle type.
Structure representing possible vehicle parameter.
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:349
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:613
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:214
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:642
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&#39;s id.
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:116