SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSStateHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Parser and output filter for routes and vehicles state saving and loading
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
11 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <sstream>
36 #include <microsim/MSEdge.h>
37 #include <microsim/MSLane.h>
38 #include <microsim/MSGlobals.h>
39 #include <microsim/MSNet.h>
41 #include <microsim/MSRoute.h>
42 #include "MSStateHandler.h"
43 
44 #ifdef HAVE_INTERNAL
45 #include <mesosim/MESegment.h>
46 #include <mesosim/MELoop.h>
47 #endif
48 
49 
50 #ifdef CHECK_MEMORY_LEAKS
51 #include <foreign/nvwa/debug_new.h>
52 #endif // CHECK_MEMORY_LEAKS
53 
54 
55 // ===========================================================================
56 // method definitions
57 // ===========================================================================
58 MSStateHandler::MSStateHandler(const std::string& file, const SUMOTime offset) :
59  SUMOSAXHandler(file), myOffset(offset),
60 #ifdef HAVE_INTERNAL
61  mySegment(0),
62 #endif
63  myEdgeAndLane(0, -1),
64  myCurrentVType(0) {
65 }
66 
67 
69 }
70 
71 
72 void
73 MSStateHandler::saveState(const std::string& file, SUMOTime step) {
80 #ifdef HAVE_INTERNAL
81  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
82  for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(i)); s != 0; s = s->getNextSegment()) {
83  s->saveState(out);
84  }
85  }
86 #endif
87  } else {
88  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
89  const std::vector<MSLane*>& lanes = MSEdge::dictionary(i)->getLanes();
90  for (std::vector<MSLane*>::const_iterator it = lanes.begin(); it != lanes.end(); ++it) {
91  (*it)->saveState(out);
92  }
93  }
94  }
95 }
96 
97 
98 void
101  switch (element) {
102  case SUMO_TAG_SNAPSHOT: {
103  myTime = attrs.getInt(SUMO_ATTR_TIME);
104  const std::string& version = attrs.getString(SUMO_ATTR_VERSION);
105  if (version != VERSION_STRING) {
106  WRITE_WARNING("State was written with sumo version " + version + " (present: " + VERSION_STRING + ")!");
107  }
108  break;
109  }
110  case SUMO_TAG_DELAY: {
113  break;
114  }
115  case SUMO_TAG_ROUTE: {
116  const std::string id = attrs.getString(SUMO_ATTR_ID);
117  if (MSRoute::dictionary(id) == 0) {
118  MSEdgeVector edges;
120  MSRoute* r = new MSRoute(id, edges, attrs.getBool(SUMO_ATTR_STATE),
121  0, std::vector<SUMOVehicleParameter::Stop>());
122  MSRoute::dictionary(id, r);
123  }
124  break;
125  }
127  const std::string id = attrs.getString(SUMO_ATTR_ID);
128  if (MSRoute::dictionary(id) == 0) {
130  std::vector<std::string> routeIDs;
131  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
133  for (std::vector<std::string>::const_iterator it = routeIDs.begin(); it != routeIDs.end(); ++it) {
134  SUMOReal prob;
135  iss >> prob;
136  const MSRoute* r = MSRoute::dictionary(*it);
137  assert(r != 0);
138  dist->add(prob, r, false);
139  r->addReference();
140  }
141  MSRoute::dictionary(id, dist, attrs.getBool(SUMO_ATTR_STATE));
142  }
143  break;
144  }
145  case SUMO_TAG_VTYPE: {
147  break;
148  }
150  const std::string id = attrs.getString(SUMO_ATTR_ID);
151  if (vc.getVType(id) == 0) {
153  std::vector<std::string> typeIDs;
154  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
156  for (std::vector<std::string>::const_iterator it = typeIDs.begin(); it != typeIDs.end(); ++it) {
157  SUMOReal prob;
158  iss >> prob;
159  MSVehicleType* t = vc.getVType(*it);
160  assert(t != 0);
161  dist->add(prob, t, false);
162  }
163  vc.addVTypeDistribution(id, dist);
164  }
165  break;
166  }
167  case SUMO_TAG_VEHICLE: {
169  p->id = attrs.getString(SUMO_ATTR_ID);
170  p->depart = attrs.getInt(SUMO_ATTR_DEPART) - myOffset;
171  p->routeid = attrs.getString(SUMO_ATTR_ROUTE);
172  p->vtypeid = attrs.getString(SUMO_ATTR_TYPE);
173  const MSRoute* route = MSRoute::dictionary(p->routeid);
174  const MSVehicleType* type = vc.getVType(p->vtypeid);
175  assert(route != 0);
176  assert(type != 0);
177  assert(vc.getVehicle(p->id) == 0);
178 
179  SUMOVehicle* v = vc.buildVehicle(p, route, type);
180  v->loadState(attrs, myOffset);
181  if (!vc.addVehicle(p->id, v)) {
182  throw ProcessError("Error: Could not build vehicle " + p->id + "!");
183  }
184  if (!v->hasDeparted()) {
185  // !!! the save did not keep the order in which the vehicles are checked for insertion
187  }
188  break;
189  }
190 #ifdef HAVE_INTERNAL
191  case SUMO_TAG_SEGMENT: {
192  if (mySegment == 0) {
193  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(0));
194  } else if (mySegment->getNextSegment() == 0) {
195  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(mySegment->getEdge().getNumericalID() + 1));
196  } else {
197  mySegment = mySegment->getNextSegment();
198  }
199  myQueIndex = 0;
200  break;
201  }
202 #endif
203  case SUMO_TAG_LANE: {
204  myEdgeAndLane.second++;
205  if (myEdgeAndLane.second == (int)MSEdge::dictionary(myEdgeAndLane.first)->getLanes().size()) {
206  myEdgeAndLane.first++;
207  myEdgeAndLane.second = 0;
208  }
209  break;
210  }
212  std::vector<std::string> vehIDs;
215 #ifdef HAVE_INTERNAL
216  mySegment->loadState(vehIDs, MSNet::getInstance()->getVehicleControl(), attrs.getInt(SUMO_ATTR_TIME) - myOffset, myQueIndex++);
217 #endif
218  } else {
219  MSEdge::dictionary(myEdgeAndLane.first)->getLanes()[myEdgeAndLane.second]->loadState(
220  vehIDs, MSNet::getInstance()->getVehicleControl());
221  }
222  break;
223  }
224  default:
225  // parse embedded vtype information
226  if (myCurrentVType != 0) {
228  }
229  break;
230  }
231 }
232 
233 
234 void
236  switch (element) {
237  case SUMO_TAG_VTYPE:
239  delete myCurrentVType;
240  myCurrentVType = 0;
241  break;
242  default:
243  break;
244  }
245 }
246 
247 
248 /****************************************************************************/
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:256
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:154
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:465
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:196
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:237
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:188
A road/street connecting two junctions.
Definition: MSEdge.h:73
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:528
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:63
Encapsulated SAX-Attributes.
std::vector< const MSEdge * > MSEdgeVector
Definition: MSPerson.h:53
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:263
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:99
#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:294
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:499
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:70
#define SUMOReal
Definition: config.h:215
static const bool gUseMesoSim
Definition: MSGlobals.h:98
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:115