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>
51 #include "MSStateHandler.h"
52 
53 #include <mesosim/MESegment.h>
54 #include <mesosim/MELoop.h>
55 
56 
57 // ===========================================================================
58 // method definitions
59 // ===========================================================================
60 MSStateHandler::MSStateHandler(const std::string& file, const SUMOTime offset) :
61  MSRouteHandler(file, true),
62  myOffset(offset),
63  mySegment(0),
64  myEdgeAndLane(0, -1),
65  myAttrs(0),
66  myLastParameterised(0) {
67  myAmLoadingState = true;
68  const std::vector<std::string> vehIDs = OptionsCont::getOptions().getStringVector("load-state.remove-vehicles");
69  myVehiclesToRemove.insert(vehIDs.begin(), vehIDs.end());
70 }
71 
72 
74 }
75 
76 
77 void
78 MSStateHandler::saveState(const std::string& file, SUMOTime step) {
81  out.writeAttr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance").writeAttr("xsi:noNamespaceSchemaLocation", "http://sumo.dlr.de/xsd/state_file.xsd");
87  for (int i = 0; i < MSEdge::dictSize(); i++) {
88  for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::getAllEdges()[i]); s != 0; s = s->getNextSegment()) {
89  s->saveState(out);
90  }
91  }
92  } else {
93  for (int i = 0; i < MSEdge::dictSize(); i++) {
94  const std::vector<MSLane*>& lanes = MSEdge::getAllEdges()[i]->getLanes();
95  for (std::vector<MSLane*>::const_iterator it = lanes.begin(); it != lanes.end(); ++it) {
96  (*it)->saveState(out);
97  }
98  }
99  }
100  out.close();
101 }
102 
103 
104 void
106  MSRouteHandler::myStartElement(element, attrs);
108  switch (element) {
109  case SUMO_TAG_SNAPSHOT: {
111  const std::string& version = attrs.getString(SUMO_ATTR_VERSION);
112  if (version != VERSION_STRING) {
113  WRITE_WARNING("State was written with sumo version " + version + " (present: " + VERSION_STRING + ")!");
114  }
115  break;
116  }
117  case SUMO_TAG_DELAY: {
118  vc.setState(attrs.getInt(SUMO_ATTR_NUMBER),
119  attrs.getInt(SUMO_ATTR_BEGIN),
120  attrs.getInt(SUMO_ATTR_END),
121  attrs.getFloat(SUMO_ATTR_DEPART),
122  attrs.getFloat(SUMO_ATTR_TIME));
123  break;
124  }
125  case SUMO_TAG_VTYPE: {
127  break;
128  }
129  case SUMO_TAG_VEHICLE: {
131  myAttrs = attrs.clone();
132  break;
133  }
134  case SUMO_TAG_DEVICE: {
135  myDeviceAttrs.push_back(attrs.clone());
136  break;
137  }
140  break;
141  }
142  case SUMO_TAG_SEGMENT: {
143  if (mySegment == 0) {
145  } else if (mySegment->getNextSegment() == 0) {
147  } else {
149  }
150  myQueIndex = 0;
151  break;
152  }
153  case SUMO_TAG_LANE: {
154  myEdgeAndLane.second++;
155  if (myEdgeAndLane.second == (int)MSEdge::getAllEdges()[myEdgeAndLane.first]->getLanes().size()) {
156  myEdgeAndLane.first++;
157  myEdgeAndLane.second = 0;
158  }
159  break;
160  }
162  std::vector<std::string> vehIDs;
165  mySegment->loadState(vehIDs, MSNet::getInstance()->getVehicleControl(), TplConvert::_2long(attrs.getString(SUMO_ATTR_TIME).c_str()) - myOffset, myQueIndex++);
166  } else {
167  MSEdge::getAllEdges()[myEdgeAndLane.first]->getLanes()[myEdgeAndLane.second]->loadState(
168  vehIDs, MSNet::getInstance()->getVehicleControl());
169  }
170  break;
171  }
172  case SUMO_TAG_PARAM: {
173  bool ok;
174  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, 0, ok);
175  // circumventing empty string test
176  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
177  assert(myLastParameterised != 0);
178  if (myLastParameterised != 0) {
180  }
181  break;
182  }
183  default:
184  break;
185  }
186 }
187 
188 
189 void
192  if (element != SUMO_TAG_PARAM && myVehicleParameter == 0 && myCurrentVType == 0) {
194  }
195 }
196 
197 
198 void
200  assert(myVehicleParameter != 0);
202  // the vehicle was already counted in MSVehicleControl::setState
204  // make a copy because myVehicleParameter is reset in closeVehicle()
205  const std::string vehID = myVehicleParameter->id;
206  if (myVehiclesToRemove.count(vehID) == 0) {
208  // reset depart
209  vc.discountStateLoaded();
210  SUMOVehicle* v = vc.getVehicle(vehID);
211  v->loadState(*myAttrs, myOffset);
212  if (v->hasDeparted()) {
213  // vehicle already departed: disable pre-insertion rerouting and enable regular routing behavior
214  MSDevice_Routing* routingDevice = static_cast<MSDevice_Routing*>(v->getDevice(typeid(MSDevice_Routing)));
215  if (routingDevice != 0) {
217  }
219  }
220  while (!myDeviceAttrs.empty()) {
221  const std::string attrID = myDeviceAttrs.back()->getString(SUMO_ATTR_ID);
222  for (std::vector<MSDevice*>::const_iterator dev = v->getDevices().begin(); dev != v->getDevices().end(); ++dev) {
223  if ((*dev)->getID() == attrID) {
224  (*dev)->loadState(*myDeviceAttrs.back());
225  }
226  }
227  delete myDeviceAttrs.back();
228  myDeviceAttrs.pop_back();
229  }
230  } else {
231  vc.discountStateLoaded(true);
232  delete myVehicleParameter;
233  myVehicleParameter = 0;
234  }
235  delete myAttrs;
236 }
237 
238 
239 
240 /****************************************************************************/
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:157
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
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
void close()
Closes the device and removes it from the dictionary.
segment of a lane
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
description of a vehicle type
Parameterised * myLastParameterised
the last object that potentially carries parameters
virtual SUMOSAXAttributes * clone() const =0
return a new deep-copy attributes object
MESegment * getSegmentForEdge(const MSEdge &e, double pos=0)
Get the segment for a given edge at a given position.
Definition: MELoop.cpp:289
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:755
SUMOVehicleParameter * myVehicleParameter
Parameter of the current vehicle, trip, person, container or flow.
void closeVehicle()
Ends the processing of a vehicle.
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
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:158
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
int getNumericalID() const
Returns the numerical id of the edge.
Definition: MSEdge.h:275
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
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
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
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:652
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.
virtual const std::vector< MSDevice * > & getDevices() const =0
Returns this vehicle&#39;s devices.
void saveState(OutputDevice &out) const
Saves the current state into the given stream.
MSStateHandler(const std::string &file, const SUMOTime offset)
standard constructor
Representation of a vehicle.
Definition: SUMOVehicle.h:67
Encapsulated SAX-Attributes.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
parameter associated to a certain key
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:310
SUMOTime depart
The vehicle&#39;s departure time.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:47
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 saveState(OutputDevice &out)
Saves the current state into the given stream.
const SUMOTime myOffset
SUMOSAXAttributes * myAttrs
cached attrs (used when loading vehicles)
void loadState(const SUMOSAXAttributes &attrs, const SUMOTime offset, MSVehicleControl &vc)
Loads one transfer vehicle state from the given descriptionn.
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
void alreadyDeparted(SUMOVehicle *veh)
stops trying to emit the given vehicle (because it already departed)
#define VERSION_STRING
Definition: config.h:210
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.
virtual double getFloat(int id) const =0
Returns the double-value of the named (by its enum-value) attribute.
trigger: the time of the step
The vehicle has departed (was inserted into the network)
bool myAmLoadingState
whether a state file is being loaded
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:363
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:761
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
virtual int getInt(int id) const =0
Returns the int-value of the named (by its enum-value) attribute.
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
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 MSDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or 0.
The class responsible for building and deletion of vehicles.
static bool gUseMesoSim
Definition: MSGlobals.h:98
const MSEdge & getEdge() const
Returns the edge this segment belongs to.
Definition: MESegment.h:271
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.