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>
50 #include <microsim/MSRoute.h>
52 #include "MSStateHandler.h"
53 
54 #include <mesosim/MESegment.h>
55 #include <mesosim/MELoop.h>
56 
57 
58 // ===========================================================================
59 // method definitions
60 // ===========================================================================
61 MSStateHandler::MSStateHandler(const std::string& file, const SUMOTime offset) :
62  MSRouteHandler(file, true),
63  myOffset(offset),
64  mySegment(0),
65  myEdgeAndLane(0, -1),
66  myAttrs(0),
67  myLastParameterised(0) {
68  myAmLoadingState = true;
69  const std::vector<std::string> vehIDs = OptionsCont::getOptions().getStringVector("load-state.remove-vehicles");
70  myVehiclesToRemove.insert(vehIDs.begin(), vehIDs.end());
71 }
72 
73 
75 }
76 
77 
78 void
79 MSStateHandler::saveState(const std::string& file, SUMOTime step) {
82  out.writeAttr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance").writeAttr("xsi:noNamespaceSchemaLocation", "http://sumo.dlr.de/xsd/state_file.xsd");
88  for (int i = 0; i < MSEdge::dictSize(); i++) {
89  for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::getAllEdges()[i]); s != 0; s = s->getNextSegment()) {
90  s->saveState(out);
91  }
92  }
93  } else {
94  for (int i = 0; i < MSEdge::dictSize(); i++) {
95  const std::vector<MSLane*>& lanes = MSEdge::getAllEdges()[i]->getLanes();
96  for (std::vector<MSLane*>::const_iterator it = lanes.begin(); it != lanes.end(); ++it) {
97  (*it)->saveState(out);
98  }
99  }
100  }
101  out.close();
102 }
103 
104 
105 void
107  MSRouteHandler::myStartElement(element, attrs);
109  switch (element) {
110  case SUMO_TAG_SNAPSHOT: {
112  const std::string& version = attrs.getString(SUMO_ATTR_VERSION);
113  if (version != VERSION_STRING) {
114  WRITE_WARNING("State was written with sumo version " + version + " (present: " + VERSION_STRING + ")!");
115  }
116  break;
117  }
118  case SUMO_TAG_DELAY: {
119  vc.setState(attrs.getInt(SUMO_ATTR_NUMBER),
120  attrs.getInt(SUMO_ATTR_BEGIN),
121  attrs.getInt(SUMO_ATTR_END),
122  attrs.getFloat(SUMO_ATTR_DEPART),
123  attrs.getFloat(SUMO_ATTR_TIME));
124  break;
125  }
126  case SUMO_TAG_VTYPE: {
128  break;
129  }
130  case SUMO_TAG_VEHICLE: {
132  myAttrs = attrs.clone();
133  break;
134  }
135  case SUMO_TAG_DEVICE: {
136  myDeviceAttrs.push_back(attrs.clone());
137  break;
138  }
141  break;
142  }
143  case SUMO_TAG_SEGMENT: {
144  if (mySegment == 0) {
146  } else if (mySegment->getNextSegment() == 0) {
148  } else {
150  }
151  myQueIndex = 0;
152  break;
153  }
154  case SUMO_TAG_LANE: {
155  myEdgeAndLane.second++;
156  if (myEdgeAndLane.second == (int)MSEdge::getAllEdges()[myEdgeAndLane.first]->getLanes().size()) {
157  myEdgeAndLane.first++;
158  myEdgeAndLane.second = 0;
159  }
160  break;
161  }
163  std::vector<std::string> vehIDs;
166  mySegment->loadState(vehIDs, MSNet::getInstance()->getVehicleControl(), TplConvert::_2long(attrs.getString(SUMO_ATTR_TIME).c_str()) - myOffset, myQueIndex++);
167  } else {
168  MSEdge::getAllEdges()[myEdgeAndLane.first]->getLanes()[myEdgeAndLane.second]->loadState(
169  vehIDs, MSNet::getInstance()->getVehicleControl());
170  }
171  break;
172  }
173  case SUMO_TAG_PARAM: {
174  bool ok;
175  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, 0, ok);
176  // circumventing empty string test
177  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
178  assert(myLastParameterised != 0);
179  if (myLastParameterised != 0) {
181  }
182  break;
183  }
184  default:
185  break;
186  }
187 }
188 
189 
190 void
193  if (element != SUMO_TAG_PARAM && myVehicleParameter == 0 && myCurrentVType == 0) {
195  }
196 }
197 
198 
199 void
201  assert(myVehicleParameter != 0);
203  // the vehicle was already counted in MSVehicleControl::setState
205  // make a copy because myVehicleParameter is reset in closeVehicle()
206  const std::string vehID = myVehicleParameter->id;
207  if (myVehiclesToRemove.count(vehID) == 0) {
209  // reset depart
210  vc.discountStateLoaded();
211  SUMOVehicle* v = vc.getVehicle(vehID);
212  v->loadState(*myAttrs, myOffset);
213  if (v->hasDeparted()) {
214  // vehicle already departed: disable pre-insertion rerouting and enable regular routing behavior
215  MSDevice_Routing* routingDevice = static_cast<MSDevice_Routing*>(v->getDevice(typeid(MSDevice_Routing)));
216  if (routingDevice != 0) {
218  }
220  }
221  while (!myDeviceAttrs.empty()) {
222  const std::string attrID = myDeviceAttrs.back()->getString(SUMO_ATTR_ID);
223  for (std::vector<MSDevice*>::const_iterator dev = v->getDevices().begin(); dev != v->getDevices().end(); ++dev) {
224  if ((*dev)->getID() == attrID) {
225  (*dev)->loadState(*myDeviceAttrs.back());
226  }
227  }
228  delete myDeviceAttrs.back();
229  myDeviceAttrs.pop_back();
230  }
231  } else {
232  vc.discountStateLoaded(true);
233  delete myVehicleParameter;
234  myVehicleParameter = 0;
235  }
236  delete myAttrs;
237 }
238 
239 
240 
241 /****************************************************************************/
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:261
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
description of a vehicle type
Parameterised * myLastParameterised
the last object that potentially carries parameters
MESegment * getSegmentForEdge(const MSEdge &e, double pos=0)
Get the segment for a given edge at a given position.
Definition: MELoop.cpp:290
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:772
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:169
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:211
virtual double getFloat(int id) const =0
Returns the double-value of the named (by its enum-value) attribute.
const MSEdge & getEdge() const
Returns the edge this segment belongs to.
Definition: MESegment.h:271
void setState(int runningVehNo, int loadedVehNo, int endedVehNo, double totalDepartureDelay, double totalTravelTime)
Sets the current state variables as loaded from the stream.
#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:261
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
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:652
bool writeHeader(const SumoXMLTag &rootElement)
Definition: OutputDevice.h:193
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.
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:305
SUMOTime depart
The vehicle&#39;s departure time.
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)
#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...
void alreadyDeparted(SUMOVehicle *veh)
stops trying to emit the given vehicle (because it already departed)
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:260
bool myAmLoadingState
whether a state file is being loaded
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:358
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:778
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.
std::vector< SUMOSAXAttributes * > myDeviceAttrs
cached device attrs (used when loading vehicles)
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition: MSGlobals.h:113
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Computes a new route on vehicle insertion.
std::set< std::string > myVehiclesToRemove
vehicles that shall be removed when loading state
virtual const std::vector< MSDevice * > & getDevices() const =0
Returns this vehicle&#39;s devices.
description of a vehicle
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
long long int SUMOTime
Definition: TraCIDefs.h:52
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:98
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.