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 /****************************************************************************/
8 // Parser and output filter for routes and vehicles state saving and loading
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
11 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <sstream>
36 #include <microsim/MSEdge.h>
37 #include <microsim/MSLane.h>
38 #include <microsim/MSGlobals.h>
39 #include <microsim/MSNet.h>
40 #include <microsim/MSRoute.h>
41 #include "MSStateHandler.h"
42 
43 #ifdef HAVE_INTERNAL
44 #include <mesosim/MESegment.h>
45 #include <mesosim/MELoop.h>
46 #endif
47 
48 
49 #ifdef CHECK_MEMORY_LEAKS
50 #include <foreign/nvwa/debug_new.h>
51 #endif // CHECK_MEMORY_LEAKS
52 
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
57 MSStateHandler::MSStateHandler(const std::string& file, const SUMOTime offset) :
58  SUMOSAXHandler(file), myOffset(offset),
59 #ifdef HAVE_INTERNAL
60  mySegment(0),
61 #endif
62  myEdgeAndLane(0, -1) {
63 }
64 
65 
67 }
68 
69 
70 void
71 MSStateHandler::saveState(const std::string& file, SUMOTime step) {
78 #ifdef HAVE_INTERNAL
79  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
80  for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(i)); s != 0; s = s->getNextSegment()) {
81  s->saveState(out);
82  }
83  }
84 #endif
85  } else {
86  for (size_t i = 0; i < MSEdge::dictSize(); i++) {
87  const std::vector<MSLane*>& lanes = MSEdge::dictionary(i)->getLanes();
88  for (std::vector<MSLane*>::const_iterator it = lanes.begin(); it != lanes.end(); ++it) {
89  (*it)->saveState(out);
90  }
91  }
92  }
93 }
94 
95 
96 void
99  switch (element) {
100  case SUMO_TAG_SNAPSHOT: {
101  myTime = attrs.getInt(SUMO_ATTR_TIME);
102  const std::string& version = attrs.getString(SUMO_ATTR_VERSION);
103  if (version != VERSION_STRING) {
104  WRITE_WARNING("State was written with sumo version " + version + " (present: " + VERSION_STRING + ")!");
105  }
106  break;
107  }
108  case SUMO_TAG_DELAY: {
111  break;
112  }
113  case SUMO_TAG_ROUTE: {
114  const std::string id = attrs.getString(SUMO_ATTR_ID);
115  if (MSRoute::dictionary(id) == 0) {
116  MSEdgeVector edges;
118  MSRoute* r = new MSRoute(id, edges, attrs.getInt(SUMO_ATTR_STATE),
119  0, std::vector<SUMOVehicleParameter::Stop>());
120  MSRoute::dictionary(id, r);
121  }
122  break;
123  }
125  const std::string id = attrs.getString(SUMO_ATTR_ID);
126  if (MSRoute::dictionary(id) == 0) {
128  std::vector<std::string> routeIDs;
129  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
131  for (std::vector<std::string>::const_iterator it = routeIDs.begin(); it != routeIDs.end(); ++it) {
132  SUMOReal prob;
133  iss >> prob;
134  const MSRoute* r = MSRoute::dictionary(*it);
135  assert(r != 0);
136  dist->add(prob, r, false);
137  }
138  MSRoute::dictionary(id, dist);
139  }
140  break;
141  }
142  case SUMO_TAG_VTYPE: {
144  vc.addVType(MSVehicleType::build(*def));
145  delete def;
146  }
148  const std::string id = attrs.getString(SUMO_ATTR_ID);
149  if (vc.getVType(id) == 0) {
151  std::vector<std::string> typeIDs;
152  std::istringstream iss(attrs.getString(SUMO_ATTR_PROBS));
154  for (std::vector<std::string>::const_iterator it = typeIDs.begin(); it != typeIDs.end(); ++it) {
155  SUMOReal prob;
156  iss >> prob;
157  MSVehicleType* t = vc.getVType(*it);
158  assert(t != 0);
159  dist->add(prob, t, false);
160  }
161  vc.addVTypeDistribution(id, dist);
162  }
163  break;
164  }
165  case SUMO_TAG_VEHICLE: {
167  p->id = attrs.getString(SUMO_ATTR_ID);
168  p->depart = attrs.getInt(SUMO_ATTR_DEPART) - myOffset;
169  p->routeid = attrs.getString(SUMO_ATTR_ROUTE);
170  p->vtypeid = attrs.getString(SUMO_ATTR_TYPE);
171  const MSRoute* route = MSRoute::dictionary(p->routeid);
172  const MSVehicleType* type = vc.getVType(p->vtypeid);
173  assert(route != 0);
174  assert(type != 0);
175  assert(vc.getVehicle(p->id) == 0);
176 
177  SUMOVehicle* v = vc.buildVehicle(p, route, type);
178  v->loadState(attrs, myOffset);
179  if (!vc.addVehicle(p->id, v)) {
180  throw ProcessError("Error: Could not build vehicle " + p->id + "!");
181  }
182  break;
183  }
184 #ifdef HAVE_INTERNAL
185  case SUMO_TAG_SEGMENT: {
186  if (mySegment == 0) {
187  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(0));
188  } else if (mySegment->getNextSegment() == 0) {
189  mySegment = MSGlobals::gMesoNet->getSegmentForEdge(*MSEdge::dictionary(mySegment->getEdge().getNumericalID() + 1));
190  } else {
191  mySegment = mySegment->getNextSegment();
192  }
193  myQueIndex = 0;
194  break;
195  }
196 #endif
197  case SUMO_TAG_LANE: {
198  myEdgeAndLane.second++;
199  if (myEdgeAndLane.second == (int)MSEdge::dictionary(myEdgeAndLane.first)->getLanes().size()) {
200  myEdgeAndLane.first++;
201  myEdgeAndLane.second = 0;
202  }
203  break;
204  }
206  std::vector<std::string> vehIDs;
209 #ifdef HAVE_INTERNAL
210  mySegment->loadState(vehIDs, MSNet::getInstance()->getVehicleControl(), attrs.getInt(SUMO_ATTR_TIME) - myOffset, myQueIndex++);
211 #endif
212  } else {
213  MSEdge::dictionary(myEdgeAndLane.first)->getLanes()[myEdgeAndLane.second]->loadState(
214  vehIDs, MSNet::getInstance()->getVehicleControl());
215  }
216  break;
217  }
218  default:
219  break;
220  }
221 }
222 
223 
224 void
226  switch (element) {
227  case SUMO_TAG_VTYPE:
228  break;
229  default:
230  break;
231  }
232 }
233 
234 
235 /****************************************************************************/