SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NLBuilder.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // The main interface for loading a microsim
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 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 "NLBuilder.h"
34 #include <microsim/MSNet.h>
35 #include <microsim/MSEdgeControl.h>
36 #include <microsim/MSGlobals.h>
37 #include <iostream>
38 #include <vector>
39 #include <xercesc/parsers/SAXParser.hpp>
40 #include <xercesc/sax2/SAX2XMLReader.hpp>
41 #include <string>
42 #include <map>
43 #include "NLHandler.h"
44 #include "NLEdgeControlBuilder.h"
46 #include "NLDetectorBuilder.h"
47 #include "NLTriggerBuilder.h"
51 #include <microsim/MSRouteLoader.h>
54 #include <utils/options/Option.h>
58 #include <utils/common/SysUtils.h>
59 #include <utils/common/ToString.h>
60 #include <utils/xml/XMLSubSys.h>
62 #include <microsim/MSFrame.h>
65 
66 #ifdef CHECK_MEMORY_LEAKS
67 #include <foreign/nvwa/debug_new.h>
68 #endif // CHECK_MEMORY_LEAKS
69 
70 
71 // ===========================================================================
72 // method definitions
73 // ===========================================================================
74 // ---------------------------------------------------------------------------
75 // NLBuilder::EdgeFloatTimeLineRetriever_EdgeWeight - methods
76 // ---------------------------------------------------------------------------
77 void
79  SUMOReal value, SUMOReal begTime, SUMOReal endTime) const {
80  MSEdge* edge = MSEdge::dictionary(id);
81  if (edge != 0) {
82  myNet.getWeightsStorage().addEffort(edge, begTime, endTime, value);
83  } else {
84  WRITE_ERROR("Trying to set the effort for the unknown edge '" + id + "'.");
85  }
86 }
87 
88 
89 // ---------------------------------------------------------------------------
90 // NLBuilder::EdgeFloatTimeLineRetriever_EdgeTravelTime - methods
91 // ---------------------------------------------------------------------------
92 void
94  SUMOReal value, SUMOReal begTime, SUMOReal endTime) const {
95  MSEdge* edge = MSEdge::dictionary(id);
96  if (edge != 0) {
97  myNet.getWeightsStorage().addTravelTime(edge, begTime, endTime, value);
98  } else {
99  WRITE_ERROR("Trying to set the travel time for the unknown edge '" + id + "'.");
100  }
101 }
102 
103 
104 // ---------------------------------------------------------------------------
105 // NLBuilder - methods
106 // ---------------------------------------------------------------------------
108  MSNet& net,
111  NLDetectorBuilder& db,
112  NLHandler& xmlHandler)
113  : myOptions(oc), myEdgeBuilder(eb), myJunctionBuilder(jb),
114  myDetectorBuilder(db),
115  myNet(net), myXMLHandler(xmlHandler) {}
116 
117 
119 
120 
121 bool
123  // try to build the net
124  if (!load("net-file")) {
125  return false;
126  }
127  buildNet();
128 #ifdef HAVE_INTERNAL
129  // load the previous state if wished
130  if (myOptions.isSet("load-state")) {
131  long before = SysUtils::getCurrentMillis();
132  BinaryInputDevice strm(myOptions.getString("load-state"));
133  if (!strm.good()) {
134  WRITE_ERROR("Could not read state from '" + myOptions.getString("load-state") + "'!");
135  } else {
136  PROGRESS_BEGIN_MESSAGE("Loading state from '" + myOptions.getString("load-state") + "'");
137  SUMOTime step = myNet.loadState(strm);
138  if (myOptions.isDefault("begin")) {
139  myOptions.set("begin", time2string(step));
140  }
141  if (step != string2time(myOptions.getString("begin"))) {
142  WRITE_WARNING("State was written at a different time " + time2string(step) + " than the begin time " + myOptions.getString("begin") + "!");
143  }
144  }
146  return false;
147  }
149  }
150 #endif
151  // load weights if wished
152  if (myOptions.isSet("weight-files")) {
153  if (!myOptions.isUsableFileList("weight-files")) {
154  return false;
155  }
156  // build and prepare the weights handler
157  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
158  // travel time, first (always used)
160  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", true, ttRetriever));
161  // the measure to use, then
163  std::string measure = myOptions.getString("weight-attribute");
164  if (measure != "traveltime") {
165  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel") {
166  measure += "_perVeh";
167  }
168  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(measure, true, eRetriever));
169  }
170  // set up handler
171  SAXWeightsHandler handler(retrieverDefs, "");
172  // start parsing; for each file in the list
173  std::vector<std::string> files = myOptions.getStringVector("weight-files");
174  for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) {
175  // report about loading when wished
176  WRITE_MESSAGE("Loading weights from '" + *i + "'...");
177  // parse the file
178  if (!XMLSubSys::runParser(handler, *i)) {
179  return false;
180  }
181  }
182  }
183  // load routes
184  if (myOptions.isSet("route-files") && string2time(myOptions.getString("route-steps")) <= 0) {
185  if (!load("route-files")) {
186  return false;
187  }
188  }
189  // load additional net elements (sources, detectors, ...)
190  if (myOptions.isSet("additional-files")) {
191  if (!load("additional-files")) {
192  return false;
193  }
194  }
195  WRITE_MESSAGE("Loading done.");
196  return true;
197 }
198 
199 
200 void
202  MSEdgeControl* edges = 0;
203  MSJunctionControl* junctions = 0;
204  MSRouteLoaderControl* routeLoaders = 0;
205  MSTLLogicControl* tlc = 0;
206  try {
207  edges = myEdgeBuilder.build();
208  junctions = myJunctionBuilder.build();
209  routeLoaders = buildRouteLoaderControl(myOptions);
212  std::vector<SUMOTime> stateDumpTimes;
213  std::vector<std::string> stateDumpFiles;
214 #ifdef HAVE_INTERNAL
215  const std::vector<int> times = myOptions.getIntVector("save-state.times");
216  for (std::vector<int>::const_iterator i = times.begin(); i != times.end(); ++i) {
217  stateDumpTimes.push_back(TIME2STEPS(*i));
218  }
219  if (myOptions.isSet("save-state.files")) {
220  stateDumpFiles = StringTokenizer(myOptions.getString("save-state.files")).getVector();
221  if (stateDumpFiles.size() != stateDumpTimes.size()) {
222  WRITE_ERROR("Wrong number of state file names!");
223  }
224  } else {
225  const std::string prefix = myOptions.getString("save-state.prefix");
226  for (std::vector<SUMOTime>::iterator i = stateDumpTimes.begin(); i != stateDumpTimes.end(); ++i) {
227  stateDumpFiles.push_back(prefix + "_" + time2string(*i) + ".bin");
228  }
229  }
230 #endif
231  myNet.closeBuilding(edges, junctions, routeLoaders, tlc, stateDumpTimes, stateDumpFiles);
232  } catch (IOError& e) {
233  delete edges;
234  delete junctions;
235  delete routeLoaders;
236  delete tlc;
237  throw ProcessError(e.what());
238  } catch (ProcessError&) {
239  delete edges;
240  delete junctions;
241  delete routeLoaders;
242  delete tlc;
243  throw;
244  }
245 }
246 
247 
248 bool
249 NLBuilder::load(const std::string& mmlWhat) {
250  if (!OptionsCont::getOptions().isUsableFileList(mmlWhat)) {
251  return false;
252  }
253  std::vector<std::string> files = OptionsCont::getOptions().getStringVector(mmlWhat);
254  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
255  PROGRESS_BEGIN_MESSAGE("Loading " + mmlWhat + " from '" + *fileIt + "'");
256  long before = SysUtils::getCurrentMillis();
257  if (!XMLSubSys::runParser(myXMLHandler, *fileIt)) {
258  WRITE_MESSAGE("Loading of " + mmlWhat + " failed.");
259  return false;
260  }
262  }
263  return true;
264 }
265 
266 
269  // build the loaders
271  // check whether a list is existing
272  if (oc.isSet("route-files") && string2time(oc.getString("route-steps")) > 0) {
273  std::vector<std::string> files = oc.getStringVector("route-files");
274  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
275  if (!FileHelpers::exists(*fileIt)) {
276  throw ProcessError("The route file '" + *fileIt + "' does not exist.");
277  }
278  }
279  // open files for reading
280  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
281  loaders.push_back(new MSRouteLoader(myNet, new MSRouteHandler(*fileIt, false)));
282  }
283  }
284  // build the route control
285  return new MSRouteLoaderControl(myNet, string2time(oc.getString("route-steps")), loaders);
286 }
287 
288 
289 /****************************************************************************/
290