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 /****************************************************************************/
9 // Parser and output filter for routes and vehicles state saving and loading
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
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 #include <sstream>
37 #include <microsim/MSEdge.h>
38 #include <microsim/MSLane.h>
39 #include <microsim/MSGlobals.h>
40 #include <microsim/MSNet.h>
42 #include <microsim/MSRoute.h>
43 #include "MSStateHandler.h"
44 
45 #ifdef HAVE_INTERNAL
46 #include <mesosim/MESegment.h>
47 #include <mesosim/MELoop.h>
48 #endif
49 
50 
51 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
59 MSStateHandler::MSStateHandler(const std::string& file, const SUMOTime offset) :
60  SUMOSAXHandler(file), myOffset(offset),
61 #ifdef HAVE_INTERNAL
62  mySegment(0),
63 #endif
64  myEdgeAndLane(0, -1),
65  myCurrentVType(0) {
66 }
67 
68 
70 }
71 
72 
73 void
74 MSStateHandler::saveState(const std::string& file, SUMOTime step) {
81 #ifdef HAVE_INTERNAL
82  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
83  for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(i)); s != 0; s = s->getNextSegment()) {
84  s->saveState(out);
85  }
86  }
87 #endif
88  } else {
89  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
90  const std::vector<MSLane*>& lanes = MSEdge::dictionary(i)->getLanes();
91  for (std::vector<MSLane*>::const_iterator it = lanes.begin(); it != lanes.end(); ++it) {
92  (*it)->saveState(out);
93  }
94  }
95  }
96 }
97 
98 
99 void
102  switch (element) {
103  case SUMO_TAG_SNAPSHOT: {
104  myTime = attrs.getInt(SUMO_ATTR_TIME);
105  const std::string& version = attrs.getString(SUMO_ATTR_VERSION);
106  if (version != VERSION_STRING) {
107  WRITE_WARNING("State was written with sumo version " + version + " (present: " + VERSION_STRING + ")!");
108  }
109  break;
110  }
111  case SUMO_TAG_DELAY: {
114  break;
115  }
116  case SUMO_TAG_ROUTE: {
117  const std::string id = attrs.getString(SUMO_ATTR_ID);
118  if (MSRoute::dictionary(id) == 0) {
119  MSEdgeVector edges;
121  MSRoute* r = new MSRoute(id, edges, attrs.getBool(SUMO_ATTR_STATE),
122  0, std::vector<SUMOVehicleParameter::Stop>());
123  MSRoute::dictionary(id, r);
124  }
125  break;
126  }
128  const std::string id = attrs.getString(SUMO_ATTR_ID);
129  if (MSRoute::dictionary(id) == 0) {
131  std::vector<std::string> routeIDs;
132  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
134  for (std::vector<std::string>::const_iterator it = routeIDs.begin(); it != routeIDs.end(); ++it) {
135  SUMOReal prob;
136  iss >> prob;
137  const MSRoute* r = MSRoute::dictionary(*it);
138  assert(r != 0);
139  dist->add(prob, r, false);
140  r->addReference();
141  }
142  MSRoute::dictionary(id, dist, attrs.getBool(SUMO_ATTR_STATE));
143  }
144  break;
145  }
146  case SUMO_TAG_VTYPE: {
148  break;
149  }
151  const std::string id = attrs.getString(SUMO_ATTR_ID);
152  if (vc.getVType(id) == 0) {
154  std::vector<std::string> typeIDs;
155  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
157  for (std::vector<std::string>::const_iterator it = typeIDs.begin(); it != typeIDs.end(); ++it) {
158  SUMOReal prob;
159  iss >> prob;
160  MSVehicleType* t = vc.getVType(*it);
161  assert(t != 0);
162  dist->add(prob, t, false);
163  }
164  vc.addVTypeDistribution(id, dist);
165  }
166  break;
167  }
168  case SUMO_TAG_VEHICLE: {
170  p->id = attrs.getString(SUMO_ATTR_ID);
171  p->depart = attrs.getInt(SUMO_ATTR_DEPART) - myOffset;
172  p->routeid = attrs.getString(SUMO_ATTR_ROUTE);
173  p->vtypeid = attrs.getString(SUMO_ATTR_TYPE);
174  const MSRoute* route = MSRoute::dictionary(p->routeid);
175  const MSVehicleType* type = vc.getVType(p->vtypeid);
176  assert(route != 0);
177  assert(type != 0);
178  assert(vc.getVehicle(p->id) == 0);
179 
180  SUMOVehicle* v = vc.buildVehicle(p, route, type);
181  v->loadState(attrs, myOffset);
182  if (!vc.addVehicle(p->id, v)) {
183  throw ProcessError("Error: Could not build vehicle " + p->id + "!");
184  }
185  if (!v->hasDeparted()) {
186  // !!! the save did not keep the order in which the vehicles are checked for insertion
188  }
189  break;
190  }
191 #ifdef HAVE_INTERNAL
192  case SUMO_TAG_SEGMENT: {
193  if (mySegment == 0) {
194  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(0));
195  } else if (mySegment->getNextSegment() == 0) {
196  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(mySegment->getEdge().getNumericalID() + 1));
197  } else {
198  mySegment = mySegment->getNextSegment();
199  }
200  myQueIndex = 0;
201  break;
202  }
203 #endif
204  case SUMO_TAG_LANE: {
205  myEdgeAndLane.second++;
206  if (myEdgeAndLane.second == (int)MSEdge::dictionary(myEdgeAndLane.first)->getLanes().size()) {
207  myEdgeAndLane.first++;
208  myEdgeAndLane.second = 0;
209  }
210  break;
211  }
213  std::vector<std::string> vehIDs;
216 #ifdef HAVE_INTERNAL
217  mySegment->loadState(vehIDs, MSNet::getInstance()->getVehicleControl(), attrs.getInt(SUMO_ATTR_TIME) - myOffset, myQueIndex++);
218 #endif
219  } else {
220  MSEdge::dictionary(myEdgeAndLane.first)->getLanes()[myEdgeAndLane.second]->loadState(
221  vehIDs, MSNet::getInstance()->getVehicleControl());
222  }
223  break;
224  }
225  default:
226  // parse embedded vtype information
227  if (myCurrentVType != 0) {
229  }
230  break;
231  }
232 }
233 
234 
235 void
237  switch (element) {
238  case SUMO_TAG_VTYPE:
240  delete myCurrentVType;
241  myCurrentVType = 0;
242  break;
243  default:
244  break;
245  }
246 }
247 
248 
249 /****************************************************************************/