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-2013 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>
40 #include <microsim/MSRoute.h>
41 #include "MSStateHandler.h"
42 
43 #ifdef HAVE_INTERNAL
44 #include <mesosim/MESegment.h>
45 #include <mesosim/MELoop.h>
46 #endif
47 
48 
49 #ifdef CHECK_MEMORY_LEAKS
50 #include <foreign/nvwa/debug_new.h>
51 #endif // CHECK_MEMORY_LEAKS
52 
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
57 MSStateHandler::MSStateHandler(const std::string& file, const SUMOTime offset) :
58  SUMOSAXHandler(file), myOffset(offset),
59 #ifdef HAVE_INTERNAL
60  mySegment(0),
61 #endif
62  myEdgeAndLane(0, -1) {
63 }
64 
65 
67 }
68 
69 
70 void
71 MSStateHandler::saveState(const std::string& file, SUMOTime step) {
78 #ifdef HAVE_INTERNAL
79  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
80  for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(i)); s != 0; s = s->getNextSegment()) {
81  s->saveState(out);
82  }
83  }
84 #endif
85  } else {
86  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
87  const std::vector<MSLane*>& lanes = MSEdge::dictionary(i)->getLanes();
88  for (std::vector<MSLane*>::const_iterator it = lanes.begin(); it != lanes.end(); ++it) {
89  (*it)->saveState(out);
90  }
91  }
92  }
93 }
94 
95 
96 void
99  switch (element) {
100  case SUMO_TAG_SNAPSHOT: {
101  myTime = attrs.getInt(SUMO_ATTR_TIME);
102  const std::string& version = attrs.getString(SUMO_ATTR_VERSION);
103  if (version != VERSION_STRING) {
104  WRITE_WARNING("State was written with sumo version " + version + " (present: " + VERSION_STRING + ")!");
105  }
106  break;
107  }
108  case SUMO_TAG_DELAY: {
111  break;
112  }
113  case SUMO_TAG_ROUTE: {
114  const std::string id = attrs.getString(SUMO_ATTR_ID);
115  if (MSRoute::dictionary(id) == 0) {
116  MSEdgeVector edges;
118  MSRoute* r = new MSRoute(id, edges, attrs.getInt(SUMO_ATTR_STATE),
119  0, std::vector<SUMOVehicleParameter::Stop>());
120  MSRoute::dictionary(id, r);
121  }
122  break;
123  }
125  const std::string id = attrs.getString(SUMO_ATTR_ID);
126  if (MSRoute::dictionary(id) == 0) {
128  std::vector<std::string> routeIDs;
129  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
131  for (std::vector<std::string>::const_iterator it = routeIDs.begin(); it != routeIDs.end(); ++it) {
132  SUMOReal prob;
133  iss >> prob;
134  const MSRoute* r = MSRoute::dictionary(*it);
135  assert(r != 0);
136  dist->add(prob, r, false);
137  }
138  MSRoute::dictionary(id, dist);
139  }
140  break;
141  }
142  case SUMO_TAG_VTYPE: {
144  vc.addVType(MSVehicleType::build(*def));
145  delete def;
146  }
148  const std::string id = attrs.getString(SUMO_ATTR_ID);
149  if (vc.getVType(id) == 0) {
151  std::vector<std::string> typeIDs;
152  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
154  for (std::vector<std::string>::const_iterator it = typeIDs.begin(); it != typeIDs.end(); ++it) {
155  SUMOReal prob;
156  iss >> prob;
157  MSVehicleType* t = vc.getVType(*it);
158  assert(t != 0);
159  dist->add(prob, t, false);
160  }
161  vc.addVTypeDistribution(id, dist);
162  }
163  break;
164  }
165  case SUMO_TAG_VEHICLE: {
167  p->id = attrs.getString(SUMO_ATTR_ID);
168  p->depart = attrs.getInt(SUMO_ATTR_DEPART) - myOffset;
169  p->routeid = attrs.getString(SUMO_ATTR_ROUTE);
170  p->vtypeid = attrs.getString(SUMO_ATTR_TYPE);
171  const MSRoute* route = MSRoute::dictionary(p->routeid);
172  const MSVehicleType* type = vc.getVType(p->vtypeid);
173  assert(route != 0);
174  assert(type != 0);
175  assert(vc.getVehicle(p->id) == 0);
176 
177  SUMOVehicle* v = vc.buildVehicle(p, route, type);
178  v->loadState(attrs, myOffset);
179  if (!vc.addVehicle(p->id, v)) {
180  throw ProcessError("Error: Could not build vehicle " + p->id + "!");
181  }
182  break;
183  }
184 #ifdef HAVE_INTERNAL
185  case SUMO_TAG_SEGMENT: {
186  if (mySegment == 0) {
187  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(0));
188  } else if (mySegment->getNextSegment() == 0) {
189  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(mySegment->getEdge().getNumericalID() + 1));
190  } else {
191  mySegment = mySegment->getNextSegment();
192  }
193  myQueIndex = 0;
194  break;
195  }
196 #endif
197  case SUMO_TAG_LANE: {
198  myEdgeAndLane.second++;
199  if (myEdgeAndLane.second == (int)MSEdge::dictionary(myEdgeAndLane.first)->getLanes().size()) {
200  myEdgeAndLane.first++;
201  myEdgeAndLane.second = 0;
202  }
203  break;
204  }
206  std::vector<std::string> vehIDs;
209 #ifdef HAVE_INTERNAL
210  mySegment->loadState(vehIDs, MSNet::getInstance()->getVehicleControl(), attrs.getInt(SUMO_ATTR_TIME) - myOffset, myQueIndex++);
211 #endif
212  } else {
213  MSEdge::dictionary(myEdgeAndLane.first)->getLanes()[myEdgeAndLane.second]->loadState(
214  vehIDs, MSNet::getInstance()->getVehicleControl());
215  }
216  break;
217  }
218  default:
219  break;
220  }
221 }
222 
223 
224 void
226  switch (element) {
227  case SUMO_TAG_VTYPE:
228  break;
229  default:
230  break;
231  }
232 }
233 
234 
235 /****************************************************************************/
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:254
unsigned int myQueIndex
std::string vtypeid
The vehicle&#39;s type id.
Structure representing possible vehicle parameter.
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:150
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&#39;t already in the dictionary...
Definition: MSEdge.cpp:461
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.
static void releaseRoute(const MSRoute *route)
release the route (to be used as function pointer with RandomDistributor)
Definition: MSRoute.h:192
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:224
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
static unsigned int getMaxRouteDistSize()
Definition: MSRoute.h:200
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:186
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:524
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: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:248
SUMOTime depart
The vehicle&#39;s departure time.
void saveState(OutputDevice &out)
Saves the current state into the given stream.
const SUMOTime myOffset
#define VERSION_STRING
Definition: config.h:233
bool addVType(MSVehicleType *vehType)
Adds a vehicle type.
Structure representing possible vehicle parameter.
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:495
virtual int getInt(int id) const =0
Returns the int-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:221
static const bool gUseMesoSim
Definition: MSGlobals.h:95
The class responsible for building and deletion of vehicles.
void add(SUMOReal prob, T val, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
static SUMOVTypeParameter * beginVTypeParsing(const SUMOSAXAttributes &attrs, const std::string &file)
Starts to parse a vehicle type.
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