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-2013 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"
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 #ifdef HAVE_INTERNAL
66 #include <mesosim/StateHandler.h>
67 #endif
68 #ifdef CHECK_MEMORY_LEAKS
69 #include <foreign/nvwa/debug_new.h>
70 #endif // CHECK_MEMORY_LEAKS
71 
72 
73 // ===========================================================================
74 // method definitions
75 // ===========================================================================
76 // ---------------------------------------------------------------------------
77 // NLBuilder::EdgeFloatTimeLineRetriever_EdgeWeight - methods
78 // ---------------------------------------------------------------------------
79 void
81  SUMOReal value, SUMOReal begTime, SUMOReal endTime) const {
82  MSEdge* edge = MSEdge::dictionary(id);
83  if (edge != 0) {
84  myNet.getWeightsStorage().addEffort(edge, begTime, endTime, value);
85  } else {
86  WRITE_ERROR("Trying to set the effort for the unknown edge '" + id + "'.");
87  }
88 }
89 
90 
91 // ---------------------------------------------------------------------------
92 // NLBuilder::EdgeFloatTimeLineRetriever_EdgeTravelTime - methods
93 // ---------------------------------------------------------------------------
94 void
96  SUMOReal value, SUMOReal begTime, SUMOReal endTime) const {
97  MSEdge* edge = MSEdge::dictionary(id);
98  if (edge != 0) {
99  myNet.getWeightsStorage().addTravelTime(edge, begTime, endTime, value);
100  } else {
101  WRITE_ERROR("Trying to set the travel time for the unknown edge '" + id + "'.");
102  }
103 }
104 
105 
106 // ---------------------------------------------------------------------------
107 // NLBuilder - methods
108 // ---------------------------------------------------------------------------
110  MSNet& net,
113  NLDetectorBuilder& db,
114  NLHandler& xmlHandler)
115  : myOptions(oc), myEdgeBuilder(eb), myJunctionBuilder(jb),
116  myDetectorBuilder(db),
117  myNet(net), myXMLHandler(xmlHandler) {}
118 
119 
121 
122 
123 bool
125  // try to build the net
126  if (!load("net-file")) {
127  return false;
128  }
129  buildNet();
130 #ifdef HAVE_INTERNAL
131  // load the previous state if wished
132  if (myOptions.isSet("load-state")) {
133  long before = SysUtils::getCurrentMillis();
134  const std::string& f = myOptions.getString("load-state");
135  PROGRESS_BEGIN_MESSAGE("Loading state from '" + f + "'");
136  StateHandler h(f, string2time(OptionsCont::getOptions().getString("load-state.offset")));
137  XMLSubSys::runParser(h, f);
138  if (myOptions.isDefault("begin")) {
139  myOptions.set("begin", time2string(h.getTime()));
140  }
141  if (h.getTime() != string2time(myOptions.getString("begin"))) {
142  WRITE_WARNING("State was written at a different time " + time2string(h.getTime()) + " than the begin time " + myOptions.getString("begin") + "!");
143  }
145  return false;
146  }
148  }
149 #endif
150  // load weights if wished
151  if (myOptions.isSet("weight-files")) {
152  if (!myOptions.isUsableFileList("weight-files")) {
153  return false;
154  }
155  // build and prepare the weights handler
156  std::vector<SAXWeightsHandler::ToRetrieveDefinition*> retrieverDefs;
157  // travel time, first (always used)
159  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition("traveltime", true, ttRetriever));
160  // the measure to use, then
162  std::string measure = myOptions.getString("weight-attribute");
163  if (measure != "traveltime") {
164  if (measure == "CO" || measure == "CO2" || measure == "HC" || measure == "PMx" || measure == "NOx" || measure == "fuel") {
165  measure += "_perVeh";
166  }
167  retrieverDefs.push_back(new SAXWeightsHandler::ToRetrieveDefinition(measure, true, eRetriever));
168  }
169  // set up handler
170  SAXWeightsHandler handler(retrieverDefs, "");
171  // start parsing; for each file in the list
172  std::vector<std::string> files = myOptions.getStringVector("weight-files");
173  for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) {
174  // report about loading when wished
175  WRITE_MESSAGE("Loading weights from '" + *i + "'...");
176  // parse the file
177  if (!XMLSubSys::runParser(handler, *i)) {
178  return false;
179  }
180  }
181  }
182  // load routes
183  if (myOptions.isSet("route-files") && string2time(myOptions.getString("route-steps")) <= 0) {
184  if (!load("route-files")) {
185  return false;
186  }
187  }
188  // load additional net elements (sources, detectors, ...)
189  if (myOptions.isSet("additional-files")) {
190  if (!load("additional-files")) {
191  return false;
192  }
193  }
194  WRITE_MESSAGE("Loading done.");
195  return true;
196 }
197 
198 
199 void
201  MSEdgeControl* edges = 0;
202  MSJunctionControl* junctions = 0;
203  SUMORouteLoaderControl* routeLoaders = 0;
204  MSTLLogicControl* tlc = 0;
205  try {
206  edges = myEdgeBuilder.build();
207  junctions = myJunctionBuilder.build();
208  routeLoaders = buildRouteLoaderControl(myOptions);
211  std::vector<SUMOTime> stateDumpTimes;
212  std::vector<std::string> stateDumpFiles;
213 #ifdef HAVE_INTERNAL
214  const std::vector<int> times = myOptions.getIntVector("save-state.times");
215  for (std::vector<int>::const_iterator i = times.begin(); i != times.end(); ++i) {
216  stateDumpTimes.push_back(TIME2STEPS(*i));
217  }
218  if (myOptions.isSet("save-state.files")) {
219  stateDumpFiles = StringTokenizer(myOptions.getString("save-state.files")).getVector();
220  if (stateDumpFiles.size() != stateDumpTimes.size()) {
221  WRITE_ERROR("Wrong number of state file names!");
222  }
223  } else {
224  const std::string prefix = myOptions.getString("save-state.prefix");
225  for (std::vector<SUMOTime>::iterator i = stateDumpTimes.begin(); i != stateDumpTimes.end(); ++i) {
226  stateDumpFiles.push_back(prefix + "_" + time2string(*i) + ".sbx");
227  }
228  }
229 #endif
230  myNet.closeBuilding(edges, junctions, routeLoaders, tlc, stateDumpTimes, stateDumpFiles);
231  } catch (IOError& e) {
232  delete edges;
233  delete junctions;
234  delete routeLoaders;
235  delete tlc;
236  throw ProcessError(e.what());
237  } catch (ProcessError&) {
238  delete edges;
239  delete junctions;
240  delete routeLoaders;
241  delete tlc;
242  throw;
243  }
244 }
245 
246 
247 bool
248 NLBuilder::load(const std::string& mmlWhat) {
249  if (!OptionsCont::getOptions().isUsableFileList(mmlWhat)) {
250  return false;
251  }
252  std::vector<std::string> files = OptionsCont::getOptions().getStringVector(mmlWhat);
253  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
254  PROGRESS_BEGIN_MESSAGE("Loading " + mmlWhat + " from '" + *fileIt + "'");
255  long before = SysUtils::getCurrentMillis();
256  if (!XMLSubSys::runParser(myXMLHandler, *fileIt)) {
257  WRITE_MESSAGE("Loading of " + mmlWhat + " failed.");
258  return false;
259  }
261  }
262  return true;
263 }
264 
265 
268  // build the loaders
269  SUMORouteLoaderControl* loaders = new SUMORouteLoaderControl(string2time(oc.getString("route-steps")));
270  // check whether a list is existing
271  if (oc.isSet("route-files") && string2time(oc.getString("route-steps")) > 0) {
272  std::vector<std::string> files = oc.getStringVector("route-files");
273  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
274  if (!FileHelpers::exists(*fileIt)) {
275  throw ProcessError("The route file '" + *fileIt + "' does not exist.");
276  }
277  }
278  // open files for reading
279  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
280  loaders->add(new SUMORouteLoader(new MSRouteHandler(*fileIt, false)));
281  }
282  }
283  return loaders;
284 }
285 
286 
287 /****************************************************************************/