SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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>
43 #include <microsim/MSEdge.h>
44 #include <microsim/MSLane.h>
45 #include <microsim/MSGlobals.h>
46 #include <microsim/MSNet.h>
48 #include <microsim/MSRoute.h>
49 #include "MSStateHandler.h"
50 
51 #include <mesosim/MESegment.h>
52 #include <mesosim/MELoop.h>
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  mySegment(0),
65  myEdgeAndLane(0, -1),
66  myCurrentVType(0) {
67 }
68 
69 
71  delete myCurrentVType;
72 }
73 
74 
75 void
76 MSStateHandler::saveState(const std::string& file, SUMOTime step) {
83  for (int i = 0; i < MSEdge::dictSize(); i++) {
84  for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::getAllEdges()[i]); s != 0; s = s->getNextSegment()) {
85  s->saveState(out);
86  }
87  }
88  } else {
89  for (int i = 0; i < MSEdge::dictSize(); i++) {
90  const std::vector<MSLane*>& lanes = MSEdge::getAllEdges()[i]->getLanes();
91  for (std::vector<MSLane*>::const_iterator it = lanes.begin(); it != lanes.end(); ++it) {
92  (*it)->saveState(out);
93  }
94  }
95  }
96  out.close();
97 }
98 
99 
100 void
103  switch (element) {
104  case SUMO_TAG_SNAPSHOT: {
106  const std::string& version = attrs.getString(SUMO_ATTR_VERSION);
107  if (version != VERSION_STRING) {
108  WRITE_WARNING("State was written with sumo version " + version + " (present: " + VERSION_STRING + ")!");
109  }
110  break;
111  }
112  case SUMO_TAG_DELAY: {
113  vc.setState(attrs.getInt(SUMO_ATTR_NUMBER),
114  attrs.getInt(SUMO_ATTR_BEGIN),
115  attrs.getInt(SUMO_ATTR_END),
116  attrs.getFloat(SUMO_ATTR_DEPART),
117  attrs.getFloat(SUMO_ATTR_TIME));
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  vc.discountStateLoaded(); // already included (see SUMO_TAG_DELAY)
186  v->loadState(attrs, myOffset);
187  if (!vc.addVehicle(p->id, v)) {
188  throw ProcessError("Error: Could not build vehicle " + p->id + "!");
189  }
190  if (!v->hasDeparted()) {
191  // !!! the save did not keep the order in which the vehicles are checked for insertion
193  } else {
194  // vehicle already departed: disable pre-insertion rerouting and enable regular routing behavior
195  MSDevice_Routing* routingDevice = static_cast<MSDevice_Routing*>(v->getDevice(typeid(MSDevice_Routing)));
196  if (routingDevice != 0) {
198  }
199  }
200  break;
201  }
202  case SUMO_TAG_SEGMENT: {
203  if (mySegment == 0) {
205  } else if (mySegment->getNextSegment() == 0) {
207  } else {
209  }
210  myQueIndex = 0;
211  break;
212  }
213  case SUMO_TAG_LANE: {
214  myEdgeAndLane.second++;
215  if (myEdgeAndLane.second == (int)MSEdge::getAllEdges()[myEdgeAndLane.first]->getLanes().size()) {
216  myEdgeAndLane.first++;
217  myEdgeAndLane.second = 0;
218  }
219  break;
220  }
222  std::vector<std::string> vehIDs;
225  mySegment->loadState(vehIDs, MSNet::getInstance()->getVehicleControl(), TplConvert::_2long(attrs.getString(SUMO_ATTR_TIME).c_str()) - myOffset, myQueIndex++);
226  } else {
227  MSEdge::getAllEdges()[myEdgeAndLane.first]->getLanes()[myEdgeAndLane.second]->loadState(
228  vehIDs, MSNet::getInstance()->getVehicleControl());
229  }
230  break;
231  }
232  default:
233  // parse embedded vtype information
234  if (myCurrentVType != 0) {
236  }
237  break;
238  }
239 }
240 
241 
242 void
244  switch (element) {
245  case SUMO_TAG_VTYPE:
247  delete myCurrentVType;
248  myCurrentVType = 0;
249  break;
250  default:
251  break;
252  }
253 }
254 
255 
256 /****************************************************************************/
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
void close()
Closes the device and removes it from the dictionary.
long long int SUMOTime
Definition: SUMOTime.h:43
std::string vtypeid
The vehicle's type id.
static int dictSize()
Returns the number of edges.
Definition: MSEdge.cpp:700
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:59
A device that performs vehicle rerouting based on current edge speeds.
MESegment * mySegment
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
std::pair< int, int > myEdgeAndLane
static long long int _2long(const E *const data)
converts a char-type array into the long value described by it
Definition: TplConvert.h:200
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:78
SAX-handler base for SUMO-files.
SUMOVTypeParameter * myCurrentVType
The currently parsed vehicle type.
const MSEdge & getEdge() const
Returns the edge this segment belongs to.
Definition: MESegment.h:272
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:265
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
void loadState(std::vector< std::string > &vehIDs, MSVehicleControl &vc, const SUMOTime blockTime, const int queIdx)
Loads the state of this segment with the given parameters.
Definition: MESegment.cpp:642
The state of a link.
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:189
A road/street connecting two junctions.
Definition: MSEdge.h:80
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
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:158
MSStateHandler(const std::string &file, const SUMOTime offset)
standard constructor
the edges of a route
std::string routeid
The vehicle's route id.
Representation of a vehicle.
Definition: SUMOVehicle.h:66
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:307
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.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
void saveState(OutputDevice &out)
Saves the current state into the given stream.
const SUMOTime myOffset
void setState(int runningVehNo, int loadedVehNo, int endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime)
Sets the current state variables as loaded from the stream.
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:103
#define VERSION_STRING
Definition: config.h:226
bool addVType(MSVehicleType *vehType)
Adds a vehicle type.
The vehicle has departed (was inserted into the network)
Structure representing possible vehicle parameter.
int getNumericalID() const
Returns the numerical id of the edge.
Definition: MSEdge.h:275
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:360
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
static const MSEdgeVector & getAllEdges()
Returns all edges with a numerical id.
Definition: MSEdge.cpp:706
virtual ~MSStateHandler()
standard destructor
A single mesoscopic segment (cell)
Definition: MESegment.h:57
void myEndElement(int element)
Called when a closing tag occurs.
MESegment * getSegmentForEdge(const MSEdge &e, SUMOReal pos=0)
Get the segment for a given edge at a given position.
Definition: MELoop.cpp:295
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition: MSGlobals.h:107
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.
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason)
Computes a new route on vehicle insertion.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
#define SUMOReal
Definition: config.h:214
The class responsible for building and deletion of vehicles.
void discountStateLoaded()
avoid counting a vehicle twice if it was loaded from state and route input
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 bool gUseMesoSim
Definition: MSGlobals.h:95
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:729
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:122