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) 2012-2017 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>
44 #include <microsim/MSEdge.h>
45 #include <microsim/MSLane.h>
46 #include <microsim/MSGlobals.h>
47 #include <microsim/MSNet.h>
49 #include <microsim/MSRoute.h>
50 #include "MSStateHandler.h"
51 
52 #include <mesosim/MESegment.h>
53 #include <mesosim/MELoop.h>
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  MSRouteHandler(file, true),
65  myOffset(offset),
66  mySegment(0),
67  myEdgeAndLane(0, -1),
68  myAttrs(0),
69  myLastParameterised(0) {
70  myAmLoadingState = true;
71  const std::vector<std::string> vehIDs = OptionsCont::getOptions().getStringVector("load-state.remove-vehicles");
72  myVehiclesToRemove.insert(vehIDs.begin(), vehIDs.end());
73 }
74 
75 
77 }
78 
79 
80 void
81 MSStateHandler::saveState(const std::string& file, SUMOTime step) {
89  for (int i = 0; i < MSEdge::dictSize(); i++) {
90  for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::getAllEdges()[i]); s != 0; s = s->getNextSegment()) {
91  s->saveState(out);
92  }
93  }
94  } else {
95  for (int i = 0; i < MSEdge::dictSize(); i++) {
96  const std::vector<MSLane*>& lanes = MSEdge::getAllEdges()[i]->getLanes();
97  for (std::vector<MSLane*>::const_iterator it = lanes.begin(); it != lanes.end(); ++it) {
98  (*it)->saveState(out);
99  }
100  }
101  }
102  out.close();
103 }
104 
105 
106 void
108  MSRouteHandler::myStartElement(element, attrs);
110  switch (element) {
111  case SUMO_TAG_SNAPSHOT: {
113  const std::string& version = attrs.getString(SUMO_ATTR_VERSION);
114  if (version != VERSION_STRING) {
115  WRITE_WARNING("State was written with sumo version " + version + " (present: " + VERSION_STRING + ")!");
116  }
117  break;
118  }
119  case SUMO_TAG_DELAY: {
120  vc.setState(attrs.getInt(SUMO_ATTR_NUMBER),
121  attrs.getInt(SUMO_ATTR_BEGIN),
122  attrs.getInt(SUMO_ATTR_END),
123  attrs.getFloat(SUMO_ATTR_DEPART),
124  attrs.getFloat(SUMO_ATTR_TIME));
125  break;
126  }
127  case SUMO_TAG_VTYPE: {
129  break;
130  }
131  case SUMO_TAG_VEHICLE: {
133  myAttrs = attrs.clone();
134  break;
135  }
138  break;
139  }
140  case SUMO_TAG_SEGMENT: {
141  if (mySegment == 0) {
143  } else if (mySegment->getNextSegment() == 0) {
145  } else {
147  }
148  myQueIndex = 0;
149  break;
150  }
151  case SUMO_TAG_LANE: {
152  myEdgeAndLane.second++;
153  if (myEdgeAndLane.second == (int)MSEdge::getAllEdges()[myEdgeAndLane.first]->getLanes().size()) {
154  myEdgeAndLane.first++;
155  myEdgeAndLane.second = 0;
156  }
157  break;
158  }
160  std::vector<std::string> vehIDs;
163  mySegment->loadState(vehIDs, MSNet::getInstance()->getVehicleControl(), TplConvert::_2long(attrs.getString(SUMO_ATTR_TIME).c_str()) - myOffset, myQueIndex++);
164  } else {
165  MSEdge::getAllEdges()[myEdgeAndLane.first]->getLanes()[myEdgeAndLane.second]->loadState(
166  vehIDs, MSNet::getInstance()->getVehicleControl());
167  }
168  break;
169  }
170  case SUMO_TAG_PARAM: {
171  bool ok;
172  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, 0, ok);
173  // circumventing empty string test
174  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
175  assert(myLastParameterised != 0);
176  if (myLastParameterised != 0) {
178  }
179  break;
180  }
181  default:
182  break;
183  }
184 }
185 
186 
187 void
190  if (element != SUMO_TAG_PARAM && myVehicleParameter == 0 && myCurrentVType == 0) {
192  }
193 }
194 
195 
196 void
198  assert(myVehicleParameter != 0);
199  // the vehicle was already counted in MSVehicleControl::setState
201  // make a copy because myVehicleParameter is reset in closeVehicle()
202  const std::string vehID = myVehicleParameter->id;
203  if (myVehiclesToRemove.count(vehID) == 0) {
205  // reset depart
206  vc.discountStateLoaded();
207  SUMOVehicle* v = vc.getVehicle(vehID);
208  v->loadState(*myAttrs, myOffset);
209  if (v->hasDeparted()) {
210  // vehicle already departed: disable pre-insertion rerouting and enable regular routing behavior
211  MSDevice_Routing* routingDevice = static_cast<MSDevice_Routing*>(v->getDevice(typeid(MSDevice_Routing)));
212  if (routingDevice != 0) {
214  }
215  }
216  } else {
217  vc.discountStateLoaded(true);
218  delete myVehicleParameter;
219  myVehicleParameter = 0;
220  }
221  delete myAttrs;
222 }
223 
224 
225 
226 /****************************************************************************/
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
virtual int getInt(int id) const =0
Returns the int-value of the named (by its enum-value) attribute.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
void discountStateLoaded(bool removed=false)
avoid counting a vehicle twice if it was loaded from state and route input
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
void close()
Closes the device and removes it from the dictionary.
segment of a lane
long long int SUMOTime
Definition: SUMOTime.h:43
description of a vehicle type
Parameterised * myLastParameterised
the last object that potentially carries parameters
virtual void closeVehicle()
Ends the processing of a vehicle.
begin/end of the description of a single lane
static int dictSize()
Returns the number of edges.
Definition: MSEdge.cpp:720
SUMOVehicleParameter * myVehicleParameter
Parameter of the current vehicle, trip, person, container or flow.
void saveState(OutputDevice &out) const
Saves the current state into the given stream.
void closeVehicle()
Ends the processing of a vehicle.
SUMOVTypeParameter * myCurrentVType
The currently parsed vehicle type.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:60
A device that performs vehicle rerouting based on current edge speeds.
weights: time range begin
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
const MSEdge & getEdge() const
Returns the edge this segment belongs to.
Definition: MESegment.h:271
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
static void dict_saveState(OutputDevice &out)
Saves all known routes into the given stream.
Definition: MSRoute.cpp:265
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
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:656
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:188
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.
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:157
MSStateHandler(const std::string &file, const SUMOTime offset)
standard constructor
Representation of a vehicle.
Definition: SUMOVehicle.h:67
Encapsulated SAX-Attributes.
parameter associated to a certain key
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:308
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:47
void saveState(OutputDevice &out)
Saves the current state into the given stream.
const SUMOTime myOffset
SUMOSAXAttributes * myAttrs
cached attrs (used when loading vehicles)
void setState(int runningVehNo, int loadedVehNo, int endedVehNo, SUMOReal totalDepartureDelay, SUMOReal totalTravelTime)
Sets the current state variables as loaded from the stream.
#define VERSION_STRING
Definition: version.h:1
void loadState(const SUMOSAXAttributes &attrs, const SUMOTime offset, MSVehicleControl &vc)
Loads one transfer vehicle state from the given descriptionn.
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
static MSVehicleTransfer * getInstance()
Returns the instance of this object.
void addParameter(const std::string &key, const std::string &value)
Adds a parameter.
virtual void myEndElement(int element)
Called when a closing tag occurs.
trigger: the time of the step
The vehicle has departed (was inserted into the network)
int getNumericalID() const
Returns the numerical id of the edge.
Definition: MSEdge.h:275
bool myAmLoadingState
whether a state file is being loaded
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:726
virtual ~MSStateHandler()
standard destructor
weights: time range end
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:293
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition: MSGlobals.h:110
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
std::set< std::string > myVehiclesToRemove
vehicles that shall be removed when loading state
description of a vehicle
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
virtual SUMOReal getFloat(int id) const =0
Returns the SUMOReal-value of the named (by its enum-value) attribute.
virtual SUMOSAXAttributes * clone() const =0
return a new deep-copy attributes object
The class responsible for building and deletion of vehicles.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
static bool gUseMesoSim
Definition: MSGlobals.h:95
virtual void loadState(const SUMOSAXAttributes &attrs, const SUMOTime offset)=0
Loads the state of this vehicle from the given description.
Parser and container for routes during their loading.
static void saveState(const std::string &file, SUMOTime step)
Saves the current state.
std::string id
The vehicle&#39;s id.
virtual MSDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or 0.